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;
019
020import java.io.IOException;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseInterfaceAudience;
024import org.apache.hadoop.hbase.HTableDescriptor;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028import org.apache.hadoop.hbase.client.TableDescriptor;
029import org.apache.hadoop.util.ReflectionUtils;
030
031/**
032 * The class that creates a flush policy from a conf and HTableDescriptor.
033 * <p>
034 * The default flush policy is {@link FlushLargeStoresPolicy}. And for 0.98, the default flush
035 * policy is {@link FlushAllStoresPolicy}.
036 */
037@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
038public class FlushPolicyFactory {
039
040  private static final Logger LOG = LoggerFactory.getLogger(FlushPolicyFactory.class);
041
042  public static final String HBASE_FLUSH_POLICY_KEY = "hbase.regionserver.flush.policy";
043
044  private static final Class<? extends FlushPolicy> DEFAULT_FLUSH_POLICY_CLASS =
045      FlushAllLargeStoresPolicy.class;
046
047  /**
048   * Create the FlushPolicy configured for the given table.
049   */
050  public static FlushPolicy create(HRegion region, Configuration conf) throws IOException {
051    Class<? extends FlushPolicy> clazz = getFlushPolicyClass(region.getTableDescriptor(), conf);
052    FlushPolicy policy = ReflectionUtils.newInstance(clazz, conf);
053    policy.configureForRegion(region);
054    return policy;
055  }
056
057  /**
058   * Get FlushPolicy class for the given table.
059   */
060  public static Class<? extends FlushPolicy> getFlushPolicyClass(TableDescriptor htd,
061      Configuration conf) throws IOException {
062    String className = htd.getFlushPolicyClassName();
063    if (className == null) {
064      className = conf.get(HBASE_FLUSH_POLICY_KEY, DEFAULT_FLUSH_POLICY_CLASS.getName());
065    }
066    try {
067      Class<? extends FlushPolicy> clazz = Class.forName(className).asSubclass(FlushPolicy.class);
068      return clazz;
069    } catch (Exception e) {
070      LOG.warn(
071        "Unable to load configured flush policy '" + className + "' for table '"
072            + htd.getTableName() + "', load default flush policy "
073            + DEFAULT_FLUSH_POLICY_CLASS.getName() + " instead", e);
074      return DEFAULT_FLUSH_POLICY_CLASS;
075    }
076  }
077}