jojochuang commented on code in PR #11085:
URL: https://github.com/apache/ozone/pull/11085#discussion_r3847159319


##########
hadoop-ozone/interface-storage/src/main/java/org/apache/hadoop/ozone/om/lock/OMLockMetrics.java:
##########
@@ -37,26 +36,24 @@ public final class OMLockMetrics implements MetricsSource {
   private static final String SOURCE_NAME =
       OMLockMetrics.class.getSimpleName();
 
-  private final MetricsRegistry registry;
-  private final MutableStat readLockWaitingTimeMsStat;
-  private final MutableStat readLockHeldTimeMsStat;
-  private final MutableStat writeLockWaitingTimeMsStat;
-  private final MutableStat writeLockHeldTimeMsStat;
+  private final ConcurrentMutableStat readLockWaitingTimeMsStat;
+  private final ConcurrentMutableStat readLockHeldTimeMsStat;
+  private final ConcurrentMutableStat writeLockWaitingTimeMsStat;
+  private final ConcurrentMutableStat writeLockHeldTimeMsStat;
 
   private OMLockMetrics() {
-    registry = new MetricsRegistry(SOURCE_NAME);
-    readLockWaitingTimeMsStat = registry.newStat("ReadLockWaitingTime",
+    readLockWaitingTimeMsStat = new 
ConcurrentMutableStat("ReadLockWaitingTime",
         "Time (in milliseconds) spent waiting for acquiring the read lock",
-        "Ops", "Time", true);
-    readLockHeldTimeMsStat = registry.newStat("ReadLockHeldTime",
+        "Ops", "Time", false);

Review Comment:
   turning the extended field from true to false means extended stats (stdev, 
min/max etc.)  are not created by default. If ConcurrentMutableStat is not 
expected to support extended stats, then let's remove this parameter.



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/util/ConcurrentMutableStat.java:
##########
@@ -0,0 +1,134 @@
+/*
+ * 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.hadoop.ozone.util;
+
+import java.util.concurrent.atomic.LongAccumulator;
+import java.util.concurrent.atomic.LongAdder;
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+import org.apache.hadoop.metrics2.lib.MutableStat;
+import org.apache.hadoop.metrics2.util.SampleStat;
+
+/**
+ * A {@link MutableStat} whose hot-path {@link #add(long)} is non-blocking
+ * under concurrent callers. Each call accumulates in {@link LongAdder} /
+ * {@link LongAccumulator} cells and the pending totals are drained into the
+ * parent's running state lazily — only when metrics are read via
+ * {@link #snapshot}, {@link #lastStat}, or {@link #toString}.
+ *
+ * <p>This avoids the {@code synchronized} contention of the base class when
+ * many threads release locks simultaneously and all attempt to record a
+ * measurement on the same metric instance.
+ *
+ * <p><b>Standard deviation accuracy:</b> {@code drainPending()} batches all
+ * pending samples except the min and max into a single
+ * {@code super.add(n, sum)} call. {@link 
org.apache.hadoop.metrics2.util.SampleStat}
+ * treats a batch as {@code n} identical samples equal to their mean, so the
+ * within-batch variance is lost. The reported standard deviation is therefore
+ * underestimated. Callers that need accurate standard deviation should use
+ * {@link org.apache.hadoop.metrics2.lib.MutableStat} directly, or pass
+ * {@code extended=false} to suppress the stdev metric.
+ */
+public class ConcurrentMutableStat extends MutableStat {
+
+  private final LongAdder pendingSum   = new LongAdder();
+  private final LongAdder pendingCount = new LongAdder();
+  /** Cell-striped min accumulator: identity = Long.MAX_VALUE, function = 
Math::min. */
+  private final LongAccumulator pendingMin = new LongAccumulator(Math::min, 
Long.MAX_VALUE);
+  /** Cell-striped max accumulator: identity = Long.MIN_VALUE, function = 
Math::max. */
+  private final LongAccumulator pendingMax = new LongAccumulator(Math::max, 
Long.MIN_VALUE);
+
+  public ConcurrentMutableStat(String name, String description,
+      String sampleName, String valueName, boolean extended) {
+    super(name, description, sampleName, valueName, extended);

Review Comment:
   ```suggestion
         String sampleName, String valueName) {
       super(name, description, sampleName, valueName, false);
   ```



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