forked from rabbitmq/rabbitmq-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutorecoveringConnection.java
1202 lines (1078 loc) · 45.7 KB
/
AutorecoveringConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
//
// This software, the RabbitMQ Java client library, is triple-licensed under the
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
// info@rabbitmq.com.
package com.rabbitmq.client.impl.recovery;
import com.rabbitmq.client.*;
import com.rabbitmq.client.impl.AMQConnection;
import com.rabbitmq.client.impl.ConnectionParams;
import com.rabbitmq.client.impl.FrameHandlerFactory;
import com.rabbitmq.client.impl.NetworkConnection;
import com.rabbitmq.utility.Utility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Predicate;
/**
* Connection implementation that performs automatic recovery when
* connection shutdown is not initiated by the application (e.g. due to
* an I/O exception).
*
* Topology (exchanges, queues, bindings, and consumers) can be (and by default is) recovered
* as well, in this order:
*
* <ol>
* <li>Exchanges</li>
* <li>Queues</li>
* <li>Bindings (both queue and exchange-to-exchange)</li>
* <li>Consumers</li>
* </ol>
*
* @see com.rabbitmq.client.Connection
* @see com.rabbitmq.client.Recoverable
* @see com.rabbitmq.client.ConnectionFactory#setAutomaticRecoveryEnabled(boolean)
* @see com.rabbitmq.client.ConnectionFactory#setTopologyRecoveryEnabled(boolean)
* @since 3.3.0
*/
public class AutorecoveringConnection implements RecoverableConnection, NetworkConnection {
public static final Predicate<ShutdownSignalException> DEFAULT_CONNECTION_RECOVERY_TRIGGERING_CONDITION =
cause -> !cause.isInitiatedByApplication() || (cause.getCause() instanceof MissedHeartbeatException);
private static final Logger LOGGER = LoggerFactory.getLogger(AutorecoveringConnection.class);
private final RecoveryAwareAMQConnectionFactory cf;
private final Map<Integer, AutorecoveringChannel> channels;
private final ConnectionParams params;
private volatile RecoveryAwareAMQConnection delegate;
private final List<ShutdownListener> shutdownHooks = Collections.synchronizedList(new ArrayList<>());
private final List<RecoveryListener> recoveryListeners = Collections.synchronizedList(new ArrayList<>());
private final List<BlockedListener> blockedListeners = Collections.synchronizedList(new ArrayList<>());
// Records topology changes
private final Map<String, RecordedQueue> recordedQueues = Collections.synchronizedMap(new LinkedHashMap<>());
private final List<RecordedBinding> recordedBindings = Collections.synchronizedList(new ArrayList<>());
private final Map<String, RecordedExchange> recordedExchanges = Collections.synchronizedMap(new LinkedHashMap<>());
private final Map<String, RecordedConsumer> consumers = Collections.synchronizedMap(new LinkedHashMap<>());
private final List<ConsumerRecoveryListener> consumerRecoveryListeners = Collections.synchronizedList(new ArrayList<>());
private final List<QueueRecoveryListener> queueRecoveryListeners = Collections.synchronizedList(new ArrayList<>());
private final TopologyRecoveryFilter topologyRecoveryFilter;
// Used to block connection recovery attempts after close() is invoked.
private volatile boolean manuallyClosed = false;
// This lock guards the manuallyClosed flag and the delegate connection. Guarding these two ensures that a new connection can never
// be created after application code has initiated shutdown.
private final Object recoveryLock = new Object();
private final Predicate<ShutdownSignalException> connectionRecoveryTriggeringCondition;
private final RetryHandler retryHandler;
private final RecoveredQueueNameSupplier recoveredQueueNameSupplier;
public AutorecoveringConnection(ConnectionParams params, FrameHandlerFactory f, List<Address> addrs) {
this(params, f, new ListAddressResolver(addrs));
}
public AutorecoveringConnection(ConnectionParams params, FrameHandlerFactory f, AddressResolver addressResolver) {
this(params, f, addressResolver, new NoOpMetricsCollector());
}
public AutorecoveringConnection(ConnectionParams params, FrameHandlerFactory f, AddressResolver addressResolver, MetricsCollector metricsCollector) {
this.cf = new RecoveryAwareAMQConnectionFactory(params, f, addressResolver, metricsCollector);
this.params = params;
this.connectionRecoveryTriggeringCondition = params.getConnectionRecoveryTriggeringCondition() == null ?
DEFAULT_CONNECTION_RECOVERY_TRIGGERING_CONDITION : params.getConnectionRecoveryTriggeringCondition();
setupErrorOnWriteListenerForPotentialRecovery();
this.channels = new ConcurrentHashMap<>();
this.topologyRecoveryFilter = params.getTopologyRecoveryFilter() == null ?
letAllPassFilter() : params.getTopologyRecoveryFilter();
this.retryHandler = params.getTopologyRecoveryRetryHandler();
this.recoveredQueueNameSupplier = params.getRecoveredQueueNameSupplier() == null ?
RecordedQueue.DEFAULT_QUEUE_NAME_SUPPLIER : params.getRecoveredQueueNameSupplier();
}
private void setupErrorOnWriteListenerForPotentialRecovery() {
final ThreadFactory threadFactory = this.params.getThreadFactory();
final Lock errorOnWriteLock = new ReentrantLock();
this.params.setErrorOnWriteListener((connection, exception) -> {
// this is called for any write error
// we should trigger the error handling and the recovery only once
if (errorOnWriteLock.tryLock()) {
try {
Thread recoveryThread = threadFactory.newThread(() -> {
AMQConnection c = (AMQConnection) connection;
c.handleIoError(exception);
});
recoveryThread.setName("RabbitMQ Error On Write Thread");
recoveryThread.start();
} finally {
errorOnWriteLock.unlock();
}
}
throw exception;
});
}
private static TopologyRecoveryFilter letAllPassFilter() {
return new TopologyRecoveryFilter() {};
}
/**
* Private API.
* @throws IOException
* @see com.rabbitmq.client.ConnectionFactory#newConnection(java.util.concurrent.ExecutorService)
*/
public void init() throws IOException, TimeoutException {
this.delegate = this.cf.newConnection();
this.addAutomaticRecoveryListener(delegate);
}
/**
* @see com.rabbitmq.client.Connection#createChannel()
*/
@Override
public Channel createChannel() throws IOException {
RecoveryAwareChannelN ch = (RecoveryAwareChannelN) delegate.createChannel();
// No Sonar: the channel could be null
if (ch == null) { //NOSONAR
return null;
} else {
return this.wrapChannel(ch);
}
}
/**
* @see com.rabbitmq.client.Connection#createChannel(int)
*/
@Override
public Channel createChannel(int channelNumber) throws IOException {
RecoveryAwareChannelN ch = (RecoveryAwareChannelN) delegate.createChannel(channelNumber);
// No Sonar: the channel could be null
if (ch == null) { //NOSONAR
return null;
} else {
return this.wrapChannel(ch);
}
}
/**
* Creates a recovering channel from a regular channel and registers it.
* If the regular channel cannot be created (e.g. too many channels are open
* already), returns null.
*
* @param delegateChannel Channel to wrap.
* @return Recovering channel.
*/
private Channel wrapChannel(RecoveryAwareChannelN delegateChannel) {
if (delegateChannel == null) {
return null;
} else {
final AutorecoveringChannel channel = new AutorecoveringChannel(this, delegateChannel);
this.registerChannel(channel);
return channel;
}
}
void registerChannel(AutorecoveringChannel channel) {
this.channels.put(channel.getChannelNumber(), channel);
}
void unregisterChannel(AutorecoveringChannel channel) {
this.channels.remove(channel.getChannelNumber());
}
/**
* @see com.rabbitmq.client.Connection#getServerProperties()
*/
@Override
public Map<String, Object> getServerProperties() {
return delegate.getServerProperties();
}
/**
* @see com.rabbitmq.client.Connection#getClientProperties()
*/
@Override
public Map<String, Object> getClientProperties() {
return delegate.getClientProperties();
}
/**
* @see com.rabbitmq.client.Connection#getClientProvidedName()
* @see ConnectionFactory#newConnection(Address[], String)
* @see ConnectionFactory#newConnection(ExecutorService, Address[], String)
*/
@Override
public String getClientProvidedName() {
return delegate.getClientProvidedName();
}
/**
* @see com.rabbitmq.client.Connection#getFrameMax()
*/
@Override
public int getFrameMax() {
return delegate.getFrameMax();
}
/**
* @see com.rabbitmq.client.Connection#getHeartbeat()
*/
@Override
public int getHeartbeat() {
return delegate.getHeartbeat();
}
/**
* @see com.rabbitmq.client.Connection#getChannelMax()
*/
@Override
public int getChannelMax() {
return delegate.getChannelMax();
}
/**
* @see com.rabbitmq.client.Connection#isOpen()
*/
@Override
public boolean isOpen() {
return delegate.isOpen();
}
/**
* @see com.rabbitmq.client.Connection#close()
*/
@Override
public void close() throws IOException {
synchronized(recoveryLock) {
this.manuallyClosed = true;
}
delegate.close();
}
/**
* @see Connection#close(int)
*/
@Override
public void close(int timeout) throws IOException {
synchronized(recoveryLock) {
this.manuallyClosed = true;
}
delegate.close(timeout);
}
/**
* @see Connection#close(int, String, int)
*/
@Override
public void close(int closeCode, String closeMessage, int timeout) throws IOException {
synchronized(recoveryLock) {
this.manuallyClosed = true;
}
delegate.close(closeCode, closeMessage, timeout);
}
/**
* @see com.rabbitmq.client.Connection#abort()
*/
@Override
public void abort() {
synchronized(recoveryLock) {
this.manuallyClosed = true;
}
delegate.abort();
}
/**
* @see Connection#abort(int, String, int)
*/
@Override
public void abort(int closeCode, String closeMessage, int timeout) {
synchronized(recoveryLock) {
this.manuallyClosed = true;
}
delegate.abort(closeCode, closeMessage, timeout);
}
/**
* @see Connection#abort(int, String)
*/
@Override
public void abort(int closeCode, String closeMessage) {
synchronized(recoveryLock) {
this.manuallyClosed = true;
}
delegate.abort(closeCode, closeMessage);
}
/**
* @see Connection#abort(int)
*/
@Override
public void abort(int timeout) {
synchronized(recoveryLock) {
this.manuallyClosed = true;
}
delegate.abort(timeout);
}
/**
* Not supposed to be used outside of automated tests.
*/
public AMQConnection getDelegate() {
return delegate;
}
/**
* @see com.rabbitmq.client.Connection#getCloseReason()
*/
@Override
public ShutdownSignalException getCloseReason() {
return delegate.getCloseReason();
}
/**
* @see com.rabbitmq.client.ShutdownNotifier#addShutdownListener(com.rabbitmq.client.ShutdownListener)
*/
@Override
public void addBlockedListener(BlockedListener listener) {
this.blockedListeners.add(listener);
delegate.addBlockedListener(listener);
}
@Override
public BlockedListener addBlockedListener(BlockedCallback blockedCallback, UnblockedCallback unblockedCallback) {
BlockedListener blockedListener = new BlockedListener() {
@Override
public void handleBlocked(String reason) throws IOException {
blockedCallback.handle(reason);
}
@Override
public void handleUnblocked() throws IOException {
unblockedCallback.handle();
}
};
this.addBlockedListener(blockedListener);
return blockedListener;
}
/**
* @see Connection#removeBlockedListener(com.rabbitmq.client.BlockedListener)
*/
@Override
public boolean removeBlockedListener(BlockedListener listener) {
this.blockedListeners.remove(listener);
return delegate.removeBlockedListener(listener);
}
/**
* @see com.rabbitmq.client.Connection#clearBlockedListeners()
*/
@Override
public void clearBlockedListeners() {
this.blockedListeners.clear();
delegate.clearBlockedListeners();
}
/**
* @see com.rabbitmq.client.Connection#close(int, String)
*/
@Override
public void close(int closeCode, String closeMessage) throws IOException {
synchronized(recoveryLock) {
this.manuallyClosed = true;
}
delegate.close(closeCode, closeMessage);
}
/**
* @see Connection#addShutdownListener(com.rabbitmq.client.ShutdownListener)
*/
@Override
public void addShutdownListener(ShutdownListener listener) {
this.shutdownHooks.add(listener);
delegate.addShutdownListener(listener);
}
/**
* @see com.rabbitmq.client.ShutdownNotifier#removeShutdownListener(com.rabbitmq.client.ShutdownListener)
*/
@Override
public void removeShutdownListener(ShutdownListener listener) {
this.shutdownHooks.remove(listener);
delegate.removeShutdownListener(listener);
}
/**
* @see com.rabbitmq.client.ShutdownNotifier#notifyListeners()
*/
@Override
public void notifyListeners() {
delegate.notifyListeners();
}
/**
* Adds the recovery listener
* @param listener {@link com.rabbitmq.client.RecoveryListener} to execute after this connection recovers from network failure
*/
@Override
public void addRecoveryListener(RecoveryListener listener) {
this.recoveryListeners.add(listener);
}
/**
* Removes the recovery listener
* @param listener {@link com.rabbitmq.client.RecoveryListener} to remove
*/
@Override
public void removeRecoveryListener(RecoveryListener listener) {
this.recoveryListeners.remove(listener);
}
/**
* @see com.rabbitmq.client.impl.AMQConnection#getExceptionHandler()
*/
@Override
public ExceptionHandler getExceptionHandler() {
return this.delegate.getExceptionHandler();
}
/**
* @see com.rabbitmq.client.Connection#getPort()
*/
@Override
public int getPort() {
return delegate.getPort();
}
/**
* @see com.rabbitmq.client.Connection#getAddress()
*/
@Override
public InetAddress getAddress() {
return delegate.getAddress();
}
/**
* @return client socket address
*/
@Override
public InetAddress getLocalAddress() {
return this.delegate.getLocalAddress();
}
/**
* @return client socket port
*/
@Override
public int getLocalPort() {
return this.delegate.getLocalPort();
}
//
// Recovery
//
private void addAutomaticRecoveryListener(final RecoveryAwareAMQConnection newConn) {
final AutorecoveringConnection c = this;
// this listener will run after shutdown listeners,
// see https://github.com/rabbitmq/rabbitmq-java-client/issues/135
RecoveryCanBeginListener starter = cause -> {
try {
if (shouldTriggerConnectionRecovery(cause)) {
c.beginAutomaticRecovery();
}
} catch (Exception e) {
newConn.getExceptionHandler().handleConnectionRecoveryException(c, e);
}
};
synchronized (this) {
newConn.addRecoveryCanBeginListener(starter);
}
}
protected boolean shouldTriggerConnectionRecovery(ShutdownSignalException cause) {
return connectionRecoveryTriggeringCondition.test(cause);
}
/**
* Not part of the public API. Mean to be used by JVM RabbitMQ clients that build on
* top of the Java client and need to be notified when server-named queue name changes
* after recovery.
*
* @param listener listener that observes queue name changes after recovery
*/
public void addQueueRecoveryListener(QueueRecoveryListener listener) {
this.queueRecoveryListeners.add(listener);
}
/**
* @see com.rabbitmq.client.impl.recovery.AutorecoveringConnection#addQueueRecoveryListener
* @param listener listener to be removed
*/
public void removeQueueRecoveryListener(QueueRecoveryListener listener) {
this.queueRecoveryListeners.remove(listener);
}
/**
* Not part of the public API. Mean to be used by JVM RabbitMQ clients that build on
* top of the Java client and need to be notified when consumer tag changes
* after recovery.
*
* @param listener listener that observes consumer tag changes after recovery
*/
public void addConsumerRecoveryListener(ConsumerRecoveryListener listener) {
this.consumerRecoveryListeners.add(listener);
}
/**
* @see com.rabbitmq.client.impl.recovery.AutorecoveringConnection#addConsumerRecoveryListener(ConsumerRecoveryListener)
* @param listener listener to be removed
*/
public void removeConsumerRecoveryListener(ConsumerRecoveryListener listener) {
this.consumerRecoveryListeners.remove(listener);
}
RecoveredQueueNameSupplier getRecoveredQueueNameSupplier() {
return this.recoveredQueueNameSupplier;
}
private synchronized void beginAutomaticRecovery() throws InterruptedException {
final long delay = this.params.getRecoveryDelayHandler().getDelay(0);
if (delay > 0) {
this.wait(delay);
}
this.notifyRecoveryListenersStarted();
final RecoveryAwareAMQConnection newConn = this.recoverConnection();
if (newConn == null) {
return;
}
LOGGER.debug("Connection {} has recovered", newConn);
this.addAutomaticRecoveryListener(newConn);
this.recoverShutdownListeners(newConn);
this.recoverBlockedListeners(newConn);
this.recoverChannels(newConn);
// don't assign new delegate connection until channel recovery is complete
this.delegate = newConn;
if (this.params.isTopologyRecoveryEnabled()) {
notifyTopologyRecoveryListenersStarted();
recoverTopology(params.getTopologyRecoveryExecutor());
}
this.notifyRecoveryListenersComplete();
}
private void recoverShutdownListeners(final RecoveryAwareAMQConnection newConn) {
for (ShutdownListener sh : Utility.copy(this.shutdownHooks)) {
newConn.addShutdownListener(sh);
}
}
private void recoverBlockedListeners(final RecoveryAwareAMQConnection newConn) {
for (BlockedListener bl : Utility.copy(this.blockedListeners)) {
newConn.addBlockedListener(bl);
}
}
// Returns new connection if the connection was recovered,
// null if application initiated shutdown while attempting recovery.
private RecoveryAwareAMQConnection recoverConnection() throws InterruptedException {
int attempts = 0;
while (!manuallyClosed) {
try {
attempts++;
// No Sonar: no need to close this resource because we're the one that creates it
// and hands it over to the user
RecoveryAwareAMQConnection newConn = this.cf.newConnection(); //NOSONAR
synchronized(recoveryLock) {
if (!manuallyClosed) {
// This is the standard case.
return newConn;
}
}
// This is the once in a blue moon case.
// Application code just called close as the connection
// was being re-established. So we attempt to close the newly created connection.
newConn.abort();
return null;
} catch (Exception e) {
Thread.sleep(this.params.getRecoveryDelayHandler().getDelay(attempts));
this.getExceptionHandler().handleConnectionRecoveryException(this, e);
}
}
return null;
}
private void recoverChannels(final RecoveryAwareAMQConnection newConn) {
for (AutorecoveringChannel ch : this.channels.values()) {
try {
ch.automaticallyRecover(this, newConn);
LOGGER.debug("Channel {} has recovered", ch);
} catch (Throwable t) {
newConn.getExceptionHandler().handleChannelRecoveryException(ch, t);
}
}
}
public void recoverChannel(AutorecoveringChannel channel) throws IOException {
channel.automaticallyRecover(this, this.delegate);
}
private void notifyRecoveryListenersComplete() {
for (RecoveryListener f : Utility.copy(this.recoveryListeners)) {
f.handleRecovery(this);
}
}
private void notifyRecoveryListenersStarted() {
for (RecoveryListener f : Utility.copy(this.recoveryListeners)) {
f.handleRecoveryStarted(this);
}
}
private void notifyTopologyRecoveryListenersStarted() {
for (RecoveryListener f : Utility.copy(this.recoveryListeners)) {
f.handleTopologyRecoveryStarted(this);
}
}
/**
* Recover a closed channel and all topology (i.e. RecordedEntities) associated to it.
* Any errors will be sent to the {@link #getExceptionHandler()}.
* @param channel channel to recover
* @throws IllegalArgumentException if this channel is not owned by this connection
*/
public void recoverChannelAndTopology(final AutorecoveringChannel channel) {
if (!channels.containsValue(channel)) {
throw new IllegalArgumentException("This channel is not owned by this connection");
}
try {
LOGGER.debug("Recovering channel={}", channel);
recoverChannel(channel);
LOGGER.debug("Recovered channel={}. Now recovering its topology", channel);
Utility.copy(recordedExchanges).values().stream()
.filter(e -> e.getChannel() == channel)
.forEach(e -> recoverExchange(e, false));
Utility.copy(recordedQueues).values().stream()
.filter(q -> q.getChannel() == channel)
.forEach(q -> recoverQueue(q.getName(), q, false));
Utility.copy(recordedBindings).stream()
.filter(b -> b.getChannel() == channel)
.forEach(b -> recoverBinding(b, false));
Utility.copy(consumers).values().stream()
.filter(c -> c.getChannel() == channel)
.forEach(c -> recoverConsumer(c.getConsumerTag(), c, false));
LOGGER.debug("Recovered topology for channel={}", channel);
} catch (Exception e) {
getExceptionHandler().handleChannelRecoveryException(channel, e);
}
}
private void recoverTopology(final ExecutorService executor) {
// The recovery sequence is the following:
// 1. Recover exchanges
// 2. Recover queues
// 3. Recover bindings
// 4. Recover consumers
if (executor == null) {
// recover entities in serial on the main connection thread
for (final RecordedExchange exchange : Utility.copy(recordedExchanges).values()) {
recoverExchange(exchange, true);
}
for (final Map.Entry<String, RecordedQueue> entry : Utility.copy(recordedQueues).entrySet()) {
recoverQueue(entry.getKey(), entry.getValue(), true);
}
for (final RecordedBinding b : Utility.copy(recordedBindings)) {
recoverBinding(b, true);
}
for (final Map.Entry<String, RecordedConsumer> entry : Utility.copy(consumers).entrySet()) {
recoverConsumer(entry.getKey(), entry.getValue(), true);
}
} else {
// Support recovering entities in parallel for connections that have a lot of queues, bindings, & consumers
// A channel is single threaded, so group things by channel and recover 1 entity at a time per channel
// We also need to recover 1 type of entity at a time in case channel1 has a binding to a queue that is currently owned and being recovered by channel2 for example
// Note: invokeAll will block until all callables are completed and all returned futures will be complete
try {
recoverEntitiesAsynchronously(executor, Utility.copy(recordedExchanges).values());
recoverEntitiesAsynchronously(executor, Utility.copy(recordedQueues).values());
recoverEntitiesAsynchronously(executor, Utility.copy(recordedBindings));
recoverEntitiesAsynchronously(executor, Utility.copy(consumers).values());
} catch (final Exception cause) {
final String message = "Caught an exception while recovering topology: " + cause.getMessage();
final TopologyRecoveryException e = new TopologyRecoveryException(message, cause);
getExceptionHandler().handleTopologyRecoveryException(delegate, null, e);
}
}
}
public void recoverExchange(RecordedExchange x, boolean retry) {
// recorded exchanges are guaranteed to be non-predefined (we filter out predefined ones in exchangeDeclare). MK.
try {
if (topologyRecoveryFilter.filterExchange(x)) {
if (retry) {
final RecordedExchange entity = x;
x = (RecordedExchange) wrapRetryIfNecessary(x, () -> {
entity.recover();
return null;
}).getRecordedEntity();
} else {
x.recover();
}
LOGGER.debug("{} has recovered", x);
}
} catch (Exception cause) {
final String message = "Caught an exception while recovering exchange " + x.getName() +
": " + cause.getMessage();
TopologyRecoveryException e = new TopologyRecoveryException(message, cause, x);
this.getExceptionHandler().handleTopologyRecoveryException(delegate, x.getDelegateChannel(), e);
}
}
/**
* Recover the queue. Any exceptions during recovery will be delivered to the connection's {@link ExceptionHandler}.
* @param oldName queue name
* @param q recorded queue
* @param retry whether to retry the recovery if an error occurs and a RetryHandler was configured on the connection
*/
public void recoverQueue(final String oldName, RecordedQueue q, boolean retry) {
try {
internalRecoverQueue(oldName, q, retry);
} catch (Exception cause) {
final String message = "Caught an exception while recovering queue " + oldName +
": " + cause.getMessage();
TopologyRecoveryException e = new TopologyRecoveryException(message, cause, q);
this.getExceptionHandler().handleTopologyRecoveryException(delegate, q.getDelegateChannel(), e);
}
}
/**
* Recover the queue. Errors are not retried and not delivered to the connection's {@link ExceptionHandler}
* @param oldName queue name
* @param q recorded queue
* @throws Exception if an error occurs recovering the queue
*/
void recoverQueue(final String oldName, RecordedQueue q) throws Exception {
internalRecoverQueue(oldName, q, false);
}
private void internalRecoverQueue(final String oldName, RecordedQueue q, boolean retry) throws Exception {
if (topologyRecoveryFilter.filterQueue(q)) {
LOGGER.debug("Recovering {}", q);
if (retry) {
final RecordedQueue entity = q;
q = (RecordedQueue) wrapRetryIfNecessary(q, () -> {
entity.recover();
return null;
}).getRecordedEntity();
} else {
q.recover();
}
String newName = q.getName();
if (!oldName.equals(newName)) {
// make sure queues are re-added with
// their new names, if applicable. MK.
propagateQueueNameChangeToBindings(oldName, newName);
propagateQueueNameChangeToConsumers(oldName, newName);
synchronized (this.recordedQueues) {
// bug26552:
// remove old name after we've updated the bindings and consumers,
deleteRecordedQueue(oldName);
this.recordedQueues.put(newName, q);
}
}
for (QueueRecoveryListener qrl : Utility.copy(this.queueRecoveryListeners)) {
qrl.queueRecovered(oldName, newName);
}
LOGGER.debug("{} has recovered", q);
}
}
public void recoverBinding(RecordedBinding b, boolean retry) {
try {
if (this.topologyRecoveryFilter.filterBinding(b)) {
if (retry) {
final RecordedBinding entity = b;
b = (RecordedBinding) wrapRetryIfNecessary(b, () -> {
entity.recover();
return null;
}).getRecordedEntity();
} else {
b.recover();
}
LOGGER.debug("{} has recovered", b);
}
} catch (Exception cause) {
String message = "Caught an exception while recovering binding between " + b.getSource() +
" and " + b.getDestination() + ": " + cause.getMessage();
TopologyRecoveryException e = new TopologyRecoveryException(message, cause, b);
this.getExceptionHandler().handleTopologyRecoveryException(delegate, b.getDelegateChannel(), e);
}
}
/**
* Recover the consumer. Any exceptions during recovery will be delivered to the connection's {@link ExceptionHandler}.
* @param tag consumer tag
* @param consumer recorded consumer
* @param retry whether to retry the recovery if an error occurs and a RetryHandler was configured on the connection
*/
public void recoverConsumer(final String tag, RecordedConsumer consumer, boolean retry) {
try {
internalRecoverConsumer(tag, consumer, retry);
} catch (Exception cause) {
final String message = "Caught an exception while recovering consumer " + tag +
": " + cause.getMessage();
TopologyRecoveryException e = new TopologyRecoveryException(message, cause, consumer);
this.getExceptionHandler().handleTopologyRecoveryException(delegate, consumer.getDelegateChannel(), e);
}
}
/**
* Recover the consumer. Errors are not retried and not delivered to the connection's {@link ExceptionHandler}
* @param tag consumer tag
* @param consumer recorded consumer
* @throws Exception if an error occurs recovering the consumer
*/
void recoverConsumer(final String tag, RecordedConsumer consumer) throws Exception {
internalRecoverConsumer(tag, consumer, false);
}
private void internalRecoverConsumer(final String tag, RecordedConsumer consumer, boolean retry) throws Exception {
if (this.topologyRecoveryFilter.filterConsumer(consumer)) {
LOGGER.debug("Recovering {}", consumer);
String newTag = null;
if (retry) {
final RecordedConsumer entity = consumer;
RetryResult retryResult = wrapRetryIfNecessary(consumer, entity::recover);
consumer = (RecordedConsumer) retryResult.getRecordedEntity();
newTag = (String) retryResult.getResult();
} else {
newTag = consumer.recover();
}
// make sure server-generated tags are re-added. MK.
if(tag != null && !tag.equals(newTag)) {
synchronized (this.consumers) {
this.consumers.remove(tag);
this.consumers.put(newTag, consumer);
}
consumer.getChannel().updateConsumerTag(tag, newTag);
}
for (ConsumerRecoveryListener crl : Utility.copy(this.consumerRecoveryListeners)) {
crl.consumerRecovered(tag, newTag);
}
LOGGER.debug("{} has recovered", consumer);
}
}
private <T> RetryResult wrapRetryIfNecessary(RecordedEntity entity, Callable<T> recoveryAction) throws Exception {
if (this.retryHandler == null) {
T result = recoveryAction.call();
return new RetryResult(entity, result);
} else {
try {
T result = recoveryAction.call();
return new RetryResult(entity, result);
} catch (Exception e) {
RetryContext retryContext = new RetryContext(entity, e, this);
RetryResult retryResult;
if (entity instanceof RecordedQueue) {
retryResult = this.retryHandler.retryQueueRecovery(retryContext);
} else if (entity instanceof RecordedExchange) {
retryResult = this.retryHandler.retryExchangeRecovery(retryContext);
} else if (entity instanceof RecordedBinding) {
retryResult = this.retryHandler.retryBindingRecovery(retryContext);
} else if (entity instanceof RecordedConsumer) {
retryResult = this.retryHandler.retryConsumerRecovery(retryContext);
} else {
throw new IllegalArgumentException("Unknown type of recorded entity: " + entity);
}
return retryResult;
}
}
}
private void propagateQueueNameChangeToBindings(String oldName, String newName) {
for (RecordedBinding b : Utility.copy(this.recordedBindings)) {
if (b.getDestination().equals(oldName)) {
b.setDestination(newName);
}
}
}
private void propagateQueueNameChangeToConsumers(String oldName, String newName) {
for (RecordedConsumer c : Utility.copy(this.consumers).values()) {
if (c.getQueue().equals(oldName)) {
c.setQueue(newName);
}
}
}
private void recoverEntitiesAsynchronously(ExecutorService executor, Collection<? extends RecordedEntity> recordedEntities) throws InterruptedException {
List<Future<Object>> tasks = executor.invokeAll(groupEntitiesByChannel(recordedEntities));
for (Future<Object> task : tasks) {
if (!task.isDone()) {
LOGGER.warn("Recovery task should be done {}", task);
} else {
try {
task.get(1, TimeUnit.MILLISECONDS);
} catch (Exception e) {
LOGGER.warn("Recovery task is done but returned an exception", e);
}
}
}
}
private <E extends RecordedEntity> List<Callable<Object>> groupEntitiesByChannel(final Collection<E> entities) {
// map entities by channel
final Map<AutorecoveringChannel, List<E>> map = new LinkedHashMap<>();
for (final E entity : entities) {
final AutorecoveringChannel channel = entity.getChannel();
map.computeIfAbsent(channel, c -> new ArrayList<>()).add(entity);
}
// now create a runnable per channel
final List<Callable<Object>> callables = new ArrayList<>();
for (final List<E> entityList : map.values()) {
callables.add(Executors.callable(() -> {
for (final E entity : entityList) {
if (entity instanceof RecordedExchange) {
recoverExchange((RecordedExchange)entity, true);
} else if (entity instanceof RecordedQueue) {
final RecordedQueue q = (RecordedQueue) entity;
recoverQueue(q.getName(), q, true);
} else if (entity instanceof RecordedBinding) {
recoverBinding((RecordedBinding) entity, true);
} else if (entity instanceof RecordedConsumer) {
final RecordedConsumer c = (RecordedConsumer) entity;
recoverConsumer(c.getConsumerTag(), c, true);
}
}
}));
}
return callables;
}
void recordQueueBinding(AutorecoveringChannel ch,
String queue,
String exchange,
String routingKey,
Map<String, Object> arguments) {
RecordedBinding binding = new RecordedQueueBinding(ch).
source(exchange).