1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.regionserver.wal;
21
22 import java.io.FilterInputStream;
23 import java.io.IOException;
24 import java.lang.reflect.Field;
25 import java.lang.reflect.Method;
26 import java.util.NavigableMap;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.hbase.classification.InterfaceAudience;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.fs.FSDataInputStream;
33 import org.apache.hadoop.fs.FileSystem;
34 import org.apache.hadoop.fs.Path;
35 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
36 import org.apache.hadoop.hbase.regionserver.wal.HLog.Entry;
37 import org.apache.hadoop.io.SequenceFile;
38 import org.apache.hadoop.io.SequenceFile.Metadata;
39 import org.apache.hadoop.io.Text;
40
41 @InterfaceAudience.LimitedPrivate({HBaseInterfaceAudience.COPROC, HBaseInterfaceAudience.PHOENIX, HBaseInterfaceAudience.CONFIG})
42 public class SequenceFileLogReader extends ReaderBase {
43 private static final Log LOG = LogFactory.getLog(SequenceFileLogReader.class);
44
45
46 private static final Text WAL_VERSION_KEY = new Text("version");
47
48
49
50 private static final int COMPRESSION_VERSION = 1;
51 private static final Text WAL_COMPRESSION_TYPE_KEY = new Text("compression.type");
52 private static final Text DICTIONARY_COMPRESSION_TYPE = new Text("dictionary");
53
54
55
56
57
58
59
60
61
62
63
64
65
66 private static class WALReader extends SequenceFile.Reader {
67
68 WALReader(final FileSystem fs, final Path p, final Configuration c)
69 throws IOException {
70 super(fs, p, c);
71 }
72
73 @Override
74 protected FSDataInputStream openFile(FileSystem fs, Path file,
75 int bufferSize, long length)
76 throws IOException {
77 return new WALReaderFSDataInputStream(super.openFile(fs, file,
78 bufferSize, length), length);
79 }
80
81
82
83
84 static class WALReaderFSDataInputStream extends FSDataInputStream {
85 private boolean firstGetPosInvocation = true;
86 private long length;
87
88 WALReaderFSDataInputStream(final FSDataInputStream is, final long l)
89 throws IOException {
90 super(is);
91 this.length = l;
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 @Override
110 public long getPos() throws IOException {
111 if (this.firstGetPosInvocation) {
112 this.firstGetPosInvocation = false;
113 long adjust = 0;
114
115 try {
116 Field fIn = FilterInputStream.class.getDeclaredField("in");
117 fIn.setAccessible(true);
118 Object realIn = fIn.get(this.in);
119
120
121 if (realIn.getClass().getName().endsWith("DFSInputStream")) {
122 Method getFileLength = realIn.getClass().
123 getDeclaredMethod("getFileLength", new Class<?> []{});
124 getFileLength.setAccessible(true);
125 long realLength = ((Long)getFileLength.
126 invoke(realIn, new Object []{})).longValue();
127 assert(realLength >= this.length);
128 adjust = realLength - this.length;
129 } else {
130 LOG.info("Input stream class: " + realIn.getClass().getName() +
131 ", not adjusting length");
132 }
133 } catch(Exception e) {
134 SequenceFileLogReader.LOG.warn(
135 "Error while trying to get accurate file length. " +
136 "Truncation / data loss may occur if RegionServers die.", e);
137 throw new IOException(e);
138 }
139
140 return adjust + super.getPos();
141 }
142 return super.getPos();
143 }
144 }
145 }
146
147
148 protected SequenceFile.Reader reader;
149 long entryStart = 0;
150
151 public SequenceFileLogReader() {
152 super();
153 }
154
155 @Override
156 public void close() throws IOException {
157 try {
158 if (reader != null) {
159 this.reader.close();
160 this.reader = null;
161 }
162 } catch (IOException ioe) {
163 throw addFileInfoToException(ioe);
164 }
165 }
166
167 @Override
168 public long getPosition() throws IOException {
169 return reader != null ? reader.getPosition() : 0;
170 }
171
172 @Override
173 public void reset() throws IOException {
174
175
176 reader = new WALReader(fs, path, conf);
177 }
178
179 @Override
180 protected String initReader(FSDataInputStream stream) throws IOException {
181
182 if (stream != null) {
183 stream.close();
184 }
185 reset();
186 return null;
187 }
188
189 @Override
190 protected void initAfterCompression(String cellCodecClsName) throws IOException {
191
192 }
193
194 @Override
195 protected void initAfterCompression() throws IOException {
196
197 }
198
199 @Override
200 protected boolean hasCompression() {
201 return isWALCompressionEnabled(reader.getMetadata());
202 }
203
204 @Override
205 protected boolean hasTagCompression() {
206
207 return false;
208 }
209
210
211
212
213
214 static boolean isWALCompressionEnabled(final Metadata metadata) {
215
216 Text txt = metadata.get(WAL_VERSION_KEY);
217 if (txt == null || Integer.parseInt(txt.toString()) < COMPRESSION_VERSION) {
218 return false;
219 }
220
221 txt = metadata.get(WAL_COMPRESSION_TYPE_KEY);
222 return txt != null && txt.equals(DICTIONARY_COMPRESSION_TYPE);
223 }
224
225
226 @Override
227 protected boolean readNext(Entry e) throws IOException {
228 try {
229 boolean hasNext = this.reader.next(e.getKey(), e.getEdit());
230 if (!hasNext) return false;
231
232 NavigableMap<byte[], Integer> scopes = e.getEdit().getAndRemoveScopes();
233 if (scopes != null) {
234 e.getKey().readOlderScopes(scopes);
235 }
236 return true;
237 } catch (IOException ioe) {
238 throw addFileInfoToException(ioe);
239 }
240 }
241
242 @Override
243 protected void seekOnFs(long pos) throws IOException {
244 try {
245 reader.seek(pos);
246 } catch (IOException ioe) {
247 throw addFileInfoToException(ioe);
248 }
249 }
250
251 protected IOException addFileInfoToException(final IOException ioe)
252 throws IOException {
253 long pos = -1;
254 try {
255 pos = getPosition();
256 } catch (IOException e) {
257 LOG.warn("Failed getting position to add to throw", e);
258 }
259
260
261 long end = Long.MAX_VALUE;
262 try {
263 Field fEnd = SequenceFile.Reader.class.getDeclaredField("end");
264 fEnd.setAccessible(true);
265 end = fEnd.getLong(this.reader);
266 } catch(NoSuchFieldException nfe) {
267
268 } catch(IllegalAccessException iae) {
269
270 } catch(Exception e) {
271
272 LOG.warn("Unexpected exception when accessing the end field", e);
273 }
274
275 String msg = (this.path == null? "": this.path.toString()) +
276 ", entryStart=" + entryStart + ", pos=" + pos +
277 ((end == Long.MAX_VALUE) ? "" : ", end=" + end) +
278 ", edit=" + this.edit;
279
280
281 try {
282 return (IOException) ioe.getClass()
283 .getConstructor(String.class)
284 .newInstance(msg)
285 .initCause(ioe);
286 } catch(NoSuchMethodException nfe) {
287
288 } catch(IllegalAccessException iae) {
289
290 } catch(Exception e) {
291
292 LOG.warn("Unexpected exception when accessing the end field", e);
293 }
294 return ioe;
295 }
296 }