siddhantsangwan commented on a change in pull request #1982:
URL: https://github.com/apache/ozone/pull/1982#discussion_r589300046
##########
File path:
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/SCMClientProtocolServer.java
##########
@@ -678,13 +680,111 @@ public boolean getReplicationManagerStatus() {
long used = stat.getScmUsed().get();
long remaining = stat.getRemaining().get();
- HddsProtos.DatanodeUsageInfo info = HddsProtos.DatanodeUsageInfo
- .newBuilder()
+ return HddsProtos.DatanodeUsageInfo.newBuilder()
.setCapacity(capacity)
.setUsed(used)
.setRemaining(remaining)
+ .setNode(node.toProto(node.getCurrentVersion()))
.build();
- return info;
+ }
+
+ /**
+ * Get information of the most or least used datanodes.
+ *
+ * @param mostUsed true if most used, false if least used
+ * @param count Integer number of nodes to get info for
+ * @return List of DatanodeUsageInfo. Each element contains usage info
+ * such as capacity, SCMUsed, and remaining space.
+ * @throws IOException
+ */
+ @Override
+ public List<HddsProtos.DatanodeUsageInfo> getDatanodeUsageInfo(
+ boolean mostUsed, int count) throws IOException {
+
+ // check admin authorisation
+ String remoteUser = getRpcRemoteUsername();
+ try {
+ getScm().checkAdminAccess(remoteUser);
+ } catch (IOException e) {
+ LOG.error("Authorisation failed", e);
+ throw e;
+ }
+
+ PriorityQueue<DatanodeDetails> nodes = null;
+ if (mostUsed) {
+ nodes = getMostUsedDatanodes(count);
+ } else {
+ nodes = getLeastUsedDatanodes(count);
+ }
+
+ List<HddsProtos.DatanodeUsageInfo> infoList = new ArrayList<>();
+ for (DatanodeDetails node : nodes) {
+ infoList.add(getUsageInfoFromDatanodeDetails(node));
+ }
+ return infoList;
+ }
+
+ /**
+ * Get most used datanodes.
+ *
+ * @param count Integer number of datanodes to get. Must be greater than
+ * zero and lesser than total (healthy + stale) number of nodes
+ * @return PriorityQueue of DatanodeDetails
+ * @throws IllegalArgumentException
+ */
+ private PriorityQueue<DatanodeDetails> getMostUsedDatanodes(int count)
+ throws IllegalArgumentException {
+ Map<DatanodeDetails, SCMNodeStat> nodeStatMap = scm.getScmNodeManager()
+ .getNodeStats();
+
+ if (count > nodeStatMap.size()) {
+ throw new IllegalArgumentException(
+ String.format("count must be lesser than %d", nodeStatMap.size()));
+ }
+ PriorityQueue<DatanodeDetails> nodes = new PriorityQueue<>(count,
+ Comparator.comparingLong(node -> nodeStatMap.get(node)
+ .getRemaining().get()).reversed());
Review comment:
Since we only need count number of nodes, I think we could avoid sorting
the entire map? Sorting could mean extra work since it will also involve
ordering the remaining `size - k` nodes.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]