netudima commented on code in PR #4003:
URL: https://github.com/apache/cassandra/pull/4003#discussion_r2032069097


##########
src/java/org/apache/cassandra/metrics/DecayingEstimatedHistogramReservoir.java:
##########
@@ -855,4 +855,319 @@ public String toString()
             return "[" + min + ',' + max + ']';
         }
     }
+
+    interface MetricCleaner
+    {
+        void clean();
+    }
+
+    private static class BucketsPhantomReference extends 
PhantomReference<Object> implements MetricCleaner
+    {
+        private final MetricCleaner cleaner;
+
+        public BucketsPhantomReference(ReferenceQueue<? super Object> q, 
MetricCleaner cleaner)
+        {
+            super(Thread.currentThread(), q);
+            this.cleaner = cleaner;
+        }
+
+        public void clean()
+        {
+            cleaner.clean();
+        }
+    }
+
+    /**
+     * Writes are exclusive to the thread-local buckets, so we can use a 
single updater for all threads.
+     * Readers will see a consistent view of the buckets and could be blocked 
for a while.
+     * <p>
+     * The class is aslso being tracked by a phantom reference queue to 
release the accumulated buckets when the thread is dead.
+     */
+    protected class BucketsThreadLocal
+    {
+        // try to use int[] instead of long[] to reduce memory usage, and move 
to the sum array when overflow
+        private final AtomicReference<DecayingArray> decayingRef;
+        private final long[] estimated;
+        private volatile boolean writing;
+
+        public BucketsThreadLocal(int size)
+        {
+            this.decayingRef = new AtomicReference<>(new DecayingArray(size, 
decayingEstimatedBuckets.decayLandmark));
+            this.estimated = new long[size];
+        }
+
+        public void update(int index, long now)
+        {
+            // This is only called by the thread that owns the thread local, 
so we don't need to worry about contention.
+            // Once the rescaling has occurred, we need to flush the values to 
the decayingBucket and report that the values are no longer in use.
+            writing = true;
+            try
+            {
+                DecayingArray decaying = decayingRef.get();
+                if (decaying.decayLandmark != 
decayingEstimatedBuckets.decayLandmark)
+                    decayingEstimatedBuckets.flush(this, decaying);
+
+                decayingRef.get().update(index, now);

Review Comment:
   do we need to do this get always twice?



-- 
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: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to