Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -210,11 +210,17 @@ public DriverChannel next(@Nullable Token routingKey, @Nullable Integer shardSug

/** @return the number of active channels in the pool. */
public int size() {
if (!singleThreaded.initialized) {
return 0;
}
return Arrays.stream(channels).mapToInt(ChannelSet::size).sum();
}

/** @return the number of available stream ids on all channels in the pool. */
public int getAvailableIds() {
if (!singleThreaded.initialized) {
return 0;
}
return Arrays.stream(channels).mapToInt(ChannelSet::getAvailableIds).sum();
}

Expand All @@ -223,6 +229,9 @@ public int getAvailableIds() {
* {@link #getOrphanedIds() orphaned ids}).
*/
public int getInFlight() {
if (!singleThreaded.initialized) {
return 0;
}
return Arrays.stream(channels).mapToInt(ChannelSet::getInFlight).sum();
}

Expand All @@ -232,6 +241,9 @@ public int getInFlight() {
* might still come from the server.
*/
public int getOrphanedIds() {
if (!singleThreaded.initialized) {
return 0;
}
return Arrays.stream(channels).mapToInt(ChannelSet::getOrphanedIds).sum();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,34 @@ public void should_fire_force_down_event_when_cluster_name_does_not_match() thro
factoryHelper.verifyNoMoreCalls();
}

@Test
public void should_return_zero_for_metric_accessors_when_pool_uninitialized() throws Exception {
// Reproduce CUSTOMER-413: when the initial connection attempt fails, connectFuture completes
// with channels == null (initialize() is only called on the success path). Before the fix,
// any call to size/getAvailableIds/getInFlight/getOrphanedIds threw NullPointerException via
// Arrays.stream(null), which propagated through the Dropwizard Metrics gauge lambdas
// registered in DropwizardNodeMetricUpdater.
when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(1);

MockChannelFactoryHelper.builder(channelFactory)
.failure(node, "mock channel init failure")
.build();

CompletionStage<ChannelPool> poolFuture =
ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");

ChannelPool pool = poolFuture.toCompletableFuture().get();

// Confirm the precondition: pool entered the map but channels was never initialized
assertThat(pool.channels).isNull();

// All four accessor methods must return 0 without throwing
assertThat(pool.size()).isEqualTo(0);
assertThat(pool.getAvailableIds()).isEqualTo(0);
assertThat(pool.getInFlight()).isEqualTo(0);
assertThat(pool.getOrphanedIds()).isEqualTo(0);
}

@Test
public void should_reconnect_when_init_incomplete() throws Exception {
// Short delay so we don't have to wait in the test
Expand Down
Loading