From 6282c02d7d377f2da6026eb26f90db0386480ec0 Mon Sep 17 00:00:00 2001 From: Ayush Saxena Date: Fri, 19 Jul 2019 17:44:13 +0530 Subject: [PATCH] HDFS-14577. RBF: FederationUtil#newInstance should allow constructor without context. Contributed by CR Hota. --- .../federation/router/FederationUtil.java | 13 +++- .../federation/router/TestFederationUtil.java | 69 +++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestFederationUtil.java diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/FederationUtil.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/FederationUtil.java index 45868a8222..9065312bc6 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/FederationUtil.java +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/FederationUtil.java @@ -149,9 +149,16 @@ private static T newInstance(final Configuration conf, final R context, final Class contextClass, final Class clazz) { try { if (contextClass == null) { - // Default constructor if no context - Constructor constructor = clazz.getConstructor(); - return constructor.newInstance(); + if (conf == null) { + // Default constructor if no context + Constructor constructor = clazz.getConstructor(); + return constructor.newInstance(); + } else { + // Constructor with configuration but no context + Constructor constructor = clazz.getConstructor( + Configuration.class); + return constructor.newInstance(conf); + } } else { // Constructor with context Constructor constructor = clazz.getConstructor( diff --git a/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestFederationUtil.java b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestFederationUtil.java new file mode 100644 index 0000000000..2ce9f57504 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestFederationUtil.java @@ -0,0 +1,69 @@ +/** + * 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.router; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hdfs.HdfsConfiguration; +import org.apache.hadoop.hdfs.server.federation.MockResolver; +import org.apache.hadoop.hdfs.server.federation.resolver.ActiveNamenodeResolver; +import org.apache.hadoop.hdfs.server.federation.resolver.FileSubclusterResolver; +import org.apache.hadoop.hdfs.server.federation.store.StateStoreService; +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.FEDERATION_FILE_RESOLVER_CLIENT_CLASS; +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.FEDERATION_NAMENODE_RESOLVER_CLIENT_CLASS; + +/** + * Tests Router federation utility methods. + */ +public class TestFederationUtil { + + @Test + public void testInstanceCreation() { + Configuration conf = new HdfsConfiguration(); + + // Use mock resolver classes + conf.setClass(FEDERATION_NAMENODE_RESOLVER_CLIENT_CLASS, + MockResolver.class, ActiveNamenodeResolver.class); + conf.setClass(FEDERATION_FILE_RESOLVER_CLIENT_CLASS, + MockResolver.class, FileSubclusterResolver.class); + + Router router = new Router(); + StateStoreService stateStore = new StateStoreService(); + + ActiveNamenodeResolver namenodeResolverWithContext = + FederationUtil.newActiveNamenodeResolver(conf, stateStore); + + ActiveNamenodeResolver namenodeResolverWithoutContext = + FederationUtil.newActiveNamenodeResolver(conf, null); + + FileSubclusterResolver subclusterResolverWithContext = + FederationUtil.newFileSubclusterResolver(conf, router); + + FileSubclusterResolver subclusterResolverWithoutContext = + FederationUtil.newFileSubclusterResolver(conf, null); + + // Check all instances are created successfully. + assertNotNull(namenodeResolverWithContext); + assertNotNull(namenodeResolverWithoutContext); + assertNotNull(subclusterResolverWithContext); + assertNotNull(subclusterResolverWithoutContext); + } +}