Skip to content

Commit

Permalink
HDFS-13743. RBF: Router throws NullPointerException due to the invali…
Browse files Browse the repository at this point in the history
…d initialization of MountTableResolver. Contributed by Takanobu Asanuma.

(cherry picked from commit 7b25fb9)
  • Loading branch information
linyiqun committed Jul 20, 2018
1 parent 510287f commit 9dc0a44
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,7 +44,6 @@
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;
Expand Down Expand Up @@ -149,14 +150,25 @@ private void registerCacheExternal() {
* @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<String> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 9dc0a44

Please sign in to comment.