Skip to content

Commit 77977dd

Browse files
committed
add ut
1 parent 5b3d751 commit 77977dd

File tree

4 files changed

+136
-13
lines changed

4 files changed

+136
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public class RouterClientProtocol implements ClientProtocol {
170170
/** Router security manager to handle token operations. */
171171
private RouterSecurityManager securityManager = null;
172172

173-
protected RouterClientProtocol(Configuration conf, RouterRpcServer rpcServer) {
173+
public RouterClientProtocol(Configuration conf, RouterRpcServer rpcServer) {
174174
this.rpcServer = rpcServer;
175175
this.rpcClient = rpcServer.getRPCClient();
176176
this.subclusterResolver = rpcServer.getSubclusterResolver();

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,6 @@ public HdfsFileStatus getFileInfo(String src) throws IOException {
568568
}, IOException.class);
569569

570570
asyncApply((AsyncApplyFunction<HdfsFileStatus, Object>) ret -> {
571-
asyncComplete(null);
572571
// If there is no real path, check mount points
573572
if (ret == null) {
574573
List<String> children = subclusterResolver.getMountPoints(src);
@@ -582,17 +581,21 @@ public HdfsFileStatus getFileInfo(String src) throws IOException {
582581
} else if (children != null) {
583582
// The src is a mount point, but there are no files or directories
584583
getMountPointStatus(src, 0, 0, false);
584+
} else {
585+
asyncComplete(null);
585586
}
586-
}
587-
asyncApply((ApplyFunction<HdfsFileStatus, HdfsFileStatus>) result -> {
588-
// Can't find mount point for path and the path didn't contain any sub monit points,
589-
// throw the NoLocationException to client.
590-
if (result == null && noLocationException[0] != null) {
591-
throw noLocationException[0];
592-
}
587+
asyncApply((ApplyFunction<HdfsFileStatus, HdfsFileStatus>) result -> {
588+
// Can't find mount point for path and the path didn't contain any sub monit points,
589+
// throw the NoLocationException to client.
590+
if (result == null && noLocationException[0] != null) {
591+
throw noLocationException[0];
592+
}
593593

594-
return result;
595-
});
594+
return result;
595+
});
596+
} else {
597+
asyncComplete(ret);
598+
}
596599
});
597600

598601
return asyncReturn(HdfsFileStatus.class);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class RouterAsyncProtocolTestBase {
5656
private FileSystem routerFs;
5757
private RouterRpcServer routerRpcServer;
5858
private RouterRpcServer routerAsyncRpcServer;
59+
protected static final String TEST_DIR_PATH = "/testdir";
5960

6061
@BeforeClass
6162
public static void setUpCluster() throws Exception {
@@ -114,19 +115,20 @@ public void setUp() throws IOException {
114115
routerRpcServer.getRouterStateIdContext());
115116
routerAsyncRpcServer = Mockito.spy(routerRpcServer);
116117
Mockito.when(routerAsyncRpcServer.getRPCClient()).thenReturn(asyncRpcClient);
118+
Mockito.when(routerAsyncRpcServer.isAsync()).thenReturn(true);
117119

118120
// Create mock locations
119121
MockResolver resolver = (MockResolver) router.getRouter().getSubclusterResolver();
120122
resolver.addLocation("/", ns0, "/");
121123
FsPermission permission = new FsPermission("705");
122-
routerFs.mkdirs(new Path("/testdir"), permission);
124+
routerFs.mkdirs(new Path(TEST_DIR_PATH), permission);
123125
}
124126

125127
@After
126128
public void tearDown() throws IOException {
127129
// clear client context
128130
CallerContext.setCurrent(null);
129-
boolean delete = routerFs.delete(new Path("/testdir"));
131+
boolean delete = routerFs.delete(new Path(TEST_DIR_PATH));
130132
assertTrue(delete);
131133
if (routerFs != null) {
132134
routerFs.close();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package org.apache.hadoop.hdfs.server.federation.router.async;
2+
3+
import org.apache.hadoop.crypto.CryptoProtocolVersion;
4+
import org.apache.hadoop.fs.CreateFlag;
5+
import org.apache.hadoop.fs.FsServerDefaults;
6+
import org.apache.hadoop.fs.permission.AclEntry;
7+
import org.apache.hadoop.fs.permission.FsPermission;
8+
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
9+
import org.apache.hadoop.hdfs.protocol.DirectoryListing;
10+
import org.apache.hadoop.hdfs.protocol.HdfsConstants;
11+
import org.apache.hadoop.hdfs.protocol.HdfsFileStatus;
12+
import org.apache.hadoop.hdfs.server.federation.router.RouterClientProtocol;
13+
import org.apache.hadoop.io.EnumSetWritable;
14+
import org.apache.hadoop.util.Lists;
15+
import org.junit.Before;
16+
import org.junit.Test;
17+
18+
import java.io.IOException;
19+
import java.util.EnumSet;
20+
import java.util.List;
21+
22+
import static org.apache.hadoop.crypto.CryptoProtocolVersion.ENCRYPTION_ZONES;
23+
import static org.apache.hadoop.fs.permission.AclEntryScope.DEFAULT;
24+
import static org.apache.hadoop.fs.permission.AclEntryType.USER;
25+
import static org.apache.hadoop.fs.permission.FsAction.ALL;
26+
import static org.apache.hadoop.fs.permission.FsAction.NONE;
27+
import static org.apache.hadoop.fs.permission.FsAction.READ;
28+
import static org.apache.hadoop.fs.permission.FsAction.READ_WRITE;
29+
import static org.apache.hadoop.hdfs.server.federation.router.async.utils.AsyncUtil.syncReturn;
30+
import static org.apache.hadoop.hdfs.server.namenode.AclTestHelpers.aclEntry;
31+
import static org.junit.Assert.assertEquals;
32+
import static org.junit.Assert.assertTrue;
33+
34+
/**
35+
* Used to test the functionality of {@link RouterAsyncClientProtocol}.
36+
*/
37+
public class TestRouterAsyncClientProtocol extends RouterAsyncProtocolTestBase {
38+
private RouterAsyncClientProtocol asyncClientProtocol;
39+
private RouterClientProtocol clientProtocol;
40+
private final String testPath = TEST_DIR_PATH + "/test";
41+
42+
@Before
43+
public void setup() throws IOException {
44+
asyncClientProtocol = new RouterAsyncClientProtocol(getRouterConf(), getRouterAsyncRpcServer());
45+
clientProtocol = new RouterClientProtocol(getRouterConf(), getRouterRpcServer());
46+
}
47+
48+
@Test
49+
public void testGetServerDefaults() throws Exception {
50+
FsServerDefaults serverDefaults = clientProtocol.getServerDefaults();
51+
asyncClientProtocol.getServerDefaults();
52+
FsServerDefaults fsServerDefaults = syncReturn(FsServerDefaults.class);
53+
assertEquals(serverDefaults.getBlockSize(), fsServerDefaults.getBlockSize());
54+
assertEquals(serverDefaults.getReplication(), fsServerDefaults.getReplication());
55+
assertEquals(serverDefaults.getChecksumType(), fsServerDefaults.getChecksumType());
56+
assertEquals(
57+
serverDefaults.getDefaultStoragePolicyId(), fsServerDefaults.getDefaultStoragePolicyId());
58+
}
59+
60+
@Test
61+
public void testClientProtocolRpc() throws Exception {
62+
asyncClientProtocol.mkdirs(testPath, new FsPermission(ALL, ALL, ALL), false);
63+
Boolean success = syncReturn(Boolean.class);
64+
assertTrue(success);
65+
66+
asyncClientProtocol.setPermission(testPath, new FsPermission(READ_WRITE, READ, NONE));
67+
syncReturn(Void.class);
68+
69+
asyncClientProtocol.getFileInfo(testPath);
70+
HdfsFileStatus hdfsFileStatus = syncReturn(HdfsFileStatus.class);
71+
assertEquals(hdfsFileStatus.getPermission(), new FsPermission(READ_WRITE, READ, NONE));
72+
73+
List<AclEntry> aclSpec = Lists.newArrayList(aclEntry(DEFAULT, USER, "tmpUser", ALL));
74+
asyncClientProtocol.setAcl(testPath, aclSpec);
75+
syncReturn(Void.class);
76+
asyncClientProtocol.setOwner(testPath, "tmpUser", "tmpUserGroup");
77+
syncReturn(Void.class);
78+
79+
asyncClientProtocol.getFileInfo(testPath);
80+
hdfsFileStatus = syncReturn(HdfsFileStatus.class);
81+
assertEquals("tmpUser", hdfsFileStatus.getOwner());
82+
assertEquals("tmpUserGroup", hdfsFileStatus.getGroup());
83+
84+
asyncClientProtocol.create(testPath + "/testCreate.file",
85+
new FsPermission(ALL, ALL, ALL), "testAsyncClient",
86+
new EnumSetWritable<>(EnumSet.of(CreateFlag.CREATE)),
87+
false, (short) 1, 128 * 1024 * 1024L, new CryptoProtocolVersion[]{ENCRYPTION_ZONES},
88+
null, null);
89+
hdfsFileStatus = syncReturn(HdfsFileStatus.class);
90+
assertTrue(hdfsFileStatus.isFile());
91+
assertEquals(128 * 1024 * 1024, hdfsFileStatus.getBlockSize());
92+
93+
asyncClientProtocol.getListing(testPath, new byte[1], true);
94+
DirectoryListing directoryListing = syncReturn(DirectoryListing.class);
95+
assertEquals(1, directoryListing.getPartialListing().length);
96+
97+
asyncClientProtocol.getDatanodeReport(HdfsConstants.DatanodeReportType.ALL);
98+
DatanodeInfo[] datanodeInfos = syncReturn(DatanodeInfo[].class);
99+
assertEquals(3, datanodeInfos.length);
100+
101+
asyncClientProtocol.createSymlink(testPath + "/testCreate.file",
102+
"/link/link.file", new FsPermission(ALL, ALL, ALL), true);
103+
syncReturn(Void.class);
104+
105+
asyncClientProtocol.getFileLinkInfo("/link/link.file");
106+
hdfsFileStatus = syncReturn(HdfsFileStatus.class);
107+
assertEquals("testCreate.file", hdfsFileStatus.getSymlink().getName());
108+
109+
asyncClientProtocol.rename(testPath + "/testCreate.file",
110+
testPath + "/testRename.file");
111+
success = syncReturn(boolean.class);
112+
assertTrue(success);
113+
114+
asyncClientProtocol.delete(testPath, true);
115+
success = syncReturn(boolean.class);
116+
assertTrue(success);
117+
}
118+
}

0 commit comments

Comments
 (0)