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 org.apache.hadoop.conf.Configuration;
021    import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.NamenodeRole;
022    import org.apache.hadoop.hdfs.DFSConfigKeys;
023    import org.apache.hadoop.metrics2.MetricsSystem;
024    import org.apache.hadoop.metrics2.annotation.Metric;
025    import org.apache.hadoop.metrics2.annotation.Metrics;
026    import static org.apache.hadoop.metrics2.impl.MsInfo.*;
027    import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
028    import org.apache.hadoop.metrics2.lib.MetricsRegistry;
029    import org.apache.hadoop.metrics2.lib.MutableCounterLong;
030    import org.apache.hadoop.metrics2.lib.MutableGaugeInt;
031    import org.apache.hadoop.metrics2.lib.MutableRate;
032    import org.apache.hadoop.metrics2.source.JvmMetrics;
033    
034    /**
035     * This class is for maintaining  the various NameNode activity statistics
036     * and publishing them through the metrics interfaces.
037     */
038    @Metrics(name="NameNodeActivity", about="NameNode metrics", context="dfs")
039    public class NameNodeMetrics {
040      final MetricsRegistry registry = new MetricsRegistry("namenode");
041    
042      @Metric MutableCounterLong createFileOps;
043      @Metric MutableCounterLong filesCreated;
044      @Metric MutableCounterLong filesAppended;
045      @Metric MutableCounterLong getBlockLocations;
046      @Metric MutableCounterLong filesRenamed;
047      @Metric MutableCounterLong getListingOps;
048      @Metric MutableCounterLong deleteFileOps;
049      @Metric("Number of files/dirs deleted by delete or rename operations")
050      MutableCounterLong filesDeleted;
051      @Metric MutableCounterLong fileInfoOps;
052      @Metric MutableCounterLong addBlockOps;
053      @Metric MutableCounterLong getAdditionalDatanodeOps;
054      @Metric MutableCounterLong createSymlinkOps;
055      @Metric MutableCounterLong getLinkTargetOps;
056      @Metric MutableCounterLong filesInGetListingOps;
057    
058      @Metric("Journal transactions") MutableRate transactions;
059      @Metric("Journal syncs") MutableRate syncs;
060      @Metric("Journal transactions batched in sync")
061      MutableCounterLong transactionsBatchedInSync;
062      @Metric("Block report") MutableRate blockReport;
063    
064      @Metric("Duration in SafeMode at startup") MutableGaugeInt safeModeTime;
065      @Metric("Time loading FS Image at startup") MutableGaugeInt fsImageLoadTime;
066    
067      NameNodeMetrics(String processName, String sessionId) {
068        registry.tag(ProcessName, processName).tag(SessionId, sessionId);
069      }
070    
071      public static NameNodeMetrics create(Configuration conf, NamenodeRole r) {
072        String sessionId = conf.get(DFSConfigKeys.DFS_METRICS_SESSION_ID_KEY);
073        String processName = r.toString();
074        MetricsSystem ms = DefaultMetricsSystem.instance();
075        JvmMetrics.create(processName, sessionId, ms);
076        return ms.register(new NameNodeMetrics(processName, sessionId));
077      }
078    
079      public void shutdown() {
080        DefaultMetricsSystem.shutdown();
081      }
082    
083      public void incrGetBlockLocations() {
084        getBlockLocations.incr();
085      }
086    
087      public void incrFilesCreated() {
088        filesCreated.incr();
089      }
090    
091      public void incrCreateFileOps() {
092        createFileOps.incr();
093      }
094    
095      public void incrFilesAppended() {
096        filesAppended.incr();
097      }
098    
099      public void incrAddBlockOps() {
100        addBlockOps.incr();
101      }
102      
103      public void incrGetAdditionalDatanodeOps() {
104        getAdditionalDatanodeOps.incr();
105      }
106    
107      public void incrFilesRenamed() {
108        filesRenamed.incr();
109      }
110    
111      public void incrFilesDeleted(int delta) {
112        filesDeleted.incr(delta);
113      }
114    
115      public void incrDeleteFileOps() {
116        deleteFileOps.incr();
117      }
118    
119      public void incrGetListingOps() {
120        getListingOps.incr();
121      }
122    
123      public void incrFilesInGetListingOps(int delta) {
124        filesInGetListingOps.incr(delta);
125      }
126    
127      public void incrFileInfoOps() {
128        fileInfoOps.incr();
129      }
130    
131      public void incrCreateSymlinkOps() {
132        createSymlinkOps.incr();
133      }
134    
135      public void incrGetLinkTargetOps() {
136        getLinkTargetOps.incr();
137      }
138    
139      public void addTransaction(long latency) {
140        transactions.add(latency);
141      }
142    
143      public void incrTransactionsBatchedInSync() {
144        transactionsBatchedInSync.incr();
145      }
146    
147      public void addSync(long elapsed) {
148        syncs.add(elapsed);
149      }
150    
151      public void setFsImageLoadTime(long elapsed) {
152        fsImageLoadTime.set((int) elapsed);
153      }
154    
155      public void addBlockReport(long latency) {
156        blockReport.add(latency);
157      }
158    
159      public void setSafeModeTime(long elapsed) {
160        safeModeTime.set((int) elapsed);
161      }
162    }