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.regionserver.throttle;
019
020import org.apache.hadoop.conf.Configuration;
021import org.apache.hadoop.hbase.HBaseInterfaceAudience;
022import org.apache.hadoop.hbase.regionserver.RegionServerServices;
023import org.apache.hadoop.util.ReflectionUtils;
024import org.apache.yetus.audience.InterfaceAudience;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
029public final class CompactionThroughputControllerFactory {
030  private static final Logger LOG =
031    LoggerFactory.getLogger(CompactionThroughputControllerFactory.class);
032
033  public static final String HBASE_THROUGHPUT_CONTROLLER_KEY =
034    "hbase.regionserver.throughput.controller";
035
036  private CompactionThroughputControllerFactory() {
037  }
038
039  private static final Class<? extends ThroughputController> DEFAULT_THROUGHPUT_CONTROLLER_CLASS =
040    PressureAwareCompactionThroughputController.class;
041
042  // for backward compatibility and may not be supported in the future
043  private static final String DEPRECATED_NAME_OF_PRESSURE_AWARE_THROUGHPUT_CONTROLLER_CLASS =
044    "org.apache.hadoop.hbase.regionserver.compactions.PressureAwareCompactionThroughputController";
045  private static final String DEPRECATED_NAME_OF_NO_LIMIT_THROUGHPUT_CONTROLLER_CLASS =
046    "org.apache.hadoop.hbase.regionserver.compactions.NoLimitThroughputController";
047
048  public static ThroughputController create(RegionServerServices server, Configuration conf) {
049    Class<? extends ThroughputController> clazz = getThroughputControllerClass(conf);
050    ThroughputController controller = ReflectionUtils.newInstance(clazz, conf);
051    controller.setup(server);
052    return controller;
053  }
054
055  public static Class<? extends ThroughputController>
056    getThroughputControllerClass(Configuration conf) {
057    String className =
058      conf.get(HBASE_THROUGHPUT_CONTROLLER_KEY, DEFAULT_THROUGHPUT_CONTROLLER_CLASS.getName());
059    className = resolveDeprecatedClassName(className);
060    try {
061      return Class.forName(className).asSubclass(ThroughputController.class);
062    } catch (Exception e) {
063      LOG.warn("Unable to load configured throughput controller '" + className
064        + "', load default throughput controller " + DEFAULT_THROUGHPUT_CONTROLLER_CLASS.getName()
065        + " instead", e);
066      return DEFAULT_THROUGHPUT_CONTROLLER_CLASS;
067    }
068  }
069
070  /**
071   * Resolve deprecated class name to keep backward compatibiliy
072   * @param oldName old name of the class
073   * @return the new name if there is any
074   */
075  private static String resolveDeprecatedClassName(String oldName) {
076    String className = oldName.trim();
077    if (className.equals(DEPRECATED_NAME_OF_PRESSURE_AWARE_THROUGHPUT_CONTROLLER_CLASS)) {
078      className = PressureAwareCompactionThroughputController.class.getName();
079    } else if (className.equals(DEPRECATED_NAME_OF_NO_LIMIT_THROUGHPUT_CONTROLLER_CLASS)) {
080      className = NoLimitThroughputController.class.getName();
081    }
082    if (!className.equals(oldName)) {
083      LOG.warn(oldName + " is deprecated, please use " + className + " instead");
084    }
085    return className;
086  }
087}