View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver.compactions;
19  
20  import java.io.IOException;
21  import java.io.InterruptedIOException;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  
26  import org.apache.hadoop.classification.InterfaceAudience;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.regionserver.Store;
30  import org.apache.hadoop.hbase.regionserver.InternalScanner;
31  import org.apache.hadoop.hbase.regionserver.ScanType;
32  import org.apache.hadoop.hbase.regionserver.StoreFile;
33  import org.apache.hadoop.hbase.regionserver.StoreFileScanner;
34  
35  /**
36   * Compact passed set of files. Create an instance and then call {@link #compact(CompactionRequest)}
37   */
38  @InterfaceAudience.Private
39  public class DefaultCompactor extends Compactor {
40    public DefaultCompactor(final Configuration conf, final Store store) {
41      super(conf, store);
42    }
43  
44    /**
45     * Do a minor/major compaction on an explicit set of storefiles from a Store.
46     */
47    public List<Path> compact(final CompactionRequest request) throws IOException {
48      FileDetails fd = getFileDetails(request.getFiles(), request.isAllFiles());
49      this.progress = new CompactionProgress(fd.maxKeyCount);
50  
51      // Find the smallest read point across all the Scanners.
52      long smallestReadPoint = getSmallestReadPoint();
53      List<StoreFileScanner> scanners = createFileScanners(request.getFiles(), smallestReadPoint);
54  
55      StoreFile.Writer writer = null;
56      List<Path> newFiles = new ArrayList<Path>();
57      boolean cleanSeqId = false;
58      try {
59        InternalScanner scanner = null;
60        try {
61          /* Include deletes, unless we are doing a compaction of all files */
62          ScanType scanType =
63              request.isAllFiles() ? ScanType.COMPACT_DROP_DELETES : ScanType.COMPACT_RETAIN_DELETES;
64          scanner = preCreateCoprocScanner(request, scanType, fd.earliestPutTs, scanners);
65          if (scanner == null) {
66            scanner = createScanner(store, scanners, scanType, smallestReadPoint, fd.earliestPutTs);
67          }
68          scanner = postCreateCoprocScanner(request, scanType, scanner);
69          if (scanner == null) {
70            // NULL scanner returned from coprocessor hooks means skip normal processing.
71            return newFiles;
72          }
73          // Create the writer even if no kv(Empty store file is also ok),
74          // because we need record the max seq id for the store file, see HBASE-6059
75          if(fd.minSeqIdToKeep > 0) {
76            smallestReadPoint = Math.min(fd.minSeqIdToKeep, smallestReadPoint);
77            cleanSeqId = true;
78          }
79          writer = store.createWriterInTmp(fd.maxKeyCount, this.compactionCompression, true,
80              fd.maxMVCCReadpoint >= smallestReadPoint, fd.maxTagsLength > 0);
81          boolean finished = performCompaction(scanner, writer, smallestReadPoint, cleanSeqId);
82          if (!finished) {
83            writer.close();
84            store.getFileSystem().delete(writer.getPath(), false);
85            writer = null;
86            throw new InterruptedIOException( "Aborting compaction of store " + store +
87                " in region " + store.getRegionInfo().getRegionNameAsString() +
88                " because it was interrupted.");
89           }
90         } finally {
91           if (scanner != null) {
92             scanner.close();
93           }
94        }
95      } finally {
96        if (writer != null) {
97          writer.appendMetadata(fd.maxSeqId, request.isAllFiles());
98          writer.close();
99          newFiles.add(writer.getPath());
100       }
101     }
102     return newFiles;
103   }
104 
105   /**
106    * Compact a list of files for testing. Creates a fake {@link CompactionRequest} to pass to
107    * {@link #compact(CompactionRequest)};
108    * @param filesToCompact the files to compact. These are used as the compactionSelection for
109    *          the generated {@link CompactionRequest}.
110    * @param isMajor true to major compact (prune all deletes, max versions, etc)
111    * @return Product of compaction or an empty list if all cells expired or deleted and nothing \
112    *         made it through the compaction.
113    * @throws IOException
114    */
115   public List<Path> compactForTesting(final Collection<StoreFile> filesToCompact, boolean isMajor)
116       throws IOException {
117     CompactionRequest cr = new CompactionRequest(filesToCompact);
118     cr.setIsMajor(isMajor, isMajor);
119     return this.compact(cr);
120   }
121 }