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