Skip to content

Commit 6282c02

Browse files
committed
HDFS-14577. RBF: FederationUtil#newInstance should allow constructor without context. Contributed by CR Hota.
1 parent 4e66cb9 commit 6282c02

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/FederationUtil.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,16 @@ private static <T, R> T newInstance(final Configuration conf,
149149
final R context, final Class<R> contextClass, final Class<T> clazz) {
150150
try {
151151
if (contextClass == null) {
152-
// Default constructor if no context
153-
Constructor<T> constructor = clazz.getConstructor();
154-
return constructor.newInstance();
152+
if (conf == null) {
153+
// Default constructor if no context
154+
Constructor<T> constructor = clazz.getConstructor();
155+
return constructor.newInstance();
156+
} else {
157+
// Constructor with configuration but no context
158+
Constructor<T> constructor = clazz.getConstructor(
159+
Configuration.class);
160+
return constructor.newInstance(conf);
161+
}
155162
} else {
156163
// Constructor with context
157164
Constructor<T> constructor = clazz.getConstructor(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.hdfs.server.federation.router;
20+
21+
import org.apache.hadoop.conf.Configuration;
22+
import org.apache.hadoop.hdfs.HdfsConfiguration;
23+
import org.apache.hadoop.hdfs.server.federation.MockResolver;
24+
import org.apache.hadoop.hdfs.server.federation.resolver.ActiveNamenodeResolver;
25+
import org.apache.hadoop.hdfs.server.federation.resolver.FileSubclusterResolver;
26+
import org.apache.hadoop.hdfs.server.federation.store.StateStoreService;
27+
import org.junit.Test;
28+
29+
import static org.junit.Assert.assertNotNull;
30+
import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.FEDERATION_FILE_RESOLVER_CLIENT_CLASS;
31+
import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.FEDERATION_NAMENODE_RESOLVER_CLIENT_CLASS;
32+
33+
/**
34+
* Tests Router federation utility methods.
35+
*/
36+
public class TestFederationUtil {
37+
38+
@Test
39+
public void testInstanceCreation() {
40+
Configuration conf = new HdfsConfiguration();
41+
42+
// Use mock resolver classes
43+
conf.setClass(FEDERATION_NAMENODE_RESOLVER_CLIENT_CLASS,
44+
MockResolver.class, ActiveNamenodeResolver.class);
45+
conf.setClass(FEDERATION_FILE_RESOLVER_CLIENT_CLASS,
46+
MockResolver.class, FileSubclusterResolver.class);
47+
48+
Router router = new Router();
49+
StateStoreService stateStore = new StateStoreService();
50+
51+
ActiveNamenodeResolver namenodeResolverWithContext =
52+
FederationUtil.newActiveNamenodeResolver(conf, stateStore);
53+
54+
ActiveNamenodeResolver namenodeResolverWithoutContext =
55+
FederationUtil.newActiveNamenodeResolver(conf, null);
56+
57+
FileSubclusterResolver subclusterResolverWithContext =
58+
FederationUtil.newFileSubclusterResolver(conf, router);
59+
60+
FileSubclusterResolver subclusterResolverWithoutContext =
61+
FederationUtil.newFileSubclusterResolver(conf, null);
62+
63+
// Check all instances are created successfully.
64+
assertNotNull(namenodeResolverWithContext);
65+
assertNotNull(namenodeResolverWithoutContext);
66+
assertNotNull(subclusterResolverWithContext);
67+
assertNotNull(subclusterResolverWithoutContext);
68+
}
69+
}

0 commit comments

Comments
 (0)