Skip to content

Commit a0443e3

Browse files
committed
refactor TestRouterFaultTolerant.
1 parent bb9e0ac commit a0443e3

File tree

1 file changed

+77
-13
lines changed

1 file changed

+77
-13
lines changed

hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterFaultTolerant.java

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import static org.apache.hadoop.hdfs.server.federation.FederationTestUtils.getFileSystem;
2424
import static org.apache.hadoop.hdfs.server.federation.FederationTestUtils.refreshRoutersCaches;
2525
import static org.apache.hadoop.hdfs.server.federation.MockNamenode.registerSubclusters;
26+
import static org.apache.hadoop.hdfs.server.federation.router.TestRouterConstants.ASYNC_MODE;
27+
import static org.apache.hadoop.hdfs.server.federation.router.TestRouterConstants.SYNC_MODE;
2628
import static org.apache.hadoop.hdfs.server.federation.store.FederationStateStoreTestUtils.getStateStoreConfiguration;
2729
import static org.junit.jupiter.api.Assertions.assertEquals;
2830
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -34,6 +36,7 @@
3436
import java.io.IOException;
3537
import java.io.PrintWriter;
3638
import java.io.StringWriter;
39+
import java.lang.reflect.Method;
3740
import java.security.PrivilegedExceptionAction;
3841
import java.util.ArrayList;
3942
import java.util.Collection;
@@ -76,9 +79,13 @@
7679
import org.apache.hadoop.ipc.RemoteException;
7780
import org.apache.hadoop.security.UserGroupInformation;
7881
import org.apache.hadoop.test.LambdaTestUtils;
79-
import org.junit.jupiter.api.AfterEach;
80-
import org.junit.jupiter.api.BeforeEach;
81-
import org.junit.jupiter.api.Test;
82+
import org.junit.jupiter.api.Nested;
83+
import org.junit.jupiter.api.extension.AfterEachCallback;
84+
import org.junit.jupiter.api.extension.BeforeEachCallback;
85+
import org.junit.jupiter.api.extension.ExtendWith;
86+
import org.junit.jupiter.api.extension.ExtensionContext;
87+
import org.junit.jupiter.params.ParameterizedTest;
88+
import org.junit.jupiter.params.provider.ValueSource;
8289
import org.slf4j.Logger;
8390
import org.slf4j.LoggerFactory;
8491

@@ -98,16 +105,14 @@ public class TestRouterFaultTolerant {
98105

99106

100107
/** Namenodes for the test per name service id (subcluster). */
101-
private Map<String, MockNamenode> namenodes = new HashMap<>();
108+
private static Map<String, MockNamenode> namenodes = new HashMap<>();
102109
/** Routers for the test. */
103-
private List<Router> routers = new ArrayList<>();
110+
private static List<Router> routers = new ArrayList<>();
104111

105112
/** Run test tasks in parallel. */
106-
private ExecutorService service;
113+
private static ExecutorService service;
107114

108-
109-
@BeforeEach
110-
public void setup() throws Exception {
115+
public static void setup(String rpcMode) throws Exception {
111116
LOG.info("Start the Namenodes");
112117
Configuration nnConf = new HdfsConfiguration();
113118
nnConf.setInt(DFSConfigKeys.DFS_NAMENODE_HANDLER_COUNT_KEY, 10);
@@ -141,6 +146,9 @@ public void setup() throws Exception {
141146
MultipleDestinationMountTableResolver.class,
142147
FileSubclusterResolver.class);
143148
routerConf.addResource(stateStoreConf);
149+
if (rpcMode.equals(ASYNC_MODE)) {
150+
routerConf.setBoolean(RBFConfigKeys.DFS_ROUTER_ASYNC_RPC_ENABLE_KEY, true);
151+
}
144152

145153
for (int i = 0; i < NUM_ROUTERS; i++) {
146154
// router0 doesn't allow partial listing
@@ -160,8 +168,7 @@ public void setup() throws Exception {
160168
service = Executors.newFixedThreadPool(10);
161169
}
162170

163-
@AfterEach
164-
public void cleanup() throws Exception {
171+
public static void cleanup() throws Exception {
165172
LOG.info("Stopping the cluster");
166173
for (final MockNamenode nn : namenodes.values()) {
167174
nn.stop();
@@ -202,12 +209,43 @@ private void updateMountPointFaultTolerant(final String mountPoint)
202209
refreshRoutersCaches(routers);
203210
}
204211

212+
@Nested
213+
@ExtendWith(RouterServerHelperInTestRouterFaultTolerant.class)
214+
class TestWithSyncRouterRpc {
215+
@ParameterizedTest
216+
@ValueSource(strings = {SYNC_MODE})
217+
public void testWriteWithFailedSubclusterSync() throws Exception {
218+
testWriteWithFailedSubcluster();
219+
}
220+
221+
@ParameterizedTest
222+
@ValueSource(strings = {SYNC_MODE})
223+
public void testReadWithFailedSubclusterSync() throws Exception {
224+
testReadWithFailedSubcluster();
225+
}
226+
}
227+
228+
@Nested
229+
@ExtendWith(RouterServerHelperInTestRouterFaultTolerant.class)
230+
class TestWithAsyncRouterRpc {
231+
@ParameterizedTest
232+
@ValueSource(strings = {ASYNC_MODE})
233+
public void testWriteWithFailedSubclusterAsync() throws Exception {
234+
testWriteWithFailedSubcluster();
235+
}
236+
237+
@ParameterizedTest
238+
@ValueSource(strings = {ASYNC_MODE})
239+
public void testReadWithFailedSubclusterAsync() throws Exception {
240+
testReadWithFailedSubcluster();
241+
}
242+
}
243+
205244
/**
206245
* Test the behavior of the Router when one of the subclusters in a mount
207246
* point fails. In particular, it checks if it can write files or not.
208247
* Related to {@link TestRouterRpcMultiDestination#testSubclusterDown()}.
209248
*/
210-
@Test
211249
public void testWriteWithFailedSubcluster() throws Exception {
212250

213251
LOG.info("Stop ns1 to simulate an unavailable subcluster");
@@ -614,7 +652,6 @@ private FileSystem getRandomRouterFileSystem() throws Exception {
614652
(PrivilegedExceptionAction<FileSystem>) () -> getFileSystem(router));
615653
}
616654

617-
@Test
618655
public void testReadWithFailedSubcluster() throws Exception {
619656

620657
DestinationOrder order = DestinationOrder.HASH_ALL;
@@ -673,3 +710,30 @@ public void testReadWithFailedSubcluster() throws Exception {
673710
}
674711
}
675712
}
713+
714+
class RouterServerHelperInTestRouterFaultTolerant implements
715+
BeforeEachCallback, AfterEachCallback {
716+
public static final ThreadLocal<RouterServerHelperInTestRouterFaultTolerant>
717+
TEST_ROUTER_SERVER_TL = new InheritableThreadLocal<>();
718+
719+
@Override
720+
public void afterEach(ExtensionContext context) throws Exception {
721+
TestRouterFaultTolerant.cleanup();
722+
TEST_ROUTER_SERVER_TL.remove();
723+
}
724+
725+
@Override
726+
public void beforeEach(ExtensionContext context) throws Exception {
727+
Method testMethod = context.getRequiredTestMethod();
728+
ValueSource enumAnnotation = testMethod.getAnnotation(ValueSource.class);
729+
if (enumAnnotation != null) {
730+
String[] strings = enumAnnotation.strings();
731+
for (String rpcMode : strings) {
732+
if (TEST_ROUTER_SERVER_TL.get() == null) {
733+
TestRouterFaultTolerant.setup(rpcMode);
734+
}
735+
}
736+
}
737+
TEST_ROUTER_SERVER_TL.set(RouterServerHelperInTestRouterFaultTolerant.this);
738+
}
739+
}

0 commit comments

Comments
 (0)