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.yetus.audience.InterfaceAudience;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025import org.apache.hadoop.hbase.regionserver.RegionServerServices;
026import org.apache.hadoop.util.ReflectionUtils;
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>
040      DEFAULT_THROUGHPUT_CONTROLLER_CLASS = 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,
049      Configuration conf) {
050    Class<? extends ThroughputController> clazz = getThroughputControllerClass(conf);
051    ThroughputController controller = ReflectionUtils.newInstance(clazz, conf);
052    controller.setup(server);
053    return controller;
054  }
055
056  public static Class<? extends ThroughputController> getThroughputControllerClass(
057      Configuration conf) {
058    String className =
059        conf.get(HBASE_THROUGHPUT_CONTROLLER_KEY, DEFAULT_THROUGHPUT_CONTROLLER_CLASS.getName());
060    className = resolveDeprecatedClassName(className);
061    try {
062      return Class.forName(className).asSubclass(ThroughputController.class);
063    } catch (Exception e) {
064      LOG.warn(
065        "Unable to load configured throughput controller '" + className
066            + "', load default throughput controller "
067            + DEFAULT_THROUGHPUT_CONTROLLER_CLASS.getName() + " instead", e);
068      return DEFAULT_THROUGHPUT_CONTROLLER_CLASS;
069    }
070  }
071
072  /**
073   * Resolve deprecated class name to keep backward compatibiliy
074   * @param oldName old name of the class
075   * @return the new name if there is any
076   */
077  private static String resolveDeprecatedClassName(String oldName) {
078    String className = oldName.trim();
079    if (className.equals(DEPRECATED_NAME_OF_PRESSURE_AWARE_THROUGHPUT_CONTROLLER_CLASS)) {
080      className = PressureAwareCompactionThroughputController.class.getName();
081    } else if (className.equals(DEPRECATED_NAME_OF_NO_LIMIT_THROUGHPUT_CONTROLLER_CLASS)) {
082      className = NoLimitThroughputController.class.getName();
083    }
084    if (!className.equals(oldName)) {
085      LOG.warn(oldName + " is deprecated, please use " + className + " instead");
086    }
087    return className;
088  }
089}