View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.regionserver;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.classification.InterfaceAudience;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.KeyValue.KVComparator;
27  import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
28  import org.apache.hadoop.hbase.regionserver.compactions.CompactionPolicy;
29  import org.apache.hadoop.hbase.regionserver.compactions.Compactor;
30  import org.apache.hadoop.hbase.regionserver.compactions.DefaultCompactionPolicy;
31  import org.apache.hadoop.hbase.regionserver.compactions.DefaultCompactor;
32  import org.apache.hadoop.hbase.util.ReflectionUtils;
33  
34  /**
35   * StoreEngine is a factory that can create the objects necessary for HStore to operate.
36   * Since not all compaction policies, compactors and store file managers are compatible,
37   * they are tied together and replaced together via StoreEngine-s.
38   */
39  @InterfaceAudience.Private
40  public abstract class StoreEngine<
41    CP extends CompactionPolicy, C extends Compactor, SFM extends StoreFileManager> {
42    protected CP compactionPolicy;
43    protected C compactor;
44    protected SFM storeFileManager;
45  
46    /**
47     * The name of the configuration parameter that specifies the class of
48     * a store engine that is used to manage and compact HBase store files.
49     */
50    public static final String STORE_ENGINE_CLASS_KEY = "hbase.hstore.engine.class";
51  
52    private static final Class<? extends StoreEngine<?, ?, ?>>
53      DEFAULT_STORE_ENGINE_CLASS = DefaultStoreEngine.class;
54  
55    /**
56     * @return Compaction policy to use.
57     */
58    public CompactionPolicy getCompactionPolicy() {
59      return this.compactionPolicy;
60    }
61  
62    /**
63     * @return Compactor to use.
64     */
65    public Compactor getCompactor() {
66      return this.compactor;
67    }
68  
69    /**
70     * @return Store file manager to use.
71     */
72    public StoreFileManager getStoreFileManager() {
73      return this.storeFileManager;
74    }
75  
76    /**
77     * Creates an instance of a compaction context specific to this engine.
78     * Doesn't actually select or start a compaction. See CompactionContext class comment.
79     * @return New CompactionContext object.
80     */
81    public abstract CompactionContext createCompaction() throws IOException;
82  
83    /**
84     * Create the StoreEngine's components.
85     */
86    protected abstract void createComponents(
87        Configuration conf, Store store, KVComparator kvComparator) throws IOException;
88  
89    private void createComponentsOnce(
90        Configuration conf, Store store, KVComparator kvComparator) throws IOException {
91      assert compactor == null && compactionPolicy == null && storeFileManager == null;
92      createComponents(conf, store, kvComparator);
93      assert compactor != null && compactionPolicy != null && storeFileManager != null;
94    }
95  
96    /**
97     * Create the StoreEngine configured for the given Store.
98     * @param store The store. An unfortunate dependency needed due to it
99     *              being passed to coprocessors via the compactor.
100    * @param conf Store configuration.
101    * @param kvComparator KVComparator for storeFileManager.
102    * @return StoreEngine to use.
103    */
104   public static StoreEngine<?, ?, ?> create(
105       Store store, Configuration conf, KVComparator kvComparator) throws IOException {
106     String className = conf.get(STORE_ENGINE_CLASS_KEY, DEFAULT_STORE_ENGINE_CLASS.getName());
107     try {
108       StoreEngine<?,?,?> se = ReflectionUtils.instantiateWithCustomCtor(
109           className, new Class[] { }, new Object[] { });
110       se.createComponentsOnce(conf, store, kvComparator);
111       return se;
112     } catch (Exception e) {
113       throw new IOException("Unable to load configured store engine '" + className + "'", e);
114     }
115   }
116 }