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