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    package org.apache.hadoop.hdfs.server.namenode.metrics;
019    
020    import static org.apache.hadoop.metrics2.impl.MsInfo.ProcessName;
021    import static org.apache.hadoop.metrics2.impl.MsInfo.SessionId;
022    
023    import org.apache.hadoop.conf.Configuration;
024    import org.apache.hadoop.hdfs.DFSConfigKeys;
025    import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.NamenodeRole;
026    import org.apache.hadoop.metrics2.MetricsSystem;
027    import org.apache.hadoop.metrics2.annotation.Metric;
028    import org.apache.hadoop.metrics2.annotation.Metrics;
029    import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
030    import org.apache.hadoop.metrics2.lib.MetricsRegistry;
031    import org.apache.hadoop.metrics2.lib.MutableCounterLong;
032    import org.apache.hadoop.metrics2.lib.MutableGaugeInt;
033    import org.apache.hadoop.metrics2.lib.MutableQuantiles;
034    import org.apache.hadoop.metrics2.lib.MutableRate;
035    import 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")
042    public 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 getListingOps;
051      @Metric MutableCounterLong deleteFileOps;
052      @Metric("Number of files/dirs deleted by delete or rename operations")
053      MutableCounterLong filesDeleted;
054      @Metric MutableCounterLong fileInfoOps;
055      @Metric MutableCounterLong addBlockOps;
056      @Metric MutableCounterLong getAdditionalDatanodeOps;
057      @Metric MutableCounterLong createSymlinkOps;
058      @Metric MutableCounterLong getLinkTargetOps;
059      @Metric MutableCounterLong filesInGetListingOps;
060    
061      @Metric("Journal transactions") MutableRate transactions;
062      @Metric("Journal syncs") MutableRate syncs;
063      MutableQuantiles[] syncsQuantiles;
064      @Metric("Journal transactions batched in sync")
065      MutableCounterLong transactionsBatchedInSync;
066      @Metric("Block report") MutableRate blockReport;
067      MutableQuantiles[] blockReportQuantiles;
068    
069      @Metric("Duration in SafeMode at startup") MutableGaugeInt safeModeTime;
070      @Metric("Time loading FS Image at startup") MutableGaugeInt fsImageLoadTime;
071    
072      NameNodeMetrics(String processName, String sessionId, int[] intervals) {
073        registry.tag(ProcessName, processName).tag(SessionId, sessionId);
074        
075        final int len = intervals.length;
076        syncsQuantiles = new MutableQuantiles[len];
077        blockReportQuantiles = new MutableQuantiles[len];
078        
079        for (int i = 0; i < len; i++) {
080          int interval = intervals[i];
081          syncsQuantiles[i] = registry.newQuantiles(
082              "syncs" + interval + "s",
083              "Journal syncs", "ops", "latency", interval);
084          blockReportQuantiles[i] = registry.newQuantiles(
085              "blockReport" + interval + "s", 
086              "Block report", "ops", "latency", interval);
087        }
088      }
089    
090      public static NameNodeMetrics create(Configuration conf, NamenodeRole r) {
091        String sessionId = conf.get(DFSConfigKeys.DFS_METRICS_SESSION_ID_KEY);
092        String processName = r.toString();
093        MetricsSystem ms = DefaultMetricsSystem.instance();
094        JvmMetrics.create(processName, sessionId, ms);
095        
096        // Percentile measurement is off by default, by watching no intervals
097        int[] intervals = 
098            conf.getInts(DFSConfigKeys.DFS_METRICS_PERCENTILES_INTERVALS_KEY);
099        return ms.register(new NameNodeMetrics(processName, sessionId, intervals));
100      }
101    
102      public void shutdown() {
103        DefaultMetricsSystem.shutdown();
104      }
105    
106      public void incrGetBlockLocations() {
107        getBlockLocations.incr();
108      }
109    
110      public void incrFilesCreated() {
111        filesCreated.incr();
112      }
113    
114      public void incrCreateFileOps() {
115        createFileOps.incr();
116      }
117    
118      public void incrFilesAppended() {
119        filesAppended.incr();
120      }
121    
122      public void incrAddBlockOps() {
123        addBlockOps.incr();
124      }
125      
126      public void incrGetAdditionalDatanodeOps() {
127        getAdditionalDatanodeOps.incr();
128      }
129    
130      public void incrFilesRenamed() {
131        filesRenamed.incr();
132      }
133    
134      public void incrFilesDeleted(int delta) {
135        filesDeleted.incr(delta);
136      }
137    
138      public void incrDeleteFileOps() {
139        deleteFileOps.incr();
140      }
141    
142      public void incrGetListingOps() {
143        getListingOps.incr();
144      }
145    
146      public void incrFilesInGetListingOps(int delta) {
147        filesInGetListingOps.incr(delta);
148      }
149    
150      public void incrFileInfoOps() {
151        fileInfoOps.incr();
152      }
153    
154      public void incrCreateSymlinkOps() {
155        createSymlinkOps.incr();
156      }
157    
158      public void incrGetLinkTargetOps() {
159        getLinkTargetOps.incr();
160      }
161    
162      public void addTransaction(long latency) {
163        transactions.add(latency);
164      }
165    
166      public void incrTransactionsBatchedInSync() {
167        transactionsBatchedInSync.incr();
168      }
169    
170      public void addSync(long elapsed) {
171        syncs.add(elapsed);
172        for (MutableQuantiles q : syncsQuantiles) {
173          q.add(elapsed);
174        }
175      }
176    
177      public void setFsImageLoadTime(long elapsed) {
178        fsImageLoadTime.set((int) elapsed);
179      }
180    
181      public void addBlockReport(long latency) {
182        blockReport.add(latency);
183        for (MutableQuantiles q : blockReportQuantiles) {
184          q.add(latency);
185        }
186      }
187    
188      public void setSafeModeTime(long elapsed) {
189        safeModeTime.set((int) elapsed);
190      }
191    }