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   * @param inc How much to add to read requests.
080   */
081  public void incrementReadRequests(final long inc) {
082    masterSource.incReadRequests(inc);
083  }
084
085  /**
086   * @param inc How much to add to write requests.
087   */
088  public void incrementWriteRequests(final long inc) {
089    masterSource.incWriteRequests(inc);
090  }
091
092  /**
093   * Sets the number of space quotas defined.
094   * @see MetricsMasterQuotaSource#updateNumSpaceQuotas(long)
095   */
096  public void setNumSpaceQuotas(final long numSpaceQuotas) {
097    masterQuotaSource.updateNumSpaceQuotas(numSpaceQuotas);
098  }
099
100  /**
101   * Sets the number of table in violation of a space quota.
102   * @see MetricsMasterQuotaSource#updateNumTablesInSpaceQuotaViolation(long)
103   */
104  public void setNumTableInSpaceQuotaViolation(final long numTablesInViolation) {
105    masterQuotaSource.updateNumTablesInSpaceQuotaViolation(numTablesInViolation);
106  }
107
108  /**
109   * Sets the number of namespaces in violation of a space quota.
110   * @see MetricsMasterQuotaSource#updateNumNamespacesInSpaceQuotaViolation(long)
111   */
112  public void setNumNamespacesInSpaceQuotaViolation(final long numNamespacesInViolation) {
113    masterQuotaSource.updateNumNamespacesInSpaceQuotaViolation(numNamespacesInViolation);
114  }
115
116  /**
117   * Sets the number of region size reports the master currently has in memory.
118   * @see MetricsMasterQuotaSource#updateNumCurrentSpaceQuotaRegionSizeReports(long)
119   */
120  public void setNumRegionSizeReports(final long numRegionReports) {
121    masterQuotaSource.updateNumCurrentSpaceQuotaRegionSizeReports(numRegionReports);
122  }
123
124  /**
125   * Sets the execution time of a period of the QuotaObserverChore.
126   * @param executionTime The execution time in milliseconds.
127   * @see MetricsMasterQuotaSource#incrementSpaceQuotaObserverChoreTime(long)
128   */
129  public void incrementQuotaObserverTime(final long executionTime) {
130    masterQuotaSource.incrementSpaceQuotaObserverChoreTime(executionTime);
131  }
132
133  /** Returns Set of metrics for assign procedure */
134  public ProcedureMetrics getServerCrashProcMetrics() {
135    return serverCrashProcMetrics;
136  }
137
138  /**
139   * This is utility function that converts {@link OperationMetrics} to {@link ProcedureMetrics}.
140   * NOTE: Procedure framework in hbase-procedure module accesses metrics common to most procedures
141   * through {@link ProcedureMetrics} interface. Metrics source classes in hbase-hadoop-compat
142   * module provides similar interface {@link OperationMetrics} that contains metrics common to most
143   * operations. As both hbase-procedure and hbase-hadoop-compat are lower level modules used by
144   * hbase-server (this) module and there is no dependency between them, this method does the
145   * required conversion.
146   */
147  public static ProcedureMetrics convertToProcedureMetrics(final OperationMetrics metrics) {
148    return new ProcedureMetrics() {
149      @Override
150      public Counter getSubmittedCounter() {
151        return metrics.getSubmittedCounter();
152      }
153
154      @Override
155      public Histogram getTimeHisto() {
156        return metrics.getTimeHisto();
157      }
158
159      @Override
160      public Counter getFailedCounter() {
161        return metrics.getFailedCounter();
162      }
163    };
164  }
165
166  /**
167   * Sets the execution time of a period of the {@code SnapshotQuotaObserverChore}.
168   */
169  public void incrementSnapshotObserverTime(final long executionTime) {
170    masterQuotaSource.incrementSnapshotObserverChoreTime(executionTime);
171  }
172
173  /**
174   * Sets the execution time to compute the size of a single snapshot.
175   */
176  public void incrementSnapshotSizeComputationTime(final long executionTime) {
177    masterQuotaSource.incrementSnapshotObserverSnapshotComputationTime(executionTime);
178  }
179
180  /**
181   * Sets the execution time to fetch the mapping of snapshots to originating table.
182   */
183  public void incrementSnapshotFetchTime(long executionTime) {
184    masterQuotaSource.incrementSnapshotObserverSnapshotFetchTime(executionTime);
185  }
186}