-
Notifications
You must be signed in to change notification settings - Fork 33
/
ClusterManagerImpl.java
1657 lines (1467 loc) · 57.5 KB
/
ClusterManagerImpl.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) 2020 Payara Services Ltd.
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
*
* 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.cluster.manager;
import java.util.*;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import com.sun.messaging.jmq.io.MQAddress;
import com.sun.messaging.jmq.util.log.*;
import com.sun.messaging.jmq.util.UID;
import com.sun.messaging.jmq.jmsserver.config.*;
import com.sun.messaging.jmq.jmsserver.Globals;
import com.sun.messaging.jmq.jmsserver.cluster.api.*;
import com.sun.messaging.jmq.jmsserver.core.BrokerMQAddress;
import com.sun.messaging.jmq.jmsserver.core.DestinationList;
import com.sun.messaging.jmq.jmsserver.util.BrokerException;
import com.sun.messaging.jmq.jmsserver.resources.BrokerResources;
import org.jvnet.hk2.annotations.Service;
import jakarta.inject.Singleton;
/**
* This class represents the non-ha implementation of ClusterManager. Configuration information is not retrieved from
* the store.
* <p>
*/
@Service(name = "com.sun.messaging.jmq.jmsserver.cluster.manager.ClusterManagerImpl")
@Singleton
public class ClusterManagerImpl implements ClusterManager, ConfigListener {
private static final String DEBUG_ALL_PROP = Globals.IMQ + ".cluster.debug.all";
private static final boolean debug_CLUSTER_ALL = Globals.getConfig().getBooleanProperty(DEBUG_ALL_PROP);
private static boolean DEBUG_CLUSTER_ALL = debug_CLUSTER_ALL;
private static final String DEBUG_LOCK_PROP = Globals.IMQ + ".cluster.debug.lock";
private static final boolean debug_CLUSTER_LOCK = Globals.getConfig().getBooleanProperty(DEBUG_LOCK_PROP);
private static boolean DEBUG_CLUSTER_LOCK = debug_CLUSTER_LOCK;
private static final String DEBUG_TXN_PROP = Globals.IMQ + ".cluster.debug.txn";
private static final boolean debug_CLUSTER_TXN = Globals.getConfig().getBooleanProperty(DEBUG_TXN_PROP);
private static boolean DEBUG_CLUSTER_TXN = debug_CLUSTER_TXN;
private static final String DEBUG_TAKEOVER_PROP = Globals.IMQ + ".cluster.debug.takeover";
private static final boolean debug_CLUSTER_TAKEOVER = Globals.getConfig().getBooleanProperty(DEBUG_TAKEOVER_PROP);
private static boolean DEBUG_CLUSTER_TAKEOVER = debug_CLUSTER_TAKEOVER;
private static final String DEBUG_MSG_PROP = Globals.IMQ + ".cluster.debug.msg";
private static final boolean debug_CLUSTER_MSG = Globals.getConfig().getBooleanProperty(DEBUG_MSG_PROP);
private static boolean DEBUG_CLUSTER_MSG = debug_CLUSTER_MSG;
private static final String DEBUG_CONN_PROP = Globals.IMQ + ".cluster.debug.conn";
private static final boolean debug_CLUSTER_CONN = Globals.getConfig().getBooleanProperty(DEBUG_CONN_PROP);
private static boolean DEBUG_CLUSTER_CONN = debug_CLUSTER_CONN;
private static final String DEBUG_PING_PROP = Globals.IMQ + ".cluster.debug.ping";
private static final boolean debug_CLUSTER_PING = Globals.getConfig().getBooleanProperty(DEBUG_PING_PROP);
private static boolean DEBUG_CLUSTER_PING = debug_CLUSTER_PING;
private static final String DEBUG_PKT_PROP = Globals.IMQ + ".cluster.debug.packet";
private static final boolean debug_CLUSTER_PACKET = Globals.getConfig().getBooleanProperty(DEBUG_PKT_PROP);
private static boolean DEBUG_CLUSTER_PACKET = debug_CLUSTER_PACKET;
/*******************************************************
* accessor methods for dynamic cluster debug flags
*******************************************************/
public static boolean isDEBUG_CLUSTER_ALL() {
return DEBUG_CLUSTER_ALL;
}
public static boolean isDEBUG_CLUSTER_LOCK() {
return DEBUG_CLUSTER_LOCK;
}
public static boolean isDEBUG_CLUSTER_TXN() {
return DEBUG_CLUSTER_TXN;
}
public static boolean isDEBUG_CLUSTER_TAKEOVER() {
return DEBUG_CLUSTER_TAKEOVER;
}
public static boolean isDEBUG_CLUSTER_MSG() {
return DEBUG_CLUSTER_MSG;
}
public static boolean isDEBUG_CLUSTER_CONN() {
return DEBUG_CLUSTER_CONN;
}
public static boolean isDEBUG_CLUSTER_PING() {
return DEBUG_CLUSTER_PING;
}
public static boolean isDEBUG_CLUSTER_PACKET() {
return DEBUG_CLUSTER_PACKET;
}
/**
* Turns on/off debugging.
*/
private static boolean DEBUG = false;
/**
* The basic configuration (name, value pairs) for the broker.
*/
protected BrokerConfig config = Globals.getConfig();
/**
* The class used for logging.
*/
protected Logger logger = Globals.getLogger();
/**
* The set of listeners waiting state or configuration changed information.
*
* @see ClusterListener
*/
protected Set listeners = null;
/**
* The current transport type for the cluster.
*/
protected String transport = null;
/**
* The current hostname used for the cluster service. A value of null indicates bind to all hosts.
*/
protected String clusterhost = null;
/**
* The current port used for the cluster service. A value of 0 indicates that the value should be obtained dynamically.
*/
protected int clusterport = 0;
/**
* This is a private initialization flag.
*/
protected boolean initialized = false;
/**
* This is the brokerid for the local broker.
*/
protected String localBroker = null;
/**
* The brokerid for the master broker, null indicates no master broker.
*/
protected String masterBroker = null;
/**
* The list of all brokers in the cluster. The list contains ClusteredBroker objects.
*
* @see ClusteredBroker
*/
protected Map allBrokers = null;
protected static final String MANUAL_AUTOCONNECT_PROPERTY = Globals.MANUAL_AUTOCONNECT_CLUSTER_PROPERTY;
/**
* Private property name used to set the number of entries in the session list.
*/
protected static final String MAX_OLD_SESSIONS_PROP = Globals.IMQ + ".cluster.maxTakeoverSessions";
protected static final String MAX_RETAITION_TIME_PROP = Globals.IMQ + ".cluster.maxTakeoverSessionRetaintionTime";
protected static final long MAX_RETAITION_TIME_DEFAULT = 1800; // secs
protected int maxTakeoverSessions = Globals.getConfig().getIntProperty(MAX_OLD_SESSIONS_PROP, 10);
protected long maxTakeoverRetaintionTime = getMAX_RETAITION_TIME(); // millisecs
/**
*/
protected LinkedHashMap<UID, Long> supportedSessionMap = new LinkedHashMap<>();
/**
* The id of the cluster.
*/
private String clusterid = Globals.getClusterID();
private BrokerResources br = Globals.getBrokerResources();
private MQAddress brokerNextToMe = null;
private Object brokerNextToMeLock = new Object();
private int clusterPingInterval = CLUSTER_PING_INTERVAL_DEFAULT;
private static long getMAX_RETAITION_TIME() {
long t = Globals.getConfig().getLongProperty(MAX_RETAITION_TIME_PROP, MAX_RETAITION_TIME_DEFAULT);
if (t < MAX_RETAITION_TIME_DEFAULT) {
t = MAX_RETAITION_TIME_DEFAULT;
}
return t * 1000L;
}
/**
* Creates a ClusterManagerImpl.
*/
public ClusterManagerImpl() {
listeners = new LinkedHashSet();
}
@Override
public int getClusterPingInterval() {
return clusterPingInterval;
}
/**
* Retrieves the cluster id associated with this cluster.
*
* @return the id or null if this is not an HA cluster
*/
@Override
public String getClusterId() {
return clusterid;
}
/**
* Internal method to return the map which contains all brokers. This method may be overridden by sub-classes.
*/
protected Map initAllBrokers(MQAddress myaddr) throws BrokerException {
return new HashMap();
}
/**
* Changes the host/port of the local broker.
*
* @param address MQAddress to the portmapper
* @throws BrokerException if something goes wrong when the address is changed
*/
@Override
public void setMQAddress(MQAddress address) throws Exception {
if (!initialized) {
initialize(address); // sets up cluster state
} else {
mqAddressChanged(address);
}
}
/**
* Retrieves the host/port of the local broker.
*
* @return the MQAddress to the portmapper
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public MQAddress getMQAddress() {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
return getLocalBroker().getBrokerURL();
}
/**
* Sets a listener for notification when the state/status or configuration of the cluster changes.
*
* <p>
* This api is used by the Monitor Service to determine when a broker should be monitored because it may be down.
*
* @see ClusterListener
* @param listener the listener to add
*/
@Override
public void addEventListener(ClusterListener listener) {
synchronized (listeners) {
listeners.add(listener);
}
}
/**
* Removes a listener for notification when the state changes.
*
* <p>
* This api is used by the Monitor Service to determine when a broker should be monitored because it may be down.
*
* @return true if the item existed and was removed.
* @see ClusterListener
* @param listener the listener to remove
*/
@Override
public boolean removeEventListener(ClusterListener listener) {
synchronized (listeners) {
return listeners.remove(listener);
}
}
/**
* Retrieves the ClusteredBroker which represents this broker.
*
* @return the local broker
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
* @see ClusterManagerImpl#getBroker(String)
*/
@Override
public ClusteredBroker getLocalBroker() {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
return getBroker(localBroker);
}
/**
* Returns the current number of brokers in the cluster. In a non-ha cluster, this includes all brokers which have a
* BrokerLink to the local broker and the local broker.
*
* @return count of all brokers in the cluster.
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public int getKnownBrokerCount() {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
synchronized (allBrokers) {
return allBrokers.size();
}
}
/**
* Returns the current number of brokers in the configuration propperties. In a non-ha cluster, this includes all
* brokers listed by -cluster or the cluster property.
*
* @return count of configured brokers in the cluster.
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public int getConfigBrokerCount() {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
int cnt = 0;
synchronized (allBrokers) {
Iterator itr = allBrokers.values().iterator();
while (itr.hasNext()) {
ClusteredBroker cb = (ClusteredBroker) itr.next();
if (cb.isConfigBroker()) {
cnt++;
}
}
}
return cnt;
}
/**
* Returns the current number of brokers in the cluster. In a non-ha cluster, this includes all brokers which have an
* active BrokerLink to the local broker and the local broker.
*
* @return count of all brokers in the cluster.
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public int getActiveBrokerCount() {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
int cnt = 0;
synchronized (allBrokers) {
Iterator itr = allBrokers.values().iterator();
while (itr.hasNext()) {
ClusteredBroker cb = (ClusteredBroker) itr.next();
if (BrokerStatus.getBrokerLinkIsUp(cb.getStatus())) {
cnt++;
}
}
}
return cnt;
}
/**
* Returns an iterator of ClusteredBroker objects for all brokers in the cluster. This is a copy of the current list.
*
* @param refresh if true refresh current list then return it
* @return iterator of ClusteredBrokers
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public Iterator getKnownBrokers(boolean refresh) {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
HashSet brokers = null;
synchronized (allBrokers) {
brokers = new HashSet(allBrokers.values());
}
return brokers.iterator();
}
static class ConfigInterator implements Iterator {
Object nextObj = null;
Iterator parent = null;
ConfigInterator(Iterator parentItr) {
parent = parentItr;
}
@Override
public boolean hasNext() {
if (nextObj != null) {
return true;
}
if (!parent.hasNext()) {
return false;
}
while (nextObj == null) {
if (!parent.hasNext()) {
break;
}
nextObj = parent.next();
ClusteredBroker cb = (ClusteredBroker) nextObj;
if (!cb.isConfigBroker()) {
parent.remove();
nextObj = null; // not valid
}
}
return nextObj != null;
}
@Override
public Object next() {
// ok, skip to the right location
if (!hasNext()) {
throw new NoSuchElementException("no more");
}
Object ret = nextObj;
nextObj = null;
return ret;
}
@Override
public void remove() {
parent.remove();
}
}
/**
* Returns an iterator of ClusteredBroker objects for all brokers in the cluster. This is a copy of the current list and
* is accurate at the time getBrokers was called.
*
* @return iterator of ClusteredBrokers
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public Iterator getConfigBrokers() {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
HashSet brokers = null;
synchronized (allBrokers) {
brokers = new HashSet(allBrokers.values());
return new ConfigInterator(brokers.iterator());
}
}
static class ActiveInterator implements Iterator
{
Object nextObj = null;
Iterator parent = null;
ActiveInterator(Iterator parentItr) {
parent = parentItr;
}
@Override
public boolean hasNext() {
if (nextObj != null) {
return true;
}
if (!parent.hasNext()) {
return false;
}
while (nextObj == null) {
if (!parent.hasNext()) {
break;
}
nextObj = parent.next();
ClusteredBroker cb = (ClusteredBroker) nextObj;
if (BrokerStatus.getBrokerLinkIsDown(cb.getStatus())) {
parent.remove();
nextObj = null; // not valid
}
}
return nextObj != null;
}
@Override
public Object next() {
// ok, skip to the right location
if (!hasNext()) {
throw new NoSuchElementException("no more");
}
Object ret = nextObj;
nextObj = null;
return ret;
}
@Override
public void remove() {
parent.remove();
}
}
/**
* Returns an iterator of ClusteredBroker objects for all active brokers in the cluster. This is a copy of the current
* list and is accurate at the time getBrokers was called.
*
* @return iterator of ClusteredBrokers
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public Iterator getActiveBrokers() {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
HashSet brokers = null;
synchronized (allBrokers) {
brokers = new HashSet(allBrokers.values());
return new ActiveInterator(brokers.iterator());
}
}
/**
* Returns a specific ClusteredBroker object by name.
*
* @param brokerid the id associated with the broker
* @return the broker associated with brokerid or null if the broker is not found
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public ClusteredBroker getBroker(String brokerid) {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
synchronized (allBrokers) {
return (ClusteredBroker) allBrokers.get(brokerid);
}
}
/**
* Method used in a dynamic cluster, it updates the system when a new broker is added.
*
* @param URL the MQAddress of the new broker
* @param uid the brokerSessionUID associated with this broker (if known)
* @param instName the instance name of the broker to be activated
* @param userData optional data associated with the status change
* @throws NoSuchElementException if the broker can not be added to the cluster (for example if the cluster is running
* in HA mode and the URL is not in the shared database)
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
* @return the uid associated with the new broker
*/
@Override
public String activateBroker(MQAddress URL, UID uid, String instName, Object userData) throws BrokerException {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
// does it exist yet ?
String brokerid = lookupBrokerID(URL);
if (brokerid == null) {
brokerid = addBroker(URL, false, false, uid);
}
return activateBroker(brokerid, uid, instName, userData);
}
/**
* method used in a all clusters, it updates the system when a new broker is added.
*
* @param brokerid the id of the broker (if known)
* @param uid the broker sessionUID
* @param instName the broker instance name
* @param userData optional data associated with the status change
* @throws NoSuchElementException if the broker can not be added to the cluster (for example if the cluster is running
* in HA mode and the brokerid is not in the shared database)
* @throws BrokerException if the database can not be accessed
* @return the uid associated with the new broker
*/
@Override
public String activateBroker(String brokerid, UID uid, String instName, Object userData) throws BrokerException {
ClusteredBroker cb = getBroker(brokerid);
if (cb == null) {
throw new BrokerException("Unknown broker " + brokerid);
}
cb.setInstanceName(instName);
if (uid != null) {
cb.setBrokerSessionUID(uid);
}
cb = updateBrokerOnActivation(cb, userData);
cb.setStatus(BrokerStatus.ACTIVATE_BROKER, userData);
return brokerid;
}
/**
* protected method used to update a newly activated broker if necessary
*/
protected ClusteredBroker updateBrokerOnActivation(ClusteredBroker broker, Object userData) {
return broker;
}
protected ClusteredBroker updateBrokerOnDeactivation(ClusteredBroker broker, Object userData) {
broker.setInstanceName(null);
return broker;
}
/** @throws NoSuchElementException */
protected String addBroker(MQAddress URL, boolean isLocal, boolean config, UID sid) throws BrokerException {
ClusteredBroker cb = newClusteredBroker(URL, isLocal, sid);
((ClusteredBrokerImpl) cb).setConfigBroker(config);
synchronized (allBrokers) {
allBrokers.put(cb.getBrokerName(), cb);
}
brokerChanged(ClusterReason.ADDED, cb.getBrokerName(), null, cb, sid, null);
return cb.getBrokerName();
}
/**
* only be called internally from cluster manager framework
*/
public ClusteredBroker newClusteredBroker(MQAddress URL, boolean isLocal, UID sid) throws BrokerException {
return new ClusteredBrokerImpl(this, URL, isLocal, sid);
}
protected void removeFromAllBrokers(String brokerName) {
synchronized (allBrokers) {
allBrokers.remove(brokerName);
}
}
/**
* method used in a dynamic cluster, it updates the system when a broker is removed.
*
* @param URL the MQAddress associated with the broker
* @param userData optional data associated with the status change
* @throws NoSuchElementException if the broker can not be found in the cluster.
*/
@Override
public void deactivateBroker(MQAddress URL, Object userData) {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
// does it exist yet ?
String brokerid = lookupBrokerID(URL);
if (brokerid == null) {
throw new NoSuchElementException("Unknown URL " + URL);
}
deactivateBroker(brokerid, userData);
}
/**
* Method used in a dynamic cluster, it updates the system when a broker is removed.
*
* @param brokerid the id associated with the broker
* @param userData optional data associated with the status change
* @throws NoSuchElementException if the broker can not be found in the cluster.
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public void deactivateBroker(String brokerid, Object userData) {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
ClusteredBroker cb = null;
boolean removed = false;
synchronized (allBrokers) {
cb = (ClusteredBroker) allBrokers.get(brokerid);
if (cb == null) {
throw new NoSuchElementException("Unknown Broker" + brokerid);
}
if (!cb.isConfigBroker()) { // remove it if its dynamic
allBrokers.remove(brokerid);
removed = true;
}
updateBrokerOnDeactivation(cb, null);
}
// OK, set the broker link down
cb.setStatus(BrokerStatus.setBrokerLinkIsDown(cb.getStatus()), userData);
if (removed) {
brokerChanged(ClusterReason.REMOVED, cb.getBrokerName(), cb, null, cb.getBrokerSessionUID(), null);
}
}
/**
* Finds the brokerid associated with the given host/port.
*
* @param broker the MQAddress of the new broker
* @return the id associated with the broker or null if the broker does not exist
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public String lookupBrokerID(MQAddress broker) {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
// the safe thing to do is to iterate
synchronized (allBrokers) {
Iterator itr = allBrokers.values().iterator();
while (itr.hasNext()) {
ClusteredBroker cb = (ClusteredBroker) itr.next();
MQAddress addr = cb.getBrokerURL();
if (addr.equals(broker)) {
return cb.getBrokerName();
}
}
}
return null;
}
/**
* finds the brokerid associated with the given session.
*
* @param uid is the session uid to search for
* @return the uid associated with the session or null we cant find it.
*/
@Override
public String lookupStoreSessionOwner(UID uid) {
return null;
}
@Override
public String getStoreSessionCreator(UID uid) {
return null;
}
/**
* finds the brokerid associated with the given session.
*
* @param uid is the session uid to search for
* @return the uid associated with the session or null we cant find it.
*/
@Override
public String lookupBrokerSessionUID(UID uid) {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
// the safe thing to do is to iterate
synchronized (allBrokers) {
Iterator itr = allBrokers.values().iterator();
while (itr.hasNext()) {
ClusteredBroker cb = (ClusteredBroker) itr.next();
UID buid = cb.getBrokerSessionUID();
if (buid.equals(uid)) {
return cb.getBrokerName();
}
}
}
return null;
}
/**
* @return true if allow configured master broker
*/
protected boolean allowMasterBroker() {
return true;
}
/**
* The master broker in the cluster (if any).
*
* @return the master broker (or null if none)
* @see ClusterManagerImpl#getBroker(String)
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public ClusteredBroker getMasterBroker() {
if (masterBroker == null) {
return null;
}
return getBroker(masterBroker);
}
/**
* The transport (as a string) used by the cluster of brokers.
*
* @return the transport (tcp, ssl)
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public String getTransport() {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
return transport;
}
/**
* Returns the port configured for the cluster service.
*
* @return the port (or 0 if a dynamic port should be used)
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public int getClusterPort() {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
return clusterport;
}
/**
* Returns the host that the cluster service should bind to .
*
* @return the hostname (or null if the service should bind to all)
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public String getClusterHost() {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
return clusterhost;
}
/**
* Is the cluster "highly available" ?
*
* @return true if the cluster is HA
* @throws RuntimeException if the cluster has not be initialized (which occurs the first time the MQAddress is set)
* @see ClusterManagerImpl#setMQAddress
* @see Globals#getHAEnabled()
*/
@Override
public boolean isHA() {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
return false;
}
/**
* Reload cluster properties from config
*
*/
@Override
public void reloadConfig() throws BrokerException {
if (!initialized) {
throw new RuntimeException("Cluster not initialized");
}
String[] props = { CLUSTERURL_PROPERTY, AUTOCONNECT_PROPERTY }; // XXX
config.reloadProps(Globals.getConfigName(), props, false);
}
protected void setupListeners() {
config.addListener(TRANSPORT_PROPERTY, this);
config.addListener(HOST_PROPERTY, this);
config.addListener(PORT_PROPERTY, this);
config.addListener(AUTOCONNECT_PROPERTY, this);
config.addListener(CONFIG_SERVER, this);
config.addListener(CLUSTER_PING_INTERVAL_PROP, this);
}
/**
* Initializes the cluster (loading all configuration). This methods is called the first time setMQAddress is called
* after the broker is created.
*
* @param address the address of the local broker
* @throws BrokerException if the cluster can not be initialized
* @see ClusterManagerImpl#setMQAddress
*/
@Override
public String initialize(MQAddress address) throws BrokerException {
initialized = true;
allBrokers = initAllBrokers(address);
setupListeners();
config.addListener(DEBUG_ALL_PROP, this);
config.addListener(DEBUG_LOCK_PROP, this);
config.addListener(DEBUG_TXN_PROP, this);
config.addListener(DEBUG_TAKEOVER_PROP, this);
config.addListener(DEBUG_MSG_PROP, this);
config.addListener(DEBUG_CONN_PROP, this);
config.addListener(DEBUG_PING_PROP, this);
config.addListener(DEBUG_PKT_PROP, this);
// handle parsing transport
transport = config.getProperty(TRANSPORT_PROPERTY);
if (transport == null) {
transport = "tcp";
}
clusterhost = config.getProperty(HOST_PROPERTY);
// if not set, try imq.hostname
if (clusterhost == null) {
clusterhost = Globals.getHostname();
if (clusterhost != null && clusterhost.equals(Globals.HOSTNAME_ALL)) {
clusterhost = null;
}
}
clusterport = config.getIntProperty(PORT_PROPERTY, 0);
clusterPingInterval = config.getIntProperty(CLUSTER_PING_INTERVAL_PROP, CLUSTER_PING_INTERVAL_DEFAULT);
if (clusterPingInterval <= 0) {
clusterPingInterval = CLUSTER_PING_INTERVAL_DEFAULT;
}
LinkedHashSet s = null;
try {
s = parseBrokerList();
} catch (Exception ex) {
logger.logStack(Logger.ERROR, Globals.getBrokerResources().getKString(BrokerResources.X_BAD_ADDRESS_BROKER_LIST, ex.toString()), ex);
throw new BrokerException(ex.getMessage(), ex);
}
localBroker = addBroker(address, true, s.remove(address), new UID());
getLocalBroker().setStatus(BrokerStatus.ACTIVATE_BROKER, null);
setBrokerNextToMe(s);
// handle broker list
Iterator itr = s.iterator();
while (itr.hasNext()) {
MQAddress addr = (MQAddress) itr.next();
try {
// ok, are we the local broker ?
ClusteredBroker lcb = getLocalBroker();
if (addr.equals(getMQAddress())) {
if (lcb instanceof ClusteredBrokerImpl) {
((ClusteredBrokerImpl) lcb).setConfigBroker(true);
}
} else {
addBroker(addr, false, true, null);
}
} catch (NoSuchElementException ex) {
logger.logStack(Logger.ERROR, ex.getMessage() + ": " + "bad address in the broker list ", ex);
}
}
// handle master broker
String mbroker = config.getProperty(CONFIG_SERVER);
if (!allowMasterBroker()) {
if (DEBUG || logger.getLevel() <= Logger.DEBUG) {
logger.log(Logger.INFO, "This broker does not allow " + CONFIG_SERVER + " to be configured."
+ (mbroker == null ? "" : " Ignore " + CONFIG_SERVER + "=" + mbroker));
}
mbroker = null;
} else if (Globals.useSharedConfigRecord()) {
if (mbroker == null) {
logger.log(logger.INFO, br.getKString(br.I_USE_SHARECC_STORE));
} else {
logger.log(logger.WARNING, br.getKString(br.I_USE_SHARECC_STORE_IGNORE_MB, CONFIG_SERVER + "=" + mbroker));
}
mbroker = null;
}