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


##########
src/java/org/apache/cassandra/metrics/ThreadLocalMeter.java:
##########
@@ -0,0 +1,372 @@
+/*
+ * 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.WeakReference;
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import com.google.common.annotations.VisibleForTesting;
+
+import com.codahale.metrics.Clock;
+import org.apache.cassandra.concurrent.ScheduledExecutors;
+import org.apache.cassandra.utils.MonotonicClock;
+import org.apache.cassandra.utils.ReflectionUtils;
+
+import static java.lang.Math.exp;
+
+/**
+ * An alternative to Dropwizard Meter which implements the same kind of API.
+ * it has more efficent mark operations and consumes less memory.
+ * Only exponential decaying moving average is supported for 1/5/15-minutes 
rate values.
+ * Tick logic is moved out from a mark operation and always executed in a 
background thread.
+ * For better cache locality rate values are extracted to a common non 
thread-local array
+ *  updated by a background thread in bulk.
+ * Counter logic inside is implemented using @see ThreadLocalMetrics 
functionality.
+ * NOTE: Dropwizard Meter is a class and there is no an interface for 
Dropwizard Meter logic,
+ *   so we have to create an alternative hierarchy.
+ */
+public class ThreadLocalMeter extends com.codahale.metrics.Meter implements 
Meter
+{
+    private static final int INTERVAL_SEC = 5;
+    private static final long TICK_INTERVAL_NS = 
TimeUnit.SECONDS.toNanos(INTERVAL_SEC);
+    private static final double SECONDS_PER_MINUTE = 60.0;
+    private static final int ONE_MINUTE = 1;
+    private static final int FIVE_MINUTES = 5;
+    private static final int FIFTEEN_MINUTES = 15;
+    private static final double M1_ALPHA = 1 - exp(-INTERVAL_SEC / 
SECONDS_PER_MINUTE / ONE_MINUTE);
+    private static final double M5_ALPHA = 1 - exp(-INTERVAL_SEC / 
SECONDS_PER_MINUTE / FIVE_MINUTES);
+    private static final double M15_ALPHA = 1 - exp(-INTERVAL_SEC / 
SECONDS_PER_MINUTE / FIFTEEN_MINUTES);
+
+    // each meter instance stores rate stats using 3 consecutive cells in 
rates array
+    private static final int M1_RATE_OFFSET = 0;
+    private static final int M5_RATE_OFFSET = 1;
+    private static final int M15_RATE_OFFSET = 2;
+    private static final int RATES_COUNT = 3;
+    private static final double NON_INITIALIZED = Double.MIN_VALUE;
+
+    private static final int BACKGROUND_TICK_INTERVAL_SEC = INTERVAL_SEC;
+    private static final List<WeakReference<ThreadLocalMeter>> allMeters = new 
CopyOnWriteArrayList<>();
+
+    /**
+     * CASSANDRA-19332
+     * If ticking would reduce even Long.MAX_VALUE in the 15 minute EWMA below 
this target then don't bother
+     * ticking in a loop and instead reset all the EWMAs.
+     */
+    private static final double maxTickZeroTarget = 0.0001;
+    private static final int maxTicks;
+
+    static
+    {
+        int m3Ticks = 1;
+        double emulatedM15Rate = 0.0;
+        emulatedM15Rate = tickFifteenMinuteEWMA(emulatedM15Rate, 
Long.MAX_VALUE);
+        do
+        {
+            emulatedM15Rate = tickFifteenMinuteEWMA(emulatedM15Rate, 0);
+            m3Ticks++;
+        }
+        while (getRatePerSecond(emulatedM15Rate) > maxTickZeroTarget);
+        maxTicks = m3Ticks;
+    }
+
+    static
+    {
+        
ScheduledExecutors.scheduledTasks.scheduleWithFixedDelay(ThreadLocalMeter::tickAll,
+                                                                 
BACKGROUND_TICK_INTERVAL_SEC,
+                                                                 
BACKGROUND_TICK_INTERVAL_SEC,
+                                                                 
TimeUnit.SECONDS);
+    }
+
+    private static final Object ratesArrayGuard = new Object();
+
+    // writes should be synchonized using ratesArrayGuard
+    // 1) write during a copy when we need to extend rates array when a new 
Meter is created
+    // 2) write during a tick
+    private static volatile double[] rates = new double[RATES_COUNT * 16];
+    static final AtomicInteger rateGroupIdGenerator = new AtomicInteger();
+
+    private static final Object freeRateGroupIdSetGuard = new Object();
+
+    // we recycle and reuse rate group IDs
+    // a set bit means the correspondent rate group id is available to use
+    private static final BitSet freeRateGroupIdSet = new BitSet();
+
+    private static int allocateRateGroupOffset()
+    {
+        int rateGroupId;
+        synchronized (freeRateGroupIdSetGuard)
+        {
+            rateGroupId = freeRateGroupIdSet.nextSetBit(0);
+            if (rateGroupId >= 0)
+                freeRateGroupIdSet.clear(rateGroupId);
+        }
+        if (rateGroupId < 0)
+        {
+            rateGroupId = rateGroupIdGenerator.getAndAdd(RATES_COUNT);
+        }
+        synchronized (ratesArrayGuard)
+        {
+            if (rates.length < rateGroupId + RATES_COUNT)
+            {
+                double[] newRates = new double[(int) (rateGroupId + 
RATES_COUNT)];

Review Comment:
   yes, it is int, I do not remember why do I have this logic :-), probably it 
is an artefact from an old alternative draft how to extend the array.



-- 
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]

Reply via email to