View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  
20  import java.io.IOException;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
26  import org.apache.hadoop.hbase.HTableDescriptor;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.util.ReflectionUtils;
29  
30  /**
31   * The class that creates a flush policy from a conf and HTableDescriptor.
32   * <p>
33   * The default flush policy is {@link FlushLargeStoresPolicy}. And for 0.98, the default flush
34   * policy is {@link FlushAllStoresPolicy}.
35   */
36  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
37  public class FlushPolicyFactory {
38  
39    private static final Log LOG = LogFactory.getLog(FlushPolicyFactory.class);
40  
41    public static final String HBASE_FLUSH_POLICY_KEY = "hbase.regionserver.flush.policy";
42  
43    private static final Class<? extends FlushPolicy> DEFAULT_FLUSH_POLICY_CLASS =
44        FlushLargeStoresPolicy.class;
45  
46    /**
47     * Create the FlushPolicy configured for the given table.
48     */
49    public static FlushPolicy create(HRegion region, Configuration conf) throws IOException {
50      Class<? extends FlushPolicy> clazz = getFlushPolicyClass(region.getTableDesc(), conf);
51      FlushPolicy policy = ReflectionUtils.newInstance(clazz, conf);
52      policy.configureForRegion(region);
53      return policy;
54    }
55  
56    /**
57     * Get FlushPolicy class for the given table.
58     */
59    public static Class<? extends FlushPolicy> getFlushPolicyClass(HTableDescriptor htd,
60        Configuration conf) throws IOException {
61      String className = htd.getFlushPolicyClassName();
62      if (className == null) {
63        className = conf.get(HBASE_FLUSH_POLICY_KEY, DEFAULT_FLUSH_POLICY_CLASS.getName());
64      }
65      try {
66        Class<? extends FlushPolicy> clazz = Class.forName(className).asSubclass(FlushPolicy.class);
67        return clazz;
68      } catch (Exception e) {
69        LOG.warn(
70          "Unable to load configured flush policy '" + className + "' for table '"
71              + htd.getTableName() + "', load default flush policy "
72              + DEFAULT_FLUSH_POLICY_CLASS.getName() + " instead", e);
73        return DEFAULT_FLUSH_POLICY_CLASS;
74      }
75    }
76  }