001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver.compactions;
019
020import java.io.IOException;
021import java.util.List;
022import org.apache.hadoop.fs.Path;
023import org.apache.hadoop.hbase.regionserver.HStoreFile;
024import org.apache.hadoop.hbase.regionserver.throttle.ThroughputController;
025import org.apache.hadoop.hbase.security.User;
026import org.apache.yetus.audience.InterfaceAudience;
027
028/**
029 * This class holds all "physical" details necessary to run a compaction, and abstracts away the
030 * details specific to a particular compaction. It also has compaction request with all the logical
031 * details. Hence, this class is basically the compaction.
032 */
033@InterfaceAudience.Private
034public abstract class CompactionContext {
035  protected CompactionRequestImpl request = null;
036
037  /**
038   * Called before coprocessor preCompactSelection and should filter the candidates for coprocessor;
039   * i.e. exclude the files that definitely cannot be compacted at this time.
040   * @param filesCompacting files currently compacting
041   * @return the list of files that can theoretically be compacted.
042   */
043  public abstract List<HStoreFile> preSelect(List<HStoreFile> filesCompacting);
044
045  /**
046   * Called to select files for compaction. Must fill in the request field if successful.
047   * @param filesCompacting  Files currently being compacted by other compactions.
048   * @param isUserCompaction Whether this is a user compaction.
049   * @param mayUseOffPeak    Whether the underlying policy may assume it's off-peak hours.
050   * @param forceMajor       Whether to force major compaction.
051   * @return Whether the selection succeeded. Selection may be empty and lead to no compaction.
052   */
053  public abstract boolean select(List<HStoreFile> filesCompacting, boolean isUserCompaction,
054    boolean mayUseOffPeak, boolean forceMajor) throws IOException;
055
056  /**
057   * Forces external selection to be applied for this compaction.
058   * @param request The pre-cooked request with selection and other settings.
059   */
060  public void forceSelect(CompactionRequestImpl request) {
061    this.request = request;
062  }
063
064  public abstract List<Path> compact(ThroughputController throughputController, User user)
065    throws IOException;
066
067  public CompactionRequestImpl getRequest() {
068    assert hasSelection();
069    return this.request;
070  }
071
072  public boolean hasSelection() {
073    return this.request != null;
074  }
075}