denis-chudov commented on code in PR #957: URL: https://github.com/apache/ignite-3/pull/957#discussion_r937741595
########## modules/metrics/src/main/java/org/apache/ignite/internal/metrics/scalar/HitRateMetric.java: ########## @@ -0,0 +1,208 @@ +/* + * 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.ignite.internal.metrics.scalar; + +import static org.apache.ignite.internal.util.IgniteUtils.currentTimeMillis; +import static org.apache.ignite.internal.util.IgniteUtils.ensure; + +import java.util.concurrent.atomic.AtomicLongArray; +import org.apache.ignite.internal.metrics.AbstractMetric; +import org.jetbrains.annotations.Nullable; + +/** + * Accumulates approximate hit rate statistics. + * Calculates number of hits in last {@code rateTimeInterval} milliseconds. + * Algorithm is based on circular array of {@code size} hit counters, each is responsible for last corresponding time + * interval of {@code rateTimeInterval}/{@code size} milliseconds. Resulting number of hits is sum of all counters. + * + * <p>Implementation is nonblocking and protected from hits loss. + * Maximum relative error is 1/{@code size}. + * 2^55 - 1 hits per interval can be accumulated without numeric overflow. + */ +public class HitRateMetric extends AbstractMetric implements LongMetric { + /** Default counters array size. */ + public static final int DFLT_SIZE = 10; + + /** Bits that store actual hit count. */ + private static final int TAG_OFFSET = 56; + + /** Useful part mask. */ + private static final long NO_TAG_MASK = ~(-1L << TAG_OFFSET); + + /** Time interval when hits are counted to calculate rate, in milliseconds. */ + private final long rateTimeInterval; + + /** Counters array size. */ + private final int size; + + /** Tagged counters. */ + private final AtomicLongArray taggedCounters; + + /** Last hit times. */ + private final AtomicLongArray lastHitTimes; + + /** + * The constructor. + * + * @param name Name. + * @param desc Description. + * @param rateTimeInterval Rate time interval in milliseconds. + */ + public HitRateMetric(String name, @Nullable String desc, long rateTimeInterval) { + this(name, desc, rateTimeInterval, DFLT_SIZE); + } + + /** + * The constructor. + * + * @param name Name. + * @param desc Description. + * @param rateTimeInterval Rate time interval in milliseconds. + * @param size Counters array size. + */ + public HitRateMetric(String name, @Nullable String desc, long rateTimeInterval, int size) { + super(name, desc); + + ensure(rateTimeInterval > 0, "rateTimeInterval should be positive"); + ensure(size > 1, "Minimum value for size is 2"); + + this.rateTimeInterval = rateTimeInterval; + this.size = size; + taggedCounters = new AtomicLongArray(size); + lastHitTimes = new AtomicLongArray(size); + } + + /** + * Adds x to the metric. + * + * @param hits Value to be added. + */ + public void add(long hits) { + long curTs = currentTimeMillis(); Review Comment: I checked both approaches and `System.currentTimeMillis` is slightly better. Are you sure that we need a separate ticket for investigation? -- 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]
