Skip to content

Commit 9a7c9f1

Browse files
author
zhuxiangyi
committed
Address comments and add unit tests
1 parent ef407a8 commit 9a7c9f1

File tree

2 files changed

+98
-36
lines changed

2 files changed

+98
-36
lines changed

hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/tools/federation/RouterAdmin.java

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ private boolean validateMin(String[] argv) {
249249
if (argv.length < 2) {
250250
return false;
251251
}
252+
} else if ("-initViewFsToMountTable".equals(cmd)) {
253+
if (argv.length < 2) {
254+
return false;
255+
}
252256
} else if ("-getDestination".equals(cmd)) {
253257
if (argv.length < 2) {
254258
return false;
@@ -392,8 +396,9 @@ public int run(String[] argv) throws Exception {
392396
} else if ("-refresh".equals(cmd)) {
393397
refresh(address);
394398
} else if ("-initViewFsToMountTable".equals(cmd)) {
395-
if (initViewFsToMountTable(argv, i)) {
396-
System.out.println("Successfully init ViewFs mapping to router " + argv[i]);
399+
if (initViewFsToMountTable(argv[i])) {
400+
System.out.println("Successfully init ViewFs mapping to router " +
401+
argv[i]);
397402
} else {
398403
exitCode = -1;
399404
}
@@ -1052,53 +1057,63 @@ private boolean updateQuota(String mount, long nsQuota, long ssQuota)
10521057

10531058
/**
10541059
* initViewFsToMountTable.
1055-
*
1056-
* @param parameters The specified cluster to initialize.
1057-
* @param i Index in the parameters
1060+
* @param clusterName The specified cluster to initialize.
10581061
* @return If the quota was updated.
10591062
* @throws IOException Error adding the mount point.
10601063
*/
1061-
public boolean initViewFsToMountTable(String[] parameters, int i) throws IOException {
1062-
String clusterName = parameters[i++];
1063-
if (clusterName == null) {
1064-
System.out.println("Please enter the cluster name.");
1065-
return false;
1066-
}
1064+
public boolean initViewFsToMountTable(String clusterName)
1065+
throws IOException {
1066+
// fs.viewfs.mounttable.ClusterX.link./data
10671067
final String mountTablePrefix =
10681068
Constants.CONFIG_VIEWFS_PREFIX + "." + clusterName + "." +
10691069
Constants.CONFIG_VIEWFS_LINK + "./";
10701070
Map<String, String> viewFsMap = getConf().getValByRegex(mountTablePrefix);
1071-
if (viewFsMap.size() == 0) {
1072-
System.out.println("Please check the cluster name and veiwfs " +
1073-
"configuration.");
1071+
if (viewFsMap.isEmpty()) {
1072+
System.out.println("There is no ViewFs mapping to initialize.");
1073+
return true;
10741074
}
1075-
for (String key : viewFsMap.keySet()) {
1076-
Path path = new Path(viewFsMap.get(key));
1077-
String owner = null;
1078-
String group = null;
1079-
FsPermission mode = null;
1080-
try {
1081-
FileSystem fs = path.getFileSystem(getConf());
1082-
if (fs.exists(path)) {
1083-
FileStatus fileStatus = fs.getFileStatus(path);
1084-
owner = fileStatus.getOwner();
1085-
group = fileStatus.getGroup();
1086-
mode = fileStatus.getPermission();
1087-
}
1088-
} catch (Exception e) {
1089-
LOG.warn("Exception encountered", e);
1090-
}
1075+
for (Entry<String, String> entry : viewFsMap.entrySet()) {
1076+
Path path = new Path(entry.getValue());
10911077
DestinationOrder order = DestinationOrder.HASH;
1092-
String mount =
1093-
key.split(clusterName + "." + Constants.CONFIG_VIEWFS_LINK + ".")[1];
1094-
String dest = path.toUri().getPath();
1078+
String[] mount = entry.getKey().split(
1079+
clusterName + "." + Constants.CONFIG_VIEWFS_LINK + ".");
1080+
if (mount.length < 2) {
1081+
System.out.println("Added Mount Point failed " + entry.getKey());
1082+
continue;
1083+
}
10951084
String[] nss = new String[]{path.toUri().getAuthority()};
1096-
addMount(mount, nss, dest, false, false, order,
1097-
new ACLEntity(owner, group, mode));
1085+
boolean added = addMount(
1086+
mount[1], nss, path.toUri().getPath(), false,
1087+
false, order, getACLEntityFormHdfsPath(path));
1088+
if (added) {
1089+
System.out.println("added mount point " + mount[1]);
1090+
}
10981091
}
10991092
return true;
11001093
}
1101-
1094+
1095+
/**
1096+
* Returns ACLEntity according to a HDFS pat.
1097+
* @param path A path of HDFS.
1098+
*/
1099+
public ACLEntity getACLEntityFormHdfsPath(Path path){
1100+
String owner = null;
1101+
String group = null;
1102+
FsPermission mode = null;
1103+
try {
1104+
FileSystem fs = path.getFileSystem(getConf());
1105+
if (fs.exists(path)) {
1106+
FileStatus fileStatus = fs.getFileStatus(path);
1107+
owner = fileStatus.getOwner();
1108+
group = fileStatus.getGroup();
1109+
mode = fileStatus.getPermission();
1110+
}
1111+
} catch (IOException e) {
1112+
System.out.println("Exception encountered " + e);
1113+
}
1114+
return new ACLEntity(owner, group, mode);
1115+
}
1116+
11021117
/**
11031118
* Update storage type quota of specified mount table.
11041119
*

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@
3434
import java.util.regex.Pattern;
3535

3636
import org.apache.hadoop.conf.Configuration;
37+
import org.apache.hadoop.fs.FileSystem;
38+
import org.apache.hadoop.fs.Path;
3739
import org.apache.hadoop.fs.StorageType;
3840
import org.apache.hadoop.ha.HAServiceProtocol.HAServiceState;
41+
import org.apache.hadoop.hdfs.DistributedFileSystem;
3942
import org.apache.hadoop.hdfs.protocol.HdfsConstants;
4043
import org.apache.hadoop.hdfs.server.federation.MiniRouterDFSCluster.RouterContext;
4144
import org.apache.hadoop.hdfs.server.federation.RouterConfigBuilder;
@@ -78,6 +81,8 @@ public class TestRouterAdminCLI {
7881
private static RouterClient client;
7982
private static Router router;
8083

84+
private static DistributedFileSystem hdfs;
85+
8186
private static final String TEST_USER = "test-user";
8287

8388
private final ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -102,10 +107,12 @@ public static void globalSetUp() throws Exception {
102107

103108
// Start routers
104109
cluster.startRouters();
110+
cluster.startCluster();
105111

106112
routerContext = cluster.getRandomRouter();
107113
router = routerContext.getRouter();
108114
stateStore = router.getStateStore();
115+
hdfs = cluster.getCluster().getFileSystem();
109116

110117
Configuration routerConf = new Configuration();
111118
InetSocketAddress routerSocket = router.getAdminServerAddress();
@@ -700,6 +707,46 @@ public void testAddMountTableIfParentExist() throws Exception {
700707
}
701708
}
702709

710+
@Test
711+
public void testInitViewFsToMountTable() throws Exception {
712+
// re-set system out for testing
713+
System.setOut(new PrintStream(out));
714+
stateStore.loadCache(MountTableStoreImpl.class, true);
715+
String nnAddress = cluster.getRandomNamenode().getNamenode().getHostAndPort();
716+
717+
String src = "/data";
718+
Path destPath = new Path("hdfs://" + nnAddress + "/data");
719+
String user = "user1";
720+
String group = "group1";
721+
String clusterName = "ClusterX";
722+
723+
// 0.mkdir destPath
724+
hdfs.mkdirs(destPath);
725+
// 1.set owner
726+
hdfs.setOwner(destPath, user, group);
727+
// 2.set viewFs mapping
728+
admin.getConf().set("fs.viewfs.mounttable.ClusterX.link." + src, destPath.toString());
729+
// 3.run initialization
730+
String[] argv = new String[]{"-initViewFsToMountTable", clusterName};
731+
assertEquals(0, ToolRunner.run(admin, argv));
732+
// 4.gets the mount point entries
733+
stateStore.loadCache(MountTableStoreImpl.class, true);
734+
GetMountTableEntriesRequest getRequest = GetMountTableEntriesRequest
735+
.newInstance("/");
736+
GetMountTableEntriesResponse getResponse = client.getMountTableManager()
737+
.getMountTableEntries(getRequest);
738+
List<MountTable> mountTables = getResponse.getEntries();
739+
// 5.check
740+
assertEquals(1, mountTables.size());
741+
assertEquals(user, mountTables.get(0).getOwnerName());
742+
assertEquals(group, mountTables.get(0).getGroupName());
743+
assertEquals(destPath.toUri().getPath(), mountTables.get(0).
744+
getDestinations().get(0).getDest());
745+
assertEquals(nnAddress, mountTables.get(0).
746+
getDestinations().get(0).getNameserviceId());
747+
assertEquals(src, mountTables.get(0).getSourcePath());
748+
}
749+
703750
@Test
704751
public void testMountTablePermissions() throws Exception {
705752
// re-set system out for testing

0 commit comments

Comments
 (0)