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  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  import java.util.List;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.fs.Path;
27  import org.apache.hadoop.hbase.CellComparator;
28  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
29  import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
30  import org.apache.hadoop.hbase.regionserver.compactions.ExploringCompactionPolicy;
31  import org.apache.hadoop.hbase.regionserver.compactions.RatioBasedCompactionPolicy;
32  import org.apache.hadoop.hbase.regionserver.compactions.DefaultCompactor;
33  import org.apache.hadoop.hbase.regionserver.compactions.CompactionThroughputController;
34  import org.apache.hadoop.hbase.util.ReflectionUtils;
35  
36  /**
37   * Default StoreEngine creates the default compactor, policy, and store file manager, or
38   * their derivatives.
39   */
40  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
41  public class DefaultStoreEngine extends StoreEngine<
42    DefaultStoreFlusher, RatioBasedCompactionPolicy, DefaultCompactor, DefaultStoreFileManager> {
43  
44    public static final String DEFAULT_STORE_FLUSHER_CLASS_KEY =
45        "hbase.hstore.defaultengine.storeflusher.class";
46    public static final String DEFAULT_COMPACTOR_CLASS_KEY =
47        "hbase.hstore.defaultengine.compactor.class";
48    public static final String DEFAULT_COMPACTION_POLICY_CLASS_KEY =
49        "hbase.hstore.defaultengine.compactionpolicy.class";
50  
51    private static final Class<? extends DefaultStoreFlusher>
52      DEFAULT_STORE_FLUSHER_CLASS = DefaultStoreFlusher.class;
53    private static final Class<? extends DefaultCompactor>
54      DEFAULT_COMPACTOR_CLASS = DefaultCompactor.class;
55    private static final Class<? extends RatioBasedCompactionPolicy>
56      DEFAULT_COMPACTION_POLICY_CLASS = ExploringCompactionPolicy.class;
57  
58    @Override
59    public boolean needsCompaction(List<StoreFile> filesCompacting) {
60      return compactionPolicy.needsCompaction(
61          this.storeFileManager.getStorefiles(), filesCompacting);
62    }
63  
64    @Override
65    protected void createComponents(
66        Configuration conf, Store store, CellComparator kvComparator) throws IOException {
67      createCompactor(conf, store);
68      createCompactionPolicy(conf, store);
69      createStoreFlusher(conf, store);
70      storeFileManager = new DefaultStoreFileManager(kvComparator, conf, compactionPolicy.getConf());
71  
72    }
73  
74    protected void createCompactor(Configuration conf, Store store) throws IOException {
75      String className = conf.get(DEFAULT_COMPACTOR_CLASS_KEY, DEFAULT_COMPACTOR_CLASS.getName());
76      try {
77        compactor = ReflectionUtils.instantiateWithCustomCtor(className,
78            new Class[] { Configuration.class, Store.class }, new Object[] { conf, store });
79      } catch (Exception e) {
80        throw new IOException("Unable to load configured compactor '" + className + "'", e);
81      }
82    }
83  
84    protected void createCompactionPolicy(Configuration conf, Store store) throws IOException {
85      String className = conf.get(
86          DEFAULT_COMPACTION_POLICY_CLASS_KEY, DEFAULT_COMPACTION_POLICY_CLASS.getName());
87      try {
88        compactionPolicy = ReflectionUtils.instantiateWithCustomCtor(className,
89            new Class[] { Configuration.class, StoreConfigInformation.class },
90            new Object[] { conf, store });
91      } catch (Exception e) {
92        throw new IOException("Unable to load configured compaction policy '" + className + "'", e);
93      }
94    }
95  
96    protected void createStoreFlusher(Configuration conf, Store store) throws IOException {
97      String className = conf.get(
98          DEFAULT_STORE_FLUSHER_CLASS_KEY, DEFAULT_STORE_FLUSHER_CLASS.getName());
99      try {
100       storeFlusher = ReflectionUtils.instantiateWithCustomCtor(className,
101           new Class[] { Configuration.class, Store.class }, new Object[] { conf, store });
102     } catch (Exception e) {
103       throw new IOException("Unable to load configured store flusher '" + className + "'", e);
104     }
105   }
106 
107   @Override
108   public CompactionContext createCompaction() {
109     return new DefaultCompactionContext();
110   }
111 
112   private class DefaultCompactionContext extends CompactionContext {
113     @Override
114     public boolean select(List<StoreFile> filesCompacting, boolean isUserCompaction,
115         boolean mayUseOffPeak, boolean forceMajor) throws IOException {
116       request = compactionPolicy.selectCompaction(storeFileManager.getStorefiles(),
117           filesCompacting, isUserCompaction, mayUseOffPeak, forceMajor);
118       return request != null;
119     }
120 
121     @Override
122     public List<Path> compact(CompactionThroughputController throughputController)
123         throws IOException {
124       return compactor.compact(request, throughputController);
125     }
126 
127     @Override
128     public List<StoreFile> preSelect(List<StoreFile> filesCompacting) {
129       return compactionPolicy.preSelectCompactionForCoprocessor(
130           storeFileManager.getStorefiles(), filesCompacting);
131     }
132   }
133 
134 }