1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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
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
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
71 return newFiles;
72 }
73
74
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
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
111
112
113
114
115
116
117
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 }