001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.client.metrics; 019 020import java.util.HashMap; 021import java.util.Map; 022import java.util.concurrent.atomic.AtomicLong; 023import org.apache.yetus.audience.InterfaceAudience; 024 025@InterfaceAudience.Private 026public final class ScanMetricsUtil { 027 028 private ScanMetricsUtil() { 029 } 030 031 /** 032 * Creates a new counter with the specified name and stores it in the counters map. 033 * @return {@link AtomicLong} instance for the counter with counterName 034 */ 035 static AtomicLong createCounter(Map<String, AtomicLong> counters, String counterName) { 036 AtomicLong c = new AtomicLong(0); 037 counters.put(counterName, c); 038 return c; 039 } 040 041 /** 042 * Sets counter with counterName to passed in value, does nothing if counter does not exist. 043 */ 044 static void setCounter(Map<String, AtomicLong> counters, String counterName, long value) { 045 AtomicLong c = counters.get(counterName); 046 if (c != null) { 047 c.set(value); 048 } 049 } 050 051 /** 052 * Increments the counter with counterName by delta, does nothing if counter does not exist. 053 */ 054 static void addToCounter(Map<String, AtomicLong> counters, String counterName, long delta) { 055 AtomicLong c = counters.get(counterName); 056 if (c != null) { 057 c.addAndGet(delta); 058 } 059 } 060 061 /** 062 * Returns true if a counter exists with the counterName. 063 */ 064 static boolean hasCounter(Map<String, AtomicLong> counters, String counterName) { 065 return counters.containsKey(counterName); 066 } 067 068 /** 069 * Returns {@link AtomicLong} instance for this counter name, null if counter does not exist. 070 */ 071 static AtomicLong getCounter(Map<String, AtomicLong> counters, String counterName) { 072 return counters.get(counterName); 073 } 074 075 /** 076 * Get all the values. If reset is true, we will reset the all AtomicLongs back to 0. 077 * @param reset whether to reset the AtomicLongs to 0. 078 * @return A Map of String -> Long for metrics 079 */ 080 static Map<String, Long> collectMetrics(Map<String, AtomicLong> counters, boolean reset) { 081 Map<String, Long> metricsSnapshot = new HashMap<>(); 082 for (Map.Entry<String, AtomicLong> e : counters.entrySet()) { 083 long value = reset ? e.getValue().getAndSet(0) : e.getValue().get(); 084 metricsSnapshot.put(e.getKey(), value); 085 } 086 return metricsSnapshot; 087 } 088}