|
| 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 | +package org.apache.hadoop.hdfs.server.federation.async; |
| 19 | + |
| 20 | +import org.apache.hadoop.conf.Configuration; |
| 21 | +import org.apache.hadoop.fs.FileSystem; |
| 22 | +import org.apache.hadoop.fs.Path; |
| 23 | +import org.apache.hadoop.fs.permission.FsPermission; |
| 24 | +import org.apache.hadoop.hdfs.server.federation.MiniRouterDFSCluster; |
| 25 | +import org.apache.hadoop.hdfs.server.federation.MockResolver; |
| 26 | +import org.apache.hadoop.hdfs.server.federation.RouterConfigBuilder; |
| 27 | +import org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys; |
| 28 | +import org.apache.hadoop.hdfs.server.federation.router.RouterAsyncRpcClient; |
| 29 | +import org.apache.hadoop.hdfs.server.federation.router.RouterRpcServer; |
| 30 | +import org.apache.hadoop.ipc.CallerContext; |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.AfterClass; |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.BeforeClass; |
| 35 | +import org.mockito.Mockito; |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | + |
| 40 | +import static org.apache.hadoop.hdfs.server.federation.FederationTestUtils.NAMENODES; |
| 41 | +import static org.apache.hadoop.hdfs.server.federation.MiniRouterDFSCluster.DEFAULT_HEARTBEAT_INTERVAL_MS; |
| 42 | +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_RPC_ASYNC_HANDLER_COUNT; |
| 43 | +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_RPC_ASYNC_RESPONDER_COUNT; |
| 44 | +import static org.junit.Assert.assertTrue; |
| 45 | + |
| 46 | +/** |
| 47 | + * Used to test the functionality of async router rps. |
| 48 | + */ |
| 49 | +public class RouterAsyncProtocolTestBase { |
| 50 | + private static Configuration routerConf; |
| 51 | + /** Federated HDFS cluster. */ |
| 52 | + private static MiniRouterDFSCluster cluster; |
| 53 | + private static String ns0; |
| 54 | + |
| 55 | + /** Random Router for this federated cluster. */ |
| 56 | + private MiniRouterDFSCluster.RouterContext router; |
| 57 | + private FileSystem routerFs; |
| 58 | + private RouterRpcServer routerRpcServer; |
| 59 | + private RouterRpcServer routerAsyncRpcServer; |
| 60 | + |
| 61 | + @BeforeClass |
| 62 | + public static void setUpCluster() throws Exception { |
| 63 | + cluster = new MiniRouterDFSCluster(true, 1, 2, |
| 64 | + DEFAULT_HEARTBEAT_INTERVAL_MS, 1000); |
| 65 | + cluster.setNumDatanodesPerNameservice(3); |
| 66 | + |
| 67 | + cluster.startCluster(); |
| 68 | + |
| 69 | + // Making one Namenode active per nameservice |
| 70 | + if (cluster.isHighAvailability()) { |
| 71 | + for (String ns : cluster.getNameservices()) { |
| 72 | + cluster.switchToActive(ns, NAMENODES[0]); |
| 73 | + cluster.switchToStandby(ns, NAMENODES[1]); |
| 74 | + } |
| 75 | + } |
| 76 | + // Start routers with only an RPC service |
| 77 | + routerConf = new RouterConfigBuilder() |
| 78 | + .rpc() |
| 79 | + .build(); |
| 80 | + |
| 81 | + // Reduce the number of RPC clients threads to overload the Router easy |
| 82 | + routerConf.setInt(RBFConfigKeys.DFS_ROUTER_CLIENT_THREADS_SIZE, 1); |
| 83 | + routerConf.setInt(DFS_ROUTER_RPC_ASYNC_HANDLER_COUNT, 1); |
| 84 | + routerConf.setInt(DFS_ROUTER_RPC_ASYNC_RESPONDER_COUNT, 1); |
| 85 | + // We decrease the DN cache times to make the test faster |
| 86 | + routerConf.setTimeDuration( |
| 87 | + RBFConfigKeys.DN_REPORT_CACHE_EXPIRE, 1, TimeUnit.SECONDS); |
| 88 | + cluster.addRouterOverrides(routerConf); |
| 89 | + // Start routers with only an RPC service |
| 90 | + cluster.startRouters(); |
| 91 | + |
| 92 | + // Register and verify all NNs with all routers |
| 93 | + cluster.registerNamenodes(); |
| 94 | + cluster.waitNamenodeRegistration(); |
| 95 | + cluster.waitActiveNamespaces(); |
| 96 | + ns0 = cluster.getNameservices().get(0); |
| 97 | + } |
| 98 | + |
| 99 | + @AfterClass |
| 100 | + public static void shutdownCluster() throws Exception { |
| 101 | + if (cluster != null) { |
| 102 | + cluster.shutdown(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Before |
| 107 | + public void setUp() throws IOException { |
| 108 | + router = cluster.getRandomRouter(); |
| 109 | + routerFs = router.getFileSystem(); |
| 110 | + routerRpcServer = router.getRouterRpcServer(); |
| 111 | + routerRpcServer.initAsyncThreadPool(); |
| 112 | + RouterAsyncRpcClient asyncRpcClient = new RouterAsyncRpcClient( |
| 113 | + routerConf, router.getRouter(), routerRpcServer.getNamenodeResolver(), |
| 114 | + routerRpcServer.getRPCMonitor(), |
| 115 | + routerRpcServer.getRouterStateIdContext()); |
| 116 | + routerAsyncRpcServer = Mockito.spy(routerRpcServer); |
| 117 | + Mockito.when(routerAsyncRpcServer.getRPCClient()).thenReturn(asyncRpcClient); |
| 118 | + |
| 119 | + // Create mock locations |
| 120 | + MockResolver resolver = (MockResolver) router.getRouter().getSubclusterResolver(); |
| 121 | + resolver.addLocation("/", ns0, "/"); |
| 122 | + FsPermission permission = new FsPermission("705"); |
| 123 | + routerFs.mkdirs(new Path("/testdir"), permission); |
| 124 | + } |
| 125 | + |
| 126 | + @After |
| 127 | + public void tearDown() throws IOException { |
| 128 | + // clear client context |
| 129 | + CallerContext.setCurrent(null); |
| 130 | + boolean delete = routerFs.delete(new Path("/testdir")); |
| 131 | + assertTrue(delete); |
| 132 | + if (routerFs != null) { |
| 133 | + routerFs.close(); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public static Configuration getRouterConf() { |
| 138 | + return routerConf; |
| 139 | + } |
| 140 | + |
| 141 | + public static MiniRouterDFSCluster getCluster() { |
| 142 | + return cluster; |
| 143 | + } |
| 144 | + |
| 145 | + public static String getNs0() { |
| 146 | + return ns0; |
| 147 | + } |
| 148 | + |
| 149 | + public MiniRouterDFSCluster.RouterContext getRouter() { |
| 150 | + return router; |
| 151 | + } |
| 152 | + |
| 153 | + public FileSystem getRouterFs() { |
| 154 | + return routerFs; |
| 155 | + } |
| 156 | + |
| 157 | + public RouterRpcServer getRouterRpcServer() { |
| 158 | + return routerRpcServer; |
| 159 | + } |
| 160 | + |
| 161 | + public RouterRpcServer getRouterAsyncRpcServer() { |
| 162 | + return routerAsyncRpcServer; |
| 163 | + } |
| 164 | +} |
0 commit comments