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      @Metric("Number of allowSnapshot operations")
061      MutableCounterLong allowSnapshotOps;
062      @Metric("Number of disallowSnapshot operations")
063      MutableCounterLong disallowSnapshotOps;
064      @Metric("Number of createSnapshot operations")
065      MutableCounterLong createSnapshotOps;
066      @Metric("Number of deleteSnapshot operations")
067      MutableCounterLong deleteSnapshotOps;
068      @Metric("Number of renameSnapshot operations")
069      MutableCounterLong renameSnapshotOps;
070      @Metric("Number of listSnapshottableDirectory operations")
071      MutableCounterLong listSnapshottableDirOps;
072      @Metric("Number of snapshotDiffReport operations")
073      MutableCounterLong snapshotDiffReportOps;
074      @Metric("Number of blockReceivedAndDeleted calls")
075      MutableCounterLong blockReceivedAndDeletedOps;
076    
077      @Metric("Journal transactions") MutableRate transactions;
078      @Metric("Journal syncs") MutableRate syncs;
079      final MutableQuantiles[] syncsQuantiles;
080      @Metric("Journal transactions batched in sync")
081      MutableCounterLong transactionsBatchedInSync;
082      @Metric("Block report") MutableRate blockReport;
083      final MutableQuantiles[] blockReportQuantiles;
084      @Metric("Cache report") MutableRate cacheReport;
085      final MutableQuantiles[] cacheReportQuantiles;
086    
087      @Metric("Duration in SafeMode at startup in msec")
088      MutableGaugeInt safeModeTime;
089      @Metric("Time loading FS Image at startup in msec")
090      MutableGaugeInt fsImageLoadTime;
091    
092      @Metric("GetImageServlet getEdit")
093      MutableRate getEdit;
094      @Metric("GetImageServlet getImage")
095      MutableRate getImage;
096      @Metric("GetImageServlet putImage")
097      MutableRate putImage;
098    
099      NameNodeMetrics(String processName, String sessionId, int[] intervals) {
100        registry.tag(ProcessName, processName).tag(SessionId, sessionId);
101        
102        final int len = intervals.length;
103        syncsQuantiles = new MutableQuantiles[len];
104        blockReportQuantiles = new MutableQuantiles[len];
105        cacheReportQuantiles = new MutableQuantiles[len];
106        
107        for (int i = 0; i < len; i++) {
108          int interval = intervals[i];
109          syncsQuantiles[i] = registry.newQuantiles(
110              "syncs" + interval + "s",
111              "Journal syncs", "ops", "latency", interval);
112          blockReportQuantiles[i] = registry.newQuantiles(
113              "blockReport" + interval + "s", 
114              "Block report", "ops", "latency", interval);
115          cacheReportQuantiles[i] = registry.newQuantiles(
116              "cacheReport" + interval + "s",
117              "Cache report", "ops", "latency", interval);
118        }
119      }
120    
121      public static NameNodeMetrics create(Configuration conf, NamenodeRole r) {
122        String sessionId = conf.get(DFSConfigKeys.DFS_METRICS_SESSION_ID_KEY);
123        String processName = r.toString();
124        MetricsSystem ms = DefaultMetricsSystem.instance();
125        JvmMetrics.create(processName, sessionId, ms);
126        
127        // Percentile measurement is off by default, by watching no intervals
128        int[] intervals = 
129            conf.getInts(DFSConfigKeys.DFS_METRICS_PERCENTILES_INTERVALS_KEY);
130        return ms.register(new NameNodeMetrics(processName, sessionId, intervals));
131      }
132    
133      public void shutdown() {
134        DefaultMetricsSystem.shutdown();
135      }
136    
137      public void incrGetBlockLocations() {
138        getBlockLocations.incr();
139      }
140    
141      public void incrFilesCreated() {
142        filesCreated.incr();
143      }
144    
145      public void incrCreateFileOps() {
146        createFileOps.incr();
147      }
148    
149      public void incrFilesAppended() {
150        filesAppended.incr();
151      }
152    
153      public void incrAddBlockOps() {
154        addBlockOps.incr();
155      }
156      
157      public void incrGetAdditionalDatanodeOps() {
158        getAdditionalDatanodeOps.incr();
159      }
160    
161      public void incrFilesRenamed() {
162        filesRenamed.incr();
163      }
164    
165      public void incrFilesDeleted(long delta) {
166        filesDeleted.incr(delta);
167      }
168    
169      public void incrDeleteFileOps() {
170        deleteFileOps.incr();
171      }
172    
173      public void incrGetListingOps() {
174        getListingOps.incr();
175      }
176    
177      public void incrFilesInGetListingOps(int delta) {
178        filesInGetListingOps.incr(delta);
179      }
180    
181      public void incrFileInfoOps() {
182        fileInfoOps.incr();
183      }
184    
185      public void incrCreateSymlinkOps() {
186        createSymlinkOps.incr();
187      }
188    
189      public void incrGetLinkTargetOps() {
190        getLinkTargetOps.incr();
191      }
192    
193      public void incrAllowSnapshotOps() {
194        allowSnapshotOps.incr();
195      }
196      
197      public void incrDisAllowSnapshotOps() {
198        disallowSnapshotOps.incr();
199      }
200      
201      public void incrCreateSnapshotOps() {
202        createSnapshotOps.incr();
203      }
204      
205      public void incrDeleteSnapshotOps() {
206        deleteSnapshotOps.incr();
207      }
208      
209      public void incrRenameSnapshotOps() {
210        renameSnapshotOps.incr();
211      }
212      
213      public void incrListSnapshottableDirOps() {
214        listSnapshottableDirOps.incr();
215      }
216      
217      public void incrSnapshotDiffReportOps() {
218        snapshotDiffReportOps.incr();
219      }
220      
221      public void incrBlockReceivedAndDeletedOps() {
222        blockReceivedAndDeletedOps.incr();
223      }
224    
225      public void addTransaction(long latency) {
226        transactions.add(latency);
227      }
228    
229      public void incrTransactionsBatchedInSync() {
230        transactionsBatchedInSync.incr();
231      }
232    
233      public void addSync(long elapsed) {
234        syncs.add(elapsed);
235        for (MutableQuantiles q : syncsQuantiles) {
236          q.add(elapsed);
237        }
238      }
239    
240      public void setFsImageLoadTime(long elapsed) {
241        fsImageLoadTime.set((int) elapsed);
242      }
243    
244      public void addBlockReport(long latency) {
245        blockReport.add(latency);
246        for (MutableQuantiles q : blockReportQuantiles) {
247          q.add(latency);
248        }
249      }
250    
251      public void addCacheBlockReport(long latency) {
252        cacheReport.add(latency);
253        for (MutableQuantiles q : cacheReportQuantiles) {
254          q.add(latency);
255        }
256      }
257    
258      public void setSafeModeTime(long elapsed) {
259        safeModeTime.set((int) elapsed);
260      }
261    
262      public void addGetEdit(long latency) {
263        getEdit.add(latency);
264      }
265    
266      public void addGetImage(long latency) {
267        getImage.add(latency);
268      }
269    
270      public void addPutImage(long latency) {
271        putImage.add(latency);
272      }
273    }