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.ArrayList;
23  import java.util.List;
24  
25  import org.apache.hadoop.classification.InterfaceAudience;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.fs.Path;
28  import org.apache.hadoop.hbase.KeyValue.KVComparator;
29  import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
30  import org.apache.hadoop.hbase.regionserver.compactions.CompactionPolicy;
31  import org.apache.hadoop.hbase.regionserver.compactions.Compactor;
32  import org.apache.hadoop.hbase.regionserver.compactions.DefaultCompactionPolicy;
33  import org.apache.hadoop.hbase.regionserver.compactions.DefaultCompactor;
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.Private
41  public class DefaultStoreEngine extends StoreEngine<
42    DefaultCompactionPolicy, DefaultCompactor, DefaultStoreFileManager> {
43  
44    public static final String DEFAULT_COMPACTOR_CLASS_KEY =
45        "hbase.hstore.defaultengine.compactor.class";
46    public static final String DEFAULT_COMPACTION_POLICY_CLASS_KEY =
47        "hbase.hstore.defaultengine.compactionpolicy.class";
48  
49    private static final Class<? extends DefaultCompactor>
50      DEFAULT_COMPACTOR_CLASS = DefaultCompactor.class;
51    private static final Class<? extends DefaultCompactionPolicy>
52      DEFAULT_COMPACTION_POLICY_CLASS = DefaultCompactionPolicy.class;
53  
54    @Override
55    protected void createComponents(
56        Configuration conf, Store store, KVComparator kvComparator) throws IOException {
57      storeFileManager = new DefaultStoreFileManager(kvComparator, conf);
58      String className = conf.get(DEFAULT_COMPACTOR_CLASS_KEY, DEFAULT_COMPACTOR_CLASS.getName());
59      try {
60        compactor = ReflectionUtils.instantiateWithCustomCtor(className,
61            new Class[] { Configuration.class, Store.class }, new Object[] { conf, store });
62      } catch (Exception e) {
63        throw new IOException("Unable to load configured compactor '" + className + "'", e);
64      }
65      className = conf.get(
66          DEFAULT_COMPACTION_POLICY_CLASS_KEY, DEFAULT_COMPACTION_POLICY_CLASS.getName());
67      try {
68        compactionPolicy = ReflectionUtils.instantiateWithCustomCtor(className,
69            new Class[] { Configuration.class, StoreConfigInformation.class },
70            new Object[] { conf, store });
71      } catch (Exception e) {
72        throw new IOException("Unable to load configured compaction policy '" + className + "'", e);
73      }
74    }
75  
76    @Override
77    public CompactionContext createCompaction() {
78      return new DefaultCompactionContext();
79    }
80  
81    private class DefaultCompactionContext extends CompactionContext {
82      @Override
83      public boolean select(List<StoreFile> filesCompacting, boolean isUserCompaction,
84          boolean mayUseOffPeak, boolean forceMajor) throws IOException {
85        request = compactionPolicy.selectCompaction(storeFileManager.getStorefiles(),
86            filesCompacting, isUserCompaction, mayUseOffPeak, forceMajor);
87        return request != null;
88      }
89  
90      @Override
91      public List<Path> compact() throws IOException {
92        return compactor.compact(request);
93      }
94  
95      @Override
96      public List<StoreFile> preSelect(List<StoreFile> filesCompacting) {
97        return compactionPolicy.preSelectCompactionForCoprocessor(
98            storeFileManager.getStorefiles(), filesCompacting);
99      }
100   }
101 }