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
025import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap;
026
027/**
028 * Provides server side metrics related to scan operations.
029 */
030@InterfaceAudience.Public
031public class ServerSideScanMetrics {
032  /**
033   * Hash to hold the String -> Atomic Long mappings for each metric
034   */
035  private final Map<String, AtomicLong> counters = new HashMap<>();
036
037  /**
038   * Create a new counter with the specified name n * @return {@link AtomicLong} instance for the
039   * counter with counterName
040   */
041  protected AtomicLong createCounter(String counterName) {
042    AtomicLong c = new AtomicLong(0);
043    counters.put(counterName, c);
044    return c;
045  }
046
047  public static final String COUNT_OF_ROWS_SCANNED_KEY_METRIC_NAME = "ROWS_SCANNED";
048  public static final String COUNT_OF_ROWS_FILTERED_KEY_METRIC_NAME = "ROWS_FILTERED";
049
050  /**
051   * number of rows filtered during scan RPC
052   */
053  public final AtomicLong countOfRowsFiltered =
054    createCounter(COUNT_OF_ROWS_FILTERED_KEY_METRIC_NAME);
055
056  /**
057   * number of rows scanned during scan RPC. Not every row scanned will be returned to the client
058   * since rows may be filtered.
059   */
060  public final AtomicLong countOfRowsScanned = createCounter(COUNT_OF_ROWS_SCANNED_KEY_METRIC_NAME);
061
062  /**
063   * nn
064   */
065  public void setCounter(String counterName, long value) {
066    AtomicLong c = this.counters.get(counterName);
067    if (c != null) {
068      c.set(value);
069    }
070  }
071
072  /**
073   * n * @return true if a counter exists with the counterName
074   */
075  public boolean hasCounter(String counterName) {
076    return this.counters.containsKey(counterName);
077  }
078
079  /**
080   * n * @return {@link AtomicLong} instance for this counter name, null if counter does not exist.
081   */
082  public AtomicLong getCounter(String counterName) {
083    return this.counters.get(counterName);
084  }
085
086  /**
087   * nn
088   */
089  public void addToCounter(String counterName, long delta) {
090    AtomicLong c = this.counters.get(counterName);
091    if (c != null) {
092      c.addAndGet(delta);
093    }
094  }
095
096  /**
097   * Get all of the values since the last time this function was called. Calling this function will
098   * reset all AtomicLongs in the instance back to 0.
099   * @return A Map of String -&gt; Long for metrics
100   */
101  public Map<String, Long> getMetricsMap() {
102    return getMetricsMap(true);
103  }
104
105  /**
106   * Get all of the values. If reset is true, we will reset the all AtomicLongs back to 0.
107   * @param reset whether to reset the AtomicLongs to 0.
108   * @return A Map of String -&gt; Long for metrics
109   */
110  public Map<String, Long> getMetricsMap(boolean reset) {
111    // Create a builder
112    ImmutableMap.Builder<String, Long> builder = ImmutableMap.builder();
113    for (Map.Entry<String, AtomicLong> e : this.counters.entrySet()) {
114      long value = reset ? e.getValue().getAndSet(0) : e.getValue().get();
115      builder.put(e.getKey(), value);
116    }
117    // Build the immutable map so that people can't mess around with it.
118    return builder.build();
119  }
120}