Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checking newNode size to make sure its not 0 #520

Merged
merged 5 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
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 @@ -990,6 +990,10 @@ public Node leastLoadedNode(long now) {
newNodes.add(new Node(nodeId--, address.getHostString(), address.getPort()));
}

if (newNodes.size() == 0) {
return null;
JobseRyan marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading