diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/resolver/MountTableResolver.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/resolver/MountTableResolver.java index 3f6efd6a615..c264de32d2e 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/resolver/MountTableResolver.java +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/resolver/MountTableResolver.java @@ -17,6 +17,8 @@ */ package org.apache.hadoop.hdfs.server.federation.resolver; +import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.DFS_NAMESERVICES; +import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.DeprecatedKeys.DFS_NAMESERVICE_ID; import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_DEFAULT_NAMESERVICE; import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.FEDERATION_MOUNT_TABLE_MAX_CACHE_SIZE; import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.FEDERATION_MOUNT_TABLE_MAX_CACHE_SIZE_DEFAULT; @@ -42,7 +44,6 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; -import org.apache.hadoop.HadoopIllegalArgumentException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hdfs.DFSUtil; @@ -149,14 +150,25 @@ public class MountTableResolver * @param conf Configuration for this resolver. */ private void initDefaultNameService(Configuration conf) { - try { - this.defaultNameService = conf.get( - DFS_ROUTER_DEFAULT_NAMESERVICE, - DFSUtil.getNamenodeNameServiceId(conf)); - } catch (HadoopIllegalArgumentException e) { - LOG.error("Cannot find default name service, setting it to the first"); + this.defaultNameService = conf.get( + DFS_ROUTER_DEFAULT_NAMESERVICE, + DFSUtil.getNamenodeNameServiceId(conf)); + + if (defaultNameService == null) { + LOG.warn( + "{} and {} is not set. Fallback to {} as the default name service.", + DFS_ROUTER_DEFAULT_NAMESERVICE, DFS_NAMESERVICE_ID, DFS_NAMESERVICES); Collection nsIds = DFSUtilClient.getNameServiceIds(conf); - this.defaultNameService = nsIds.iterator().next(); + if (nsIds.isEmpty()) { + this.defaultNameService = ""; + } else { + this.defaultNameService = nsIds.iterator().next(); + } + } + + if (this.defaultNameService.equals("")) { + LOG.warn("Default name service is not set."); + } else { LOG.info("Default name service: {}", this.defaultNameService); } } diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/resolver/TestInitializeMountTableResolver.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/resolver/TestInitializeMountTableResolver.java new file mode 100644 index 00000000000..5db7531c4dd --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/resolver/TestInitializeMountTableResolver.java @@ -0,0 +1,82 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hdfs.server.federation.resolver; + +import org.apache.hadoop.conf.Configuration; +import org.junit.Test; + +import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMESERVICE_ID; +import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.DFS_NAMESERVICES; +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_DEFAULT_NAMESERVICE; +import static org.junit.Assert.assertEquals; + +/** + * Test {@link MountTableResolver} initialization. + */ +public class TestInitializeMountTableResolver { + + @Test + public void testDefaultNameserviceIsMissing() { + Configuration conf = new Configuration(); + MountTableResolver mountTable = new MountTableResolver(conf); + assertEquals("", mountTable.getDefaultNamespace()); + } + + @Test + public void testDefaultNameserviceWithEmptyString() { + Configuration conf = new Configuration(); + conf.set(DFS_ROUTER_DEFAULT_NAMESERVICE, ""); + MountTableResolver mountTable = new MountTableResolver(conf); + assertEquals("", mountTable.getDefaultNamespace()); + } + + @Test + public void testRouterDefaultNameservice() { + Configuration conf = new Configuration(); + conf.set(DFS_ROUTER_DEFAULT_NAMESERVICE, "router_ns"); // this is priority + conf.set(DFS_NAMESERVICE_ID, "ns_id"); + conf.set(DFS_NAMESERVICES, "nss"); + MountTableResolver mountTable = new MountTableResolver(conf); + assertEquals("router_ns", mountTable.getDefaultNamespace()); + } + + @Test + public void testNameserviceID() { + Configuration conf = new Configuration(); + conf.set(DFS_NAMESERVICE_ID, "ns_id"); // this is priority + conf.set(DFS_NAMESERVICES, "nss"); + MountTableResolver mountTable = new MountTableResolver(conf); + assertEquals("ns_id", mountTable.getDefaultNamespace()); + } + + @Test + public void testSingleNameservices() { + Configuration conf = new Configuration(); + conf.set(DFS_NAMESERVICES, "ns1"); + MountTableResolver mountTable = new MountTableResolver(conf); + assertEquals("ns1", mountTable.getDefaultNamespace()); + } + + @Test + public void testMultipleNameservices() { + Configuration conf = new Configuration(); + conf.set(DFS_NAMESERVICES, "ns1,ns2"); + MountTableResolver mountTable = new MountTableResolver(conf); + assertEquals("ns1", mountTable.getDefaultNamespace()); + } +} \ No newline at end of file