From 7395ffac447bdb159b5c9a2bc65f95af0eea11fe Mon Sep 17 00:00:00 2001 From: tedyu Date: Mon, 5 Nov 2018 08:29:04 -0800 Subject: [PATCH] HBASE-21247 Custom WAL Provider cannot be specified by configuration whose value is outside the enums in Providers --- .../apache/hadoop/hbase/wal/WALFactory.java | 14 +++++++++-- .../hadoop/hbase/wal/IOTestProvider.java | 5 +++- .../hadoop/hbase/wal/TestWALFactory.java | 24 +++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/wal/WALFactory.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/wal/WALFactory.java index 0e6e3657dd5..8bde6d20011 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/wal/WALFactory.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/wal/WALFactory.java @@ -260,8 +260,18 @@ public class WALFactory { if (provider != null) { return provider; } - provider = createProvider(getProviderClass(META_WAL_PROVIDER, - conf.get(WAL_PROVIDER, DEFAULT_WAL_PROVIDER))); + Class clz = null; + if (conf.get(META_WAL_PROVIDER) == null) { + try { + clz = conf.getClass(WAL_PROVIDER, Providers.defaultProvider.clazz, WALProvider.class); + } catch (Throwable t) { + // the WAL provider should be an enum. Proceed + } + } + if (clz == null){ + clz = getProviderClass(META_WAL_PROVIDER, conf.get(WAL_PROVIDER, DEFAULT_WAL_PROVIDER)); + } + provider = createProvider(clz); provider.init(this, conf, AbstractFSWALProvider.META_WAL_PROVIDER_ID); provider.addWALActionsListener(new MetricsWAL()); if (metaProvider.compareAndSet(null, provider)) { diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/wal/IOTestProvider.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/wal/IOTestProvider.java index 453b742f9be..d062c77cb33 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/wal/IOTestProvider.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/wal/IOTestProvider.java @@ -27,6 +27,8 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; @@ -87,6 +89,7 @@ public class IOTestProvider implements WALProvider { private volatile FSHLog log; private String providerId; + protected AtomicBoolean initialized = new AtomicBoolean(false); private List listeners = new ArrayList<>(); /** @@ -97,7 +100,7 @@ public class IOTestProvider implements WALProvider { */ @Override public void init(WALFactory factory, Configuration conf, String providerId) throws IOException { - if (factory != null) { + if (!initialized.compareAndSet(false, true)) { throw new IllegalStateException("WALProvider.init should only be called once."); } this.factory = factory; diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/wal/TestWALFactory.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/wal/TestWALFactory.java index a20a0717df5..8fbe09dd30b 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/wal/TestWALFactory.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/wal/TestWALFactory.java @@ -742,4 +742,28 @@ public class TestWALFactory { WALFactory.WAL_PROVIDER, Providers.multiwal.name()); assertEquals(Providers.multiwal.clazz, multiwalProviderClass); } + + @Test + public void testCustomProvider() throws IOException { + final Configuration config = new Configuration(); + config.set(WALFactory.WAL_PROVIDER, IOTestProvider.class.getName()); + final WALFactory walFactory = new WALFactory(config, this.currentServername.toString()); + Class walProvider = walFactory.getProviderClass( + WALFactory.WAL_PROVIDER, Providers.filesystem.name()); + assertEquals(IOTestProvider.class, walProvider); + WALProvider metaWALProvider = walFactory.getMetaProvider(); + assertEquals(IOTestProvider.class, metaWALProvider.getClass()); + } + + @Test + public void testCustomMetaProvider() throws IOException { + final Configuration config = new Configuration(); + config.set(WALFactory.META_WAL_PROVIDER, IOTestProvider.class.getName()); + final WALFactory walFactory = new WALFactory(config, this.currentServername.toString()); + Class walProvider = walFactory.getProviderClass( + WALFactory.WAL_PROVIDER, Providers.filesystem.name()); + assertEquals(Providers.filesystem.clazz, walProvider); + WALProvider metaWALProvider = walFactory.getMetaProvider(); + assertEquals(IOTestProvider.class, metaWALProvider.getClass()); + } }