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