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.regionserver;
019
020import java.util.Map;
021import java.util.concurrent.ConcurrentHashMap;
022import org.apache.hadoop.hbase.TableName;
023import org.apache.hadoop.hbase.metrics.Meter;
024import org.apache.hadoop.hbase.metrics.MetricRegistry;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * Implementation of {@link MetricsTableQueryMeter} to track query per second for each table in a
029 * RegionServer.
030 */
031@InterfaceAudience.Private
032public class MetricsTableQueryMeterImpl implements MetricsTableQueryMeter {
033  private final Map<TableName, TableMeters> metersByTable = new ConcurrentHashMap<>();
034  private final MetricRegistry metricRegistry;
035
036  public MetricsTableQueryMeterImpl(MetricRegistry metricRegistry) {
037    this.metricRegistry = metricRegistry;
038  }
039
040  private static class TableMeters {
041    final Meter tableReadQueryMeter;
042    final Meter tableWriteQueryMeter;
043
044    TableMeters(MetricRegistry metricRegistry, TableName tableName) {
045      this.tableReadQueryMeter =
046        metricRegistry.meter(qualifyMetricsName(tableName, TABLE_READ_QUERY_PER_SECOND));
047      this.tableWriteQueryMeter =
048        metricRegistry.meter(qualifyMetricsName(tableName, TABLE_WRITE_QUERY_PER_SECOND));
049    }
050
051    public void updateTableReadQueryMeter(long count) {
052      tableReadQueryMeter.mark(count);
053    }
054
055    public void updateTableReadQueryMeter() {
056      tableReadQueryMeter.mark();
057    }
058
059    public void updateTableWriteQueryMeter(long count) {
060      tableWriteQueryMeter.mark(count);
061    }
062
063    public void updateTableWriteQueryMeter() {
064      tableWriteQueryMeter.mark();
065    }
066  }
067
068  private static String qualifyMetricsName(TableName tableName, String metric) {
069    StringBuilder sb = new StringBuilder();
070    sb.append("Namespace_").append(tableName.getNamespaceAsString());
071    sb.append("_table_").append(tableName.getQualifierAsString());
072    sb.append("_metric_").append(metric);
073    return sb.toString();
074  }
075
076  private TableMeters getOrCreateTableMeter(TableName tableName) {
077    return metersByTable.computeIfAbsent(tableName, tbn -> new TableMeters(metricRegistry, tbn));
078  }
079
080  @Override
081  public void updateTableReadQueryMeter(TableName tableName, long count) {
082    getOrCreateTableMeter(tableName).updateTableReadQueryMeter(count);
083  }
084
085  @Override
086  public void updateTableReadQueryMeter(TableName tableName) {
087    getOrCreateTableMeter(tableName).updateTableReadQueryMeter();
088  }
089
090  @Override
091  public void updateTableWriteQueryMeter(TableName tableName, long count) {
092    getOrCreateTableMeter(tableName).updateTableWriteQueryMeter(count);
093  }
094
095  @Override
096  public void updateTableWriteQueryMeter(TableName tableName) {
097    getOrCreateTableMeter(tableName).updateTableWriteQueryMeter();
098  }
099}