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.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.Path;
29  import org.apache.hadoop.hbase.regionserver.InternalScanner;
30  import org.apache.hadoop.hbase.regionserver.ScanType;
31  import org.apache.hadoop.hbase.regionserver.Store;
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.isMajor());
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      IOException e = null;
58      try {
59        InternalScanner scanner = null;
60        try {
61          /* Include deletes, unless we are doing a major compaction */
62          ScanType scanType =
63              request.isMajor() ? 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          writer = store.createWriterInTmp(fd.maxKeyCount, this.compactionCompression, true,
76              fd.maxMVCCReadpoint >= smallestReadPoint, fd.maxTagsLength > 0);
77          boolean finished = performCompaction(scanner, writer, smallestReadPoint);
78          if (!finished) {
79            writer.close();
80            store.getFileSystem().delete(writer.getPath(), false);
81            writer = null;
82            throw new InterruptedIOException( "Aborting compaction of store " + store +
83                " in region " + store.getRegionInfo().getRegionNameAsString() +
84                " because it was interrupted.");
85           }
86         } finally {
87           if (scanner != null) {
88             scanner.close();
89           }
90        }
91      } catch (IOException ioe) {
92        e = ioe;
93        // Throw the exception;
94        throw ioe;
95      } finally {
96        if (writer != null) {
97          if (e != null) {
98            writer.close();
99          } else {
100           writer.appendMetadata(fd.maxSeqId, request.isMajor());
101           writer.close();
102           newFiles.add(writer.getPath());
103         }
104       }
105     }
106     return newFiles;
107   }
108 
109   /**
110    * Compact a list of files for testing. Creates a fake {@link CompactionRequest} to pass to
111    * {@link #compact(CompactionRequest)};
112    * @param filesToCompact the files to compact. These are used as the compactionSelection for
113    *          the generated {@link CompactionRequest}.
114    * @param isMajor true to major compact (prune all deletes, max versions, etc)
115    * @return Product of compaction or an empty list if all cells expired or deleted and nothing \
116    *         made it through the compaction.
117    * @throws IOException
118    */
119   public List<Path> compactForTesting(final Collection<StoreFile> filesToCompact, boolean isMajor)
120       throws IOException {
121     CompactionRequest cr = new CompactionRequest(filesToCompact);
122     cr.setIsMajor(isMajor);
123     return this.compact(cr);
124   }
125 }