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 */
019package org.apache.hadoop.hbase.regionserver;
020
021import org.apache.hadoop.conf.Configuration;
022import org.apache.yetus.audience.InterfaceAudience;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026/**
027 * MemStoreCompactionStrategy is the root of a class hierarchy which defines the strategy for
028 * choosing the next action to apply in an (in-memory) memstore compaction.
029 * Possible action are:
030 *  - No-op - do nothing
031 *  - Flatten - to change the segment's index from CSLM to a flat representation
032 *  - Merge - to merge the indices of the segments in the pipeline
033 *  - Compact - to merge the indices while removing data redundancies
034 *
035 * In addition while applying flat/merge actions it is possible to count the number of unique
036 * keys in the result segment.
037 */
038@InterfaceAudience.Private
039public abstract class MemStoreCompactionStrategy {
040
041  protected static final Logger LOG = LoggerFactory.getLogger(MemStoreCompactionStrategy.class);
042  // The upper bound for the number of segments we store in the pipeline prior to merging.
043  public static final String COMPACTING_MEMSTORE_THRESHOLD_KEY =
044      "hbase.hregion.compacting.pipeline.segments.limit";
045  public static final int COMPACTING_MEMSTORE_THRESHOLD_DEFAULT = 2;
046
047  /**
048   * Types of actions to be done on the pipeline upon MemStoreCompaction invocation.
049   * Note that every value covers the previous ones, i.e. if MERGE is the action it implies
050   * that the youngest segment is going to be flatten anyway.
051   */
052  public enum Action {
053    NOOP,
054    FLATTEN,  // flatten a segment in the pipeline
055    FLATTEN_COUNT_UNIQUE_KEYS,  // flatten a segment in the pipeline and count its unique keys
056    MERGE,    // merge all the segments in the pipeline into one
057    MERGE_COUNT_UNIQUE_KEYS,    // merge all pipeline segments into one and count its unique keys
058    COMPACT   // compact the data of all pipeline segments
059  }
060
061  protected final String cfName;
062  // The limit on the number of the segments in the pipeline
063  protected final int pipelineThreshold;
064
065
066  public MemStoreCompactionStrategy(Configuration conf, String cfName) {
067    this.cfName = cfName;
068    if(conf == null) {
069      pipelineThreshold = COMPACTING_MEMSTORE_THRESHOLD_DEFAULT;
070    } else {
071      pipelineThreshold =         // get the limit on the number of the segments in the pipeline
072          conf.getInt(COMPACTING_MEMSTORE_THRESHOLD_KEY, COMPACTING_MEMSTORE_THRESHOLD_DEFAULT);
073    }
074  }
075
076  @Override
077  public String toString() {
078    return getName() + ", pipelineThreshold=" + this.pipelineThreshold;
079  }
080
081  protected abstract String getName();
082
083  // get next compaction action to apply on compaction pipeline
084  public abstract Action getAction(VersionedSegmentsList versionedList);
085  // update policy stats based on the segment that replaced previous versioned list (in
086  // compaction pipeline)
087  public void updateStats(Segment replacement) {}
088  // resets policy stats
089  public void resetStats() {}
090
091  protected Action simpleMergeOrFlatten(VersionedSegmentsList versionedList, String strategy) {
092    int numOfSegments = versionedList.getNumOfSegments();
093    if (numOfSegments > pipelineThreshold) {
094      // to avoid too many segments, merge now
095      LOG.trace("Strategy={}, store={}; merging {} segments", strategy, cfName, numOfSegments);
096      return getMergingAction();
097    }
098
099    // just flatten a segment
100    LOG.trace("Strategy={}, store={}; flattening a segment", strategy, cfName);
101    return getFlattenAction();
102  }
103
104  protected Action getMergingAction() {
105    return Action.MERGE;
106  }
107
108  protected Action getFlattenAction() {
109    return Action.FLATTEN;
110  }
111
112  protected Action compact(VersionedSegmentsList versionedList, String strategyInfo) {
113    int numOfSegments = versionedList.getNumOfSegments();
114    LOG.trace("{} in-memory compaction for store={} compacting {} segments", strategyInfo,
115        cfName, numOfSegments);
116    return Action.COMPACT;
117  }
118}