-
Notifications
You must be signed in to change notification settings - Fork 33
/
RaptorProtocol.java
4966 lines (4336 loc) · 209 KB
/
RaptorProtocol.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) 2000, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2020 Payara Services Ltd.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.jmq.jmsserver.multibroker.raptor;
import java.util.*;
import java.io.*;
import java.nio.*;
import com.sun.messaging.jmq.jmsserver.Globals;
import com.sun.messaging.jmq.jmsserver.Broker;
import com.sun.messaging.jmq.jmsservice.BrokerEvent;
import com.sun.messaging.jmq.util.log.*;
import com.sun.messaging.jmq.io.*;
import com.sun.messaging.jmq.util.*;
import com.sun.messaging.jmq.util.admin.MessageType;
import com.sun.messaging.jmq.jmsserver.FaultInjection;
import com.sun.messaging.jmq.jmsserver.BrokerStateHandler;
import com.sun.messaging.jmq.jmsserver.core.*;
import com.sun.messaging.jmq.jmsserver.resources.*;
import com.sun.messaging.jmq.jmsserver.util.*;
import com.sun.messaging.jmq.jmsserver.data.TransactionUID;
import com.sun.messaging.jmq.jmsserver.data.TransactionList;
import com.sun.messaging.jmq.jmsserver.data.TransactionState;
import com.sun.messaging.jmq.jmsserver.data.TransactionBroker;
import com.sun.messaging.jmq.jmsserver.persist.api.Store;
import com.sun.messaging.jmq.jmsserver.persist.api.MigratableStore;
import com.sun.messaging.jmq.jmsserver.persist.api.ReplicableStore;
import com.sun.messaging.jmq.jmsserver.persist.api.PartitionListener;
import com.sun.messaging.jmq.jmsserver.persist.api.StoreSessionReaperListener;
import com.sun.messaging.jmq.jmsserver.cluster.api.*;
import com.sun.messaging.jmq.jmsserver.cluster.manager.ClusterManagerImpl;
import com.sun.messaging.jmq.jmsserver.data.handlers.admin.ExclusiveRequest;
import com.sun.messaging.jmq.jmsserver.service.ConnectionUID;
import com.sun.messaging.jmq.jmsserver.service.ServiceRestriction;
import com.sun.messaging.jmq.jmsserver.multibroker.MessageBusCallback;
import com.sun.messaging.jmq.jmsserver.multibroker.ClusterBrokerInfoReply;
import com.sun.messaging.jmq.jmsserver.multibroker.HandshakeInProgressException;
import com.sun.messaging.jmq.jmsserver.multibroker.Protocol;
import com.sun.messaging.jmq.jmsserver.multibroker.Cluster;
import com.sun.messaging.jmq.jmsserver.multibroker.ClusterGlobals;
import com.sun.messaging.jmq.jmsserver.multibroker.ChangeRecord;
import com.sun.messaging.jmq.jmsserver.multibroker.DestinationUpdateChangeRecord;
import com.sun.messaging.jmq.jmsserver.multibroker.InterestUpdateChangeRecord;
import com.sun.messaging.jmq.jmsserver.multibroker.CallbackDispatcher;
import com.sun.messaging.jmq.jmsserver.multibroker.BrokerInfo;
import com.sun.messaging.jmq.jmsserver.multibroker.raptor.handlers.*;
import com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo;
public class RaptorProtocol implements Protocol, PartitionListener, StoreSessionReaperListener {
protected static final Logger logger = Globals.getLogger();
protected static final BrokerResources br = Globals.getBrokerResources();
private static boolean DEBUG = false;
protected static final boolean DEBUG_CLUSTER_ALL = Globals.getConfig().getBooleanProperty(Globals.IMQ + ".cluster.debug.all") || DEBUG;
protected static final boolean DEBUG_CLUSTER_CONN = Globals.getConfig().getBooleanProperty(Globals.IMQ + ".cluster.debug.conn") || DEBUG_CLUSTER_ALL;
protected static final boolean DEBUG_CLUSTER_TXN = Globals.getConfig().getBooleanProperty(Globals.IMQ + ".cluster.debug.txn") || DEBUG_CLUSTER_ALL;
protected static final boolean DEBUG_CLUSTER_MSG = Globals.getConfig().getBooleanProperty(Globals.IMQ + ".cluster.debug.msg") || DEBUG_CLUSTER_TXN
|| DEBUG_CLUSTER_ALL;
protected static final boolean DEBUG_CLUSTER_TAKEOVER = Globals.getConfig().getBooleanProperty(Globals.IMQ + ".cluster.debug.takeover")
|| DEBUG_CLUSTER_ALL;
protected MessageBusCallback cb = null;
protected Cluster c = null;
protected BrokerAddress selfAddress = null;
protected BrokerInfo selfInfo = null;
protected CallbackDispatcher cbDispatcher = null;
private static final int version = ProtocolGlobals.getCurrentVersion();
private boolean shutdown = false;
protected Map brokerList = null;
private Map<TakingoverEntry, TakingoverEntry> takingoverBrokers = null;
protected HashMap resTable = null;
protected Random random = null;
protected boolean configSyncComplete = false;
private Map eventLogWaiters = null;
protected Object cfgSrvWaitObject = null;
protected int cfgSrvRequestCount = 0;
protected boolean cfgSrvRequestErr = false;
protected Store store = null;
protected GPacketHandler[] handlers;
protected GPacketHandler unknownPacketHandler;
protected boolean flowStopped = false;
private int takeoverPendingReplyTimeout = 60; // XXX seconds
private Map myPretakeovers = null;
private ThreadGroup takeoverCleanupTG = new MQThreadGroup("TakeoverCleanup", logger, br.getKString(br.W_UNCAUGHT_EXCEPTION_IN_THREAD));
private FaultInjection fi = null;
private HashMap ackCounts = new HashMap(); // for msg remote ack fi
private ReplyTracker ackackTracker = null;
private ReplyTracker broadcastAnyOKReplyTracker = null;
private ReplyTracker takeoverPendingReplyTracker = null;
private ReplyTracker newMasterBrokerReplyTracker = null;
private ReplyTracker takeoverMEReplyTracker = null;
protected int changeMasterBrokerWaitTimeout = Globals.getConfig().getIntProperty(Globals.IMQ + ".cluster.changeMasterBrokerWaitTimeout",
ProtocolGlobals.DEFAULT_WAIT_REPLY_TIMEOUT);
private Object masterBrokerBlockedLock = new Object();
private Object configOpLock = new Object();
private int configOpInProgressCount = 0;
private boolean masterBrokerBlocked = false;
private Object newMasterBrokerLock = new Object();
private String newMasterBrokerPreparedUUID = null;
private BrokerAddress newMasterBrokerPreparedSender = null;
private DestinationList DL = Globals.getDestinationList();
private Map<String, ChangeRecord> inDSubToBrokerMap = Collections.synchronizedMap(new HashMap<>());
public RaptorProtocol(MessageBusCallback cb, Cluster c, BrokerAddress myaddress, BrokerInfo myinfo) throws BrokerException {
if (DEBUG_CLUSTER_ALL) {
DEBUG = DEBUG_CLUSTER_ALL;
}
if (DEBUG) {
logger.log(logger.INFO, "Initializing RaptorProtocol");
}
this.cb = cb;
this.c = c;
this.selfAddress = myaddress;
this.selfInfo = myinfo;
this.cbDispatcher = new CallbackDispatcher(cb);
store = Globals.getStore();
resTable = new HashMap();
random = new Random();
brokerList = Collections.synchronizedMap(new LinkedHashMap());
takingoverBrokers = Collections.synchronizedMap(new LinkedHashMap<>());
myPretakeovers = Collections.synchronizedMap(new LinkedHashMap());
cfgSrvWaitObject = new Object();
eventLogWaiters = Collections.synchronizedMap(new LinkedHashMap());
initHandlers();
String backupFileName = Globals.getConfig().getProperty(Globals.IMQ + ".cluster.masterbroker.backup");
String restoreFileName = Globals.getConfig().getProperty(Globals.IMQ + ".cluster.masterbroker.restore");
if (backupFileName != null) {
configServerBackup(backupFileName);
}
if (restoreFileName != null) {
configServerRestore(restoreFileName);
}
ackackTracker = new ReplyTracker();
newMasterBrokerReplyTracker = new ReplyTracker();
takeoverMEReplyTracker = new ReplyTracker();
takeoverPendingReplyTracker = new ReplyTracker();
broadcastAnyOKReplyTracker = new ReplyTracker();
fi = FaultInjection.getInjection();
}
private void addHandler(int id, GPacketHandler h) {
if (id > ProtocolGlobals.G_MAX_PACKET_TYPE) {
throw new ArrayIndexOutOfBoundsException("Bad ProtocolHandler");
}
handlers[id] = h;
}
private void initHandlers() {
if (DEBUG) {
logger.log(logger.INFO, "Initializing RaptorProtocol handlers");
}
handlers = new GPacketHandler[ProtocolGlobals.G_MAX_PACKET_TYPE + 1];
GPacketHandler h;
h = new MessageDataHandler(this);
addHandler(ProtocolGlobals.G_MESSAGE_DATA, h);
addHandler(ProtocolGlobals.G_MESSAGE_DATA_REPLY, h);
h = new MessageAckHandler(this);
addHandler(ProtocolGlobals.G_MESSAGE_ACK, h);
addHandler(ProtocolGlobals.G_MESSAGE_ACK_REPLY, h);
h = new NewInterestHandler(this);
addHandler(ProtocolGlobals.G_NEW_INTEREST, h);
addHandler(ProtocolGlobals.G_NEW_INTEREST_REPLY, h);
addHandler(ProtocolGlobals.G_DURABLE_ATTACH, h);
addHandler(ProtocolGlobals.G_DURABLE_ATTACH_REPLY, h);
h = new RemDurableHandler(this);
addHandler(ProtocolGlobals.G_REM_DURABLE_INTEREST, h);
addHandler(ProtocolGlobals.G_REM_DURABLE_INTEREST_REPLY, h);
h = new InterestUpdateHandler(this);
addHandler(ProtocolGlobals.G_INTEREST_UPDATE, h);
addHandler(ProtocolGlobals.G_INTEREST_UPDATE_REPLY, h);
h = new LockHandler(this);
addHandler(ProtocolGlobals.G_LOCK, h);
addHandler(ProtocolGlobals.G_LOCK_REPLY, h);
h = new DestinationUpdateHandler(this);
addHandler(ProtocolGlobals.G_UPDATE_DESTINATION, h);
addHandler(ProtocolGlobals.G_UPDATE_DESTINATION_REPLY, h);
addHandler(ProtocolGlobals.G_REM_DESTINATION, h);
addHandler(ProtocolGlobals.G_REM_DESTINATION_REPLY, h);
h = new ConfigChangeEventHandler(this);
addHandler(ProtocolGlobals.G_CONFIG_CHANGE_EVENT, h);
addHandler(ProtocolGlobals.G_CONFIG_CHANGE_EVENT_REPLY, h);
h = new GetConfigChangesHandler(this);
addHandler(ProtocolGlobals.G_GET_CONFIG_CHANGES_REQUEST, h);
addHandler(ProtocolGlobals.G_GET_CONFIG_CHANGES_REPLY, h);
h = new ClientClosedHandler(this);
addHandler(ProtocolGlobals.G_CLIENT_CLOSED, h);
addHandler(ProtocolGlobals.G_CLIENT_CLOSED_REPLY, h);
h = new ClusterFlowControlHandler(this);
addHandler(ProtocolGlobals.G_STOP_MESSAGE_FLOW, h);
addHandler(ProtocolGlobals.G_STOP_MESSAGE_FLOW_REPLY, h);
addHandler(ProtocolGlobals.G_RESUME_MESSAGE_FLOW, h);
addHandler(ProtocolGlobals.G_RESUME_MESSAGE_FLOW_REPLY, h);
h = new ReloadClusterHandler(this);
addHandler(ProtocolGlobals.G_RELOAD_CLUSTER, h);
addHandler(ProtocolGlobals.G_RELOAD_CLUSTER_REPLY, h);
h = new GetInterestUpdateHandler(this);
addHandler(ProtocolGlobals.G_GET_INTEREST_UPDATE, h);
addHandler(ProtocolGlobals.G_GET_INTEREST_UPDATE_REPLY, h);
h = new PingHandler(this);
addHandler(ProtocolGlobals.G_PING, h);
addHandler(ProtocolGlobals.G_PING_REPLY, h);
h = new GoodbyeHandler(this);
addHandler(ProtocolGlobals.G_GOODBYE, h);
addHandler(ProtocolGlobals.G_GOODBYE_REPLY, h);
h = new TakeoverCompleteHandler(this);
addHandler(ProtocolGlobals.G_TAKEOVER_COMPLETE, h);
h = new TakeoverPendingHandler(this);
addHandler(ProtocolGlobals.G_TAKEOVER_PENDING, h);
addHandler(ProtocolGlobals.G_TAKEOVER_PENDING_REPLY, h);
h = new TakeoverAbortHandler(this);
addHandler(ProtocolGlobals.G_TAKEOVER_ABORT, h);
h = new TransactionInquiryHandler(this);
addHandler(ProtocolGlobals.G_TRANSACTION_INQUIRY, h);
h = new TransactionInfoHandler(this);
addHandler(ProtocolGlobals.G_TRANSACTION_INFO, h);
h = new FirstInfoHandler(this);
addHandler(ProtocolGlobals.G_FIRST_INFO, h);
h = new NewMasterBrokerHandler(this);
addHandler(ProtocolGlobals.G_NEW_MASTER_BROKER_PREPARE, h);
addHandler(ProtocolGlobals.G_NEW_MASTER_BROKER_PREPARE_REPLY, h);
addHandler(ProtocolGlobals.G_NEW_MASTER_BROKER, h);
addHandler(ProtocolGlobals.G_NEW_MASTER_BROKER_REPLY, h);
h = new ReplicationGroupInfoHandler(this);
addHandler(ProtocolGlobals.G_REPLICATION_GROUP_INFO, h);
h = new TakeoverMEHandler(this);
addHandler(ProtocolGlobals.G_TAKEOVER_ME_PREPARE, h);
addHandler(ProtocolGlobals.G_TAKEOVER_ME_PREPARE_REPLY, h);
addHandler(ProtocolGlobals.G_TAKEOVER_ME, h);
addHandler(ProtocolGlobals.G_TAKEOVER_ME_REPLY, h);
addHandler(ProtocolGlobals.G_TAKEOVER_ME_REPLY_ACK, h);
h = new TransferFileRequestHandler(this);
addHandler(ProtocolGlobals.G_TRANSFER_FILE_REQUEST, h);
addHandler(ProtocolGlobals.G_TRANSFER_FILE_REQUEST_REPLY, h);
h = new InfoHandler(this);
addHandler(ProtocolGlobals.G_INFO_REQUEST, h);
addHandler(ProtocolGlobals.G_INFO, h);
h = new NotifyPartitionArrivalHandler(this);
addHandler(ProtocolGlobals.G_NOTIFY_PARTITION_ARRIVAL, h);
addHandler(ProtocolGlobals.G_NOTIFY_PARTITION_ARRIVAL_REPLY, h);
unknownPacketHandler = new UnknownPacketHandler(this);
}
@Override
public Hashtable getDebugState() {
Hashtable ht = new Hashtable();
ArrayList l = null;
synchronized (brokerList) {
l = new ArrayList(brokerList.keySet());
}
ht.put("brokerListCount", l.size());
Iterator itr = l.iterator();
while (itr.hasNext()) {
BrokerAddress ba = (BrokerAddress) itr.next();
BrokerInfoEx bie = (BrokerInfoEx) brokerList.get(ba);
ht.put("[brokerList]" + ba.toString(), bie.toString());
}
if (cbDispatcher != null) {
ht.put("callbackDispatcher", cbDispatcher.getDebugState());
}
if (Globals.getHAEnabled()) {
synchronized (takingoverBrokers) {
l = new ArrayList<>(takingoverBrokers.keySet());
}
ht.put("takingoverBrokersCount", l.size());
Iterator<TakingoverEntry> itr1 = l.iterator();
while (itr1.hasNext()) {
TakingoverEntry toe = itr1.next();
ht.put(toe.toString(), toe.toLongString());
}
}
return ht;
}
@Override
public int getHighestSupportedVersion() {
return ProtocolGlobals.getCurrentVersion();
}
/**
* Get the message bus (cluster) protocol version used by this broker.
*/
@Override
public int getClusterVersion() {
return version;
}
/**
* Receive a unicast packet (raptor protocol).
*/
@Override
public void receiveUnicast(BrokerAddress sender, GPacket pkt) {
if (DEBUG) {
logger.log(logger.DEBUGMED, "RaptorProtocol.receiveUnicast(GPacket) from : " + sender + " Packet :\n" + pkt.toLongString());
}
int pktType = pkt.getType();
if (pktType > ProtocolGlobals.G_MAX_PACKET_TYPE) {
unknownPacketHandler.handle(sender, pkt);
return;
}
switch (pktType) {
case ProtocolGlobals.G_MESSAGE_ACK_REPLY:
cbDispatcher.processMessageAckReply(sender, pkt, this);
return;
case ProtocolGlobals.G_NEW_INTEREST_REPLY:
case ProtocolGlobals.G_INTEREST_UPDATE_REPLY:
case ProtocolGlobals.G_DURABLE_ATTACH_REPLY:
case ProtocolGlobals.G_REM_DURABLE_INTEREST_REPLY:
case ProtocolGlobals.G_MESSAGE_DATA:
case ProtocolGlobals.G_CLIENT_CLOSED_REPLY:
cbDispatcher.processMessageData(sender, pkt, this);
return;
case ProtocolGlobals.G_NEW_INTEREST:
case ProtocolGlobals.G_DURABLE_ATTACH:
if (((NewInterestHandler) handlers[pktType]).ignoreNewInterest(null, pkt)) {
return;
}
case ProtocolGlobals.G_UPDATE_DESTINATION:
case ProtocolGlobals.G_REM_DESTINATION:
case ProtocolGlobals.G_UPDATE_DESTINATION_REPLY:
case ProtocolGlobals.G_INTEREST_UPDATE:
case ProtocolGlobals.G_REM_DURABLE_INTEREST:
case ProtocolGlobals.G_MESSAGE_ACK:
case ProtocolGlobals.G_MESSAGE_DATA_REPLY:
case ProtocolGlobals.G_CLIENT_CLOSED:
case ProtocolGlobals.G_TRANSACTION_INQUIRY:
case ProtocolGlobals.G_TRANSACTION_INFO:
case ProtocolGlobals.G_INFO:
case ProtocolGlobals.G_INFO_REQUEST:
cbDispatcher.processGPacket(sender, pkt, this);
return;
}
handlers[pktType].handle(sender, pkt);
}
/**
* Receive a broadcast packet (raptor protocol).
*/
@Override
public void receiveBroadcast(BrokerAddress sender, GPacket pkt) {
if (DEBUG) {
logger.log(logger.DEBUGMED, "RaptorProtocol.receiveBroadcast(GPacket) from : " + sender + " Packet :\n" + pkt.toLongString());
}
int pktType = pkt.getType();
if (pktType > ProtocolGlobals.G_MAX_PACKET_TYPE) {
unknownPacketHandler.handle(sender, pkt);
return;
}
switch (pktType) {
case ProtocolGlobals.G_NEW_INTEREST:
case ProtocolGlobals.G_DURABLE_ATTACH:
if (((NewInterestHandler) handlers[pktType]).ignoreNewInterest(null, pkt)) {
return;
}
case ProtocolGlobals.G_UPDATE_DESTINATION:
case ProtocolGlobals.G_REM_DESTINATION:
case ProtocolGlobals.G_UPDATE_DESTINATION_REPLY:
case ProtocolGlobals.G_NEW_INTEREST_REPLY:
case ProtocolGlobals.G_DURABLE_ATTACH_REPLY:
case ProtocolGlobals.G_INTEREST_UPDATE:
case ProtocolGlobals.G_INTEREST_UPDATE_REPLY:
case ProtocolGlobals.G_REM_DURABLE_INTEREST:
case ProtocolGlobals.G_REM_DURABLE_INTEREST_REPLY:
case ProtocolGlobals.G_MESSAGE_ACK:
case ProtocolGlobals.G_MESSAGE_ACK_REPLY:
case ProtocolGlobals.G_MESSAGE_DATA:
case ProtocolGlobals.G_MESSAGE_DATA_REPLY:
case ProtocolGlobals.G_CLIENT_CLOSED:
case ProtocolGlobals.G_CLIENT_CLOSED_REPLY:
case ProtocolGlobals.G_TRANSACTION_INQUIRY:
case ProtocolGlobals.G_TRANSACTION_INFO:
case ProtocolGlobals.G_INFO:
case ProtocolGlobals.G_INFO_REQUEST:
cbDispatcher.processGPacket(sender, pkt, this);
return;
}
handlers[pktType].handle(sender, pkt);
}
@Override
public void handleGPacket(MessageBusCallback mbcb, BrokerAddress sender, GPacket pkt) {
handlers[pkt.getType()].handle(mbcb, sender, pkt);
}
/**
* Obsolete.
*/
@Override
public void receiveUnicast(BrokerAddress sender, int destId, byte[] pkt) {
// This should never happen.
logger.log(Logger.WARNING, "Protocol Mismatch. sender = " + sender);
Thread.dumpStack();
}
/**
* Obsolete.
*/
@Override
public void receiveBroadcast(BrokerAddress sender, int destId, byte[] pkt) {
// This should never happen.
logger.log(Logger.WARNING, "Protocol Mismatch. sender = " + sender);
Thread.dumpStack();
}
/**
* Obsolete.
*/
public void receiveBroadcast(BrokerAddress sender, int destId, byte[] pkt, boolean configSyncResponse) {
// This should never happen.
logger.log(Logger.WARNING, "Protocol Mismatch. sender = " + sender);
Thread.dumpStack();
}
@Override
public void syncChangeRecordOnJoin(BrokerAddress broker, ChangeRecordInfo cri) throws BrokerException {
cb.syncChangeRecordOnJoin(broker, cri);
}
@Override
public ChangeRecordInfo getLastStoredChangeRecord() {
return cb.getLastStoredChangeRecord();
}
/**
* Construct a BrokerInfo object that describes this broker. This object is exchanged during initial handshake between
* brokers.
*
* @return BrokerInfo object describing the current state of the broker.
*/
@Override
public BrokerInfo getBrokerInfo() {
logger.logStack(logger.ERROR, BrokerResources.E_INTERNAL_BROKER_ERROR, "Unexpected call", new Exception());
return null;
}
@Override
public ClusterBrokerInfoReply getBrokerInfoReply(BrokerInfo remoteInfo) throws Exception {
checkUIDPrefixClash(remoteInfo);
int status = ProtocolGlobals.G_BROKER_INFO_OK;
if (isTakeoverTarget(remoteInfo.getBrokerAddr())) {
status = ProtocolGlobals.G_BROKER_INFO_TAKINGOVER;
}
ClusterBrokerInfoReply cbi = ClusterBrokerInfoReply.newInstance(selfInfo, status);
return cbi;
}
private void checkUIDPrefixClash(BrokerInfo info) throws BrokerException {
if (c.getConfigServer() == null && (UniqueID.getPrefix(info.getBrokerAddr().getBrokerSessionUID().longValue()) == UniqueID
.getPrefix(selfInfo.getBrokerAddr().getBrokerSessionUID().longValue()))) {
final BrokerAddress remote = info.getBrokerAddr();
if (selfInfo.getStartTime() > info.getStartTime()
|| (selfInfo.getStartTime() == info.getStartTime() && selfInfo.getBrokerAddr().getMQAddress().toString()
.compareTo(remote.getMQAddress().toString()) > 0)) {
String msg = br.getKString(BrokerResources.E_CLUSTER_UID_PREFIX_CLASH_RESTART, remote);
BrokerException ex = new BrokerException(msg);
logger.logStack(logger.ERROR, msg, ex);
Broker.getBroker().exit(Globals.getBrokerStateHandler().getRestartCode(), msg, BrokerEvent.Type.RESTART, null, false, true, false);
}
}
}
/**
* Set the matchProps for the cluster.
*/
@Override
public void setMatchProps(Properties matchProps) {
if (DEBUG) {
logger.log(logger.INFO, "RaptorProtocol.setMatchProps :\n" + matchProps);
}
c.setMatchProps(matchProps);
}
/**
* Start the I/O operations on the cluster, now that the upper layers are ready to receive notifications.
*/
@Override
public void startClusterIO() {
if (DEBUG) {
logger.log(logger.INFO, "RaptorProtocol.startClusterIO");
}
// If this broker is the config server, make sure it's own
// persistent store is in sync with the config server
// change record.
try {
BrokerAddress configServer = c.getConfigServer();
if (configServer == null) { // No config server...
configSyncComplete = true;
if (DEBUG) {
logger.log(logger.INFO, "No master broker. configSyncComplete=true");
}
} else if (configServer.equals(selfAddress)) {
// I am the config server...
initConfigServer();
long timestamp = getLastRefreshTime();
sendConfigChangesRequest(selfAddress, timestamp);
// At this point the persistent store is in sync..
//
// Obviously, this is not very efficient - we can
// avoid all the marshalling / unmarshalling since
// the target is selfAddress.
}
} catch (Exception e) {
// There is a master broker, but we don't yet know who.
// Ignore this exception...
}
Globals.getDestinationList().addPartitionListener(this);
try {
store.addPartitionListener(this);
if (Globals.getHAEnabled()) {
store.addStoreSessionReaperListener(this);
}
} catch (Exception e) {
if (DEBUG) {
logger.logStack(logger.WARNING, "Unable to add store listener: " + e.getMessage(), e);
} else {
logger.log(logger.WARNING, "Unable to add store listener: " + e.getMessage(), e);
}
}
}
/*********************************************
* implement PartitionListener interface
*********************************************/
/**
* @param partitionID the partition id
*/
@Override
public void partitionAdded(UID partitionID, Object source) {
if (!(source instanceof DestinationList)) {
return;
}
ClusterInfoInfo cii = ClusterInfoInfo.newInstance();
cii.partitionAdded(partitionID);
GPacket gp = cii.getGPacket();
BrokerAddress[] addrs = getBrokerList(null, Globals.getMyAddress());
for (int i = 0; i < addrs.length; i++) {
logger.log(logger.INFO, br.getKString(br.I_CLUSTER_UNICAST, ProtocolGlobals.getPacketTypeDisplayString(gp.getType()) + "[" + cii + "]", addrs[i]));
try {
c.unicast(addrs[i], gp);
} catch (Exception e) {
logger.log(logger.WARNING,
br.getKString(BrokerResources.W_CLUSTER_UNICAST_FAILED, ProtocolGlobals.getPacketTypeDisplayString(gp.getType()), addrs[i]) + ": "
+ e.getMessage(),
e);
}
}
}
/**
* @param partitionID the partition id
*/
@Override
public void partitionRemoved(UID partitionID, Object source, Object destinedTo) {
if (destinedTo != null) {
return;
}
if (!(source instanceof Store)) {
return;
}
List<TakingoverEntry> entries = null;
synchronized (takingoverBrokers) {
entries = new ArrayList<>(takingoverBrokers.keySet());
}
Iterator<TakingoverEntry> itr = entries.iterator();
TakingoverEntry toe = null;
while (itr.hasNext()) {
toe = itr.next();
if (!toe.storeSession.equals(partitionID)) {
continue;
}
takingoverBrokers.remove(toe);
logger.log(logger.INFO, br.getKString(br.I_REMOVE_CACHED_TAKEOVER_NOTIFICATION_ENTRY, toe.toLongString()));
}
}
@Override
public void runStoreSessionTask() {
List<TakingoverEntry> entries = null;
synchronized (takingoverBrokers) {
entries = new ArrayList<>(takingoverBrokers.keySet());
}
Iterator<TakingoverEntry> itr = entries.iterator();
TakingoverEntry toe = null;
while (itr.hasNext()) {
toe = itr.next();
try {
if (store.getStoreSessionOwner(toe.storeSession.longValue()) == null) {
takingoverBrokers.remove(toe);
logger.log(logger.INFO,
br.getKString(br.I_REMOVE_CACHED_TAKEOVER_NOTIFICATION_ENTRY, toe.toLongString()) + ", (" + takingoverBrokers.size() + ")");
}
} catch (Exception e) {
logger.logStack(logger.WARNING, e.getMessage(), e);
}
}
}
private static class BrokerInfoEx {
private int GOODBYE_SENT = 0x00000001;
private int GOODBYE_RECEIVED = 0x00000002;
private int GOODBYE_REPLY_SENT = 0x00000004;
private int GOODBYE_REPLY_RECEIVED = 0x00000008;
private int finStatus = 0;
private BrokerInfo info = null;
private ClusterGoodbyeInfo cgi = null;
private boolean deactivated = false;
BrokerInfoEx(BrokerInfo info) {
this.info = info;
}
public synchronized boolean deactivated() {
return deactivated;
}
public synchronized void deactivate() {
deactivated = true;
notifyAll();
}
public synchronized void setGoodbyeInfo(ClusterGoodbyeInfo cgi) {
this.cgi = cgi;
}
public BrokerInfo getBrokerInfo() {
return info;
}
public synchronized boolean goodbyeDone() {
return sentGoodbyeReply() && gotGoodbyeReply();
}
public synchronized void goodbyeSent() {
finStatus = finStatus | GOODBYE_SENT;
}
public synchronized void goodbyeReceived() {
finStatus = finStatus | GOODBYE_RECEIVED;
}
public synchronized void goodbyeReplySent() {
finStatus = finStatus | GOODBYE_REPLY_SENT;
}
public synchronized void goodbyeReplyReceived() {
finStatus = finStatus | GOODBYE_REPLY_RECEIVED;
}
public synchronized boolean gotGoodbye() {
return cgi != null && ((finStatus & GOODBYE_RECEIVED) == GOODBYE_RECEIVED);
}
public synchronized boolean sentGoodbye() {
return (finStatus & GOODBYE_SENT) == GOODBYE_SENT;
}
public synchronized boolean gotGoodbyeReply() {
return (finStatus & GOODBYE_REPLY_RECEIVED) == GOODBYE_REPLY_RECEIVED;
}
public synchronized boolean sentGoodbyeReply() {
return (finStatus & GOODBYE_REPLY_SENT) == GOODBYE_REPLY_SENT;
}
}
@Override
public void stopClusterIO(boolean requestTakeover, boolean force, BrokerAddress excludedBroker) {
if (DEBUG) {
logger.log(logger.INFO, "RaptorProtocol.shutdown");
}
if (excludedBroker == null) {
synchronized (brokerList) {
shutdown = true;
}
synchronized (configOpLock) {
configOpLock.notifyAll();
}
}
cbDispatcher.shutdown();
try {
if (Globals.getClusterBroadcast().getClusterVersion() < ClusterBroadcast.VERSION_400) {
return;
}
} catch (Exception e) {
logger.log(logger.DEBUG, "Unable to get cluster version on stop cluster IO:" + e.getMessage());
return;
}
ClusterGoodbyeInfo cgi = ClusterGoodbyeInfo.newInstance(requestTakeover, c);
BrokerInfoEx be;
BrokerAddress ba;
GPacket gp;
synchronized (brokerList) {
Iterator itr = brokerList.values().iterator();
while (itr.hasNext()) {
be = (BrokerInfoEx) itr.next();
ba = be.getBrokerInfo().getBrokerAddr();
if (excludedBroker != null && ba.equals(excludedBroker)) {
continue;
}
gp = cgi.getGPacket();
try {
logger.log(logger.INFO, br.getKString(BrokerResources.I_CLUSTER_UNICAST,
ProtocolGlobals.getPacketTypeDisplayString(gp.getType()) + "[" + cgi + "]", ba.toString()));
c.unicast(ba, gp);
be.goodbyeSent();
} catch (IOException e) {
logger.log(logger.WARNING,
br.getKString(BrokerResources.W_CLUSTER_UNICAST_FAILED, ProtocolGlobals.getPacketTypeDisplayString(gp.getType()), ba) + ": "
+ e.getMessage(),
e);
}
}
}
}
public void goodbyeReceived(BrokerAddress sender, ClusterGoodbyeInfo cgi) {
Object[] args = new Object[] { ProtocolGlobals.getPacketTypeDisplayString(ProtocolGlobals.G_GOODBYE), "[" + cgi + "]", sender };
logger.log(logger.INFO, br.getKString(BrokerResources.I_CLUSTER_RECEIVE, args));
synchronized (brokerList) {
BrokerInfoEx be = (BrokerInfoEx) brokerList.get(sender);
if (be != null) {
be.setGoodbyeInfo(cgi);
be.goodbyeReceived();
}
}
}
public void goodbyeReplySent(BrokerAddress sender) {
synchronized (brokerList) {
BrokerInfoEx be = (BrokerInfoEx) brokerList.get(sender);
if (be != null) {
be.goodbyeReplySent();
if (DEBUG) {
logger.log(logger.INFO, "Sent GOODBYE_REPLY to " + sender);
}
}
}
}
public void goodbyeReplyReceived(BrokerAddress sender) {
synchronized (brokerList) {
BrokerInfoEx be = (BrokerInfoEx) brokerList.get(sender);
if (be != null) {
be.goodbyeReplyReceived();
if (DEBUG) {
logger.log(logger.INFO, "Received GOODBYE_REPLY from " + sender);
}
}
}
}
public void sendGoodbye(BrokerAddress remote) {
try {
synchronized (brokerList) {
BrokerInfoEx be = (BrokerInfoEx) brokerList.get(remote);
if (be != null) {
if (be.sentGoodbye()) {
return;
}
be.goodbyeSent();
}
}
c.unicast(remote, ClusterGoodbyeInfo.newInstance(c).getGPacket());
if (DEBUG) {
logger.log(logger.INFO, "Sent GOODBYE to " + remote);
}
} catch (Exception e) {
logger.logStack(logger.WARNING, "Unable to send GOODBYE to " + remote, e);
}
}
private BrokerAddress[] getBrokerList() {
return getBrokerList(null, null);
}
@Override
public BrokerAddress lookupBrokerAddress(String brokerid) {
if (!Globals.getHAEnabled()) {
return null;
}
synchronized (brokerList) {
Iterator itr = brokerList.keySet().iterator();
BrokerAddress ba = null;
while (itr.hasNext()) {
ba = (BrokerAddress) itr.next();
if (ba.getBrokerID().equals(brokerid)) {
return ba;
}
}
return null;
}
}
@Override
public BrokerAddress lookupBrokerAddress(BrokerMQAddress url) {
synchronized (brokerList) {
Iterator itr = brokerList.keySet().iterator();
BrokerAddress ba = null;
while (itr.hasNext()) {
ba = (BrokerAddress) itr.next();
if (ba.getMQAddress().equals(url)) {
return ba;
}
}
return null;
}
}
@Override
public String lookupStoreSessionOwner(UID uid) {
logger.log(logger.ERROR, "Internal Error: unexpected call:\n", SupportUtil.getStackTrace("lookupStoreSessionOwner"));
synchronized (brokerList) {
Iterator itr = brokerList.keySet().iterator();
BrokerAddress ba = null;
while (itr.hasNext()) {
ba = (BrokerAddress) itr.next();
if (ba.getStoreSessionUID().equals(uid)) {
return ba.getBrokerID();
}
}
}
BrokerAddress[] addrs = getBrokerList(null, Globals.getMyAddress());
Long xid = broadcastAnyOKReplyTracker.addWaiter(new BroadcastAnyOKReplyWaiter(addrs, ProtocolGlobals.G_INFO));
ClusterInfoRequestInfo cir = ClusterInfoRequestInfo.newInstance(xid);
try {
cir.storeSessionOwnerRequest(uid.longValue());
} catch (Exception e) {
logger.log(logger.ERROR, e.toString());
return null;
}
GPacket gp = cir.getGPacket();
for (int i = 0; i < addrs.length; i++) {
try {
c.unicast(addrs[i], gp);
} catch (Exception e) {
logger.log(logger.WARNING,
br.getKString(BrokerResources.W_CLUSTER_UNICAST_FAILED, ProtocolGlobals.getPacketTypeDisplayString(gp.getType()), addrs[i]) + ": "
+ e.getMessage(),
e);
}
}
ReplyStatus reply = null;
try {
reply = broadcastAnyOKReplyTracker.waitForReply(xid, ProtocolGlobals.getWaitInfoReplyTimeout());
if (reply != null && reply.getStatus() == Status.OK) {
gp = reply.getReply();
return ClusterInfoInfo.newInstance(gp).getStoreSessionOwner();
}
} catch (BrokerException e) {
String cause = e.getMessage();
if (e.getStatusCode() == Status.GONE) {
cause = br.getKString(br.X_CLUSTER_BROKER_LINK_DOWN, Arrays.toString(addrs));
} else if (e.getStatusCode() == Status.TIMEOUT) {
String[] args = new String[] { String.valueOf(ProtocolGlobals.getWaitInfoReplyTimeout()),
ProtocolGlobals.getPacketTypeDisplayString(ProtocolGlobals.G_INFO), Arrays.toString(addrs) };
cause = br.getKString(br.X_CLUSTER_WAIT_REPLY_TIMEOUT, args);
}
Object[] args = { ProtocolGlobals.getPacketTypeDisplayString(ProtocolGlobals.G_INFO), Arrays.toString(addrs), cause };
logger.log(Logger.WARNING, br.getKString(br.E_CLUSTER_ABORT_WAIT_REPLY, args));
}
return null;
}
public void receiveLookupStoreSessionOwnerRequest(BrokerAddress sender, ClusterInfoRequestInfo cir) {
String owner = null;
int status = Status.NOT_FOUND;
String reason = null;
logger.log(logger.WARNING,
"Unexpected protocol " + ProtocolGlobals.getPacketTypeDisplayString(ProtocolGlobals.G_INFO_REQUEST) + cir.toString() + " from " + sender);
status = Status.UNSUPPORTED_TYPE;
try {
long sid = cir.getStoreSession();
if (Globals.getStore().ifOwnStoreSession(sid, null)) {
status = Status.OK;
owner = Globals.getBrokerID();
}
} catch (Exception e) {
status = Status.ERROR;
reason = e.getMessage();
if (e instanceof BrokerException) {
status = ((BrokerException) e).getStatusCode();
}
logger.logStack(logger.WARNING, e.getMessage(), e);
}
ClusterInfoInfo cii = cir.getReply(status, reason, owner);
try {
c.unicast(sender, cii.getGPacket());
} catch (IOException e) {
logger.logStack(Logger.WARNING,
br.getKString(br.W_CLUSTER_UNICAST_FAILED, ProtocolGlobals.getPacketTypeDisplayString(ProtocolGlobals.G_INFO) + cii, sender), e);
}
}
public void receiveStoreSessionOwnerInfo(BrokerAddress sender, ClusterInfoInfo cii, GPacket pkt) {
Long xid = cii.getXid();
if (!broadcastAnyOKReplyTracker.notifyReply(xid, sender, pkt)) {
Object[] args = new Object[] { ProtocolGlobals.getPacketTypeDisplayString(ProtocolGlobals.G_INFO), "[" + cii.toString() + "]", sender };
logger.log(logger.WARNING, br.getKString(br.W_CLUSTER_UNABLE_NOTIFY_REPLY, args));
}