1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.hdfs.server.namenode.metrics; |
---|
19 | |
---|
20 | |
---|
21 | import org.apache.commons.logging.Log; |
---|
22 | import org.apache.commons.logging.LogFactory; |
---|
23 | import org.apache.hadoop.conf.Configuration; |
---|
24 | import org.apache.hadoop.hdfs.server.namenode.FSNamesystem; |
---|
25 | import org.apache.hadoop.metrics.*; |
---|
26 | import org.apache.hadoop.metrics.util.MetricsBase; |
---|
27 | import org.apache.hadoop.metrics.util.MetricsIntValue; |
---|
28 | import org.apache.hadoop.metrics.util.MetricsLongValue; |
---|
29 | import org.apache.hadoop.metrics.util.MetricsRegistry; |
---|
30 | |
---|
31 | /** |
---|
32 | * |
---|
33 | * This class is for maintaining the various FSNamesystem status metrics |
---|
34 | * and publishing them through the metrics interfaces. |
---|
35 | * The SNamesystem creates and registers the JMX MBean. |
---|
36 | * <p> |
---|
37 | * This class has a number of metrics variables that are publicly accessible; |
---|
38 | * these variables (objects) have methods to update their values; |
---|
39 | * for example: |
---|
40 | * <p> {@link #filesTotal}.set() |
---|
41 | * |
---|
42 | */ |
---|
43 | public class FSNamesystemMetrics implements Updater { |
---|
44 | private static Log log = LogFactory.getLog(FSNamesystemMetrics.class); |
---|
45 | final MetricsRecord metricsRecord; |
---|
46 | public MetricsRegistry registry = new MetricsRegistry(); |
---|
47 | |
---|
48 | final MetricsIntValue filesTotal = new MetricsIntValue("FilesTotal", registry); |
---|
49 | final MetricsLongValue blocksTotal = new MetricsLongValue("BlocksTotal", registry); |
---|
50 | final MetricsIntValue capacityTotalGB = new MetricsIntValue("CapacityTotalGB", registry); |
---|
51 | final MetricsIntValue capacityUsedGB = new MetricsIntValue("CapacityUsedGB", registry); |
---|
52 | final MetricsIntValue capacityRemainingGB = new MetricsIntValue("CapacityRemainingGB", registry); |
---|
53 | final MetricsIntValue totalLoad = new MetricsIntValue("TotalLoad", registry); |
---|
54 | final MetricsIntValue pendingDeletionBlocks = new MetricsIntValue("PendingDeletionBlocks", registry); |
---|
55 | final MetricsIntValue corruptBlocks = new MetricsIntValue("CorruptBlocks", registry); |
---|
56 | final MetricsIntValue excessBlocks = new MetricsIntValue("ExcessBlocks", registry); |
---|
57 | final MetricsIntValue pendingReplicationBlocks = new MetricsIntValue("PendingReplicationBlocks", registry); |
---|
58 | final MetricsIntValue underReplicatedBlocks = new MetricsIntValue("UnderReplicatedBlocks", registry); |
---|
59 | final MetricsIntValue scheduledReplicationBlocks = new MetricsIntValue("ScheduledReplicationBlocks", registry); |
---|
60 | final MetricsIntValue missingBlocks = new MetricsIntValue("MissingBlocks", registry); |
---|
61 | final MetricsIntValue blockCapacity = new MetricsIntValue("BlockCapacity", registry); |
---|
62 | |
---|
63 | public FSNamesystemMetrics(Configuration conf) { |
---|
64 | String sessionId = conf.get("session.id"); |
---|
65 | |
---|
66 | // Create a record for FSNamesystem metrics |
---|
67 | MetricsContext metricsContext = MetricsUtil.getContext("dfs"); |
---|
68 | metricsRecord = MetricsUtil.createRecord(metricsContext, "FSNamesystem"); |
---|
69 | metricsRecord.setTag("sessionId", sessionId); |
---|
70 | metricsContext.registerUpdater(this); |
---|
71 | log.info("Initializing FSNamesystemMetrics using context object:" + |
---|
72 | metricsContext.getClass().getName()); |
---|
73 | } |
---|
74 | |
---|
75 | private int roundBytesToGBytes(long bytes) { |
---|
76 | return Math.round(((float)bytes/(1024 * 1024 * 1024))); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Since this object is a registered updater, this method will be called |
---|
81 | * periodically, e.g. every 5 seconds. |
---|
82 | * We set the metrics value within this function before pushing it out. |
---|
83 | * FSNamesystem updates its own local variables which are |
---|
84 | * light weight compared to Metrics counters. |
---|
85 | * |
---|
86 | * Some of the metrics are explicity casted to int. Few metrics collectors |
---|
87 | * do not handle long values. It is safe to cast to int for now as all these |
---|
88 | * values fit in int value. |
---|
89 | * Metrics related to DFS capacity are stored in bytes which do not fit in |
---|
90 | * int, so they are rounded to GB |
---|
91 | */ |
---|
92 | public void doUpdates(MetricsContext unused) { |
---|
93 | /** |
---|
94 | * ToFix |
---|
95 | * If the metrics counter were instead stored in the metrics objects themselves |
---|
96 | * we could avoid copying the values on each update. |
---|
97 | */ |
---|
98 | synchronized (this) { |
---|
99 | FSNamesystem fsNameSystem = FSNamesystem.getFSNamesystem(); |
---|
100 | filesTotal.set((int)fsNameSystem.getFilesTotal()); |
---|
101 | blocksTotal.set((int)fsNameSystem.getBlocksTotal()); |
---|
102 | capacityTotalGB.set(roundBytesToGBytes(fsNameSystem.getCapacityTotal())); |
---|
103 | capacityUsedGB.set(roundBytesToGBytes(fsNameSystem.getCapacityUsed())); |
---|
104 | capacityRemainingGB.set(roundBytesToGBytes(fsNameSystem. |
---|
105 | getCapacityRemaining())); |
---|
106 | totalLoad.set(fsNameSystem.getTotalLoad()); |
---|
107 | corruptBlocks.set((int)fsNameSystem.getCorruptReplicaBlocks()); |
---|
108 | excessBlocks.set((int)fsNameSystem.getExcessBlocks()); |
---|
109 | pendingDeletionBlocks.set((int)fsNameSystem.getPendingDeletionBlocks()); |
---|
110 | pendingReplicationBlocks.set((int)fsNameSystem. |
---|
111 | getPendingReplicationBlocks()); |
---|
112 | underReplicatedBlocks.set((int)fsNameSystem.getUnderReplicatedBlocks()); |
---|
113 | scheduledReplicationBlocks.set((int)fsNameSystem. |
---|
114 | getScheduledReplicationBlocks()); |
---|
115 | missingBlocks.set((int)fsNameSystem.getMissingBlocksCount()); |
---|
116 | blockCapacity.set(fsNameSystem.getBlockCapacity()); |
---|
117 | |
---|
118 | for (MetricsBase m : registry.getMetricsList()) { |
---|
119 | m.pushMetric(metricsRecord); |
---|
120 | } |
---|
121 | } |
---|
122 | metricsRecord.update(); |
---|
123 | } |
---|
124 | } |
---|