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.hdfs.server.namenode.metrics;
019
020import static org.apache.hadoop.metrics2.impl.MsInfo.ProcessName;
021import static org.apache.hadoop.metrics2.impl.MsInfo.SessionId;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hdfs.DFSConfigKeys;
025import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.NamenodeRole;
026import org.apache.hadoop.metrics2.MetricsSystem;
027import org.apache.hadoop.metrics2.annotation.Metric;
028import org.apache.hadoop.metrics2.annotation.Metrics;
029import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
030import org.apache.hadoop.metrics2.lib.MetricsRegistry;
031import org.apache.hadoop.metrics2.lib.MutableCounterLong;
032import org.apache.hadoop.metrics2.lib.MutableGaugeInt;
033import org.apache.hadoop.metrics2.lib.MutableQuantiles;
034import org.apache.hadoop.metrics2.lib.MutableRate;
035import org.apache.hadoop.metrics2.source.JvmMetrics;
036
037/**
038 * This class is for maintaining  the various NameNode activity statistics
039 * and publishing them through the metrics interfaces.
040 */
041@Metrics(name="NameNodeActivity", about="NameNode metrics", context="dfs")
042public class NameNodeMetrics {
043  final MetricsRegistry registry = new MetricsRegistry("namenode");
044
045  @Metric MutableCounterLong createFileOps;
046  @Metric MutableCounterLong filesCreated;
047  @Metric MutableCounterLong filesAppended;
048  @Metric MutableCounterLong getBlockLocations;
049  @Metric MutableCounterLong filesRenamed;
050  @Metric MutableCounterLong filesTruncated;
051  @Metric MutableCounterLong getListingOps;
052  @Metric MutableCounterLong deleteFileOps;
053  @Metric("Number of files/dirs deleted by delete or rename operations")
054  MutableCounterLong filesDeleted;
055  @Metric MutableCounterLong fileInfoOps;
056  @Metric MutableCounterLong addBlockOps;
057  @Metric MutableCounterLong getAdditionalDatanodeOps;
058  @Metric MutableCounterLong createSymlinkOps;
059  @Metric MutableCounterLong getLinkTargetOps;
060  @Metric MutableCounterLong filesInGetListingOps;
061  @Metric("Number of allowSnapshot operations")
062  MutableCounterLong allowSnapshotOps;
063  @Metric("Number of disallowSnapshot operations")
064  MutableCounterLong disallowSnapshotOps;
065  @Metric("Number of createSnapshot operations")
066  MutableCounterLong createSnapshotOps;
067  @Metric("Number of deleteSnapshot operations")
068  MutableCounterLong deleteSnapshotOps;
069  @Metric("Number of renameSnapshot operations")
070  MutableCounterLong renameSnapshotOps;
071  @Metric("Number of listSnapshottableDirectory operations")
072  MutableCounterLong listSnapshottableDirOps;
073  @Metric("Number of snapshotDiffReport operations")
074  MutableCounterLong snapshotDiffReportOps;
075  @Metric("Number of blockReceivedAndDeleted calls")
076  MutableCounterLong blockReceivedAndDeletedOps;
077  @Metric("Number of blockReports from individual storages")
078  MutableCounterLong storageBlockReportOps;
079
080  @Metric("Number of file system operations")
081  public long totalFileOps(){
082    return
083      getBlockLocations.value() +
084      createFileOps.value() +
085      filesAppended.value() +
086      addBlockOps.value() +
087      getAdditionalDatanodeOps.value() +
088      filesRenamed.value() +
089      filesTruncated.value() +
090      deleteFileOps.value() +
091      getListingOps.value() +
092      fileInfoOps.value() +
093      getLinkTargetOps.value() +
094      createSnapshotOps.value() +
095      deleteSnapshotOps.value() +
096      allowSnapshotOps.value() +
097      disallowSnapshotOps.value() +
098      renameSnapshotOps.value() +
099      listSnapshottableDirOps.value() +
100      createSymlinkOps.value() +
101      snapshotDiffReportOps.value();
102  }
103
104
105  @Metric("Journal transactions") MutableRate transactions;
106  @Metric("Journal syncs") MutableRate syncs;
107  final MutableQuantiles[] syncsQuantiles;
108  @Metric("Journal transactions batched in sync")
109  MutableCounterLong transactionsBatchedInSync;
110  @Metric("Block report") MutableRate blockReport;
111  final MutableQuantiles[] blockReportQuantiles;
112  @Metric("Cache report") MutableRate cacheReport;
113  final MutableQuantiles[] cacheReportQuantiles;
114
115  @Metric("Duration in SafeMode at startup in msec")
116  MutableGaugeInt safeModeTime;
117  @Metric("Time loading FS Image at startup in msec")
118  MutableGaugeInt fsImageLoadTime;
119
120  @Metric("GetImageServlet getEdit")
121  MutableRate getEdit;
122  @Metric("GetImageServlet getImage")
123  MutableRate getImage;
124  @Metric("GetImageServlet putImage")
125  MutableRate putImage;
126
127  JvmMetrics jvmMetrics = null;
128  
129  NameNodeMetrics(String processName, String sessionId, int[] intervals,
130      final JvmMetrics jvmMetrics) {
131    this.jvmMetrics = jvmMetrics;
132    registry.tag(ProcessName, processName).tag(SessionId, sessionId);
133    
134    final int len = intervals.length;
135    syncsQuantiles = new MutableQuantiles[len];
136    blockReportQuantiles = new MutableQuantiles[len];
137    cacheReportQuantiles = new MutableQuantiles[len];
138    
139    for (int i = 0; i < len; i++) {
140      int interval = intervals[i];
141      syncsQuantiles[i] = registry.newQuantiles(
142          "syncs" + interval + "s",
143          "Journal syncs", "ops", "latency", interval);
144      blockReportQuantiles[i] = registry.newQuantiles(
145          "blockReport" + interval + "s", 
146          "Block report", "ops", "latency", interval);
147      cacheReportQuantiles[i] = registry.newQuantiles(
148          "cacheReport" + interval + "s",
149          "Cache report", "ops", "latency", interval);
150    }
151  }
152
153  public static NameNodeMetrics create(Configuration conf, NamenodeRole r) {
154    String sessionId = conf.get(DFSConfigKeys.DFS_METRICS_SESSION_ID_KEY);
155    String processName = r.toString();
156    MetricsSystem ms = DefaultMetricsSystem.instance();
157    JvmMetrics jm = JvmMetrics.create(processName, sessionId, ms);
158    
159    // Percentile measurement is off by default, by watching no intervals
160    int[] intervals = 
161        conf.getInts(DFSConfigKeys.DFS_METRICS_PERCENTILES_INTERVALS_KEY);
162    return ms.register(new NameNodeMetrics(processName, sessionId,
163        intervals, jm));
164  }
165
166  public JvmMetrics getJvmMetrics() {
167    return jvmMetrics;
168  }
169  
170  public void shutdown() {
171    DefaultMetricsSystem.shutdown();
172  }
173
174  public void incrGetBlockLocations() {
175    getBlockLocations.incr();
176  }
177
178  public void incrFilesCreated() {
179    filesCreated.incr();
180  }
181
182  public void incrCreateFileOps() {
183    createFileOps.incr();
184  }
185
186  public void incrFilesAppended() {
187    filesAppended.incr();
188  }
189
190  public void incrAddBlockOps() {
191    addBlockOps.incr();
192  }
193  
194  public void incrGetAdditionalDatanodeOps() {
195    getAdditionalDatanodeOps.incr();
196  }
197
198  public void incrFilesRenamed() {
199    filesRenamed.incr();
200  }
201
202  public void incrFilesTruncated() {
203    filesTruncated.incr();
204  }
205
206  public void incrFilesDeleted(long delta) {
207    filesDeleted.incr(delta);
208  }
209
210  public void incrDeleteFileOps() {
211    deleteFileOps.incr();
212  }
213
214  public void incrGetListingOps() {
215    getListingOps.incr();
216  }
217
218  public void incrFilesInGetListingOps(int delta) {
219    filesInGetListingOps.incr(delta);
220  }
221
222  public void incrFileInfoOps() {
223    fileInfoOps.incr();
224  }
225
226  public void incrCreateSymlinkOps() {
227    createSymlinkOps.incr();
228  }
229
230  public void incrGetLinkTargetOps() {
231    getLinkTargetOps.incr();
232  }
233
234  public void incrAllowSnapshotOps() {
235    allowSnapshotOps.incr();
236  }
237  
238  public void incrDisAllowSnapshotOps() {
239    disallowSnapshotOps.incr();
240  }
241  
242  public void incrCreateSnapshotOps() {
243    createSnapshotOps.incr();
244  }
245  
246  public void incrDeleteSnapshotOps() {
247    deleteSnapshotOps.incr();
248  }
249  
250  public void incrRenameSnapshotOps() {
251    renameSnapshotOps.incr();
252  }
253  
254  public void incrListSnapshottableDirOps() {
255    listSnapshottableDirOps.incr();
256  }
257  
258  public void incrSnapshotDiffReportOps() {
259    snapshotDiffReportOps.incr();
260  }
261  
262  public void incrBlockReceivedAndDeletedOps() {
263    blockReceivedAndDeletedOps.incr();
264  }
265  
266  public void incrStorageBlockReportOps() {
267    storageBlockReportOps.incr();
268  }
269
270  public void addTransaction(long latency) {
271    transactions.add(latency);
272  }
273
274  public void incrTransactionsBatchedInSync() {
275    transactionsBatchedInSync.incr();
276  }
277
278  public void addSync(long elapsed) {
279    syncs.add(elapsed);
280    for (MutableQuantiles q : syncsQuantiles) {
281      q.add(elapsed);
282    }
283  }
284
285  public void setFsImageLoadTime(long elapsed) {
286    fsImageLoadTime.set((int) elapsed);
287  }
288
289  public void addBlockReport(long latency) {
290    blockReport.add(latency);
291    for (MutableQuantiles q : blockReportQuantiles) {
292      q.add(latency);
293    }
294  }
295
296  public void addCacheBlockReport(long latency) {
297    cacheReport.add(latency);
298    for (MutableQuantiles q : cacheReportQuantiles) {
299      q.add(latency);
300    }
301  }
302
303  public void setSafeModeTime(long elapsed) {
304    safeModeTime.set((int) elapsed);
305  }
306
307  public void addGetEdit(long latency) {
308    getEdit.add(latency);
309  }
310
311  public void addGetImage(long latency) {
312    getImage.add(latency);
313  }
314
315  public void addPutImage(long latency) {
316    putImage.add(latency);
317  }
318}