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.hadoop.hbase.ServerName;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * Captures region level scan metrics as a map of metric name ({@link String}) -> Value
028 * ({@link AtomicLong}). <br/>
029 * <br/>
030 * One instance stores scan metrics for a single region only.
031 */
032@InterfaceAudience.Private
033public class RegionScanMetricsData {
034  private final Map<String, AtomicLong> counters = new HashMap<>();
035  private ScanMetricsRegionInfo scanMetricsRegionInfo =
036    ScanMetricsRegionInfo.EMPTY_SCAN_METRICS_REGION_INFO;
037
038  AtomicLong createCounter(String counterName) {
039    return ScanMetricsUtil.createCounter(counters, counterName);
040  }
041
042  void setCounter(String counterName, long value) {
043    ScanMetricsUtil.setCounter(counters, counterName, value);
044  }
045
046  void addToCounter(String counterName, long delta) {
047    ScanMetricsUtil.addToCounter(counters, counterName, delta);
048  }
049
050  Map<String, Long> collectMetrics(boolean reset) {
051    return ScanMetricsUtil.collectMetrics(counters, reset);
052  }
053
054  @Override
055  public String toString() {
056    return getClass().getSimpleName() + "[" + scanMetricsRegionInfo + "," + "Counters=" + counters
057      + "]";
058  }
059
060  /**
061   * Populate encoded region name and server name details if not already populated. If details are
062   * already populated and a re-attempt is done then {@link UnsupportedOperationException} is
063   * thrown.
064   */
065  void initScanMetricsRegionInfo(String encodedRegionName, ServerName serverName) {
066    // Check by reference
067    if (scanMetricsRegionInfo == ScanMetricsRegionInfo.EMPTY_SCAN_METRICS_REGION_INFO) {
068      scanMetricsRegionInfo = new ScanMetricsRegionInfo(encodedRegionName, serverName);
069    } else {
070      throw new UnsupportedOperationException("ScanMetricsRegionInfo has already been initialized");
071    }
072  }
073
074  ScanMetricsRegionInfo getScanMetricsRegionInfo() {
075    return scanMetricsRegionInfo;
076  }
077}