View Javadoc

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  
19  package org.apache.hadoop.hbase.io;
20  
21  import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
22  import org.apache.hadoop.metrics2.MetricHistogram;
23  import org.apache.hadoop.metrics2.MetricsCollector;
24  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
25  import org.apache.hadoop.metrics2.lib.Interns;
26  
27  public class MetricsIOSourceImpl extends BaseSourceImpl implements MetricsIOSource {
28  
29    private final MetricsIOWrapper wrapper;
30  
31    private final MetricHistogram fsReadTimeHisto;
32    private final MetricHistogram fsPReadTimeHisto;
33    private final MetricHistogram fsWriteTimeHisto;
34  
35    public MetricsIOSourceImpl(MetricsIOWrapper wrapper) {
36      this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT, wrapper);
37    }
38  
39    public MetricsIOSourceImpl(String metricsName,
40        String metricsDescription,
41        String metricsContext,
42        String metricsJmxContext,
43        MetricsIOWrapper wrapper) {
44      super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
45  
46      this.wrapper = wrapper;
47  
48      fsReadTimeHisto = getMetricsRegistry()
49          .newTimeHistogram(FS_READ_TIME_HISTO_KEY, FS_READ_TIME_HISTO_DESC);
50      fsPReadTimeHisto = getMetricsRegistry()
51          .newTimeHistogram(FS_PREAD_TIME_HISTO_KEY, FS_PREAD_TIME_HISTO_DESC);
52      fsWriteTimeHisto = getMetricsRegistry()
53          .newTimeHistogram(FS_WRITE_HISTO_KEY, FS_WRITE_TIME_HISTO_DESC);
54    }
55  
56    @Override
57    public void updateFsReadTime(long t) {
58      fsReadTimeHisto.add(t);
59    };
60  
61    @Override
62    public void updateFsPReadTime(long t) {
63      fsPReadTimeHisto.add(t);
64    };
65  
66    @Override
67    public void updateFsWriteTime(long t) {
68      fsWriteTimeHisto.add(t);
69    }
70  
71    @Override
72    public void getMetrics(MetricsCollector metricsCollector, boolean all) {
73      MetricsRecordBuilder mrb = metricsCollector.addRecord(metricsName);
74  
75      // wrapper can be null because this function is called inside of init.
76      if (wrapper != null) {
77        mrb.addCounter(Interns.info(CHECKSUM_FAILURES_KEY, CHECKSUM_FAILURES_DESC),
78          wrapper.getChecksumFailures());
79      }
80  
81      metricsRegistry.snapshot(mrb, all);
82    }
83  
84  }