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 while (true) {
283 try {
284 if (lrClass != ProtobufLogReader.class) {
285
286 DefaultWALProvider.Reader reader = lrClass.newInstance();
287 reader.init(fs, path, conf, null);
288 return reader;
289 } else {
290 FSDataInputStream stream = fs.open(path);
291
292
293
294
295 byte[] magic = new byte[ProtobufLogReader.PB_WAL_MAGIC.length];
296 boolean isPbWal = (stream.read(magic) == magic.length)
297 && Arrays.equals(magic, ProtobufLogReader.PB_WAL_MAGIC);
298 DefaultWALProvider.Reader reader =
299 isPbWal ? new ProtobufLogReader() : new SequenceFileLogReader();
300 reader.init(fs, path, conf, stream);
301 return reader;
302 }
303 } catch (IOException e) {
304 String msg = e.getMessage();
305 if (msg != null && (msg.contains("Cannot obtain block length")
306 || msg.contains("Could not obtain the last block")
307 || msg.matches("Blocklist for [^ ]* has changed.*"))) {
308 if (++nbAttempt == 1) {
309 LOG.warn("Lease should have recovered. This is not expected. Will retry", e);
310 }
311 if (reporter != null && !reporter.progress()) {
312 throw new InterruptedIOException("Operation is cancelled");
313 }
314 if (nbAttempt > 2 && openTimeout < EnvironmentEdgeManager.currentTime()) {
315 LOG.error("Can't open after " + nbAttempt + " attempts and "
316 + (EnvironmentEdgeManager.currentTime() - startWaiting)
317 + "ms " + " for " + path);
318 } else {
319 try {
320 Thread.sleep(nbAttempt < 3 ? 500 : 1000);
321 continue;
322 } catch (InterruptedException ie) {
323 InterruptedIOException iioe = new InterruptedIOException();
324 iioe.initCause(ie);
325 throw iioe;
326 }
327 }
328 }
329 throw e;
330 }
331 }
332 } catch (IOException ie) {
333 throw ie;
334 } catch (Exception e) {
335 throw new IOException("Cannot get log reader", e);
336 }
337 }
338
339
340
341
342
343
344
345
346 public Writer createWALWriter(final FileSystem fs, final Path path) throws IOException {
347 return DefaultWALProvider.createWriter(conf, fs, path, false);
348 }
349
350
351
352
353
354 @VisibleForTesting
355 public Writer createRecoveredEditsWriter(final FileSystem fs, final Path path)
356 throws IOException {
357 return DefaultWALProvider.createWriter(conf, fs, path, true);
358 }
359
360
361
362
363
364 private static final AtomicReference<WALFactory> singleton = new AtomicReference<WALFactory>();
365 private static final String SINGLETON_ID = WALFactory.class.getName();
366
367
368 public static WALFactory getInstance(Configuration configuration) {
369 WALFactory factory = singleton.get();
370 if (null == factory) {
371 WALFactory temp = new WALFactory(configuration);
372 if (singleton.compareAndSet(null, temp)) {
373 factory = temp;
374 } else {
375
376 try {
377 temp.close();
378 } catch (IOException exception) {
379 LOG.debug("failed to close temporary singleton. ignoring.", exception);
380 }
381 factory = singleton.get();
382 }
383 }
384 return factory;
385 }
386
387
388
389
390
391
392 public static Reader createReader(final FileSystem fs, final Path path,
393 final Configuration configuration) throws IOException {
394 return getInstance(configuration).createReader(fs, path);
395 }
396
397
398
399
400
401
402 static Reader createReader(final FileSystem fs, final Path path,
403 final Configuration configuration, final CancelableProgressable reporter) throws IOException {
404 return getInstance(configuration).createReader(fs, path, reporter);
405 }
406
407
408
409
410
411
412
413 public static Reader createReaderIgnoreCustomClass(final FileSystem fs, final Path path,
414 final Configuration configuration) throws IOException {
415 return getInstance(configuration).createReader(fs, path, null, false);
416 }
417
418
419
420
421
422 static Writer createRecoveredEditsWriter(final FileSystem fs, final Path path,
423 final Configuration configuration)
424 throws IOException {
425 return DefaultWALProvider.createWriter(configuration, fs, path, true);
426 }
427
428
429
430
431
432 @VisibleForTesting
433 public static Writer createWALWriter(final FileSystem fs, final Path path,
434 final Configuration configuration)
435 throws IOException {
436 return DefaultWALProvider.createWriter(configuration, fs, path, false);
437 }
438 }