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.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.io.compress.Compression;
32  import org.apache.hadoop.hbase.regionserver.InternalScanner;
33  import org.apache.hadoop.hbase.regionserver.ScanType;
34  import org.apache.hadoop.hbase.regionserver.Store;
35  import org.apache.hadoop.hbase.regionserver.StoreFile;
36  import org.apache.hadoop.hbase.regionserver.StoreFileScanner;
37  import org.apache.hadoop.hbase.regionserver.StoreScanner;
38  import org.apache.hadoop.hbase.regionserver.StripeMultiFileWriter;
39  import org.apache.hadoop.hbase.regionserver.StoreFile.Writer;
40  import org.apache.hadoop.hbase.util.Bytes;
41  
42  /**
43   * This is the placeholder for stripe compactor. The implementation,
44   * as well as the proper javadoc, will be added in HBASE-7967.
45   */
46  @InterfaceAudience.Private
47  public class StripeCompactor extends Compactor {
48    private static final Log LOG = LogFactory.getLog(StripeCompactor.class);
49    public StripeCompactor(Configuration conf, Store store) {
50      super(conf, store);
51    }
52  
53    public List<Path> compact(CompactionRequest request, List<byte[]> targetBoundaries,
54        byte[] majorRangeFromRow, byte[] majorRangeToRow,
55        CompactionThroughputController throughputController) throws IOException {
56      if (LOG.isDebugEnabled()) {
57        StringBuilder sb = new StringBuilder();
58        sb.append("Executing compaction with " + targetBoundaries.size() + " boundaries:");
59        for (byte[] tb : targetBoundaries) {
60          sb.append(" [").append(Bytes.toString(tb)).append("]");
61        }
62        LOG.debug(sb.toString());
63      }
64      StripeMultiFileWriter writer = new StripeMultiFileWriter.BoundaryMultiWriter(
65          targetBoundaries, majorRangeFromRow, majorRangeToRow);
66      return compactInternal(writer, request, majorRangeFromRow, majorRangeToRow,
67        throughputController);
68    }
69  
70    public List<Path> compact(CompactionRequest request, int targetCount, long targetSize,
71        byte[] left, byte[] right, byte[] majorRangeFromRow, byte[] majorRangeToRow,
72        CompactionThroughputController throughputController) throws IOException {
73      if (LOG.isDebugEnabled()) {
74        LOG.debug("Executing compaction with " + targetSize
75            + " target file size, no more than " + targetCount + " files, in ["
76            + Bytes.toString(left) + "] [" + Bytes.toString(right) + "] range");
77      }
78      StripeMultiFileWriter writer = new StripeMultiFileWriter.SizeMultiWriter(
79          targetCount, targetSize, left, right);
80      return compactInternal(writer, request, majorRangeFromRow, majorRangeToRow,
81        throughputController);
82    }
83  
84    private List<Path> compactInternal(StripeMultiFileWriter mw, CompactionRequest request,
85        byte[] majorRangeFromRow, byte[] majorRangeToRow,
86        CompactionThroughputController throughputController) throws IOException {
87      final Collection<StoreFile> filesToCompact = request.getFiles();
88      final FileDetails fd = getFileDetails(filesToCompact, request.isMajor());
89      this.progress = new CompactionProgress(fd.maxKeyCount);
90  
91      long smallestReadPoint = getSmallestReadPoint();
92      List<StoreFileScanner> scanners = createFileScanners(filesToCompact, smallestReadPoint);
93  
94      boolean finished = false;
95      InternalScanner scanner = null;
96      boolean cleanSeqId = false;
97      try {
98        // Get scanner to use.
99        ScanType coprocScanType = ScanType.COMPACT_RETAIN_DELETES;
100       scanner = preCreateCoprocScanner(request, coprocScanType, fd.earliestPutTs, scanners);
101       if (scanner == null) {
102         scanner = (majorRangeFromRow == null)
103             ? createScanner(store, scanners,
104                 ScanType.COMPACT_RETAIN_DELETES, smallestReadPoint, fd.earliestPutTs)
105             : createScanner(store, scanners,
106                 smallestReadPoint, fd.earliestPutTs, majorRangeFromRow, majorRangeToRow);
107       }
108       scanner = postCreateCoprocScanner(request, coprocScanType, scanner);
109       if (scanner == null) {
110         // NULL scanner returned from coprocessor hooks means skip normal processing.
111         return new ArrayList<Path>();
112       }
113 
114       // Create the writer factory for compactions.
115       if(fd.minSeqIdToKeep > 0) {
116         smallestReadPoint = Math.min(fd.minSeqIdToKeep, smallestReadPoint);
117         cleanSeqId = true;
118       }
119 
120       final Compression.Algorithm compression = store.getFamily().getCompactionCompression();
121       StripeMultiFileWriter.WriterFactory factory = new StripeMultiFileWriter.WriterFactory() {
122         @Override
123         public Writer createWriter() throws IOException {
124           return store.createWriterInTmp(
125               fd.maxKeyCount, compression, true, true, fd.maxTagsLength > 0);
126         }
127       };
128 
129       // Prepare multi-writer, and perform the compaction using scanner and writer.
130       // It is ok here if storeScanner is null.
131       StoreScanner storeScanner = (scanner instanceof StoreScanner) ? (StoreScanner)scanner : null;
132       mw.init(storeScanner, factory, store.getComparator());
133       finished =
134           performCompaction(scanner, mw, smallestReadPoint, cleanSeqId, throughputController);
135       if (!finished) {
136         throw new InterruptedIOException( "Aborting compaction of store " + store +
137             " in region " + store.getRegionInfo().getRegionNameAsString() +
138             " because it was interrupted.");
139       }
140     } finally {
141       if (scanner != null) {
142         try {
143           scanner.close();
144         } catch (Throwable t) {
145           // Don't fail the compaction if this fails.
146           LOG.error("Failed to close scanner after compaction.", t);
147         }
148       }
149       if (!finished) {
150         for (Path leftoverFile : mw.abortWriters()) {
151           try {
152             store.getFileSystem().delete(leftoverFile, false);
153           } catch (Exception ex) {
154             LOG.error("Failed to delete the leftover file after an unfinished compaction.", ex);
155           }
156         }
157       }
158     }
159 
160     assert finished : "We should have exited the method on all error paths";
161     List<Path> newFiles = mw.commitWriters(fd.maxSeqId, request.isMajor());
162     assert !newFiles.isEmpty() : "Should have produced an empty file to preserve metadata.";
163     return newFiles;
164   }
165 }