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