Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,41 @@ private Pair<String, Integer> getHelixClusterLeader() {
* @param instanceId instance id without any prefix, e.g. localhost_9000
*/
private Pair<String, Integer> convertToHostAndPortPair(String instanceId) {
// TODO: improve the exception handling.
if (instanceId == null) {
if (instanceId == null || instanceId.trim().isEmpty()) {
LOGGER.warn("Instance ID is null or empty");
return null;
}

try {
int index = instanceId.lastIndexOf('_');
if (index <= 0 || index >= instanceId.length() - 1) {
LOGGER.error("Invalid instance ID format: {}. Expected format: hostname_port", instanceId);
return null;
}

String leaderHost = instanceId.substring(0, index);
String portStr = instanceId.substring(index + 1);

if (leaderHost.trim().isEmpty()) {
LOGGER.error("Empty hostname in instance ID: {}", instanceId);
return null;
}

int leaderPort = Integer.parseInt(portStr);
if (leaderPort <= 0 || leaderPort > 65535) {
LOGGER.error("Invalid port number {} in instance ID: {}. Port must be between 1 and 65535",
leaderPort, instanceId);
return null;
}

return Pair.of(leaderHost, leaderPort);
} catch (NumberFormatException e) {
LOGGER.error("Failed to parse port number from instance ID: {}. Error: {}", instanceId, e.getMessage());
return null;
} catch (Exception e) {
LOGGER.error("Unexpected error while parsing instance ID: {}. Error: {}", instanceId, e.getMessage());
return null;
}
int index = instanceId.lastIndexOf('_');
String leaderHost = instanceId.substring(0, index);
int leaderPort = Integer.parseInt(instanceId.substring(index + 1));
return Pair.of(leaderHost, leaderPort);
}

/**
Expand Down
Loading