-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-11268. Add --table mode for OM/SCM Roles CLI #7016
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 all commits
5d51969
b5e7a3a
b37d601
c6ce277
92a11d0
f12f7f6
e69f1d6
1b0fbc2
d427166
e8b99ad
1360309
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 |
|---|---|---|
|
|
@@ -128,6 +128,7 @@ | |
|
|
||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
|
|
@@ -157,6 +158,12 @@ public final class StorageContainerLocationProtocolClientSideTranslatorPB | |
| private final StorageContainerLocationProtocolPB rpcProxy; | ||
| private final SCMContainerLocationFailoverProxyProvider fpp; | ||
|
|
||
| /** | ||
| * This is used to check if 'leader' or 'follower' exists, | ||
| * in order to confirm whether we have enabled Ratis. | ||
| */ | ||
| private final List<String> scmRatisRolesToCheck = Arrays.asList("leader", "follower"); | ||
|
|
||
| /** | ||
| * Creates a new StorageContainerLocationProtocolClientSideTranslatorPB. | ||
| * | ||
|
|
@@ -760,8 +767,23 @@ public ScmInfo getScmInfo() throws IOException { | |
| .setScmId(resp.getScmId()) | ||
| .setRatisPeerRoles(resp.getPeerRolesList()); | ||
|
|
||
| return builder.build(); | ||
| // By default, we assume that SCM Ratis is not enabled. | ||
|
Contributor
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. We have considered whether the
Contributor
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. @ChenSammi Kindly ping, Can you review this PR again? Thank you very much! |
||
|
|
||
| // If the response contains the `ScmRatisEnabled` field, | ||
| // we will set it directly; otherwise, | ||
| // we will determine if Ratis is enabled based on | ||
| // whether the `peerRolesList` contains the keywords 'leader' or 'follower'. | ||
| if (resp.hasScmRatisEnabled()) { | ||
| builder.setScmRatisEnabled(resp.getScmRatisEnabled()); | ||
| } else { | ||
| List<String> peerRolesList = resp.getPeerRolesList(); | ||
| if (!peerRolesList.isEmpty()) { | ||
| boolean containsScmRoles = peerRolesList.stream().map(String::toLowerCase) | ||
| .anyMatch(scmRatisRolesToCheck::contains); | ||
| builder.setScmRatisEnabled(containsScmRoles); | ||
| } | ||
| } | ||
| return builder.build(); | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| package org.apache.hadoop.ozone.admin.scm; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
|
|
@@ -27,6 +28,7 @@ | |
| import org.apache.hadoop.hdds.scm.cli.ScmSubcommand; | ||
| import org.apache.hadoop.hdds.scm.client.ScmClient; | ||
| import org.apache.hadoop.hdds.server.JsonUtils; | ||
| import org.apache.hadoop.ozone.utils.FormattingCLIUtils; | ||
| import picocli.CommandLine; | ||
|
|
||
| import static java.lang.System.err; | ||
|
|
@@ -50,13 +52,44 @@ public class GetScmRatisRolesSubcommand extends ScmSubcommand { | |
| description = "Format output as JSON") | ||
| private boolean json; | ||
|
|
||
| @CommandLine.Option(names = { "--table" }, | ||
| defaultValue = "false", | ||
| description = "Format output as Table") | ||
| private boolean table; | ||
|
|
||
| private static final String SCM_ROLES_TITLE = "Storage Container Manager Roles"; | ||
|
|
||
| private static final List<String> RATIS_SCM_ROLES_HEADER = Arrays.asList( | ||
| "Host Name", "Ratis Port", "Role", "Node ID", "Host Address"); | ||
|
|
||
| private static final List<String> STANDALONE_SCM_ROLES_HEADER = Arrays.asList("Host Name", "Port"); | ||
|
|
||
| @Override | ||
| protected void execute(ScmClient scmClient) throws IOException { | ||
| public void execute(ScmClient scmClient) throws IOException { | ||
| List<String> ratisRoles = scmClient.getScmRatisRoles(); | ||
| boolean isRatisEnabled = scmClient.isScmRatisEnable(); | ||
| if (json) { | ||
| Map<String, Map<String, String>> scmRoles = parseScmRoles(ratisRoles); | ||
| System.out.print( | ||
| JsonUtils.toJsonStringWithDefaultPrettyPrinter(scmRoles)); | ||
| } else if (table) { | ||
| FormattingCLIUtils formattingCLIUtils = new FormattingCLIUtils(SCM_ROLES_TITLE); | ||
|
|
||
| // Determine which header to use based on whether Ratis is enabled or not. | ||
| if (isRatisEnabled) { | ||
| formattingCLIUtils.addHeaders(RATIS_SCM_ROLES_HEADER); | ||
|
Contributor
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.
Thank you very much for reviewing this PR! I’d like to explain why I added the When using For non-HA SCMs, we will display it in the following format: For HA SCMs, we will display it in the following format: I need a clear method to determine if SCM has Ratis enabled. I am considering two approaches: Approach 1: Use the From my perspective, Approach 2 is better, as the code looks clearer. |
||
| } else { | ||
| formattingCLIUtils.addHeaders(STANDALONE_SCM_ROLES_HEADER); | ||
| } | ||
|
|
||
| for (String role : ratisRoles) { | ||
| String[] roleItems = role.split(":"); | ||
| if (roleItems.length < 2) { | ||
| err.println("Invalid response received for ScmRatisRoles."); | ||
| } | ||
| formattingCLIUtils.addLine(roleItems); | ||
| } | ||
| System.out.println(formattingCLIUtils.render()); | ||
| } else { | ||
| for (String role: ratisRoles) { | ||
| System.out.println(role); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable
scmRatisRolesToCheckis only referenced in one place, and I'm not sure if defining it as a global variable is a good idea. Additionally, from my understanding, usingstaticmight be better if it needs to be used. @ChenSammi Please help give some suggestions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@errose28 @adoroszlai May I take up some of your time to ask for some advice? Thank you very much!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@whbing How should I modify this? Should I change the variable to uppercase and make it static?