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 public HLog.Entry readNextAndSetPosition() throws IOException {
86 HLog.Entry entry = this.reader.next();
87
88
89
90
91 this.position = this.reader.getPosition();
92
93 if (entry != null) {
94 entry.setCompressionContext(null);
95 }
96 return entry;
97 }
98
99
100
101
102
103 public void seek() throws IOException {
104 if (this.position != 0) {
105 this.reader.seek(this.position);
106 }
107 }
108
109
110
111
112
113 public long getPosition() {
114 return this.position;
115 }
116
117 public void setPosition(long pos) {
118 this.position = pos;
119 }
120
121
122
123
124
125 public void closeReader() throws IOException {
126 if (this.reader != null) {
127 this.reader.close();
128 this.reader = null;
129 }
130 }
131
132
133
134
135 void finishCurrentFile() {
136 this.position = 0;
137 try {
138 this.closeReader();
139 } catch (IOException e) {
140 LOG.warn("Unable to close reader", e);
141 }
142 }
143
144 }