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  import java.util.List;
24  
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.KeyValue.KVComparator;
28  import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
29  import org.apache.hadoop.hbase.regionserver.compactions.CompactionPolicy;
30  import org.apache.hadoop.hbase.regionserver.compactions.Compactor;
31  import org.apache.hadoop.hbase.util.ReflectionUtils;
32  
33  /**
34   * StoreEngine is a factory that can create the objects necessary for HStore to operate.
35   * Since not all compaction policies, compactors and store file managers are compatible,
36   * they are tied together and replaced together via StoreEngine-s.
37   */
38  @InterfaceAudience.Private
39  public abstract class StoreEngine<SF extends StoreFlusher,
40      CP extends CompactionPolicy, C extends Compactor, SFM extends StoreFileManager> {
41    protected SF storeFlusher;
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     * @return Store flusher to use.
78     */
79    public StoreFlusher getStoreFlusher() {
80      return this.storeFlusher;
81    }
82  
83    /**
84     * @param filesCompacting Files currently compacting
85     * @return whether a compaction selection is possible
86     */
87    public abstract boolean needsCompaction(List<StoreFile> filesCompacting);
88  
89    /**
90     * Creates an instance of a compaction context specific to this engine.
91     * Doesn't actually select or start a compaction. See CompactionContext class comment.
92     * @return New CompactionContext object.
93     */
94    public abstract CompactionContext createCompaction() throws IOException;
95  
96    /**
97     * Create the StoreEngine's components.
98     */
99    protected abstract void createComponents(
100       Configuration conf, Store store, KVComparator kvComparator) throws IOException;
101 
102   private void createComponentsOnce(
103       Configuration conf, Store store, KVComparator kvComparator) throws IOException {
104     assert compactor == null && compactionPolicy == null
105         && storeFileManager == null && storeFlusher == null;
106     createComponents(conf, store, kvComparator);
107     assert compactor != null && compactionPolicy != null
108         && storeFileManager != null && storeFlusher != null;
109   }
110 
111   /**
112    * Create the StoreEngine configured for the given Store.
113    * @param store The store. An unfortunate dependency needed due to it
114    *              being passed to coprocessors via the compactor.
115    * @param conf Store configuration.
116    * @param kvComparator KVComparator for storeFileManager.
117    * @return StoreEngine to use.
118    */
119   public static StoreEngine<?, ?, ?, ?> create(
120       Store store, Configuration conf, KVComparator kvComparator) throws IOException {
121     String className = conf.get(STORE_ENGINE_CLASS_KEY, DEFAULT_STORE_ENGINE_CLASS.getName());
122     try {
123       StoreEngine<?,?,?,?> se = ReflectionUtils.instantiateWithCustomCtor(
124           className, new Class[] { }, new Object[] { });
125       se.createComponentsOnce(conf, store, kvComparator);
126       return se;
127     } catch (Exception e) {
128       throw new IOException("Unable to load configured store engine '" + className + "'", e);
129     }
130   }
131 }