chihsuan commented on code in PR #10831:
URL: https://github.com/apache/ozone/pull/10831#discussion_r3720811071
##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/net/HostAndPort.java:
##########
@@ -37,7 +38,7 @@ public class HostAndPort {
public HostAndPort(String host, int port) {
this.host = host;
this.port = port;
- this.hostAndPortString = host + ":" + port;
+ this.hostAndPortString = HddsUtils.getHostPortString(host, port);
Review Comment:
Since this changes the serialized address for IPv6 hosts, would it make
sense to add a small test case verifying the changes?
##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/HddsUtils.java:
##########
@@ -243,6 +243,67 @@ public static String getHostPortString(String host, int
port) {
return HostAndPort.fromParts(host, port).toString();
}
+ /**
+ * Parse a Ratis role string produced by
+ * {@code SCMRatisServerImpl.getRatisRoles()} into its constituent fields.
+ * The format is {@code [host]:port:ROLE:id:hostIP} where host and hostIP
+ * may be bracketed IPv6 literals.
+ *
+ * @param roleString the encoded role string
+ * @return a 5-element array: {host, port, role, id, hostIP}
+ */
+ public static String[] parseRatisRoleString(String roleString) {
+ Preconditions.checkArgument(roleString != null && !roleString.isEmpty(),
+ "Role string must not be null or empty");
+
+ // Parse from the right: the last field is hostIP (possibly bracketed),
+ // then id (uuid, no colons), then role (LEADER/FOLLOWER, no colons),
+ // and the remainder is host:port (which may be bracketed IPv6).
+ int idx = roleString.length();
+
+ // Field 5: hostIP — may be bracketed IPv6 like [2001:db8::1]
+ String hostIp;
+ if (roleString.charAt(idx - 1) == ']') {
+ int bracket = roleString.lastIndexOf('[');
+ Preconditions.checkArgument(bracket > 0,
+ "Malformed role string (unmatched bracket): %s", roleString);
+ hostIp = roleString.substring(bracket + 1, idx - 1);
+ Preconditions.checkArgument(roleString.charAt(bracket - 1) == ':',
+ "Malformed role string (expected ':' before '['): %s", roleString);
+ idx = bracket - 1;
+ } else {
+ int sep = roleString.lastIndexOf(':');
+ Preconditions.checkArgument(sep > 0,
+ "Malformed role string (expected host:port:role:id:hostIP): %s",
roleString);
+ hostIp = roleString.substring(sep + 1);
+ idx = sep;
+ }
+
+ // Field 4: id (uuid or peer id, no colons)
+ int sep3 = roleString.lastIndexOf(':', idx - 1);
+ Preconditions.checkArgument(sep3 > 0,
+ "Malformed role string (cannot find id field): %s", roleString);
+ String id = roleString.substring(sep3 + 1, idx);
+ idx = sep3;
+
+ // Field 3: role (LEADER/FOLLOWER, no colons)
+ int sep2 = roleString.lastIndexOf(':', idx - 1);
+ Preconditions.checkArgument(sep2 > 0,
+ "Malformed role string (cannot find role field): %s", roleString);
+ String role = roleString.substring(sep2 + 1, idx);
+ idx = sep2;
+
+ // Remainder is host:port — use HostAndPort to parse safely
+ String hostPort = roleString.substring(0, idx);
+ Preconditions.checkArgument(!hostPort.isEmpty(),
+ "Malformed role string (empty host:port): %s", roleString);
+ HostAndPort hp = HostAndPort.fromString(hostPort);
+ String host = hp.getHost();
+ String port = String.valueOf(hp.getPort());
Review Comment:
nit: could we validate `hp.hasPort()` before calling `getPort()`? ? For
example, a malformed four-field string can leave "host" as the remainder;
`HostAndPort.fromString("host")` succeeds, but `getPort()` throws
`IllegalStateException`, which the caller-side catches do not handle.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]