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