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 */
018
019package org.apache.hadoop.hbase.master;
020
021import org.apache.yetus.audience.InterfaceAudience;
022import org.apache.yetus.audience.InterfaceStability;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
026import org.apache.hadoop.hbase.metrics.Counter;
027import org.apache.hadoop.hbase.metrics.Histogram;
028import org.apache.hadoop.hbase.metrics.OperationMetrics;
029import org.apache.hadoop.hbase.procedure2.ProcedureMetrics;
030
031/**
032 * This class is for maintaining the various master statistics
033 * and publishing them through the metrics interfaces.
034 * <p>
035 * This class has a number of metrics variables that are publicly accessible;
036 * these variables (objects) have methods to update their values.
037 */
038@InterfaceStability.Evolving
039@InterfaceAudience.Private
040public class MetricsMaster {
041  private static final Logger LOG = LoggerFactory.getLogger(MetricsMaster.class);
042  private MetricsMasterSource masterSource;
043  private MetricsMasterProcSource masterProcSource;
044  private MetricsMasterQuotaSource masterQuotaSource;
045
046  private ProcedureMetrics serverCrashProcMetrics;
047
048  public MetricsMaster(MetricsMasterWrapper masterWrapper) {
049    masterSource = CompatibilitySingletonFactory.getInstance(MetricsMasterSourceFactory.class).create(masterWrapper);
050    masterProcSource =
051            CompatibilitySingletonFactory.getInstance(MetricsMasterProcSourceFactory.class).create(masterWrapper);
052    masterQuotaSource =
053            CompatibilitySingletonFactory.getInstance(MetricsMasterQuotaSourceFactory.class).create(masterWrapper);
054
055    serverCrashProcMetrics = convertToProcedureMetrics(masterSource.getServerCrashMetrics());
056  }
057
058  // for unit-test usage
059  public MetricsMasterSource getMetricsSource() {
060    return masterSource;
061  }
062
063  public MetricsMasterProcSource getMetricsProcSource() {
064    return masterProcSource;
065  }
066
067  public MetricsMasterQuotaSource getMetricsQuotaSource() {
068    return masterQuotaSource;
069  }
070
071  /**
072   * @param inc How much to add to requests.
073   */
074  public void incrementRequests(final long inc) {
075    masterSource.incRequests(inc);
076  }
077
078  /**
079   * Sets the number of space quotas defined.
080   *
081   * @see MetricsMasterQuotaSource#updateNumSpaceQuotas(long)
082   */
083  public void setNumSpaceQuotas(final long numSpaceQuotas) {
084    masterQuotaSource.updateNumSpaceQuotas(numSpaceQuotas);
085  }
086
087  /**
088   * Sets the number of table in violation of a space quota.
089   *
090   * @see MetricsMasterQuotaSource#updateNumTablesInSpaceQuotaViolation(long)
091   */
092  public void setNumTableInSpaceQuotaViolation(final long numTablesInViolation) {
093    masterQuotaSource.updateNumTablesInSpaceQuotaViolation(numTablesInViolation);
094  }
095
096  /**
097   * Sets the number of namespaces in violation of a space quota.
098   *
099   * @see MetricsMasterQuotaSource#updateNumNamespacesInSpaceQuotaViolation(long)
100   */
101  public void setNumNamespacesInSpaceQuotaViolation(final long numNamespacesInViolation) {
102    masterQuotaSource.updateNumNamespacesInSpaceQuotaViolation(numNamespacesInViolation);
103  }
104
105  /**
106   * Sets the number of region size reports the master currently has in memory.
107   *
108   * @see MetricsMasterQuotaSource#updateNumCurrentSpaceQuotaRegionSizeReports(long)
109   */
110  public void setNumRegionSizeReports(final long numRegionReports) {
111    masterQuotaSource.updateNumCurrentSpaceQuotaRegionSizeReports(numRegionReports);
112  }
113
114  /**
115   * Sets the execution time of a period of the QuotaObserverChore.
116   *
117   * @param executionTime The execution time in milliseconds.
118   * @see MetricsMasterQuotaSource#incrementSpaceQuotaObserverChoreTime(long)
119   */
120  public void incrementQuotaObserverTime(final long executionTime) {
121    masterQuotaSource.incrementSpaceQuotaObserverChoreTime(executionTime);
122  }
123
124  /**
125   * @return Set of metrics for assign procedure
126   */
127  public ProcedureMetrics getServerCrashProcMetrics() {
128    return serverCrashProcMetrics;
129  }
130
131  /**
132   * This is utility function that converts {@link OperationMetrics} to {@link ProcedureMetrics}.
133   *
134   * NOTE: Procedure framework in hbase-procedure module accesses metrics common to most procedures
135   * through {@link ProcedureMetrics} interface. Metrics source classes in hbase-hadoop-compat
136   * module provides similar interface {@link OperationMetrics} that contains metrics common to
137   * most operations. As both hbase-procedure and hbase-hadoop-compat are lower level modules used
138   * by hbase-server (this) module and there is no dependency between them, this method does the
139   * required conversion.
140   */
141  public static ProcedureMetrics convertToProcedureMetrics(final OperationMetrics metrics) {
142    return new ProcedureMetrics() {
143      @Override
144      public Counter getSubmittedCounter() {
145        return metrics.getSubmittedCounter();
146      }
147
148      @Override
149      public Histogram getTimeHisto() {
150        return metrics.getTimeHisto();
151      }
152
153      @Override
154      public Counter getFailedCounter() {
155        return metrics.getFailedCounter();
156      }
157    };
158  }
159
160  /**
161   * Sets the execution time of a period of the {@code SnapshotQuotaObserverChore}.
162   */
163  public void incrementSnapshotObserverTime(final long executionTime) {
164    masterQuotaSource.incrementSnapshotObserverChoreTime(executionTime);
165  }
166
167  /**
168   * Sets the execution time to compute the size of a single snapshot.
169   */
170  public void incrementSnapshotSizeComputationTime(final long executionTime) {
171    masterQuotaSource.incrementSnapshotObserverSnapshotComputationTime(executionTime);
172  }
173
174  /**
175   * Sets the execution time to fetch the mapping of snapshots to originating table.
176   */
177  public void incrementSnapshotFetchTime(long executionTime) {
178    masterQuotaSource.incrementSnapshotObserverSnapshotFetchTime(executionTime);
179  }
180}