1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.replication.regionserver;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hadoop.classification.InterfaceAudience;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.fs.FileSystem;
26 import org.apache.hadoop.fs.Path;
27 import org.apache.hadoop.hbase.regionserver.wal.HLog;
28
29 import java.io.IOException;
30
31
32
33
34
35 @InterfaceAudience.Private
36 public class ReplicationHLogReaderManager {
37
38 private static final Log LOG = LogFactory.getLog(ReplicationHLogReaderManager.class);
39 private final FileSystem fs;
40 private final Configuration conf;
41 private long position = 0;
42 private HLog.Reader reader;
43 private Path lastPath;
44
45
46
47
48
49
50
51 public ReplicationHLogReaderManager(FileSystem fs, Configuration conf) {
52 this.fs = fs;
53 this.conf = conf;
54 }
55
56
57
58
59
60
61
62 public HLog.Reader openReader(Path path) throws IOException {
63
64
65 if (this.reader == null || !this.lastPath.equals(path)) {
66 this.closeReader();
67 this.reader = HLog.getReader(this.fs, path, this.conf);
68 this.lastPath = path;
69 } else {
70 try {
71 this.reader.reset();
72 } catch (NullPointerException npe) {
73 throw new IOException("NPE resetting reader, likely HDFS-4380", npe);
74 }
75 }
76 return this.reader;
77 }
78
79
80
81
82
83
84
85
86 public HLog.Entry readNextAndSetPosition(HLog.Entry[] entriesArray,
87 int currentNbEntries) throws IOException {
88 HLog.Entry entry = this.reader.next(entriesArray[currentNbEntries]);
89
90
91
92
93 this.position = this.reader.getPosition();
94
95 if (entry != null) {
96 entry.setCompressionContext(null);
97 }
98 return entry;
99 }
100
101
102
103
104
105 public void seek() throws IOException {
106 if (this.position != 0) {
107 this.reader.seek(this.position);
108 }
109 }
110
111
112
113
114
115 public long getPosition() {
116 return this.position;
117 }
118
119 public void setPosition(long pos) {
120 this.position = pos;
121 }
122
123
124
125
126
127 public void closeReader() throws IOException {
128 if (this.reader != null) {
129 this.reader.close();
130 this.reader = null;
131 }
132 }
133
134
135
136
137 public void finishCurrentFile() {
138 this.position = 0;
139 try {
140 this.closeReader();
141 } catch (IOException e) {
142 LOG.warn("Unable to close reader", e);
143 }
144 }
145
146 }