Skip to content

Commit

Permalink
checking newNode size to make sure its not 0 (#520)
Browse files Browse the repository at this point in the history
* checking newNode size to make sure its not 0
  • Loading branch information
JobseRyan authored Sep 3, 2024
1 parent 05c9ff8 commit cfa6f94
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,10 @@ public Node leastLoadedNode(long now) {
newNodes.add(new Node(nodeId--, address.getHostString(), address.getPort()));
}

if (newNodes.size() == 0) {
return null;
}

int offset = this.randOffset.nextInt(newNodes.size());
Node node = newNodes.get(offset);
log.trace("Resolved bootstrap server again, randomly picked node {} as least loaded node from the resolved node set", node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.PriorityQueue;
import org.apache.kafka.common.Cluster;
import org.apache.kafka.common.config.ConfigException;
import org.apache.kafka.common.KafkaException;
import org.apache.kafka.common.Node;
import org.apache.kafka.common.errors.AuthenticationException;
Expand Down Expand Up @@ -631,6 +632,19 @@ public void testResolveBootstrapInLeastLoadedNode() {
assertNotEquals(node, clusterClient.leastLoadedNode(time.milliseconds()));
}

@Test
public void noLeastLoadedNode() {
NetworkClient nc = new NetworkClient(selector, clusterMetadataUpdater, "mock-cluster-md", Integer.MAX_VALUE,
0, 0, 64 * 1024, 64 * 1024,
defaultRequestTimeoutMs, connectionSetupTimeoutMsTest, connectionSetupTimeoutMaxMsTest, time, true, new ApiVersions(), new LogContext(),
LeastLoadedNodeAlgorithm.VANILLA, new ArrayList<>());
nc.ready(node, time.milliseconds());
assertFalse(client.isReady(node, time.milliseconds()));
assertThrows(ConfigException.class, () -> nc.leastLoadedNode(time.milliseconds()));

assertEquals(null, nc.leastLoadedNode(time.milliseconds()));
}

@Test
public void testLeastLoadedNode() {
client.ready(node, time.milliseconds());
Expand Down

0 comments on commit cfa6f94

Please sign in to comment.