1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.regionserver;
19
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
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.fs.Path;
29 import org.apache.hadoop.hbase.classification.InterfaceAudience;
30 import org.apache.hadoop.hbase.regionserver.StoreFile.Writer;
31 import org.apache.hadoop.hbase.regionserver.compactions.Compactor.CellSink;
32
33
34
35
36 @InterfaceAudience.Private
37 public abstract class AbstractMultiFileWriter implements CellSink {
38
39 private static final Log LOG = LogFactory.getLog(AbstractMultiFileWriter.class);
40
41
42 protected WriterFactory writerFactory;
43
44
45 protected StoreScanner sourceScanner;
46
47 public interface WriterFactory {
48 public StoreFile.Writer createWriter() throws IOException;
49 }
50
51
52
53
54
55
56 public void init(StoreScanner sourceScanner, WriterFactory factory) {
57 this.writerFactory = factory;
58 this.sourceScanner = sourceScanner;
59 }
60
61
62
63
64
65
66
67
68 public List<Path> commitWriters(long maxSeqId, boolean majorCompaction) throws IOException {
69 return commitWriters(maxSeqId, majorCompaction, Collections.<StoreFile>emptySet());
70 }
71
72 public List<Path> commitWriters(long maxSeqId, boolean majorCompaction,
73 Collection<StoreFile> storeFiles) throws IOException {
74 preCommitWriters();
75 Collection<StoreFile.Writer> writers = this.writers();
76 if (LOG.isDebugEnabled()) {
77 LOG.debug("Commit " + writers.size() + " writers, maxSeqId=" + maxSeqId + ", majorCompaction="
78 + majorCompaction);
79 }
80 List<Path> paths = new ArrayList<Path>();
81 for (Writer writer : writers) {
82 if (writer == null) {
83 continue;
84 }
85 writer.appendMetadata(maxSeqId, majorCompaction, storeFiles);
86 preCloseWriter(writer);
87 paths.add(writer.getPath());
88 writer.close();
89 }
90 return paths;
91 }
92
93
94
95
96 public List<Path> abortWriters() {
97 List<Path> paths = new ArrayList<Path>();
98 for (StoreFile.Writer writer : writers()) {
99 try {
100 if (writer != null) {
101 paths.add(writer.getPath());
102 writer.close();
103 }
104 } catch (Exception ex) {
105 LOG.error("Failed to close the writer after an unfinished compaction.", ex);
106 }
107 }
108 return paths;
109 }
110
111 protected abstract Collection<StoreFile.Writer> writers();
112
113
114
115
116
117 protected void preCommitWriters() throws IOException {
118 }
119
120
121
122
123
124 protected void preCloseWriter(StoreFile.Writer writer) throws IOException {
125 }
126 }