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