Skip to content

Commit dea5e9c

Browse files
Improve exception handling in ControllerLeaderLocator (#17366)
1 parent 0da08f2 commit dea5e9c

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

pinot-core/src/main/java/org/apache/pinot/server/realtime/ControllerLeaderLocator.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,41 @@ private Pair<String, Integer> getHelixClusterLeader() {
206206
* @param instanceId instance id without any prefix, e.g. localhost_9000
207207
*/
208208
private Pair<String, Integer> convertToHostAndPortPair(String instanceId) {
209-
// TODO: improve the exception handling.
210-
if (instanceId == null) {
209+
if (instanceId == null || instanceId.trim().isEmpty()) {
210+
LOGGER.warn("Instance ID is null or empty");
211+
return null;
212+
}
213+
214+
try {
215+
int index = instanceId.lastIndexOf('_');
216+
if (index <= 0 || index >= instanceId.length() - 1) {
217+
LOGGER.error("Invalid instance ID format: {}. Expected format: hostname_port", instanceId);
218+
return null;
219+
}
220+
221+
String leaderHost = instanceId.substring(0, index);
222+
String portStr = instanceId.substring(index + 1);
223+
224+
if (leaderHost.trim().isEmpty()) {
225+
LOGGER.error("Empty hostname in instance ID: {}", instanceId);
226+
return null;
227+
}
228+
229+
int leaderPort = Integer.parseInt(portStr);
230+
if (leaderPort <= 0 || leaderPort > 65535) {
231+
LOGGER.error("Invalid port number {} in instance ID: {}. Port must be between 1 and 65535",
232+
leaderPort, instanceId);
233+
return null;
234+
}
235+
236+
return Pair.of(leaderHost, leaderPort);
237+
} catch (NumberFormatException e) {
238+
LOGGER.error("Failed to parse port number from instance ID: {}. Error: {}", instanceId, e.getMessage());
239+
return null;
240+
} catch (Exception e) {
241+
LOGGER.error("Unexpected error while parsing instance ID: {}. Error: {}", instanceId, e.getMessage());
211242
return null;
212243
}
213-
int index = instanceId.lastIndexOf('_');
214-
String leaderHost = instanceId.substring(0, index);
215-
int leaderPort = Integer.parseInt(instanceId.substring(index + 1));
216-
return Pair.of(leaderHost, leaderPort);
217244
}
218245

219246
/**

0 commit comments

Comments
 (0)