Skip to content

Commit

Permalink
[Java Client] Improve state changes to Connecting
Browse files Browse the repository at this point in the history
This commit is a partial cherry pick from apache@6162ccf.
That commit relied on several other transaction commits not present in this branch.
This commit makes it easier to cherry-pick a fix to the Java Client Producer.
  • Loading branch information
michaeljmarshall committed Dec 8, 2021
1 parent 56078df commit 6b7ab4e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,16 @@ protected void reconnectLater(Throwable exception) {
long delayMs = backoff.next();
log.warn("[{}] [{}] Could not get connection to broker: {} -- Will try again in {} s", state.topic, state.getHandlerName(),
exception.getMessage(), delayMs / 1000.0);
state.setState(State.Connecting);
state.client.timer().newTimeout(timeout -> {
log.info("[{}] [{}] Reconnecting after connection was closed", state.topic, state.getHandlerName());
incrementEpoch();
grabCnx();
}, delayMs, TimeUnit.MILLISECONDS);
if (state.changeToConnecting()) {
state.client.timer().newTimeout(timeout -> {
log.info("[{}] [{}] Reconnecting after connection was closed", state.topic, state.getHandlerName());
incrementEpoch();
grabCnx();
}, delayMs, TimeUnit.MILLISECONDS);
} else {
log.info("[{}] [{}] Ignoring reconnection request (state: {})",
state.topic, state.getHandlerName(), state.getState());
}
}

protected long incrementEpoch() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ protected State getState() {
return STATE_UPDATER.get(this);
}

protected boolean changeToConnecting() {
return (STATE_UPDATER.compareAndSet(this, State.Uninitialized, State.Connecting)
|| STATE_UPDATER.compareAndSet(this, State.Ready, State.Connecting)
|| STATE_UPDATER.compareAndSet(this, State.RegisteringSchema, State.Connecting)
|| STATE_UPDATER.compareAndSet(this, State.Connecting, State.Connecting));
}

protected void setState(State s) {
STATE_UPDATER.set(this, s);
}
Expand Down

0 comments on commit 6b7ab4e

Please sign in to comment.