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 */
018package org.apache.hadoop.hbase.io;
019
020import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
021import org.apache.hadoop.metrics2.MetricHistogram;
022import org.apache.hadoop.metrics2.MetricsCollector;
023import org.apache.hadoop.metrics2.MetricsRecordBuilder;
024import org.apache.hadoop.metrics2.lib.Interns;
025import org.apache.hadoop.metrics2.lib.MutableFastCounter;
026import org.apache.yetus.audience.InterfaceAudience;
027
028@InterfaceAudience.Private
029public class MetricsIOSourceImpl extends BaseSourceImpl implements MetricsIOSource {
030
031  private final MetricsIOWrapper wrapper;
032
033  private final MetricHistogram fsReadTimeHisto;
034  private final MetricHistogram fsPReadTimeHisto;
035  private final MetricHistogram fsWriteTimeHisto;
036  private final MutableFastCounter fsSlowReads;
037
038  public MetricsIOSourceImpl(MetricsIOWrapper wrapper) {
039    this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT, wrapper);
040  }
041
042  public MetricsIOSourceImpl(String metricsName, String metricsDescription, String metricsContext,
043    String metricsJmxContext, MetricsIOWrapper wrapper) {
044    super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
045
046    this.wrapper = wrapper;
047
048    fsReadTimeHisto =
049      getMetricsRegistry().newTimeHistogram(FS_READ_TIME_HISTO_KEY, FS_READ_TIME_HISTO_DESC);
050    fsPReadTimeHisto =
051      getMetricsRegistry().newTimeHistogram(FS_PREAD_TIME_HISTO_KEY, FS_PREAD_TIME_HISTO_DESC);
052    fsWriteTimeHisto =
053      getMetricsRegistry().newTimeHistogram(FS_WRITE_HISTO_KEY, FS_WRITE_TIME_HISTO_DESC);
054    fsSlowReads = getMetricsRegistry().newCounter(SLOW_FS_READS_KEY, SLOW_FS_READS_DESC, 0L);
055  }
056
057  @Override
058  public void updateFsReadTime(long t) {
059    fsReadTimeHisto.add(t);
060  }
061
062  @Override
063  public void updateFsPReadTime(long t) {
064    fsPReadTimeHisto.add(t);
065  }
066
067  @Override
068  public void updateFsWriteTime(long t) {
069    fsWriteTimeHisto.add(t);
070  }
071
072  @Override
073  public void incrSlowFsRead() {
074    fsSlowReads.incr();
075  }
076
077  @Override
078  public void getMetrics(MetricsCollector metricsCollector, boolean all) {
079    MetricsRecordBuilder mrb = metricsCollector.addRecord(metricsName);
080
081    // wrapper can be null because this function is called inside of init.
082    if (wrapper != null) {
083      mrb.addCounter(Interns.info(CHECKSUM_FAILURES_KEY, CHECKSUM_FAILURES_DESC),
084        wrapper.getChecksumFailures());
085    }
086
087    metricsRegistry.snapshot(mrb, all);
088  }
089}