Skip to content

Commit 73954c1

Browse files
ChenSammixiaoyuyao
authored andcommitted
HDDS-1612. Add 'scmcli printTopology' shell command to print datanode topology. Contributed by Sammi Chen.(#910)
1 parent 294695d commit 73954c1

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/protocol/DatanodeDetails.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ public HddsProtos.DatanodeDetailsProto getProtoBufMessage() {
212212
if (certSerialId != null) {
213213
builder.setCertSerialId(certSerialId);
214214
}
215+
builder.setNetworkLocation(getNetworkLocation());
216+
215217
for (Port port : ports) {
216218
builder.addPorts(HddsProtos.Port.newBuilder()
217219
.setName(port.getName().toString())

hadoop-hdds/common/src/main/proto/hdds.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ message DatanodeDetailsProto {
3434
required string hostName = 3; // hostname
3535
repeated Port ports = 4;
3636
optional string certSerialId = 5; // Certificate serial id.
37+
optional string networkLocation = 6; // Network topology location
3738
}
3839

3940
/**

hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/SCMCLI.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
CreateSubcommand.class,
8585
CloseSubcommand.class,
8686
ListPipelinesSubcommand.class,
87-
ClosePipelineSubcommand.class
87+
ClosePipelineSubcommand.class,
88+
TopologySubcommand.class
8889
},
8990
mixinStandardHelpOptions = true)
9091
public class SCMCLI extends GenericCli {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
* <p>
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* <p>
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+
19+
package org.apache.hadoop.hdds.scm.cli;
20+
21+
import org.apache.hadoop.hdds.cli.HddsVersionProvider;
22+
import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
23+
import org.apache.hadoop.hdds.scm.client.ScmClient;
24+
import picocli.CommandLine;
25+
import static org.apache.hadoop.hdds.protocol.proto.HddsProtos.NodeState.DEAD;
26+
import static org.apache.hadoop.hdds.protocol.proto.HddsProtos.NodeState.DECOMMISSIONED;
27+
import static org.apache.hadoop.hdds.protocol.proto.HddsProtos.NodeState.DECOMMISSIONING;
28+
import static org.apache.hadoop.hdds.protocol.proto.HddsProtos.NodeState.HEALTHY;
29+
import static org.apache.hadoop.hdds.protocol.proto.HddsProtos.NodeState.STALE;
30+
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
import java.util.concurrent.Callable;
34+
35+
/**
36+
* Handler of printTopology command.
37+
*/
38+
@CommandLine.Command(
39+
name = "printTopology",
40+
description = "Print a tree of the network topology as reported by SCM",
41+
mixinStandardHelpOptions = true,
42+
versionProvider = HddsVersionProvider.class)
43+
public class TopologySubcommand implements Callable<Void> {
44+
45+
@CommandLine.ParentCommand
46+
private SCMCLI parent;
47+
48+
private static List<HddsProtos.NodeState> stateArray = new ArrayList<>();
49+
50+
static {
51+
stateArray.add(HEALTHY);
52+
stateArray.add(STALE);
53+
stateArray.add(DEAD);
54+
stateArray.add(DECOMMISSIONING);
55+
stateArray.add(DECOMMISSIONED);
56+
}
57+
58+
@Override
59+
public Void call() throws Exception {
60+
try (ScmClient scmClient = parent.createScmClient()) {
61+
for (HddsProtos.NodeState state : stateArray) {
62+
List<HddsProtos.Node> nodes = scmClient.queryNode(state,
63+
HddsProtos.QueryScope.CLUSTER, "");
64+
if (nodes != null && nodes.size() > 0) {
65+
// show node state
66+
System.out.println("State = " + state.toString());
67+
// format "hostname/ipAddress networkLocation"
68+
nodes.forEach(node -> {
69+
System.out.print(node.getNodeID().getHostName() + "/" +
70+
node.getNodeID().getIpAddress());
71+
System.out.println(" " +
72+
(node.getNodeID().getNetworkLocation() != null ?
73+
node.getNodeID().getNetworkLocation() : "NA"));
74+
});
75+
}
76+
}
77+
return null;
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)