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.util.ReflectionUtils;
31  
32  /**
33   * StoreEngine is a factory that can create the objects necessary for HStore to operate.
34   * Since not all compaction policies, compactors and store file managers are compatible,
35   * they are tied together and replaced together via StoreEngine-s.
36   */
37  @InterfaceAudience.Private
38  public abstract class StoreEngine<SF extends StoreFlusher,
39      CP extends CompactionPolicy, C extends Compactor, SFM extends StoreFileManager> {
40    protected SF storeFlusher;
41    protected CP compactionPolicy;
42    protected C compactor;
43    protected SFM storeFileManager;
44  
45    /**
46     * The name of the configuration parameter that specifies the class of
47     * a store engine that is used to manage and compact HBase store files.
48     */
49    public static final String STORE_ENGINE_CLASS_KEY = "hbase.hstore.engine.class";
50  
51    private static final Class<? extends StoreEngine<?, ?, ?, ?>>
52      DEFAULT_STORE_ENGINE_CLASS = DefaultStoreEngine.class;
53  
54    /**
55     * @return Compaction policy to use.
56     */
57    public CompactionPolicy getCompactionPolicy() {
58      return this.compactionPolicy;
59    }
60  
61    /**
62     * @return Compactor to use.
63     */
64    public Compactor getCompactor() {
65      return this.compactor;
66    }
67  
68    /**
69     * @return Store file manager to use.
70     */
71    public StoreFileManager getStoreFileManager() {
72      return this.storeFileManager;
73    }
74  
75    /**
76     * @return Store flusher to use.
77     */
78    public StoreFlusher getStoreFlusher() {
79      return this.storeFlusher;
80    }
81  
82    /**
83     * Creates an instance of a compaction context specific to this engine.
84     * Doesn't actually select or start a compaction. See CompactionContext class comment.
85     * @return New CompactionContext object.
86     */
87    public abstract CompactionContext createCompaction() throws IOException;
88  
89    /**
90     * Create the StoreEngine's components.
91     */
92    protected abstract void createComponents(
93        Configuration conf, Store store, KVComparator kvComparator) throws IOException;
94  
95    private void createComponentsOnce(
96        Configuration conf, Store store, KVComparator kvComparator) throws IOException {
97      assert compactor == null && compactionPolicy == null
98          && storeFileManager == null && storeFlusher == null;
99      createComponents(conf, store, kvComparator);
100     assert compactor != null && compactionPolicy != null
101         && storeFileManager != null && storeFlusher != null;
102   }
103 
104   /**
105    * Create the StoreEngine configured for the given Store.
106    * @param store The store. An unfortunate dependency needed due to it
107    *              being passed to coprocessors via the compactor.
108    * @param conf Store configuration.
109    * @param kvComparator KVComparator for storeFileManager.
110    * @return StoreEngine to use.
111    */
112   public static StoreEngine<?, ?, ?, ?> create(
113       Store store, Configuration conf, KVComparator kvComparator) throws IOException {
114     String className = conf.get(STORE_ENGINE_CLASS_KEY, DEFAULT_STORE_ENGINE_CLASS.getName());
115     try {
116       StoreEngine<?,?,?,?> se = ReflectionUtils.instantiateWithCustomCtor(
117           className, new Class[] { }, new Object[] { });
118       se.createComponentsOnce(conf, store, kvComparator);
119       return se;
120     } catch (Exception e) {
121       throw new IOException("Unable to load configured store engine '" + className + "'", e);
122     }
123   }
124 }