View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master.metrics;
19  
20  import java.io.IOException;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.hbase.metrics.HBaseInfo;
25  import org.apache.hadoop.hbase.metrics.MetricsRate;
26  import org.apache.hadoop.hbase.metrics.PersistentMetricsTimeVaryingRate;
27  import org.apache.hadoop.metrics.ContextFactory;
28  import org.apache.hadoop.metrics.MetricsContext;
29  import org.apache.hadoop.metrics.MetricsRecord;
30  import org.apache.hadoop.metrics.MetricsUtil;
31  import org.apache.hadoop.metrics.Updater;
32  import org.apache.hadoop.metrics.jvm.JvmMetrics;
33  import org.apache.hadoop.metrics.util.MetricsLongValue;
34  import org.apache.hadoop.metrics.util.MetricsRegistry;
35  
36  
37  /**
38   * This class is for maintaining the various master statistics
39   * and publishing them through the metrics interfaces.
40   * <p>
41   * This class has a number of metrics variables that are publicly accessible;
42   * these variables (objects) have methods to update their values.
43   */
44  public class MasterMetrics implements Updater {
45    private final Log LOG = LogFactory.getLog(this.getClass());
46    private final MetricsRecord metricsRecord;
47    private final MetricsRegistry registry = new MetricsRegistry();
48    private final MasterStatistics masterStatistics;
49  
50    private long lastUpdate = System.currentTimeMillis();
51    private long lastExtUpdate = System.currentTimeMillis();
52    private long extendedPeriod = 0;
53  
54    /*
55     * Count of requests to the cluster since last call to metrics update
56     */
57    private final MetricsRate cluster_requests =
58      new MetricsRate("cluster_requests", registry);
59  
60    /** Time it takes to finish HLog.splitLog() */
61    final PersistentMetricsTimeVaryingRate splitTime =
62      new PersistentMetricsTimeVaryingRate("splitTime", registry);
63  
64    /** Size of HLog files being split */
65    final PersistentMetricsTimeVaryingRate splitSize =
66      new PersistentMetricsTimeVaryingRate("splitSize", registry);
67  
68    /** Time it takes to finish snapshot() */
69    final PersistentMetricsTimeVaryingRate snapshotTime =
70      new PersistentMetricsTimeVaryingRate("snapshotTime", registry);
71  
72    /** Time it takes to finish restoreSnapshot() */
73    final PersistentMetricsTimeVaryingRate snapshotRestoreTime =
74      new PersistentMetricsTimeVaryingRate("snapshotRestoreTime", registry);
75  
76    /** Time it takes to finish cloneSnapshotTime() */
77    final PersistentMetricsTimeVaryingRate snapshotCloneTime =
78      new PersistentMetricsTimeVaryingRate("snapshotCloneTime", registry);
79  
80    public MasterMetrics(final String name) {
81      MetricsContext context = MetricsUtil.getContext("hbase");
82      metricsRecord = MetricsUtil.createRecord(context, "master");
83      metricsRecord.setTag("Master", name);
84      context.registerUpdater(this);
85      JvmMetrics.init("Master", name);
86      HBaseInfo.init();
87  
88      // expose the MBean for metrics
89      masterStatistics = new MasterStatistics(this.registry);
90  
91      // get custom attributes
92      try {
93        Object m = 
94          ContextFactory.getFactory().getAttribute("hbase.extendedperiod");
95        if (m instanceof String) {
96          this.extendedPeriod = Long.parseLong((String) m)*1000;
97        }
98      } catch (IOException ioe) {
99        LOG.info("Couldn't load ContextFactory for Metrics config info");
100     }
101 
102     LOG.info("Initialized");
103   }
104 
105   public void shutdown() {
106     if (masterStatistics != null)
107       masterStatistics.shutdown();
108   }
109 
110   /**
111    * Since this object is a registered updater, this method will be called
112    * periodically, e.g. every 5 seconds.
113    * @param unused
114    */
115   public void doUpdates(MetricsContext unused) {
116     synchronized (this) {
117       this.lastUpdate = System.currentTimeMillis();
118 
119       // has the extended period for long-living stats elapsed?
120       if (this.extendedPeriod > 0 &&
121           this.lastUpdate - this.lastExtUpdate >= this.extendedPeriod) {
122         this.lastExtUpdate = this.lastUpdate;
123         this.splitTime.resetMinMaxAvg();
124         this.splitSize.resetMinMaxAvg();
125         this.resetAllMinMax();
126       }
127 
128       this.cluster_requests.pushMetric(metricsRecord);
129       this.splitTime.pushMetric(metricsRecord);
130       this.splitSize.pushMetric(metricsRecord);
131     }
132     this.metricsRecord.update();
133   }
134 
135   public void resetAllMinMax() {
136     // Nothing to do
137   }
138   
139   /**
140    * Record a single instance of a split
141    * @param time time that the split took
142    * @param size length of original HLogs that were split
143    */
144   public synchronized void addSplit(long time, long size) {
145     splitTime.inc(time);
146     splitSize.inc(size);
147   }
148 
149   /**
150    * @return Count of requests.
151    */
152   public float getRequests() {
153     return this.cluster_requests.getPreviousIntervalValue();
154   }
155 
156   /**
157    * @param inc How much to add to requests.
158    */
159   public void incrementRequests(final int inc) {
160     this.cluster_requests.inc(inc);
161   }
162 
163   /**
164    * Record a single instance of a snapshot
165    * @param time time that the snapshot took
166    */
167   public void addSnapshot(long time) {
168     snapshotTime.inc(time);
169   }
170 
171   /**
172    * Record a single instance of a snapshot
173    * @param time time that the snapshot restore took
174    */
175   public void addSnapshotRestore(long time) {
176     snapshotRestoreTime.inc(time);
177   }
178 
179   /**
180    * Record a single instance of a snapshot cloned table
181    * @param time time that the snapshot clone took
182    */
183   public void addSnapshotClone(long time) {
184     snapshotCloneTime.inc(time);
185   }
186 }