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