Skip to content

Commit f70f5d3

Browse files
committed
Add some logging for recovery
1 parent 5fdfca5 commit f70f5d3

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

src/main/java/com/rabbitmq/client/amqp/impl/AmqpConnection.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ final class AmqpConnection extends ResourceBase implements Connection {
7070
private final Supplier<SessionHandler> sessionHandlerSupplier;
7171
private final ConnectionUtils.AffinityContext affinity;
7272
private final ConnectionSettings.AffinityStrategy affinityStrategy;
73+
private final String name;
7374

7475
AmqpConnection(AmqpConnectionBuilder builder) {
7576
super(builder.listeners());
7677
this.id = ID_SEQUENCE.getAndIncrement();
78+
this.name = builder.name();
7779
this.environment = builder.environment();
7880
this.connectionSettings = builder.connectionSettings().consolidate();
7981
this.sessionHandlerSupplier =
@@ -95,11 +97,13 @@ final class AmqpConnection extends ResourceBase implements Connection {
9597
while (!Thread.currentThread().isInterrupted()) {
9698
try {
9799
Runnable recoveryTask = this.recoveryRequestQueue.take();
100+
LOGGER.debug("Running recovery task for connection '{}'...", this.name());
98101
recoveryTask.run();
102+
LOGGER.debug("Recovery task for connection '{}' has run.", this.name());
99103
} catch (InterruptedException e) {
100104
return;
101105
} catch (Exception e) {
102-
LOGGER.warn("Error during connection recovery", e);
106+
LOGGER.warn("Error during connection recovery for '{}'", this.name(), e);
103107
}
104108
}
105109
});
@@ -125,6 +129,7 @@ final class AmqpConnection extends ResourceBase implements Connection {
125129
this.affinityStrategy = null;
126130
}
127131
this.management = createManagement();
132+
LOGGER.debug("Opening native connection for connection '{}'...", this.name());
128133
NativeConnectionWrapper ncw =
129134
ConnectionUtils.enforceAffinity(
130135
addrs -> {
@@ -139,6 +144,7 @@ final class AmqpConnection extends ResourceBase implements Connection {
139144
this.affinityStrategy,
140145
ConnectionUtils.NO_RETRY_STRATEGY);
141146
this.sync(ncw);
147+
LOGGER.debug("Opened connection '{}' on node '{}'.", this.name(), this.connectionNodename());
142148
this.state(OPEN);
143149
this.environment.metricsCollector().openConnection();
144150
}
@@ -286,29 +292,36 @@ TopologyListener createTopologyListener(AmqpConnectionBuilder builder) {
286292
BiConsumer<org.apache.qpid.protonj2.client.Connection, DisconnectionEvent> result =
287293
(conn, event) -> {
288294
ClientIOException ioex = event.failureCause();
289-
LOGGER.debug("Disconnect handler, error is the following:", ioex);
295+
LOGGER.debug("Disconnect handler, error is the following: {}", ioex.getMessage());
290296
if (this.state() == OPENING) {
291297
LOGGER.debug("Connection is still opening, disconnect handler skipped");
292298
// the broker is not available when opening the connection
293299
// nothing to do in this listener
294300
return;
295301
}
296302
if (this.recoveringConnection.get()) {
297-
LOGGER.debug("Filtering recovery task enqueueing, connection recovery in progress");
303+
LOGGER.debug(
304+
"Filtering recovery task enqueueing, connection recovery of '{}' in progress",
305+
this.name());
298306
return;
299307
}
300308
AmqpException exception = ExceptionUtils.convert(event.failureCause());
301309
LOGGER.debug("Converted native exception to {}", exception.getClass().getSimpleName());
302310

303311
if (RECOVERY_PREDICATE.test(exception) && this.state() != OPENING) {
304-
LOGGER.debug("Queueing recovery task, error is {}", exception.getMessage());
312+
LOGGER.debug(
313+
"Queueing recovery task for '{}', error is {}",
314+
this.name(),
315+
exception.getMessage());
305316
this.recoveryRequestQueue.add(
306317
() ->
307318
recoverAfterConnectionFailure(
308319
recoveryConfiguration, name, exception, resultReference));
309320
} else {
310321
LOGGER.debug(
311-
"Not recovering connection for error {}", event.failureCause().getMessage());
322+
"Not recovering connection '{}' for error {}",
323+
this.name(),
324+
event.failureCause().getMessage());
312325
}
313326
};
314327

@@ -323,7 +336,9 @@ private void recoverAfterConnectionFailure(
323336
AtomicReference<BiConsumer<org.apache.qpid.protonj2.client.Connection, DisconnectionEvent>>
324337
disconnectedHandlerReference) {
325338
LOGGER.info(
326-
"Connection to {} has been disconnected, trying to recover", this.currentConnectionLabel());
339+
"Connection '{}' to '{}' has been disconnected, trying to recover",
340+
this.name(),
341+
this.currentConnectionLabel());
327342
this.state(RECOVERING, failureCause);
328343
this.changeStateOfPublishers(RECOVERING, failureCause);
329344
this.changeStateOfConsumers(RECOVERING, failureCause);
@@ -337,7 +352,7 @@ private void recoverAfterConnectionFailure(
337352
recoverNativeConnection(
338353
recoveryConfiguration, connectionName, disconnectedHandlerReference);
339354
this.sync(ncw);
340-
LOGGER.debug("Reconnected to {}", this.currentConnectionLabel());
355+
LOGGER.debug("Reconnected '{}' to {}", this.name(), this.currentConnectionLabel());
341356
} catch (Exception ex) {
342357
if (ex instanceof InterruptedException) {
343358
Thread.currentThread().interrupt();
@@ -353,11 +368,11 @@ private void recoverAfterConnectionFailure(
353368

354369
try {
355370
if (recoveryConfiguration.topology()) {
356-
LOGGER.debug("Recovering topology");
371+
LOGGER.debug("Recovering topology of connection '{}'...", this.name());
357372
this.recoverTopology();
358373
this.recoverConsumers();
359374
this.recoverPublishers();
360-
LOGGER.debug("Recovered topology");
375+
LOGGER.debug("Recovered topology of connection '{}'.", this.name());
361376
}
362377

363378
LOGGER.info("Recovered connection to {}", this.currentConnectionLabel());
@@ -631,6 +646,10 @@ String connectionNodename() {
631646
return this.connectionNodename;
632647
}
633648

649+
String name() {
650+
return this.name == null ? "<no-name>" : this.name;
651+
}
652+
634653
ConnectionUtils.AffinityContext affinity() {
635654
return this.affinity;
636655
}

src/test/java/com/rabbitmq/client/amqp/impl/RecoveryClusterTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.concurrent.atomic.AtomicBoolean;
3737
import java.util.concurrent.atomic.AtomicInteger;
3838
import java.util.concurrent.atomic.AtomicReference;
39+
import java.util.function.Consumer;
3940
import java.util.stream.IntStream;
4041
import org.junit.jupiter.api.*;
4142
import org.slf4j.Logger;
@@ -187,6 +188,13 @@ void clusterRestart() {
187188
System.out.println("Consumers:");
188189
consumerStates.forEach(
189190
p -> System.out.printf(" queue %s, is on member? %s%n", p.queue, p.isOnMember()));
191+
} catch (Throwable e) {
192+
LOGGER.info("Test failed with {}", e.getMessage());
193+
Consumer<AmqpConnection> log = c -> LOGGER.info("Connection {}: {}", c.name(), c.state());
194+
log.accept(this.connection);
195+
publisherStates.forEach(s -> log.accept(s.connection));
196+
consumerStates.forEach(s -> log.accept(s.connection));
197+
throw e;
190198
} finally {
191199
publisherStates.forEach(PublisherState::close);
192200
consumerStates.forEach(ConsumerState::close);

src/test/resources/logback-test.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<configuration>
22
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
33
<encoder>
4-
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
4+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0} - %msg%n</pattern>
55
</encoder>
66
</appender>
77

@@ -11,10 +11,10 @@
1111
<logger name="com.rabbitmq.client.amqp.impl.AmqpConnection" level="warn" />
1212
<logger name="com.rabbitmq.client.amqp.impl.AmqpConsumer" level="warn" />
1313
<logger name="com.rabbitmq.client.amqp.impl.AmqpManagement" level="warn" />
14-
<logger name="org.apache.qpid" level="warn" />
14+
<logger name="org.apache.qpid" level="error" />
1515
<logger name="com.rabbitmq.client.amqp.perf" level="info" />
16-
<logger name="com.rabbitmq.client.amqp.impl.ConnectionUtils" level="debug" />
17-
<logger name="com.rabbitmq.client.amqp.impl.AmqpConnection" level="warn" />
16+
<logger name="com.rabbitmq.client.amqp.impl.ConnectionUtils" level="warn" />
17+
<logger name="com.rabbitmq.client.amqp.impl.AmqpConnection" level="debug" />
1818
<logger name="com.rabbitmq.client.amqp.impl.RecoveryClusterTest" level="info" />
1919

2020
<root level="warn">

0 commit comments

Comments
 (0)