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 com.google.errorprone.annotations.RestrictedApi;
021import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
022import org.apache.hadoop.hbase.regionserver.MetricsRegionServerSourceFactory;
023import org.apache.yetus.audience.InterfaceAudience;
024
025@InterfaceAudience.Private
026public class MetricsIO {
027
028  private static volatile MetricsIO instance;
029  private final MetricsIOSource source;
030  private final MetricsIOWrapper wrapper;
031
032  @RestrictedApi(explanation = "Should only be called in TestMetricsIO", link = "",
033      allowedOnPath = ".*/(MetricsIO|TestMetricsIO).java")
034  MetricsIO(MetricsIOWrapper wrapper) {
035    this(CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class)
036      .createIO(wrapper), wrapper);
037  }
038
039  MetricsIO(MetricsIOSource source, MetricsIOWrapper wrapper) {
040    this.source = source;
041    this.wrapper = wrapper;
042  }
043
044  /**
045   * Get a static instance for the MetricsIO so that accessors access the same instance. We want to
046   * lazy initialize so that correct process name is in place. See HBASE-27966 for more details.
047   */
048  public static MetricsIO getInstance() {
049    if (instance == null) {
050      synchronized (MetricsIO.class) {
051        if (instance == null) {
052          instance = new MetricsIO(new MetricsIOWrapperImpl());
053        }
054      }
055    }
056    return instance;
057  }
058
059  public MetricsIOSource getMetricsSource() {
060    return source;
061  }
062
063  public MetricsIOWrapper getWrapper() {
064    return wrapper;
065  }
066
067  public void updateFsReadTime(long t) {
068    source.updateFsReadTime(t);
069  }
070
071  public void updateFsPreadTime(long t) {
072    source.updateFsPReadTime(t);
073  }
074
075  public void updateFsWriteTime(long t) {
076    source.updateFsWriteTime(t);
077  }
078
079  public void incrSlowFsRead() {
080    source.incrSlowFsRead();
081  }
082}