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