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.Collections;
25  import java.util.List;
26  
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.regionserver.InternalScanner;
31  import org.apache.hadoop.hbase.regionserver.ScanType;
32  import org.apache.hadoop.hbase.regionserver.Store;
33  import org.apache.hadoop.hbase.regionserver.StoreFile;
34  import org.apache.hadoop.hbase.regionserver.StoreFileScanner;
35  
36  /**
37   * Compact passed set of files. Create an instance and then call {@link #compact(CompactionRequest)}
38   */
39  @InterfaceAudience.Private
40  public class DefaultCompactor extends Compactor {
41    public DefaultCompactor(final Configuration conf, final Store store) {
42      super(conf, store);
43    }
44  
45    /**
46     * Do a minor/major compaction on an explicit set of storefiles from a Store.
47     */
48    public List<Path> compact(final CompactionRequest request) throws IOException {
49      FileDetails fd = getFileDetails(request.getFiles(), request.isAllFiles());
50      this.progress = new CompactionProgress(fd.maxKeyCount);
51  
52      // Find the smallest read point across all the Scanners.
53      long smallestReadPoint = getSmallestReadPoint();
54  
55      List<StoreFileScanner> scanners;
56      Collection<StoreFile> readersToClose;
57      if (this.conf.getBoolean("hbase.regionserver.compaction.private.readers", false)) {
58        // clone all StoreFiles, so we'll do the compaction on a independent copy of StoreFiles,
59        // HFileFiles, and their readers
60        readersToClose = new ArrayList<StoreFile>(request.getFiles().size());
61        for (StoreFile f : request.getFiles()) {
62          readersToClose.add(new StoreFile(f));
63        }
64        scanners = createFileScanners(readersToClose, smallestReadPoint);
65      } else {
66        readersToClose = Collections.emptyList();
67        scanners = createFileScanners(request.getFiles(), smallestReadPoint);
68      }
69  
70      StoreFile.Writer writer = null;
71      List<Path> newFiles = new ArrayList<Path>();
72      boolean cleanSeqId = false;
73      IOException e = null;
74      try {
75        InternalScanner scanner = null;
76        try {
77          /* Include deletes, unless we are doing a compaction of all files */
78          ScanType scanType =
79              request.isAllFiles() ? ScanType.COMPACT_DROP_DELETES : ScanType.COMPACT_RETAIN_DELETES;
80          scanner = preCreateCoprocScanner(request, scanType, fd.earliestPutTs, scanners);
81          if (scanner == null) {
82            scanner = createScanner(store, scanners, scanType, smallestReadPoint, fd.earliestPutTs);
83          }
84          scanner = postCreateCoprocScanner(request, scanType, scanner);
85          if (scanner == null) {
86            // NULL scanner returned from coprocessor hooks means skip normal processing.
87            return newFiles;
88          }
89          // Create the writer even if no kv(Empty store file is also ok),
90          // because we need record the max seq id for the store file, see HBASE-6059
91          if(fd.minSeqIdToKeep > 0) {
92            smallestReadPoint = Math.min(fd.minSeqIdToKeep, smallestReadPoint);
93            cleanSeqId = true;
94          }
95          writer = store.createWriterInTmp(fd.maxKeyCount, this.compactionCompression, true,
96              fd.maxMVCCReadpoint >= smallestReadPoint, fd.maxTagsLength > 0);
97          boolean finished = performCompaction(scanner, writer, smallestReadPoint, cleanSeqId);
98          if (!finished) {
99            writer.close();
100           store.getFileSystem().delete(writer.getPath(), false);
101           writer = null;
102           throw new InterruptedIOException( "Aborting compaction of store " + store +
103               " in region " + store.getRegionInfo().getRegionNameAsString() +
104               " because it was interrupted.");
105          }
106        } finally {
107          if (scanner != null) {
108            scanner.close();
109          }
110       }
111     } catch (IOException ioe) {
112       e = ioe;
113       // Throw the exception
114       throw ioe;
115     }
116     finally {
117       try {
118         if (writer != null) {
119           if (e != null) {
120             writer.close();
121           } else {
122             writer.appendMetadata(fd.maxSeqId, request.isAllFiles());
123             writer.close();
124             newFiles.add(writer.getPath());
125           }
126         }
127       } finally {
128         for (StoreFile f : readersToClose) {
129           f.closeReader(true);
130         }
131       }
132     }
133     return newFiles;
134   }
135 
136   /**
137    * Compact a list of files for testing. Creates a fake {@link CompactionRequest} to pass to
138    * {@link #compact(CompactionRequest)};
139    * @param filesToCompact the files to compact. These are used as the compactionSelection for
140    *          the generated {@link CompactionRequest}.
141    * @param isMajor true to major compact (prune all deletes, max versions, etc)
142    * @return Product of compaction or an empty list if all cells expired or deleted and nothing \
143    *         made it through the compaction.
144    * @throws IOException
145    */
146   public List<Path> compactForTesting(final Collection<StoreFile> filesToCompact, boolean isMajor)
147       throws IOException {
148     CompactionRequest cr = new CompactionRequest(filesToCompact);
149     cr.setIsMajor(isMajor, isMajor);
150     return this.compact(cr);
151   }
152 }