Always validate network connectivity and cluster state before attempting network operations. Check for leader availability, partition existence, and proper metadata state to avoid unnecessary network calls and provide better error handling.
Always validate network connectivity and cluster state before attempting network operations. Check for leader availability, partition existence, and proper metadata state to avoid unnecessary network calls and provide better error handling.
Key practices:
Example from partition validation:
// Check the partitions have leader
List<TopicPartition> partitionsWithoutLeader = filterNoneLeaderPartitions(partitionsToReset);
if (!partitionsWithoutLeader.isEmpty()) {
String partitionStr = partitionsWithoutLeader.stream()
.map(TopicPartition::toString)
.collect(Collectors.joining(","));
throw new LeaderNotAvailableException("The partitions \"" + partitionStr + "\" have no leader");
}
// Prepare data for partitions with leaders only
topicPartitions.removeAll(partitionsWithoutLeader);
Example from fetch error handling:
if (partitionData.currentLeader().leaderId() != -1 &&
partitionData.currentLeader().leaderEpoch() != -1) {
// Process with valid leader info
partitionsWithUpdatedLeaderInfo.put(partition, new Metadata.LeaderIdAndEpoch(...));
} else {
// Request metadata update for invalid leader state
requestMetadataUpdate(metadata, subscriptions, partition);
subscriptions.awaitUpdate(partition);
}
This approach prevents wasted network calls, provides clearer error messages, and ensures operations only proceed when the network state is valid.
Enter the URL of a public GitHub repository