-
Notifications
You must be signed in to change notification settings - Fork 9.2k
HDFS-17596. [ARR] RouterStoragePolicy supports asynchronous rpc. #6988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
e7c516d
2af8558
9bc5c7f
f03962d
6a8eacf
bde6bf1
215e3b5
8d2879f
9f2d6d0
9a617e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /** | ||
| * 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.hdfs.protocol.BlockStoragePolicy; | ||
| import org.apache.hadoop.hdfs.server.federation.resolver.RemoteLocation; | ||
| import org.apache.hadoop.hdfs.server.namenode.NameNode; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| import static org.apache.hadoop.hdfs.server.federation.router.async.AsyncUtil.asyncReturn; | ||
|
|
||
| public class RouterAsyncStoragePolicy extends RouterStoragePolicy { | ||
| /** RPC server to receive client calls. */ | ||
| private final RouterRpcServer rpcServer; | ||
| /** RPC clients to connect to the Namenodes. */ | ||
| private final RouterRpcClient rpcClient; | ||
|
|
||
| public RouterAsyncStoragePolicy(RouterRpcServer server) { | ||
| super(server); | ||
| this.rpcServer = server; | ||
| this.rpcClient = this.rpcServer.getRPCClient(); | ||
| } | ||
|
|
||
| @Override | ||
| public BlockStoragePolicy getStoragePolicy(String path) | ||
| throws IOException { | ||
| rpcServer.checkOperation(NameNode.OperationCategory.READ, true); | ||
|
|
||
| List<RemoteLocation> locations = | ||
| rpcServer.getLocationsForPath(path, false, false); | ||
| RemoteMethod method = new RemoteMethod("getStoragePolicy", | ||
| new Class<?>[] {String.class}, | ||
| new RemoteParam()); | ||
| rpcClient.invokeSequential(locations, method); | ||
| return asyncReturn(BlockStoragePolicy.class); | ||
| } | ||
|
|
||
| @Override | ||
| public BlockStoragePolicy[] getStoragePolicies() throws IOException { | ||
| rpcServer.checkOperation(NameNode.OperationCategory.READ); | ||
|
|
||
| RemoteMethod method = new RemoteMethod("getStoragePolicies"); | ||
| rpcServer.invokeAtAvailableNsAsync(method, BlockStoragePolicy[].class); | ||
| return asyncReturn(BlockStoragePolicy[].class); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,12 @@ | |
| import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_FEDERATION_RENAME_OPTION; | ||
| import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_FEDERATION_RENAME_OPTION_DEFAULT; | ||
| import static org.apache.hadoop.hdfs.server.federation.router.RouterFederationRename.RouterRenameOption; | ||
| import static org.apache.hadoop.hdfs.server.federation.router.RouterRpcClient.isExpectedClass; | ||
| import static org.apache.hadoop.hdfs.server.federation.router.async.AsyncUtil.asyncApply; | ||
| import static org.apache.hadoop.hdfs.server.federation.router.async.AsyncUtil.asyncCatch; | ||
| import static org.apache.hadoop.hdfs.server.federation.router.async.AsyncUtil.asyncForEach; | ||
| import static org.apache.hadoop.hdfs.server.federation.router.async.AsyncUtil.asyncReturn; | ||
| import static org.apache.hadoop.hdfs.server.federation.router.async.AsyncUtil.asyncTry; | ||
| import static org.apache.hadoop.tools.fedbalance.FedBalanceConfigs.SCHEDULER_JOURNAL_URI; | ||
|
|
||
| import java.io.FileNotFoundException; | ||
|
|
@@ -49,6 +55,7 @@ | |
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.EnumSet; | ||
| import java.util.Iterator; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
|
|
@@ -68,6 +75,8 @@ | |
| import org.apache.hadoop.hdfs.HAUtil; | ||
| import org.apache.hadoop.hdfs.protocol.UnresolvedPathException; | ||
| import org.apache.hadoop.hdfs.protocolPB.AsyncRpcProtocolPBUtil; | ||
| import org.apache.hadoop.hdfs.server.federation.router.async.AsyncCatchFunction; | ||
| import org.apache.hadoop.hdfs.server.federation.router.async.CatchFunction; | ||
| import org.apache.hadoop.thirdparty.com.google.common.cache.CacheBuilder; | ||
| import org.apache.hadoop.thirdparty.com.google.common.cache.CacheLoader; | ||
| import org.apache.hadoop.thirdparty.com.google.common.cache.LoadingCache; | ||
|
|
@@ -791,6 +800,36 @@ <T> T invokeAtAvailableNs(RemoteMethod method, Class<T> clazz) | |
| return invokeOnNs(method, clazz, io, nss); | ||
| } | ||
|
|
||
| <T> T invokeAtAvailableNsAsync(RemoteMethod method, Class<T> clazz) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about to split this changes to separate PR which is common update and not relate with StoragePolicy only. Others look good to me. Thanks.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Hexiaoqiao @KeeProMise Agree with you, sir. This PR depends on #7108 |
||
| throws IOException { | ||
| String nsId = subclusterResolver.getDefaultNamespace(); | ||
| // If default Ns is not present return result from first namespace. | ||
| Set<FederationNamespaceInfo> nss = namenodeResolver.getNamespaces(); | ||
| // If no namespace is available, throw IOException. | ||
| IOException io = new IOException("No namespace available."); | ||
|
|
||
| // If default Ns is present return result from that namespace. | ||
| if (!nsId.isEmpty()) { | ||
| asyncTry(() -> { | ||
| getRPCClient().invokeSingle(nsId, method, clazz); | ||
| }); | ||
|
|
||
| asyncCatch((AsyncCatchFunction<T, IOException>)(res, ioe) -> { | ||
| if (!clientProto.isUnavailableSubclusterException(ioe)) { | ||
| LOG.debug("{} exception cannot be retried", | ||
| ioe.getClass().getSimpleName()); | ||
| throw ioe; | ||
| } | ||
| nss.removeIf(n -> n.getNameserviceId().equals(nsId)); | ||
| invokeOnNsAsync(method, clazz, io, nss); | ||
| }, IOException.class); | ||
| } else { | ||
| // If not have default NS. | ||
| invokeOnNsAsync(method, clazz, io, nss); | ||
| } | ||
| return asyncReturn(clazz); | ||
| } | ||
|
|
||
| /** | ||
| * Invoke the method sequentially on available namespaces, | ||
| * throw no namespace available exception, if no namespaces are available. | ||
|
|
@@ -824,6 +863,49 @@ <T> T invokeOnNs(RemoteMethod method, Class<T> clazz, IOException ioe, | |
| throw ioe; | ||
| } | ||
|
|
||
| <T> T invokeOnNsAsync(RemoteMethod method, Class<T> clazz, IOException ioe, | ||
| Set<FederationNamespaceInfo> nss) throws IOException { | ||
| if (nss.isEmpty()) { | ||
| throw ioe; | ||
| } | ||
|
|
||
| Iterator<FederationNamespaceInfo> nsIterator = nss.iterator(); | ||
| asyncForEach(nsIterator, (foreach, fnInfo) -> { | ||
| String nsId = fnInfo.getNameserviceId(); | ||
| LOG.debug("Invoking {} on namespace {}", method, nsId); | ||
| asyncTry(() -> { | ||
| getRPCClient().invokeSingle(nsId, method, clazz); | ||
| asyncApply(result -> { | ||
| if (result != null && isExpectedClass(clazz, result)) { | ||
| foreach.breakNow(); | ||
| return result; | ||
| } | ||
| return null; | ||
| }); | ||
| }); | ||
|
|
||
| asyncCatch((CatchFunction<T, IOException>)(ret, ex) -> { | ||
| LOG.debug("Failed to invoke {} on namespace {}", method, nsId, ex); | ||
| // Ignore the exception and try on other namespace, if the tried | ||
| // namespace is unavailable, else throw the received exception. | ||
| if (!clientProto.isUnavailableSubclusterException(ex)) { | ||
| throw ex; | ||
| } | ||
| return null; | ||
| }, IOException.class); | ||
| }); | ||
|
|
||
| asyncApply(obj -> { | ||
| if (obj == null) { | ||
| // Couldn't get a response from any of the namespace, throw ioe. | ||
| throw ioe; | ||
| } | ||
| return obj; | ||
| }); | ||
|
|
||
| return asyncReturn(clazz); | ||
| } | ||
|
|
||
| @Override // ClientProtocol | ||
| public Token<DelegationTokenIdentifier> getDelegationToken(Text renewer) | ||
| throws IOException { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /** | ||
| * 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.fs.FSDataOutputStream; | ||
| import org.apache.hadoop.fs.FileSystem; | ||
| import org.apache.hadoop.fs.Path; | ||
| import org.apache.hadoop.fs.permission.FsPermission; | ||
| import org.apache.hadoop.hdfs.protocol.BlockStoragePolicy; | ||
| import org.apache.hadoop.hdfs.server.federation.MiniRouterDFSCluster; | ||
| import org.apache.hadoop.hdfs.server.federation.MockResolver; | ||
| import org.apache.hadoop.hdfs.server.federation.RouterConfigBuilder; | ||
| import org.apache.hadoop.ipc.CallerContext; | ||
| import org.junit.After; | ||
| import org.junit.AfterClass; | ||
| import org.junit.Before; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.apache.hadoop.hdfs.server.federation.FederationTestUtils.NAMENODES; | ||
| import static org.apache.hadoop.hdfs.server.federation.MiniRouterDFSCluster.DEFAULT_HEARTBEAT_INTERVAL_MS; | ||
| import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_RPC_ASYNC_HANDLER_COUNT; | ||
| import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_RPC_ASYNC_RESPONDER_COUNT; | ||
| import static org.apache.hadoop.hdfs.server.federation.router.async.AsyncUtil.syncReturn; | ||
| import static org.junit.Assert.assertArrayEquals; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| public class TestRouterAsyncStoragePolicy { | ||
| private static Configuration routerConf; | ||
| /** Federated HDFS cluster. */ | ||
| private static MiniRouterDFSCluster cluster; | ||
| private static String ns0; | ||
|
|
||
| /** Random Router for this federated cluster. */ | ||
| private MiniRouterDFSCluster.RouterContext router; | ||
| private FileSystem routerFs; | ||
| private RouterRpcServer routerRpcServer; | ||
| private RouterAsyncStoragePolicy asyncStoragePolicy; | ||
|
|
||
| private final String testfilePath = "/testdir/testAsyncStoragePolicy.file"; | ||
|
|
||
| @BeforeClass | ||
| public static void setUpCluster() throws Exception { | ||
| cluster = new MiniRouterDFSCluster(true, 1, 2, | ||
| DEFAULT_HEARTBEAT_INTERVAL_MS, 1000); | ||
| cluster.setNumDatanodesPerNameservice(3); | ||
|
|
||
| cluster.startCluster(); | ||
|
|
||
| // Making one Namenode active per nameservice | ||
| if (cluster.isHighAvailability()) { | ||
| for (String ns : cluster.getNameservices()) { | ||
| cluster.switchToActive(ns, NAMENODES[0]); | ||
| cluster.switchToStandby(ns, NAMENODES[1]); | ||
| } | ||
| } | ||
| // Start routers with only an RPC service | ||
| routerConf = new RouterConfigBuilder() | ||
| .rpc() | ||
| .build(); | ||
|
|
||
| // Reduce the number of RPC clients threads to overload the Router easy | ||
| routerConf.setInt(RBFConfigKeys.DFS_ROUTER_CLIENT_THREADS_SIZE, 1); | ||
| routerConf.setInt(DFS_ROUTER_RPC_ASYNC_HANDLER_COUNT, 1); | ||
| routerConf.setInt(DFS_ROUTER_RPC_ASYNC_RESPONDER_COUNT, 1); | ||
| // We decrease the DN cache times to make the test faster | ||
| routerConf.setTimeDuration( | ||
| RBFConfigKeys.DN_REPORT_CACHE_EXPIRE, 1, TimeUnit.SECONDS); | ||
| cluster.addRouterOverrides(routerConf); | ||
| // Start routers with only an RPC service | ||
| cluster.startRouters(); | ||
|
|
||
| // Register and verify all NNs with all routers | ||
| cluster.registerNamenodes(); | ||
| cluster.waitNamenodeRegistration(); | ||
| cluster.waitActiveNamespaces(); | ||
| ns0 = cluster.getNameservices().get(0); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void shutdownCluster() throws Exception { | ||
| if (cluster != null) { | ||
| cluster.shutdown(); | ||
| } | ||
| } | ||
|
|
||
| @Before | ||
| public void setUp() throws IOException { | ||
| router = cluster.getRandomRouter(); | ||
| routerFs = router.getFileSystem(); | ||
| routerRpcServer = router.getRouterRpcServer(); | ||
| routerRpcServer.initAsyncThreadPool(); | ||
| RouterAsyncRpcClient asyncRpcClient = new RouterAsyncRpcClient( | ||
| routerConf, router.getRouter(), routerRpcServer.getNamenodeResolver(), | ||
| routerRpcServer.getRPCMonitor(), | ||
| routerRpcServer.getRouterStateIdContext()); | ||
| RouterRpcServer spy = Mockito.spy(routerRpcServer); | ||
| Mockito.when(spy.getRPCClient()).thenReturn(asyncRpcClient); | ||
| asyncStoragePolicy = new RouterAsyncStoragePolicy(spy); | ||
|
|
||
| // Create mock locations | ||
| MockResolver resolver = (MockResolver) router.getRouter().getSubclusterResolver(); | ||
| resolver.addLocation("/", ns0, "/"); | ||
| FsPermission permission = new FsPermission("705"); | ||
| routerFs.mkdirs(new Path("/testdir"), permission); | ||
| FSDataOutputStream fsDataOutputStream = routerFs.create( | ||
| new Path(testfilePath), true); | ||
| fsDataOutputStream.write(new byte[1024]); | ||
| fsDataOutputStream.close(); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() throws IOException { | ||
| // clear client context | ||
| CallerContext.setCurrent(null); | ||
| boolean delete = routerFs.delete(new Path("/testdir")); | ||
| assertTrue(delete); | ||
| if (routerFs != null) { | ||
| routerFs.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testRouterAsyncStoragePolicy() throws Exception { | ||
| BlockStoragePolicy[] storagePolicies = cluster.getNamenodes().get(0) | ||
| .getClient().getStoragePolicies(); | ||
| asyncStoragePolicy.getStoragePolicies(); | ||
| BlockStoragePolicy[] storagePoliciesAsync = syncReturn(BlockStoragePolicy[].class); | ||
| assertArrayEquals(storagePolicies, storagePoliciesAsync); | ||
|
|
||
| asyncStoragePolicy.getStoragePolicy(testfilePath); | ||
| BlockStoragePolicy blockStoragePolicy1 = syncReturn(BlockStoragePolicy.class); | ||
|
|
||
| asyncStoragePolicy.setStoragePolicy(testfilePath, "COLD"); | ||
| syncReturn(null); | ||
| asyncStoragePolicy.getStoragePolicy(testfilePath); | ||
| BlockStoragePolicy blockStoragePolicy2 = syncReturn(BlockStoragePolicy.class); | ||
| assertNotEquals(blockStoragePolicy1, blockStoragePolicy2); | ||
| assertEquals("COLD", blockStoragePolicy2.getName()); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.