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    
075      @Metric("Journal transactions") MutableRate transactions;
076      @Metric("Journal syncs") MutableRate syncs;
077      MutableQuantiles[] syncsQuantiles;
078      @Metric("Journal transactions batched in sync")
079      MutableCounterLong transactionsBatchedInSync;
080      @Metric("Block report") MutableRate blockReport;
081      MutableQuantiles[] blockReportQuantiles;
082    
083      @Metric("Duration in SafeMode at startup") MutableGaugeInt safeModeTime;
084      @Metric("Time loading FS Image at startup") MutableGaugeInt fsImageLoadTime;
085    
086      NameNodeMetrics(String processName, String sessionId, int[] intervals) {
087        registry.tag(ProcessName, processName).tag(SessionId, sessionId);
088        
089        final int len = intervals.length;
090        syncsQuantiles = new MutableQuantiles[len];
091        blockReportQuantiles = new MutableQuantiles[len];
092        
093        for (int i = 0; i < len; i++) {
094          int interval = intervals[i];
095          syncsQuantiles[i] = registry.newQuantiles(
096              "syncs" + interval + "s",
097              "Journal syncs", "ops", "latency", interval);
098          blockReportQuantiles[i] = registry.newQuantiles(
099              "blockReport" + interval + "s", 
100              "Block report", "ops", "latency", interval);
101        }
102      }
103    
104      public static NameNodeMetrics create(Configuration conf, NamenodeRole r) {
105        String sessionId = conf.get(DFSConfigKeys.DFS_METRICS_SESSION_ID_KEY);
106        String processName = r.toString();
107        MetricsSystem ms = DefaultMetricsSystem.instance();
108        JvmMetrics.create(processName, sessionId, ms);
109        
110        // Percentile measurement is off by default, by watching no intervals
111        int[] intervals = 
112            conf.getInts(DFSConfigKeys.DFS_METRICS_PERCENTILES_INTERVALS_KEY);
113        return ms.register(new NameNodeMetrics(processName, sessionId, intervals));
114      }
115    
116      public void shutdown() {
117        DefaultMetricsSystem.shutdown();
118      }
119    
120      public void incrGetBlockLocations() {
121        getBlockLocations.incr();
122      }
123    
124      public void incrFilesCreated() {
125        filesCreated.incr();
126      }
127    
128      public void incrCreateFileOps() {
129        createFileOps.incr();
130      }
131    
132      public void incrFilesAppended() {
133        filesAppended.incr();
134      }
135    
136      public void incrAddBlockOps() {
137        addBlockOps.incr();
138      }
139      
140      public void incrGetAdditionalDatanodeOps() {
141        getAdditionalDatanodeOps.incr();
142      }
143    
144      public void incrFilesRenamed() {
145        filesRenamed.incr();
146      }
147    
148      public void incrFilesDeleted(long delta) {
149        filesDeleted.incr(delta);
150      }
151    
152      public void incrDeleteFileOps() {
153        deleteFileOps.incr();
154      }
155    
156      public void incrGetListingOps() {
157        getListingOps.incr();
158      }
159    
160      public void incrFilesInGetListingOps(int delta) {
161        filesInGetListingOps.incr(delta);
162      }
163    
164      public void incrFileInfoOps() {
165        fileInfoOps.incr();
166      }
167    
168      public void incrCreateSymlinkOps() {
169        createSymlinkOps.incr();
170      }
171    
172      public void incrGetLinkTargetOps() {
173        getLinkTargetOps.incr();
174      }
175    
176      public void incrAllowSnapshotOps() {
177        allowSnapshotOps.incr();
178      }
179      
180      public void incrDisAllowSnapshotOps() {
181        disallowSnapshotOps.incr();
182      }
183      
184      public void incrCreateSnapshotOps() {
185        createSnapshotOps.incr();
186      }
187      
188      public void incrDeleteSnapshotOps() {
189        deleteSnapshotOps.incr();
190      }
191      
192      public void incrRenameSnapshotOps() {
193        renameSnapshotOps.incr();
194      }
195      
196      public void incrListSnapshottableDirOps() {
197        listSnapshottableDirOps.incr();
198      }
199      
200      public void incrSnapshotDiffReportOps() {
201        snapshotDiffReportOps.incr();
202      }
203      
204      public void addTransaction(long latency) {
205        transactions.add(latency);
206      }
207    
208      public void incrTransactionsBatchedInSync() {
209        transactionsBatchedInSync.incr();
210      }
211    
212      public void addSync(long elapsed) {
213        syncs.add(elapsed);
214        for (MutableQuantiles q : syncsQuantiles) {
215          q.add(elapsed);
216        }
217      }
218    
219      public void setFsImageLoadTime(long elapsed) {
220        fsImageLoadTime.set((int) elapsed);
221      }
222    
223      public void addBlockReport(long latency) {
224        blockReport.add(latency);
225        for (MutableQuantiles q : blockReportQuantiles) {
226          q.add(latency);
227        }
228      }
229    
230      public void setSafeModeTime(long elapsed) {
231        safeModeTime.set((int) elapsed);
232      }
233    }