Skip to content

Commit 0d94a5b

Browse files
committed
refactor: better error msg for ConnectionWatchDog
1 parent 19e6463 commit 0d94a5b

File tree

2 files changed

+45
-43
lines changed

2 files changed

+45
-43
lines changed

src/main/java/io/lettuce/core/protocol/ConnectionWatchdog.java

+43-42
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121

2222
import java.net.SocketAddress;
2323
import java.time.Duration;
24-
import java.util.concurrent.CancellationException;
2524
import java.util.concurrent.CompletableFuture;
2625
import java.util.concurrent.TimeUnit;
2726
import java.util.concurrent.atomic.AtomicBoolean;
27+
import java.util.function.Consumer;
28+
import java.util.function.Supplier;
2829

2930
import io.lettuce.core.ClientOptions;
3031
import io.lettuce.core.ConnectionBuilder;
3132
import io.lettuce.core.ConnectionEvents;
33+
import io.lettuce.core.RedisException;
3234
import io.lettuce.core.event.EventBus;
3335
import io.lettuce.core.event.connection.ReconnectAttemptEvent;
3436
import io.lettuce.core.event.connection.ReconnectFailedEvent;
@@ -84,9 +86,9 @@ public class ConnectionWatchdog extends ChannelInboundHandlerAdapter {
8486

8587
private final String epid;
8688

87-
private final boolean useAutoBatchFlushEndpoint;
89+
private final boolean useAutoBatchFlush;
8890

89-
private final Endpoint endpoint;
91+
private final Consumer<Supplier<Throwable>> endpointFailedToReconnectNotifier;
9092

9193
private Channel channel;
9294

@@ -148,8 +150,15 @@ public ConnectionWatchdog(Delay reconnectDelay, ClientOptions clientOptions, Boo
148150
this.eventBus = eventBus;
149151
this.redisUri = (String) bootstrap.config().attrs().get(ConnectionBuilder.REDIS_URI);
150152
this.epid = endpoint.getId();
151-
this.endpoint = endpoint;
152-
this.useAutoBatchFlushEndpoint = endpoint instanceof AutoBatchFlushEndpoint;
153+
if (endpoint instanceof AutoBatchFlushEndpoint) {
154+
this.useAutoBatchFlush = true;
155+
endpointFailedToReconnectNotifier = throwableSupplier -> ((AutoBatchFlushEndpoint) endpoint)
156+
.notifyReconnectFailed(throwableSupplier.get());
157+
} else {
158+
this.useAutoBatchFlush = false;
159+
endpointFailedToReconnectNotifier = ignoredThrowableSupplier -> {
160+
};
161+
}
153162

154163
Mono<SocketAddress> wrappedSocketAddressSupplier = socketAddressSupplier.doOnNext(addr -> remoteAddress = addr)
155164
.onErrorResume(t -> {
@@ -215,20 +224,11 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
215224
channel = null;
216225

217226
if (listenOnChannelInactive && !reconnectionHandler.isReconnectSuspended()) {
218-
if (!isEventLoopGroupActive()) {
219-
logger.debug("isEventLoopGroupActive() == false");
220-
return;
221-
}
222-
223-
if (!isListenOnChannelInactive()) {
224-
logger.debug("Skip reconnect scheduling, listener disabled");
225-
return;
226-
}
227-
228-
if (!useAutoBatchFlushEndpoint) {
227+
if (!useAutoBatchFlush) {
229228
this.scheduleReconnect();
229+
} else {
230+
doReconnectOnAutoBatchFlushEndpointQuiescence = this::scheduleReconnect;
230231
}
231-
doReconnectOnAutoBatchFlushEndpointQuiescence = this::scheduleReconnect;
232232
// otherwise, will be called later by BatchFlushEndpoint#onEndpointQuiescence
233233
} else {
234234
logger.debug("{} Reconnect scheduling disabled", logPrefix(), ctx);
@@ -237,7 +237,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
237237
super.channelInactive(ctx);
238238
}
239239

240-
boolean willReconnect() {
240+
boolean willReconnectOnAutoBatchFlushEndpointQuiescence() {
241241
return doReconnectOnAutoBatchFlushEndpointQuiescence != null;
242242
}
243243

@@ -261,14 +261,16 @@ public void scheduleReconnect() {
261261
logger.debug("{} scheduleReconnect()", logPrefix());
262262

263263
if (!isEventLoopGroupActive()) {
264-
logger.debug("isEventLoopGroupActive() == false");
265-
notifyEndpointFailedToConnectIfNeeded();
264+
final String errMsg = "isEventLoopGroupActive() == false";
265+
logger.debug(errMsg);
266+
notifyEndpointFailedToReconnect(errMsg);
266267
return;
267268
}
268269

269270
if (!isListenOnChannelInactive()) {
270-
logger.debug("Skip reconnect scheduling, listener disabled");
271-
notifyEndpointFailedToConnectIfNeeded();
271+
final String errMsg = "Skip reconnect scheduling, listener disabled";
272+
logger.debug(errMsg);
273+
notifyEndpointFailedToReconnect(errMsg);
272274
return;
273275
}
274276

@@ -285,8 +287,9 @@ public void scheduleReconnect() {
285287
reconnectScheduleTimeout = null;
286288

287289
if (!isEventLoopGroupActive()) {
288-
logger.warn("Cannot execute scheduled reconnect timer, reconnect workers are terminated");
289-
notifyEndpointFailedToConnectIfNeeded();
290+
final String errMsg = "Cannot execute scheduled reconnect timer, reconnect workers are terminated";
291+
logger.warn(errMsg);
292+
notifyEndpointFailedToReconnect(errMsg);
290293
return;
291294
}
292295

@@ -302,18 +305,12 @@ public void scheduleReconnect() {
302305
}
303306
} else {
304307
logger.debug("{} Skipping scheduleReconnect() because I have an active channel", logPrefix());
305-
notifyEndpointFailedToConnectIfNeeded();
308+
notifyEndpointFailedToReconnect("Skipping scheduleReconnect() because I have an active channel");
306309
}
307310
}
308311

309-
private void notifyEndpointFailedToConnectIfNeeded() {
310-
notifyEndpointFailedToConnectIfNeeded(new CancellationException());
311-
}
312-
313-
private void notifyEndpointFailedToConnectIfNeeded(Exception e) {
314-
if (useAutoBatchFlushEndpoint) {
315-
((AutoBatchFlushEndpoint) endpoint).notifyReconnectFailed(e);
316-
}
312+
void notifyEndpointFailedToReconnect(String msg) {
313+
endpointFailedToReconnectNotifier.accept(() -> new RedisException(msg));
317314
}
318315

319316
/**
@@ -335,26 +332,29 @@ public void run(int attempt) throws Exception {
335332
* @param delay retry delay.
336333
* @throws Exception when reconnection fails.
337334
*/
338-
private void run(int attempt, Duration delay) throws Exception {
335+
private void run(int attempt, Duration delay) {
339336

340337
reconnectSchedulerSync.set(false);
341338
reconnectScheduleTimeout = null;
342339

343340
if (!isEventLoopGroupActive()) {
344-
logger.debug("isEventLoopGroupActive() == false");
345-
notifyEndpointFailedToConnectIfNeeded();
341+
final String errMsg = "isEventLoopGroupActive() == false";
342+
logger.debug(errMsg);
343+
notifyEndpointFailedToReconnect(errMsg);
346344
return;
347345
}
348346

349347
if (!isListenOnChannelInactive()) {
350-
logger.debug("Skip reconnect scheduling, listener disabled");
351-
notifyEndpointFailedToConnectIfNeeded();
348+
final String errMsg = "Skip reconnect scheduling, listener disabled";
349+
logger.debug(errMsg);
350+
notifyEndpointFailedToReconnect(errMsg);
352351
return;
353352
}
354353

355354
if (isReconnectSuspended()) {
356-
logger.debug("Skip reconnect scheduling, reconnect is suspended");
357-
notifyEndpointFailedToConnectIfNeeded();
355+
final String msg = "Skip reconnect scheduling, reconnect is suspended";
356+
logger.debug(msg);
357+
notifyEndpointFailedToReconnect(msg);
358358
return;
359359
}
360360

@@ -411,13 +411,14 @@ private void run(int attempt, Duration delay) throws Exception {
411411
if (!isReconnectSuspended()) {
412412
scheduleReconnect();
413413
} else {
414-
notifyEndpointFailedToConnectIfNeeded();
414+
endpointFailedToReconnectNotifier
415+
.accept(() -> new RedisException("got error and then reconnect is suspended", t));
415416
}
416417
});
417418
} catch (Exception e) {
418419
logger.log(warnLevel, "Cannot reconnect: {}", e.toString());
419420
eventBus.publish(new ReconnectFailedEvent(redisUri, epid, LocalAddress.ANY, remoteAddress, e, attempt));
420-
notifyEndpointFailedToConnectIfNeeded(e);
421+
endpointFailedToReconnectNotifier.accept(() -> e);
421422
}
422423
}
423424

src/main/java/io/lettuce/core/protocol/DefaultAutoBatchFlushEndpoint.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ public void notifyChannelInactiveAfterWatchdogDecision(Channel channel,
390390
return;
391391
}
392392

393-
boolean willReconnect = connectionWatchdog != null && connectionWatchdog.willReconnect();
393+
boolean willReconnect = connectionWatchdog != null
394+
&& connectionWatchdog.willReconnectOnAutoBatchFlushEndpointQuiescence();
394395
RedisException exception = null;
395396
// Unlike DefaultEndpoint, here we don't check reliability since connectionWatchdog.willReconnect() already does it.
396397
if (isClosed()) {

0 commit comments

Comments
 (0)