belliottsmith commented on code in PR #4324: URL: https://github.com/apache/cassandra/pull/4324#discussion_r2318248340
########## src/java/org/apache/cassandra/metrics/ThreadLocalMetrics.java: ########## @@ -0,0 +1,352 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.metrics; + +import java.lang.ref.PhantomReference; +import java.lang.ref.ReferenceQueue; +import java.util.BitSet; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import com.google.common.annotations.VisibleForTesting; + +import io.netty.util.concurrent.FastThreadLocal; +import org.apache.cassandra.concurrent.Shutdownable; + +import static com.google.common.collect.ImmutableList.of; +import static org.apache.cassandra.concurrent.ExecutorFactory.Global.executorFactory; +import static org.apache.cassandra.concurrent.InfiniteLoopExecutor.SimulatorSafe.UNSAFE; +import static org.apache.cassandra.utils.ExecutorUtils.shutdownAndWait; + +/** + * A thread-local counter implementation designed to use in metrics as an alternative to LongAdder used by Dropwizard metrics. + * This implementation has reduced write (increment) CPU usage costs in exchange for a higher read cost. + * We keep and increment parts of a counter locally for each thread. + * To reduce memory footprint per counter they are grouped together to a long[] array for each thread. + * A position of a counter value is the same for every thread for the same counter id. + * Piggyback volatile visibility is expected for readers who execute getCount method to see recent writes to thread local arrays. + * If a metric is not used anymore the position in the array is reused. Phantom references are used to track aliveness of metric users. + * When a thread died the counter values accumulated by it are transfered to a shared summaryValues collection. + * Threads death is tracked using 2 approaches: FastThreadLocal.onRemoval callback and phantom references to Thread objects. + */ +public class ThreadLocalMetrics +{ + static final AtomicInteger idGenerator = new AtomicInteger(); + + private static final Object freeMetricIdSetGuard = new Object(); + + @VisibleForTesting + static final BitSet freeMetricIdSet = new BitSet(); + + private static final List<ThreadLocalMetrics> allThreadLocalMetrics = new CopyOnWriteArrayList<>(); + + /* the lock is used to coordinate the threads which: + * 1) transfer values from a dead thread to summaryValues + * 2) calculate a getCount value. + * Using this lock we want to avoid + * a value lost while moving it in getCount + * as well as a double-counting + */ + private static final ReadWriteLock summaryLock = new ReentrantReadWriteLock(); + + private static final FastThreadLocal<ThreadLocalMetrics> threadLocalMetricsCurrent = new FastThreadLocal<>() + { + @Override + protected ThreadLocalMetrics initialValue() + { + ThreadLocalMetrics result = new ThreadLocalMetrics(); + allThreadLocalMetrics.add(result); + destroyWhenUnreachable(Thread.currentThread(), result::release); + return result; + } + + // this method is invoked when a thread is going to finish, but it works only for FastThreadLocalThread + // so, we use phantom references for other cases + @Override + protected void onRemoval(ThreadLocalMetrics value) + { + value.release(); + } + }; + + private static final Map<Integer, AtomicLong> summaryValues = new ConcurrentHashMap<>(); Review Comment: This is just a suggestion btw, it's not strictly necessary. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

