1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.wal;
22
23 import java.io.IOException;
24 import java.util.Arrays;
25 import java.io.InterruptedIOException;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.concurrent.atomic.AtomicReference;
29
30 import com.google.common.annotations.VisibleForTesting;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.hadoop.hbase.classification.InterfaceAudience;
34 import org.apache.hadoop.conf.Configuration;
35 import org.apache.hadoop.fs.FSDataInputStream;
36 import org.apache.hadoop.fs.FileSystem;
37 import org.apache.hadoop.fs.Path;
38 import org.apache.hadoop.hbase.HConstants;
39 import org.apache.hadoop.hbase.wal.WAL.Reader;
40 import org.apache.hadoop.hbase.wal.WALProvider.Writer;
41 import org.apache.hadoop.hbase.util.CancelableProgressable;
42 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
43
44
45 import org.apache.hadoop.hbase.regionserver.wal.MetricsWAL;
46 import org.apache.hadoop.hbase.regionserver.wal.ProtobufLogReader;
47 import org.apache.hadoop.hbase.regionserver.wal.SequenceFileLogReader;
48 import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 @InterfaceAudience.Private
69 public class WALFactory {
70
71 private static final Log LOG = LogFactory.getLog(WALFactory.class);
72
73
74
75
76 static enum Providers {
77 defaultProvider(DefaultWALProvider.class),
78 filesystem(DefaultWALProvider.class),
79 multiwal(BoundedRegionGroupingProvider.class);
80
81 Class<? extends WALProvider> clazz;
82 Providers(Class<? extends WALProvider> clazz) {
83 this.clazz = clazz;
84 }
85 }
86
87 static final String WAL_PROVIDER = "hbase.wal.provider";
88 static final String DEFAULT_WAL_PROVIDER = Providers.defaultProvider.name();
89
90 static final String META_WAL_PROVIDER = "hbase.wal.meta_provider";
91 static final String DEFAULT_META_WAL_PROVIDER = Providers.defaultProvider.name();
92
93 final String factoryId;
94 final WALProvider provider;
95
96
97
98 final AtomicReference<WALProvider> metaProvider = new AtomicReference<WALProvider>();
99
100
101
102
103 private final Class<? extends DefaultWALProvider.Reader> logReaderClass;
104
105
106
107
108 private final int timeoutMillis;
109
110 private final Configuration conf;
111
112
113 private WALFactory(Configuration conf) {
114
115
116
117 timeoutMillis = conf.getInt("hbase.hlog.open.timeout", 300000);
118
119 logReaderClass = conf.getClass("hbase.regionserver.hlog.reader.impl", ProtobufLogReader.class,
120 DefaultWALProvider.Reader.class);
121 this.conf = conf;
122
123
124
125 provider = null;
126 factoryId = SINGLETON_ID;
127 }
128
129
130
131
132
133 WALProvider getProvider(final String key, final String defaultValue,
134 final List<WALActionsListener> listeners, final String providerId) throws IOException {
135 Class<? extends WALProvider> clazz;
136 try {
137 clazz = Providers.valueOf(conf.get(key, defaultValue)).clazz;
138 } catch (IllegalArgumentException exception) {
139
140
141
142 clazz = conf.getClass(key, DefaultWALProvider.class, WALProvider.class);
143 }
144 LOG.info("Instantiating WALProvider of type " + clazz);
145 try {
146 final WALProvider result = clazz.newInstance();
147 result.init(this, conf, listeners, providerId);
148 return result;
149 } catch (InstantiationException exception) {
150 LOG.error("couldn't set up WALProvider, check config key " + key);
151 LOG.debug("Exception details for failure to load WALProvider.", exception);
152 throw new IOException("couldn't set up WALProvider", exception);
153 } catch (IllegalAccessException exception) {
154 LOG.error("couldn't set up WALProvider, check config key " + key);
155 LOG.debug("Exception details for failure to load WALProvider.", exception);
156 throw new IOException("couldn't set up WALProvider", exception);
157 }
158 }
159
160
161
162
163
164
165
166
167 public WALFactory(final Configuration conf, final List<WALActionsListener> listeners,
168 final String factoryId) throws IOException {
169
170
171 timeoutMillis = conf.getInt("hbase.hlog.open.timeout", 300000);
172
173 logReaderClass = conf.getClass("hbase.regionserver.hlog.reader.impl", ProtobufLogReader.class,
174 DefaultWALProvider.Reader.class);
175 this.conf = conf;
176 this.factoryId = factoryId;
177
178 if (conf.getBoolean("hbase.regionserver.hlog.enabled", true)) {
179 provider = getProvider(WAL_PROVIDER, DEFAULT_WAL_PROVIDER, listeners, null);
180 } else {
181
182 LOG.warn("Running with WAL disabled.");
183 provider = new DisabledWALProvider();
184 provider.init(this, conf, null, factoryId);
185 }
186 }
187
188
189
190
191
192
193 public void close() throws IOException {
194 final WALProvider metaProvider = this.metaProvider.get();
195 if (null != metaProvider) {
196 metaProvider.close();
197 }
198
199
200 if (null != provider) {
201 provider.close();
202 }
203 }
204
205
206
207
208
209
210 public void shutdown() throws IOException {
211 IOException exception = null;
212 final WALProvider metaProvider = this.metaProvider.get();
213 if (null != metaProvider) {
214 try {
215 metaProvider.shutdown();
216 } catch(IOException ioe) {
217 exception = ioe;
218 }
219 }
220 provider.shutdown();
221 if (null != exception) {
222 throw exception;
223 }
224 }
225
226
227
228
229 public WAL getWAL(final byte[] identifier) throws IOException {
230 return provider.getWAL(identifier);
231 }
232
233
234
235
236 public WAL getMetaWAL(final byte[] identifier) throws IOException {
237 WALProvider metaProvider = this.metaProvider.get();
238 if (null == metaProvider) {
239 final WALProvider temp = getProvider(META_WAL_PROVIDER, DEFAULT_META_WAL_PROVIDER,
240 Collections.<WALActionsListener>singletonList(new MetricsWAL()),
241 DefaultWALProvider.META_WAL_PROVIDER_ID);
242 if (this.metaProvider.compareAndSet(null, temp)) {
243 metaProvider = temp;
244 } else {
245
246 temp.close();
247 metaProvider = this.metaProvider.get();
248 }
249 }
250 return metaProvider.getWAL(identifier);
251 }
252
253 public Reader createReader(final FileSystem fs, final Path path) throws IOException {
254 return createReader(fs, path, (CancelableProgressable)null);
255 }
256
257
258
259
260
261
262
263
264 public Reader createReader(final FileSystem fs, final Path path,
265 CancelableProgressable reporter) throws IOException {
266 return createReader(fs, path, reporter, true);
267 }
268
269 public Reader createReader(final FileSystem fs, final Path path,
270 CancelableProgressable reporter, boolean allowCustom)
271 throws IOException {
272 Class<? extends DefaultWALProvider.Reader> lrClass =
273 allowCustom ? logReaderClass : ProtobufLogReader.class;
274
275 try {
276
277
278
279 long startWaiting = EnvironmentEdgeManager.currentTime();
280 long openTimeout = timeoutMillis + startWaiting;
281 int nbAttempt = 0;
282 FSDataInputStream stream = null;
283 while (true) {
284 try {
285 if (lrClass != ProtobufLogReader.class) {
286
287 DefaultWALProvider.Reader reader = lrClass.newInstance();
288 reader.init(fs, path, conf, null);
289 return reader;
290 } else {
291 stream = fs.open(path);
292
293
294
295
296 byte[] magic = new byte[ProtobufLogReader.PB_WAL_MAGIC.length];
297 boolean isPbWal = (stream.read(magic) == magic.length)
298 && Arrays.equals(magic, ProtobufLogReader.PB_WAL_MAGIC);
299 DefaultWALProvider.Reader reader =
300 isPbWal ? new ProtobufLogReader() : new SequenceFileLogReader();
301 reader.init(fs, path, conf, stream);
302 return reader;
303 }
304 } catch (IOException e) {
305 try {
306 if (stream != null) {
307 stream.close();
308 }
309 } catch (IOException exception) {
310 LOG.warn("Could not close FSDataInputStream" + exception.getMessage());
311 LOG.debug("exception details", exception);
312 }
313 String msg = e.getMessage();
314 if (msg != null && (msg.contains("Cannot obtain block length")
315 || msg.contains("Could not obtain the last block")
316 || msg.matches("Blocklist for [^ ]* has changed.*"))) {
317 if (++nbAttempt == 1) {
318 LOG.warn("Lease should have recovered. This is not expected. Will retry", e);
319 }
320 if (reporter != null && !reporter.progress()) {
321 throw new InterruptedIOException("Operation is cancelled");
322 }
323 if (nbAttempt > 2 && openTimeout < EnvironmentEdgeManager.currentTime()) {
324 LOG.error("Can't open after " + nbAttempt + " attempts and "
325 + (EnvironmentEdgeManager.currentTime() - startWaiting)
326 + "ms " + " for " + path);
327 } else {
328 try {
329 Thread.sleep(nbAttempt < 3 ? 500 : 1000);
330 continue;
331 } catch (InterruptedException ie) {
332 InterruptedIOException iioe = new InterruptedIOException();
333 iioe.initCause(ie);
334 throw iioe;
335 }
336 }
337 }
338 throw e;
339 }
340 }
341 } catch (IOException ie) {
342 throw ie;
343 } catch (Exception e) {
344 throw new IOException("Cannot get log reader", e);
345 }
346 }
347
348
349
350
351
352
353
354
355 public Writer createWALWriter(final FileSystem fs, final Path path) throws IOException {
356 return DefaultWALProvider.createWriter(conf, fs, path, false);
357 }
358
359
360
361
362
363 @VisibleForTesting
364 public Writer createRecoveredEditsWriter(final FileSystem fs, final Path path)
365 throws IOException {
366 return DefaultWALProvider.createWriter(conf, fs, path, true);
367 }
368
369
370
371
372
373 private static final AtomicReference<WALFactory> singleton = new AtomicReference<WALFactory>();
374 private static final String SINGLETON_ID = WALFactory.class.getName();
375
376
377 public static WALFactory getInstance(Configuration configuration) {
378 WALFactory factory = singleton.get();
379 if (null == factory) {
380 WALFactory temp = new WALFactory(configuration);
381 if (singleton.compareAndSet(null, temp)) {
382 factory = temp;
383 } else {
384
385 try {
386 temp.close();
387 } catch (IOException exception) {
388 LOG.debug("failed to close temporary singleton. ignoring.", exception);
389 }
390 factory = singleton.get();
391 }
392 }
393 return factory;
394 }
395
396
397
398
399
400
401 public static Reader createReader(final FileSystem fs, final Path path,
402 final Configuration configuration) throws IOException {
403 return getInstance(configuration).createReader(fs, path);
404 }
405
406
407
408
409
410
411 static Reader createReader(final FileSystem fs, final Path path,
412 final Configuration configuration, final CancelableProgressable reporter) throws IOException {
413 return getInstance(configuration).createReader(fs, path, reporter);
414 }
415
416
417
418
419
420
421
422 public static Reader createReaderIgnoreCustomClass(final FileSystem fs, final Path path,
423 final Configuration configuration) throws IOException {
424 return getInstance(configuration).createReader(fs, path, null, false);
425 }
426
427
428
429
430
431 static Writer createRecoveredEditsWriter(final FileSystem fs, final Path path,
432 final Configuration configuration)
433 throws IOException {
434 return DefaultWALProvider.createWriter(configuration, fs, path, true);
435 }
436
437
438
439
440
441 @VisibleForTesting
442 public static Writer createWALWriter(final FileSystem fs, final Path path,
443 final Configuration configuration)
444 throws IOException {
445 return DefaultWALProvider.createWriter(configuration, fs, path, false);
446 }
447 }