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.compactions;
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.fs.Path;
26  import org.apache.hadoop.hbase.regionserver.StoreFile;
27  
28  
29  /**
30   * This class holds all "physical" details necessary to run a compaction,
31   * and abstracts away the details specific to a particular compaction.
32   * It also has compaction request with all the logical details.
33   * Hence, this class is basically the compaction.
34   */
35  @InterfaceAudience.Private
36  public abstract class CompactionContext {
37    protected CompactionRequest request = null;
38  
39    /**
40     * Called before coprocessor preCompactSelection and should filter the candidates
41     * for coprocessor; i.e. exclude the files that definitely cannot be compacted at this time.
42     * @param filesCompacting files currently compacting
43     * @return the list of files that can theoretically be compacted.
44     */
45    public abstract List<StoreFile> preSelect(final List<StoreFile> filesCompacting);
46  
47    /**
48     * Called to select files for compaction. Must fill in the request field if successful.
49     * @param filesCompacting Files currently being compacted by other compactions.
50     * @param isUserCompaction Whether this is a user compaction.
51     * @param mayUseOffPeak Whether the underlying policy may assume it's off-peak hours.
52     * @param forceMajor Whether to force major compaction.
53     * @return Whether the selection succeeded. Selection may be empty and lead to no compaction.
54     */
55    public abstract boolean select(
56        final List<StoreFile> filesCompacting, final boolean isUserCompaction,
57        final boolean mayUseOffPeak, final boolean forceMajor) throws IOException;
58  
59    /**
60     * Forces external selection to be applied for this compaction.
61     * @param request The pre-cooked request with selection and other settings.
62     */
63    public void forceSelect(CompactionRequest request) {
64      this.request = request;
65    }
66  
67    /**
68     * Runs the compaction based on current selection. select/forceSelect must have been called.
69     * @return The new file paths resulting from compaction.
70     */
71    public abstract List<Path> compact() throws IOException;
72  
73    public CompactionRequest getRequest() {
74      assert hasSelection();
75      return this.request;
76    }
77  
78    public boolean hasSelection() {
79      return this.request != null;
80    }
81  }