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 isMajor) throws IOException {
68 preCommitWriters();
69 Collection<StoreFile.Writer> writers = this.writers();
70 if (LOG.isDebugEnabled()) {
71 LOG.debug("Commit " + writers.size() + " writers");
72 }
73 List<Path> paths = new ArrayList<Path>();
74 for (Writer writer : writers) {
75 if (writer == null) {
76 continue;
77 }
78 writer.appendMetadata(maxSeqId, isMajor);
79 preCloseWriter(writer);
80 paths.add(writer.getPath());
81 writer.close();
82 }
83 return paths;
84 }
85
86
87
88
89 public List<Path> abortWriters() {
90 List<Path> paths = new ArrayList<Path>();
91 for (StoreFile.Writer writer : writers()) {
92 try {
93 if (writer != null) {
94 paths.add(writer.getPath());
95 writer.close();
96 }
97 } catch (Exception ex) {
98 LOG.error("Failed to close the writer after an unfinished compaction.", ex);
99 }
100 }
101 return paths;
102 }
103
104 protected abstract Collection<StoreFile.Writer> writers();
105
106
107
108
109
110 protected void preCommitWriters() throws IOException {
111 }
112
113
114
115
116
117 protected void preCloseWriter(StoreFile.Writer writer) throws IOException {
118 }
119 }