summaryrefslogtreecommitdiff
path: root/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/helpers/ContainerMetrics.java
blob: d4d732b8b69eadabc42713d25dafae62dd52f584 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.hadoop.ozone.container.common.helpers;

import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.DFSConfigKeys;
import org.apache.hadoop.hdds.protocol.proto.ContainerProtos;
import org.apache.hadoop.metrics2.MetricsSystem;
import org.apache.hadoop.metrics2.annotation.Metric;
import org.apache.hadoop.metrics2.annotation.Metrics;
import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
import org.apache.hadoop.metrics2.lib.MetricsRegistry;
import org.apache.hadoop.metrics2.lib.MutableCounterLong;
import org.apache.hadoop.metrics2.lib.MutableQuantiles;
import org.apache.hadoop.metrics2.lib.MutableRate;

/**
 *
 * This class is for maintaining  the various Storage Container
 * DataNode statistics and publishing them through the metrics interfaces.
 * This also registers the JMX MBean for RPC.
 * <p>
 * This class has a number of metrics variables that are publicly accessible;
 * these variables (objects) have methods to update their values;
 *  for example:
 *  <p> {@link #numOps}.inc()
 *
 */
@InterfaceAudience.Private
@Metrics(about="Storage Container DataNode Metrics", context="dfs")
public class ContainerMetrics {
  @Metric private MutableCounterLong numOps;
  private MutableCounterLong[] numOpsArray;
  private MutableCounterLong[] opsBytesArray;
  private MutableRate[] opsLatency;
  private MutableQuantiles[][] opsLatQuantiles;
  private MetricsRegistry registry = null;

  public ContainerMetrics(int[] intervals) {
    int numEnumEntries = ContainerProtos.Type.values().length;
    final int len = intervals.length;
    this.numOpsArray = new MutableCounterLong[numEnumEntries];
    this.opsBytesArray = new MutableCounterLong[numEnumEntries];
    this.opsLatency = new MutableRate[numEnumEntries];
    this.opsLatQuantiles = new MutableQuantiles[numEnumEntries][len];
    this.registry = new MetricsRegistry("StorageContainerMetrics");
    for (int i = 0; i < numEnumEntries; i++) {
      numOpsArray[i] = registry.newCounter(
          "num" + ContainerProtos.Type.valueOf(i + 1),
          "number of " + ContainerProtos.Type.valueOf(i + 1) + " ops",
          (long) 0);
      opsBytesArray[i] = registry.newCounter(
          "bytes" + ContainerProtos.Type.valueOf(i + 1),
          "bytes used by " + ContainerProtos.Type.valueOf(i + 1) + "op",
          (long) 0);
      opsLatency[i] = registry.newRate(
          "latency" + ContainerProtos.Type.valueOf(i + 1),
          ContainerProtos.Type.valueOf(i + 1) + " op");

      for (int j = 0; j < len; j++) {
        int interval = intervals[j];
        String quantileName = ContainerProtos.Type.valueOf(i + 1) + "Nanos"
            + interval + "s";
        opsLatQuantiles[i][j] = registry.newQuantiles(quantileName,
            "latency of Container ops", "ops", "latency", interval);
      }
    }
  }

  public static ContainerMetrics create(Configuration conf) {
    MetricsSystem ms = DefaultMetricsSystem.instance();
    // Percentile measurement is off by default, by watching no intervals
    int[] intervals =
             conf.getInts(DFSConfigKeys.DFS_METRICS_PERCENTILES_INTERVALS_KEY);
    return ms.register("StorageContainerMetrics",
                       "Storage Container Node Metrics",
                       new ContainerMetrics(intervals));
  }

  public void incContainerOpcMetrics(ContainerProtos.Type type){
    numOps.incr();
    numOpsArray[type.ordinal()].incr();
  }

  public long getContainerOpsMetrics(ContainerProtos.Type type){
    return numOpsArray[type.ordinal()].value();
  }

  public void incContainerOpsLatencies(ContainerProtos.Type type,
                                       long latencyNanos) {
    opsLatency[type.ordinal()].add(latencyNanos);
    for (MutableQuantiles q: opsLatQuantiles[type.ordinal()]) {
      q.add(latencyNanos);
    }
  }

  public void incContainerBytesStats(ContainerProtos.Type type, long bytes) {
    opsBytesArray[type.ordinal()].incr(bytes);
  }

  public long getContainerBytesMetrics(ContainerProtos.Type type){
    return opsBytesArray[type.ordinal()].value();
  }
}