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.master;
019
020import java.util.Map;
021import java.util.Map.Entry;
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(String metricsName, String metricsDescription,
052    String metricsContext, String metricsJmxContext, MetricsMasterWrapper wrapper) {
053    super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
054    this.wrapper = wrapper;
055
056    spaceQuotasGauge =
057      getMetricsRegistry().newGauge(NUM_SPACE_QUOTAS_NAME, NUM_SPACE_QUOTAS_DESC, 0L);
058    tablesViolatingQuotasGauge = getMetricsRegistry().newGauge(NUM_TABLES_QUOTA_VIOLATIONS_NAME,
059      NUM_TABLES_QUOTA_VIOLATIONS_DESC, 0L);
060    namespacesViolatingQuotasGauge =
061      getMetricsRegistry().newGauge(NUM_NS_QUOTA_VIOLATIONS_NAME, NUM_NS_QUOTA_VIOLATIONS_DESC, 0L);
062    regionSpaceReportsGauge =
063      getMetricsRegistry().newGauge(NUM_REGION_SIZE_REPORTS_NAME, NUM_REGION_SIZE_REPORTS_DESC, 0L);
064
065    quotaObserverTimeHisto = getMetricsRegistry().newTimeHistogram(QUOTA_OBSERVER_CHORE_TIME_NAME,
066      QUOTA_OBSERVER_CHORE_TIME_DESC);
067    snapshotObserverTimeHisto = getMetricsRegistry()
068      .newTimeHistogram(SNAPSHOT_OBSERVER_CHORE_TIME_NAME, SNAPSHOT_OBSERVER_CHORE_TIME_DESC);
069
070    snapshotObserverSizeComputationTimeHisto = getMetricsRegistry().newTimeHistogram(
071      SNAPSHOT_OBSERVER_SIZE_COMPUTATION_TIME_NAME, SNAPSHOT_OBSERVER_SIZE_COMPUTATION_TIME_DESC);
072    snapshotObserverSnapshotFetchTimeHisto = getMetricsRegistry()
073      .newTimeHistogram(SNAPSHOT_OBSERVER_FETCH_TIME_NAME, SNAPSHOT_OBSERVER_FETCH_TIME_DESC);
074  }
075
076  @Override
077  public void updateNumSpaceQuotas(long numSpaceQuotas) {
078    spaceQuotasGauge.set(numSpaceQuotas);
079  }
080
081  @Override
082  public void updateNumTablesInSpaceQuotaViolation(long numTablesInViolation) {
083    tablesViolatingQuotasGauge.set(numTablesInViolation);
084  }
085
086  @Override
087  public void updateNumNamespacesInSpaceQuotaViolation(long numNamespacesInViolation) {
088    namespacesViolatingQuotasGauge.set(numNamespacesInViolation);
089  }
090
091  @Override
092  public void updateNumCurrentSpaceQuotaRegionSizeReports(long numCurrentRegionSizeReports) {
093    regionSpaceReportsGauge.set(numCurrentRegionSizeReports);
094  }
095
096  @Override
097  public void incrementSpaceQuotaObserverChoreTime(long time) {
098    quotaObserverTimeHisto.add(time);
099  }
100
101  @Override
102  public void incrementSnapshotObserverChoreTime(long time) {
103    snapshotObserverTimeHisto.add(time);
104  }
105
106  @Override
107  public void getMetrics(MetricsCollector metricsCollector, boolean all) {
108    MetricsRecordBuilder record = metricsCollector.addRecord(metricsRegistry.info());
109    if (wrapper != null) {
110      // Summarize the tables
111      Map<String, Entry<Long, Long>> tableUsages = wrapper.getTableSpaceUtilization();
112      String tableSummary = "[]";
113      if (tableUsages != null && !tableUsages.isEmpty()) {
114        tableSummary = generateJsonQuotaSummary(tableUsages.entrySet(), "table");
115      }
116      record.tag(Interns.info(TABLE_QUOTA_USAGE_NAME, TABLE_QUOTA_USAGE_DESC), tableSummary);
117
118      // Summarize the namespaces
119      String nsSummary = "[]";
120      Map<String, Entry<Long, Long>> namespaceUsages = wrapper.getNamespaceSpaceUtilization();
121      if (namespaceUsages != null && !namespaceUsages.isEmpty()) {
122        nsSummary = generateJsonQuotaSummary(namespaceUsages.entrySet(), "namespace");
123      }
124      record.tag(Interns.info(NS_QUOTA_USAGE_NAME, NS_QUOTA_USAGE_DESC), nsSummary);
125    }
126    metricsRegistry.snapshot(record, all);
127  }
128
129  /**
130   * Summarizes the usage and limit for many targets (table or namespace) into JSON.
131   */
132  private String generateJsonQuotaSummary(Iterable<Entry<String, Entry<Long, Long>>> data,
133    String target) {
134    StringBuilder sb = new StringBuilder();
135    for (Entry<String, Entry<Long, Long>> tableUsage : data) {
136      String tableName = tableUsage.getKey();
137      long usage = tableUsage.getValue().getKey();
138      long limit = tableUsage.getValue().getValue();
139      if (sb.length() > 0) {
140        sb.append(", ");
141      }
142      sb.append("{").append(target).append("=").append(tableName).append(", usage=").append(usage)
143        .append(", limit=").append(limit).append("}");
144    }
145    sb.insert(0, "[").append("]");
146    return sb.toString();
147  }
148
149  @Override
150  public void incrementSnapshotObserverSnapshotComputationTime(long time) {
151    snapshotObserverSizeComputationTimeHisto.add(time);
152  }
153
154  @Override
155  public void incrementSnapshotObserverSnapshotFetchTime(long time) {
156    snapshotObserverSnapshotFetchTimeHisto.add(time);
157  }
158}