001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.regionserver;
021
022import java.io.IOException;
023import java.util.List;
024
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.CellComparator;
027import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
028import org.apache.hadoop.hbase.regionserver.compactions.CompactionPolicy;
029import org.apache.hadoop.hbase.regionserver.compactions.Compactor;
030import org.apache.hadoop.hbase.util.ReflectionUtils;
031import org.apache.yetus.audience.InterfaceAudience;
032
033/**
034 * StoreEngine is a factory that can create the objects necessary for HStore to operate.
035 * Since not all compaction policies, compactors and store file managers are compatible,
036 * they are tied together and replaced together via StoreEngine-s.
037 */
038@InterfaceAudience.Private
039public abstract class StoreEngine<SF extends StoreFlusher,
040    CP extends CompactionPolicy, C extends Compactor, SFM extends StoreFileManager> {
041  protected SF storeFlusher;
042  protected CP compactionPolicy;
043  protected C compactor;
044  protected SFM storeFileManager;
045
046  /**
047   * The name of the configuration parameter that specifies the class of
048   * a store engine that is used to manage and compact HBase store files.
049   */
050  public static final String STORE_ENGINE_CLASS_KEY = "hbase.hstore.engine.class";
051
052  private static final Class<? extends StoreEngine<?, ?, ?, ?>>
053    DEFAULT_STORE_ENGINE_CLASS = DefaultStoreEngine.class;
054
055  /**
056   * @return Compaction policy to use.
057   */
058  public CompactionPolicy getCompactionPolicy() {
059    return this.compactionPolicy;
060  }
061
062  /**
063   * @return Compactor to use.
064   */
065  public Compactor getCompactor() {
066    return this.compactor;
067  }
068
069  /**
070   * @return Store file manager to use.
071   */
072  public StoreFileManager getStoreFileManager() {
073    return this.storeFileManager;
074  }
075
076  /**
077   * @return Store flusher to use.
078   */
079  public StoreFlusher getStoreFlusher() {
080    return this.storeFlusher;
081  }
082
083  /**
084   * @param filesCompacting Files currently compacting
085   * @return whether a compaction selection is possible
086   */
087  public abstract boolean needsCompaction(List<HStoreFile> filesCompacting);
088
089  /**
090   * Creates an instance of a compaction context specific to this engine.
091   * Doesn't actually select or start a compaction. See CompactionContext class comment.
092   * @return New CompactionContext object.
093   */
094  public abstract CompactionContext createCompaction() throws IOException;
095
096  /**
097   * Create the StoreEngine's components.
098   */
099  protected abstract void createComponents(
100      Configuration conf, HStore store, CellComparator cellComparator) throws IOException;
101
102  private void createComponentsOnce(
103      Configuration conf, HStore store, CellComparator cellComparator) throws IOException {
104    assert compactor == null && compactionPolicy == null
105        && storeFileManager == null && storeFlusher == null;
106    createComponents(conf, store, cellComparator);
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 cellComparator CellComparator for storeFileManager.
117   * @return StoreEngine to use.
118   */
119  public static StoreEngine<?, ?, ?, ?> create(
120      HStore store, Configuration conf, CellComparator cellComparator) 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, cellComparator);
126      return se;
127    } catch (Exception e) {
128      throw new IOException("Unable to load configured store engine '" + className + "'", e);
129    }
130  }
131}