BewareMyPower commented on code in PR #24719: URL: https://github.com/apache/pulsar/pull/24719#discussion_r2333753425
########## pulsar-broker/src/main/java/org/apache/pulsar/broker/stats/prometheus/metrics/ThreadLocalAccessor.java: ########## @@ -0,0 +1,113 @@ +/* + * 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.pulsar.broker.stats.prometheus.metrics; + +import com.google.common.annotations.VisibleForTesting; +import com.yahoo.sketches.quantiles.DoublesSketch; +import com.yahoo.sketches.quantiles.DoublesSketchBuilder; +import com.yahoo.sketches.quantiles.DoublesUnion; +import io.netty.util.concurrent.FastThreadLocal; +import io.netty.util.concurrent.FastThreadLocalThread; +import java.lang.ref.WeakReference; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.StampedLock; +import org.jspecify.annotations.Nullable; + +class ThreadLocalAccessor { + + private final ConcurrentHashMap<LocalData, Boolean> map = new ConcurrentHashMap<>(); + final FastThreadLocal<LocalData> localData = new FastThreadLocal<>() { + + @Override + protected LocalData initialValue() { + LocalData localData = new LocalData(Thread.currentThread()); + map.put(localData, Boolean.TRUE); + return localData; + } + + @Override + protected void onRemoval(LocalData value) { + map.remove(value); + } + }; + + void record(DoublesUnion aggregateSuccess, @Nullable DoublesUnion aggregateFail) { + map.keySet().forEach(key -> { + key.record(aggregateSuccess, aggregateFail); + if (key.shouldRemove()) { + map.remove(key); + } + }); + } + + @VisibleForTesting + int getLocalDataCount() { + return map.keySet().size(); + } + + static class LocalData { + + final DoublesSketch successSketch = new DoublesSketchBuilder().build(); + final DoublesSketch failSketch = new DoublesSketchBuilder().build(); + final StampedLock lock = new StampedLock(); + // Keep a weak reference to the owner thread so that we can remove the LocalData when the thread + // is not alive anymore or has been garbage collected. + // This reference isn't needed when the owner thread is a FastThreadLocalThread and will be null in that case. + // The removal is handled by FastThreadLocal#onRemoval when the owner thread is a FastThreadLocalThread. + final WeakReference<Thread> ownerThreadReference; Review Comment: Good suggestion, addressed -- 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: commits-unsubscr...@pulsar.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org