sanjeet006py commented on code in PR #6868: URL: https://github.com/apache/hbase/pull/6868#discussion_r2096313881
########## hbase-client/src/main/java/org/apache/hadoop/hbase/client/metrics/ScanMetricsHolder.java: ########## @@ -0,0 +1,109 @@ +/* + * 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.hbase.client.metrics; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.hadoop.hbase.ServerName; +import org.apache.yetus.audience.InterfaceAudience; + +/** + * Generic holder class for capturing Scan Metrics as a map of metric name ({@link String}) -> Value + * ({@link AtomicLong}). + */ [email protected] +public class ScanMetricsHolder { + private final Map<String, AtomicLong> counters = new HashMap<>(); + private ScanMetricsRegionInfo scanMetricsRegionInfo = + ScanMetricsRegionInfo.EMPTY_SCAN_METRICS_REGION_INFO; + + /** + * Create a new counter with the specified name + * @return {@link AtomicLong} instance for the counter with counterName + */ + AtomicLong createCounter(String counterName) { + AtomicLong c = new AtomicLong(0); + counters.put(counterName, c); + return c; + } + + /** Sets counter with counterName to passed in value, does nothing if counter does not exist. */ + void setCounter(String counterName, long value) { + AtomicLong c = this.counters.get(counterName); + if (c != null) { + c.set(value); + } + } + + /** Returns true if a counter exists with the counterName */ + boolean hasCounter(String counterName) { + return this.counters.containsKey(counterName); + } + + /** Returns {@link AtomicLong} instance for this counter name, null if counter does not exist. */ + AtomicLong getCounter(String counterName) { + return this.counters.get(counterName); + } + + /** Increments the counter with counterName by delta, does nothing if counter does not exist. */ + void addToCounter(String counterName, long delta) { + AtomicLong c = this.counters.get(counterName); + if (c != null) { + c.addAndGet(delta); + } + } + + /** + * Get all of the values. If reset is true, we will reset the all AtomicLongs back to 0. + * @param reset whether to reset the AtomicLongs to 0. + * @return A Map of String -> Long for metrics + */ + Map<String, Long> getMetricsMap(boolean reset) { Review Comment: I saw this convention was being followed in `ServerSideScanMetrics` so did the same in here. I agree modifying state in a getter is not good. Will fix it. -- 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]
