yandrey321 commented on code in PR #11085: URL: https://github.com/apache/ozone/pull/11085#discussion_r3844869376
########## hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/util/ConcurrentMutableStat.java: ########## @@ -0,0 +1,125 @@ +/* + * 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. + */ +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: Good catch, stdev_time would be underestimated with the current implementation. -- 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]
