001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to you under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.hadoop.hbase.master;
018
019import java.util.Map;
020import java.util.Map.Entry;
021
022import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
023import org.apache.hadoop.metrics2.MetricHistogram;
024import org.apache.hadoop.metrics2.MetricsCollector;
025import org.apache.hadoop.metrics2.MetricsRecordBuilder;
026import org.apache.hadoop.metrics2.lib.Interns;
027import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * Implementation of {@link MetricsMasterQuotaSource} which writes the values passed in via the
032 * interface to the metrics backend.
033 */
034@InterfaceAudience.Private
035public class MetricsMasterQuotaSourceImpl extends BaseSourceImpl
036        implements MetricsMasterQuotaSource {
037  private final MetricsMasterWrapper wrapper;
038  private final MutableGaugeLong spaceQuotasGauge;
039  private final MutableGaugeLong tablesViolatingQuotasGauge;
040  private final MutableGaugeLong namespacesViolatingQuotasGauge;
041  private final MutableGaugeLong regionSpaceReportsGauge;
042  private final MetricHistogram quotaObserverTimeHisto;
043  private final MetricHistogram snapshotObserverTimeHisto;
044  private final MetricHistogram snapshotObserverSizeComputationTimeHisto;
045  private final MetricHistogram snapshotObserverSnapshotFetchTimeHisto;
046
047  public MetricsMasterQuotaSourceImpl(MetricsMasterWrapper wrapper) {
048    this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT, wrapper);
049  }
050
051  public MetricsMasterQuotaSourceImpl(
052      String metricsName, String metricsDescription, String metricsContext,
053      String metricsJmxContext, MetricsMasterWrapper wrapper) {
054    super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
055    this.wrapper = wrapper;
056
057    spaceQuotasGauge = getMetricsRegistry().newGauge(
058        NUM_SPACE_QUOTAS_NAME, NUM_SPACE_QUOTAS_DESC, 0L);
059    tablesViolatingQuotasGauge = getMetricsRegistry().newGauge(
060        NUM_TABLES_QUOTA_VIOLATIONS_NAME, NUM_TABLES_QUOTA_VIOLATIONS_DESC, 0L);
061    namespacesViolatingQuotasGauge = getMetricsRegistry().newGauge(
062        NUM_NS_QUOTA_VIOLATIONS_NAME, NUM_NS_QUOTA_VIOLATIONS_DESC, 0L);
063    regionSpaceReportsGauge = getMetricsRegistry().newGauge(
064        NUM_REGION_SIZE_REPORTS_NAME, NUM_REGION_SIZE_REPORTS_DESC, 0L);
065
066    quotaObserverTimeHisto = getMetricsRegistry().newTimeHistogram(
067        QUOTA_OBSERVER_CHORE_TIME_NAME, QUOTA_OBSERVER_CHORE_TIME_DESC);
068    snapshotObserverTimeHisto = getMetricsRegistry().newTimeHistogram(
069        SNAPSHOT_OBSERVER_CHORE_TIME_NAME, SNAPSHOT_OBSERVER_CHORE_TIME_DESC);
070
071    snapshotObserverSizeComputationTimeHisto = getMetricsRegistry().newTimeHistogram(
072        SNAPSHOT_OBSERVER_SIZE_COMPUTATION_TIME_NAME, SNAPSHOT_OBSERVER_SIZE_COMPUTATION_TIME_DESC);
073    snapshotObserverSnapshotFetchTimeHisto = getMetricsRegistry().newTimeHistogram(
074        SNAPSHOT_OBSERVER_FETCH_TIME_NAME, SNAPSHOT_OBSERVER_FETCH_TIME_DESC);
075  }
076
077  @Override
078  public void updateNumSpaceQuotas(long numSpaceQuotas) {
079    spaceQuotasGauge.set(numSpaceQuotas);
080  }
081
082  @Override
083  public void updateNumTablesInSpaceQuotaViolation(long numTablesInViolation) {
084    tablesViolatingQuotasGauge.set(numTablesInViolation);
085  }
086
087  @Override
088  public void updateNumNamespacesInSpaceQuotaViolation(long numNamespacesInViolation) {
089    namespacesViolatingQuotasGauge.set(numNamespacesInViolation);
090  }
091
092  @Override
093  public void updateNumCurrentSpaceQuotaRegionSizeReports(long numCurrentRegionSizeReports) {
094    regionSpaceReportsGauge.set(numCurrentRegionSizeReports);
095  }
096
097  @Override
098  public void incrementSpaceQuotaObserverChoreTime(long time) {
099    quotaObserverTimeHisto.add(time);
100  }
101
102  @Override
103  public void incrementSnapshotObserverChoreTime(long time) {
104    snapshotObserverTimeHisto.add(time);
105  }
106
107  @Override
108  public void getMetrics(MetricsCollector metricsCollector, boolean all) {
109    MetricsRecordBuilder record = metricsCollector.addRecord(metricsRegistry.info());
110    if (wrapper != null) {
111      // Summarize the tables
112      Map<String,Entry<Long,Long>> tableUsages = wrapper.getTableSpaceUtilization();
113      String tableSummary = "[]";
114      if (tableUsages != null && !tableUsages.isEmpty()) {
115        tableSummary = generateJsonQuotaSummary(tableUsages.entrySet(), "table");
116      }
117      record.tag(Interns.info(TABLE_QUOTA_USAGE_NAME, TABLE_QUOTA_USAGE_DESC), tableSummary);
118
119      // Summarize the namespaces
120      String nsSummary = "[]";
121      Map<String,Entry<Long,Long>> namespaceUsages = wrapper.getNamespaceSpaceUtilization();
122      if (namespaceUsages != null && !namespaceUsages.isEmpty()) {
123        nsSummary = generateJsonQuotaSummary(namespaceUsages.entrySet(), "namespace");
124      }
125      record.tag(Interns.info(NS_QUOTA_USAGE_NAME, NS_QUOTA_USAGE_DESC), nsSummary);
126    }
127    metricsRegistry.snapshot(record, all);
128  }
129
130  /**
131   * Summarizes the usage and limit for many targets (table or namespace) into JSON.
132   */
133  private String generateJsonQuotaSummary(
134      Iterable<Entry<String,Entry<Long,Long>>> data, String target) {
135    StringBuilder sb = new StringBuilder();
136    for (Entry<String,Entry<Long,Long>> tableUsage : data) {
137      String tableName = tableUsage.getKey();
138      long usage = tableUsage.getValue().getKey();
139      long limit = tableUsage.getValue().getValue();
140      if (sb.length() > 0) {
141        sb.append(", ");
142      }
143      sb.append("{").append(target).append("=").append(tableName).append(", usage=").append(usage)
144          .append(", limit=").append(limit).append("}");
145    }
146    sb.insert(0, "[").append("]");
147    return sb.toString();
148  }
149
150  @Override
151  public void incrementSnapshotObserverSnapshotComputationTime(long time) {
152    snapshotObserverSizeComputationTimeHisto.add(time);
153  }
154
155  @Override
156  public void incrementSnapshotObserverSnapshotFetchTime(long time) {
157    snapshotObserverSnapshotFetchTimeHisto.add(time);
158  }
159}