Skip to content

Commit

Permalink
loadbalancer: fix flaky test (#3195)
Browse files Browse the repository at this point in the history
Motivation:

We have a flaky test. This is because we're able to get a connection
but only transiently. This happens because the DefaultHost is willing
to add a connection to an expired host without connections, which is a
state that is about to be closed.

Modifications:

Don't add the connection to the host if the host is is the transient
expired state with zero connections.

Fixes #2677
  • Loading branch information
bryce-anderson authored Feb 20, 2025
1 parent e0d6fc6 commit dd6f26e
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ public boolean markExpired() {
} else if (oldState.state == State.CLOSED) {
return true;
}
Object nextState = oldState.connections.isEmpty() ? State.CLOSED : State.EXPIRED;
assert oldState.state == State.ACTIVE || oldState.state == State.UNHEALTHY;
if (connStateUpdater.compareAndSet(this, oldState, oldState.toExpired())) {
cancelIfHealthCheck(oldState);
hostObserver.onHostMarkedExpired(oldState.connections.size());
if (nextState == State.CLOSED) {
if (oldState.connections.isEmpty()) {
// Trigger the callback to remove the host from usedHosts array.
this.closeAsync().subscribe();
return true;
Expand Down Expand Up @@ -326,7 +326,10 @@ private boolean addConnection(final C connection) {
int addAttempt = 0;
for (;;) {
final ConnState previous = connStateUpdater.get(this);
if (previous.state == State.CLOSED) {
if (previous.state == State.CLOSED ||
// an expired state with no connections is only a transient state and the host is going
// to be closing, so reject the connection.
previous.state == State.EXPIRED && previous.connections.isEmpty()) {
return false;
}
++addAttempt;
Expand Down

0 comments on commit dd6f26e

Please sign in to comment.