/
JGroupsTransport.java
875 lines (773 loc) · 36 KB
/
JGroupsTransport.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
package org.infinispan.remoting.transport.jgroups;
import org.infinispan.commands.ReplicableCommand;
import org.infinispan.commands.remote.ClusteredGetCommand;
import org.infinispan.commons.CacheConfigurationException;
import org.infinispan.commons.CacheException;
import org.infinispan.commons.marshall.StreamingMarshaller;
import org.infinispan.commons.util.FileLookup;
import org.infinispan.commons.util.FileLookupFactory;
import org.infinispan.commons.util.InfinispanCollections;
import org.infinispan.commons.util.TypedProperties;
import org.infinispan.commons.util.Util;
import org.infinispan.configuration.global.TransportConfiguration;
import org.infinispan.configuration.parsing.XmlConfigHelper;
import org.infinispan.factories.GlobalComponentRegistry;
import org.infinispan.factories.annotations.ComponentName;
import org.infinispan.factories.annotations.Inject;
import org.infinispan.jmx.JmxUtil;
import org.infinispan.notifications.cachemanagerlistener.CacheManagerNotifier;
import org.infinispan.remoting.inboundhandler.DeliverOrder;
import org.infinispan.remoting.inboundhandler.InboundInvocationHandler;
import org.infinispan.remoting.responses.ExceptionResponse;
import org.infinispan.remoting.responses.Response;
import org.infinispan.remoting.rpc.ResponseFilter;
import org.infinispan.remoting.rpc.ResponseMode;
import org.infinispan.remoting.transport.AbstractTransport;
import org.infinispan.remoting.transport.Address;
import org.infinispan.remoting.transport.BackupResponse;
import org.infinispan.util.TimeService;
import org.infinispan.util.concurrent.TimeoutException;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import org.infinispan.xsite.XSiteBackup;
import org.infinispan.xsite.XSiteReplicateCommand;
import org.jgroups.Channel;
import org.jgroups.Event;
import org.jgroups.JChannel;
import org.jgroups.MembershipListener;
import org.jgroups.MergeView;
import org.jgroups.SuspectedException;
import org.jgroups.UpHandler;
import org.jgroups.View;
import org.jgroups.blocks.RequestOptions;
import org.jgroups.blocks.RspFilter;
import org.jgroups.blocks.mux.Muxer;
import org.jgroups.jmx.JmxConfigurator;
import org.jgroups.protocols.relay.SiteMaster;
import org.jgroups.protocols.tom.TOA;
import org.jgroups.stack.AddressGenerator;
import org.jgroups.util.Buffer;
import org.jgroups.util.Rsp;
import org.jgroups.util.RspList;
import org.jgroups.util.TopologyUUID;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import static org.infinispan.factories.KnownComponentNames.ASYNC_TRANSPORT_EXECUTOR;
import static org.infinispan.factories.KnownComponentNames.GLOBAL_MARSHALLER;
/**
* An encapsulation of a JGroups transport. JGroups transports can be configured using a variety of
* methods, usually by passing in one of the following properties:
* <ul>
* <li><tt>configurationString</tt> - a JGroups configuration String</li>
* <li><tt>configurationXml</tt> - JGroups configuration XML as a String</li>
* <li><tt>configurationFile</tt> - String pointing to a JGroups XML configuration file</li>
* <li><tt>channelLookup</tt> - Fully qualified class name of a
* {@link org.infinispan.remoting.transport.jgroups.JGroupsChannelLookup} instance</li>
* </ul>
* These are normally passed in as Properties in
* {@link org.infinispan.configuration.global.TransportConfigurationBuilder#withProperties(java.util.Properties)} or
* in the Infinispan XML configuration file.
*
* @author Manik Surtani
* @author Galder Zamarreño
* @since 4.0
*/
public class JGroupsTransport extends AbstractTransport implements MembershipListener {
public static final String CONFIGURATION_STRING = "configurationString";
public static final String CONFIGURATION_XML = "configurationXml";
public static final String CONFIGURATION_FILE = "configurationFile";
public static final String CHANNEL_LOOKUP = "channelLookup";
protected static final String DEFAULT_JGROUPS_CONFIGURATION_FILE = "default-configs/default-jgroups-udp.xml";
static final Log log = LogFactory.getLog(JGroupsTransport.class);
static final boolean trace = log.isTraceEnabled();
protected boolean connectChannel = true, disconnectChannel = true, closeChannel = true;
protected CommandAwareRpcDispatcher dispatcher;
protected TypedProperties props;
protected StreamingMarshaller marshaller;
protected ExecutorService asyncExecutor;
protected CacheManagerNotifier notifier;
protected GlobalComponentRegistry gcr;
private TimeService timeService;
protected InboundInvocationHandler globalHandler;
private boolean globalStatsEnabled;
private MBeanServer mbeanServer;
private String domain;
protected Channel channel;
protected Address address;
protected Address physicalAddress;
// these members are not valid until we have received the first view on a second thread
// and channelConnectedLatch is signaled
protected volatile int viewId;
protected volatile List<Address> members = null;
protected volatile Address coordinator = null;
protected volatile boolean isCoordinator = false;
protected Lock viewUpdateLock = new ReentrantLock();
protected Condition viewUpdateCondition = viewUpdateLock.newCondition();
/**
* This form is used when the transport is created by an external source and passed in to the
* GlobalConfiguration.
*
* @param channel
* created and running channel to use
*/
public JGroupsTransport(Channel channel) {
this.channel = channel;
if (channel == null)
throw new IllegalArgumentException("Cannot deal with a null channel!");
if (channel.isConnected())
throw new IllegalArgumentException("Channel passed in cannot already be connected!");
}
public JGroupsTransport() {
}
@Override
public Log getLog() {
return log;
}
// ------------------------------------------------------------------------------------------------------------------
// Lifecycle and setup stuff
// ------------------------------------------------------------------------------------------------------------------
/**
* Initializes the transport with global cache configuration and transport-specific properties.
*
* @param marshaller marshaller to use for marshalling and unmarshalling
* @param asyncExecutor executor to use for asynchronous calls
* @param notifier notifier to use
* @param gcr the global component registry
*/
@Inject
public void initialize(@ComponentName(GLOBAL_MARSHALLER) StreamingMarshaller marshaller,
@ComponentName(ASYNC_TRANSPORT_EXECUTOR) ExecutorService asyncExecutor,
CacheManagerNotifier notifier, GlobalComponentRegistry gcr,
TimeService timeService, InboundInvocationHandler globalHandler) {
this.marshaller = marshaller;
this.asyncExecutor = asyncExecutor;
this.notifier = notifier;
this.gcr = gcr;
this.timeService = timeService;
this.globalHandler = globalHandler;
}
@Override
public void start() {
props = TypedProperties.toTypedProperties(configuration.transport().properties());
if (log.isInfoEnabled())
log.startingJGroupsChannel(configuration.transport().clusterName());
initChannelAndRPCDispatcher();
startJGroupsChannelIfNeeded();
waitForChannelToConnect();
}
protected void startJGroupsChannelIfNeeded() {
String clusterName = configuration.transport().clusterName();
if (connectChannel) {
try {
channel.connect(clusterName);
} catch (Exception e) {
throw new CacheException("Unable to start JGroups Channel", e);
}
try {
// Normally this would be done by CacheManagerJmxRegistration but
// the channel is not started when the cache manager starts but
// when first cache starts, so it's safer to do it here.
globalStatsEnabled = configuration.globalJmxStatistics().enabled();
if (globalStatsEnabled) {
String groupName = String.format("type=channel,cluster=%s", ObjectName.quote(clusterName));
mbeanServer = JmxUtil.lookupMBeanServer(configuration);
domain = JmxUtil.buildJmxDomain(configuration, mbeanServer, groupName);
JmxConfigurator.registerChannel((JChannel) channel, mbeanServer, domain, clusterName, true);
}
} catch (Exception e) {
throw new CacheException("Channel connected, but unable to register MBeans", e);
}
}
address = fromJGroupsAddress(channel.getAddress());
if (!connectChannel) {
// the channel was already started externally, we need to initialize our member list
viewAccepted(channel.getView());
}
if (log.isInfoEnabled())
log.localAndPhysicalAddress(clusterName, getAddress(), getPhysicalAddresses());
}
@Override
public int getViewId() {
if (channel == null)
throw new CacheException("The cache has been stopped and invocations are not allowed!");
return viewId;
}
@Override
public void waitForView(int viewId) throws InterruptedException {
if (channel == null)
return;
log.tracef("Waiting on view %d being accepted", viewId);
viewUpdateLock.lock();
try {
while (channel != null && getViewId() < viewId) {
viewUpdateCondition.await();
}
} finally {
viewUpdateLock.unlock();
}
}
@Override
public void stop() {
String clusterName = configuration.transport().clusterName();
try {
if (disconnectChannel && channel != null && channel.isConnected()) {
log.disconnectJGroups(clusterName);
// Unregistering before disconnecting/closing because
// after that the cluster name is null
if (globalStatsEnabled) {
JmxConfigurator.unregisterChannel((JChannel) channel, mbeanServer, domain, channel.getClusterName());
}
channel.disconnect();
}
if (closeChannel && channel != null && channel.isOpen()) {
channel.close();
}
} catch (Exception toLog) {
log.problemClosingChannel(toLog, clusterName);
}
if (dispatcher != null) {
log.stoppingRpcDispatcher(clusterName);
dispatcher.stop();
if (channel != null) {
// Remove reference to up_handler
UpHandler handler = channel.getUpHandler();
if (handler instanceof Muxer<?>) {
@SuppressWarnings("unchecked")
Muxer<UpHandler> mux = (Muxer<UpHandler>) handler;
mux.setDefaultHandler(null);
} else {
channel.setUpHandler(null);
}
}
}
channel = null;
viewId = -1;
members = InfinispanCollections.emptyList();
coordinator = null;
isCoordinator = false;
dispatcher = null;
// Wake up any view waiters
viewUpdateLock.lock();
try {
viewUpdateCondition.signalAll();
} finally {
viewUpdateLock.unlock();
}
}
protected void initChannel() {
final TransportConfiguration transportCfg = configuration.transport();
if (channel == null) {
buildChannel();
String transportNodeName = transportCfg.nodeName();
if (transportNodeName != null && transportNodeName.length() > 0) {
long range = Short.MAX_VALUE * 2;
long randomInRange = (long) ((Math.random() * range) % range) + 1;
transportNodeName = transportNodeName + "-" + randomInRange;
channel.setName(transportNodeName);
}
}
// Channel.LOCAL *must* be set to false so we don't see our own messages - otherwise
// invalidations targeted at remote instances will be received by self.
// NOTE: total order needs to deliver own messages. the invokeRemotely method has a total order boolean
// that when it is false, it discard our own messages, maintaining the property needed
channel.setDiscardOwnMessages(false);
// if we have a TopologyAwareConsistentHash, we need to set our own address generator in JGroups
if (transportCfg.hasTopologyInfo()) {
// We can do this only if the channel hasn't been started already
if (connectChannel) {
((JChannel) channel).setAddressGenerator(new AddressGenerator() {
@Override
public org.jgroups.Address generateAddress() {
return TopologyUUID.randomUUID(channel.getName(),
transportCfg.siteId(), transportCfg.rackId(),
transportCfg.machineId());
}
});
} else {
if (channel.getAddress() instanceof TopologyUUID) {
TopologyUUID topologyAddress = (TopologyUUID) channel.getAddress();
if (!transportCfg.siteId().equals(topologyAddress.getSiteId())
|| !transportCfg.rackId().equals(topologyAddress.getRackId())
|| !transportCfg.machineId().equals(topologyAddress.getMachineId())) {
throw new CacheException("Topology information does not match the one set by the provided JGroups channel");
}
} else {
throw new CacheException("JGroups address does not contain topology coordinates");
}
}
}
}
private void initChannelAndRPCDispatcher() throws CacheException {
initChannel();
initRPCDispatcher();
}
protected void initRPCDispatcher() {
dispatcher = new CommandAwareRpcDispatcher(channel, this, asyncExecutor, gcr.getTimeService(), globalHandler);
MarshallerAdapter adapter = new MarshallerAdapter(marshaller);
dispatcher.setRequestMarshaller(adapter);
dispatcher.setResponseMarshaller(adapter);
dispatcher.start();
}
// This is per CM, so the CL in use should be the CM CL
private void buildChannel() {
FileLookup fileLookup = FileLookupFactory.newInstance();
// in order of preference - we first look for an external JGroups file, then a set of XML
// properties, and
// finally the legacy JGroups String properties.
String cfg;
if (props != null) {
if (props.containsKey(CHANNEL_LOOKUP)) {
String channelLookupClassName = props.getProperty(CHANNEL_LOOKUP);
try {
JGroupsChannelLookup lookup = Util.getInstance(channelLookupClassName, configuration.classLoader());
channel = lookup.getJGroupsChannel(props);
connectChannel = lookup.shouldConnect();
disconnectChannel = lookup.shouldDisconnect();
closeChannel = lookup.shouldClose();
} catch (ClassCastException e) {
log.wrongTypeForJGroupsChannelLookup(channelLookupClassName, e);
throw new CacheException(e);
} catch (Exception e) {
log.errorInstantiatingJGroupsChannelLookup(channelLookupClassName, e);
throw new CacheException(e);
}
}
if (channel == null && props.containsKey(CONFIGURATION_FILE)) {
cfg = props.getProperty(CONFIGURATION_FILE);
Collection<URL> confs = null;
try {
confs = fileLookup.lookupFileLocations(cfg, configuration.classLoader());
} catch (IOException io) {
//ignore, we check confs later for various states
}
if (confs.isEmpty()) {
throw new CacheConfigurationException(CONFIGURATION_FILE
+ " property specifies value " + cfg + " that could not be read!",
new FileNotFoundException(cfg));
} else if (confs.size() > 1) {
log.ambiguousConfigurationFiles(Util.toStr(confs));
}
try {
channel = new JChannel(confs.iterator().next());
} catch (Exception e) {
log.errorCreatingChannelFromConfigFile(cfg);
throw new CacheException(e);
}
}
if (channel == null && props.containsKey(CONFIGURATION_XML)) {
cfg = props.getProperty(CONFIGURATION_XML);
try {
channel = new JChannel(XmlConfigHelper.stringToElement(cfg));
} catch (Exception e) {
log.errorCreatingChannelFromXML(cfg);
throw new CacheException(e);
}
}
if (channel == null && props.containsKey(CONFIGURATION_STRING)) {
cfg = props.getProperty(CONFIGURATION_STRING);
try {
channel = new JChannel(cfg);
} catch (Exception e) {
log.errorCreatingChannelFromConfigString(cfg);
throw new CacheException(e);
}
}
}
if (channel == null) {
log.unableToUseJGroupsPropertiesProvided(props);
try {
channel = new JChannel(fileLookup.lookupFileLocation(DEFAULT_JGROUPS_CONFIGURATION_FILE, configuration.classLoader()));
} catch (Exception e) {
throw new CacheException("Unable to start JGroups channel", e);
}
}
}
// ------------------------------------------------------------------------------------------------------------------
// querying cluster status
// ------------------------------------------------------------------------------------------------------------------
@Override
public boolean isCoordinator() {
return isCoordinator;
}
@Override
public Address getCoordinator() {
return coordinator;
}
public void waitForChannelToConnect() {
try {
waitForView(0);
} catch (InterruptedException e) {
// The start method can't throw checked exceptions
log.interruptedWaitingForCoordinator(e);
Thread.currentThread().interrupt();
}
}
@Override
public List<Address> getMembers() {
return members != null ? members : InfinispanCollections.<Address>emptyList();
}
@Override
public boolean isMulticastCapable() {
return channel.getProtocolStack().getTransport().supportsMulticasting();
}
@Override
public Address getAddress() {
if (address == null && channel != null) {
address = fromJGroupsAddress(channel.getAddress());
}
return address;
}
@Override
public List<Address> getPhysicalAddresses() {
if (physicalAddress == null && channel != null) {
org.jgroups.Address addr = (org.jgroups.Address) channel.down(new Event(Event.GET_PHYSICAL_ADDRESS, channel.getAddress()));
if (addr == null) {
return InfinispanCollections.emptyList();
}
physicalAddress = new JGroupsAddress(addr);
}
return Collections.singletonList(physicalAddress);
}
// ------------------------------------------------------------------------------------------------------------------
// outbound RPC
// ------------------------------------------------------------------------------------------------------------------
@Override
public Map<Address, Response> invokeRemotely(Collection<Address> recipients, ReplicableCommand rpcCommand,
ResponseMode mode, long timeout, boolean usePriorityQueue, ResponseFilter responseFilter,
boolean totalOrder, boolean anycast) throws Exception {
DeliverOrder deliverOrder = DeliverOrder.PER_SENDER;
if (totalOrder) {
deliverOrder = DeliverOrder.TOTAL;
} else if (usePriorityQueue) {
deliverOrder = DeliverOrder.NONE;
}
return invokeRemotely(recipients, rpcCommand, mode, timeout, responseFilter, deliverOrder, anycast);
}
@Override
public Map<Address, Response> invokeRemotely(Collection<Address> recipients, ReplicableCommand rpcCommand,
ResponseMode mode, long timeout, ResponseFilter responseFilter,
DeliverOrder deliverOrder, boolean anycast) throws Exception {
if (recipients != null && recipients.isEmpty()) {
// don't send if recipients list is empty
log.trace("Destination list is empty: no need to send message");
return InfinispanCollections.emptyMap();
}
boolean totalOrder = deliverOrder == DeliverOrder.TOTAL;
if (trace)
log.tracef("dests=%s, command=%s, mode=%s, timeout=%s", recipients, rpcCommand, mode, timeout);
Address self = getAddress();
boolean ignoreLeavers = mode == ResponseMode.SYNCHRONOUS_IGNORE_LEAVERS || mode == ResponseMode.WAIT_FOR_VALID_RESPONSE;
if (mode.isSynchronous() && recipients != null && !getMembers().containsAll(recipients)) {
if (!ignoreLeavers) { // SYNCHRONOUS
throw new SuspectException("One or more nodes have left the cluster while replicating command " + rpcCommand);
}
}
boolean asyncMarshalling = mode == ResponseMode.ASYNCHRONOUS;
List<org.jgroups.Address> jgAddressList = toJGroupsAddressListExcludingSelf(recipients, totalOrder);
int membersSize = members.size();
boolean broadcast = jgAddressList == null || recipients.size() == membersSize;
if (membersSize < 3 || (jgAddressList != null && jgAddressList.size() < 2)) broadcast = false;
RspList<Object> rsps = null;
Response singleResponse = null;
org.jgroups.Address singleJGAddress = null;
if (broadcast) {
rsps = dispatcher.broadcastRemoteCommands(rpcCommand, toJGroupsMode(mode), timeout,
toJGroupsFilter(responseFilter), deliverOrder, asyncMarshalling, ignoreLeavers);
} else if (totalOrder) {
rsps = dispatcher.invokeRemoteCommands(jgAddressList, rpcCommand, toJGroupsMode(mode), timeout,
toJGroupsFilter(responseFilter), deliverOrder, asyncMarshalling, ignoreLeavers);
} else {
if (jgAddressList == null || !jgAddressList.isEmpty()) {
boolean singleRecipient = !ignoreLeavers && jgAddressList != null && jgAddressList.size() == 1;
boolean skipRpc = false;
if (jgAddressList == null) {
ArrayList<Address> others = new ArrayList<>(members);
others.remove(self);
skipRpc = others.isEmpty();
singleRecipient = !ignoreLeavers && others.size() == 1;
if (singleRecipient) singleJGAddress = toJGroupsAddress(others.get(0));
}
if (!skipRpc) {
if (singleRecipient) {
if (singleJGAddress == null) singleJGAddress = jgAddressList.get(0);
singleResponse = dispatcher.invokeRemoteCommand(singleJGAddress, rpcCommand, toJGroupsMode(mode), timeout,
deliverOrder, asyncMarshalling);
} else {
rsps = dispatcher.invokeRemoteCommands(jgAddressList, rpcCommand, toJGroupsMode(mode), timeout,
toJGroupsFilter(responseFilter), deliverOrder, asyncMarshalling, ignoreLeavers);
}
}
}
}
if (mode.isAsynchronous())
return InfinispanCollections.emptyMap();// async case
Map<Address, Response> responses;
if (rsps == null) {
if (singleJGAddress == null || (singleResponse == null && rpcCommand instanceof ClusteredGetCommand)) {
responses = InfinispanCollections.emptyMap();
} else {
responses = Collections.singletonMap(fromJGroupsAddress(singleJGAddress), singleResponse);
}
} else {
Map<Address, Response> retval = new HashMap<>(rsps.size());
boolean noValidResponses = true;
for (Rsp<Object> rsp : rsps.values()) {
noValidResponses &= parseResponseAndAddToResponseList(rsp.getValue(), rsp.getException(), retval, rsp.wasSuspected(), rsp.wasReceived(), fromJGroupsAddress(rsp.getSender()),
responseFilter != null, ignoreLeavers);
}
if (noValidResponses)
throw new TimeoutException("Timed out waiting for valid responses!");
responses = retval;
}
return responses;
}
@Override
public Map<Address, Response> invokeRemotely(Map<Address, ReplicableCommand> rpcCommands, ResponseMode mode,
long timeout, boolean usePriorityQueue, ResponseFilter responseFilter, boolean totalOrder, boolean anycast)
throws Exception {
if (rpcCommands == null || rpcCommands.isEmpty()) {
// don't send if recipients list is empty
log.trace("Destination list is empty: no need to send message");
return InfinispanCollections.emptyMap();
}
if (trace)
log.tracef("commands=%s, mode=%s, timeout=%s", rpcCommands, mode, timeout);
boolean ignoreLeavers = mode == ResponseMode.SYNCHRONOUS_IGNORE_LEAVERS || mode == ResponseMode.WAIT_FOR_VALID_RESPONSE;
List<Address> members = getMembers();
Map<org.jgroups.Address, ReplicableCommand> forJGroups = new HashMap<>();
for (Map.Entry<Address, ReplicableCommand> entry : rpcCommands.entrySet()) {
if (!members.contains(entry.getKey())) {
if (mode.isSynchronous() && ignoreLeavers) {
throw new SuspectException("One or more nodes have left the cluster while replicating commands " + rpcCommands);
}
} else {
forJGroups.put(toJGroupsAddress(entry.getKey()), entry.getValue());
}
}
boolean asyncMarshalling = mode == ResponseMode.ASYNCHRONOUS;
if (!usePriorityQueue && (ResponseMode.SYNCHRONOUS == mode || ResponseMode.SYNCHRONOUS_IGNORE_LEAVERS == mode))
usePriorityQueue = true;
RspList<Object> rsps = dispatcher.invokeRemoteCommands(forJGroups, toJGroupsMode(mode), timeout,
usePriorityQueue, asyncMarshalling, ignoreLeavers);
if (mode.isAsynchronous() || rsps == null)
return InfinispanCollections.emptyMap();// async case
Set<Address> suspectedAddresses = new HashSet<>();
Map<Address, Response> retval = new HashMap<Address, Response>(rsps.size());
boolean noValidResponses = true;
for (Entry<org.jgroups.Address, Rsp<Object>> entry : rsps.entrySet()) {
Rsp<Object> rsp = entry.getValue();
Object value = rsp.getValue();
if (value instanceof ExceptionResponse) {
ExceptionResponse exceptionResponse = (ExceptionResponse) value;
if (exceptionResponse.getException() instanceof SuspectedException) {
suspectedAddresses.add(fromJGroupsAddress(entry.getKey()));
continue;
}
}
noValidResponses &= parseResponseAndAddToResponseList(value,
rsp.getException(), retval, rsp.wasSuspected(), rsp.wasReceived(),
fromJGroupsAddress(rsp.getSender()), responseFilter != null, ignoreLeavers);
}
if (noValidResponses) {
boolean throwException;
// It is possible for their to be no valid response if we ignored leavers and
// all of our targets left
if (ignoreLeavers) {
// If all the targets were suspected we didn't have a timeout
throwException = !suspectedAddresses.containsAll(rpcCommands.keySet());
} else {
throwException = true;
}
if (throwException)
throw new TimeoutException("Timed out waiting for valid responses!");
}
return retval;
}
@Override
public BackupResponse backupRemotely(Collection<XSiteBackup> backups, XSiteReplicateCommand rpcCommand) throws Exception {
log.tracef("About to send to backups %s, command %s", backups, rpcCommand);
Buffer buf = CommandAwareRpcDispatcher.marshallCall(dispatcher.getMarshaller(), rpcCommand);
Map<XSiteBackup, Future<Object>> syncBackupCalls = new HashMap<>(backups.size());
for (XSiteBackup xsb : backups) {
SiteMaster recipient = new SiteMaster(xsb.getSiteName());
if (xsb.isSync()) {
RequestOptions sync = new RequestOptions(org.jgroups.blocks.ResponseMode.GET_ALL, xsb.getTimeout());
syncBackupCalls.put(xsb, dispatcher.sendMessageWithFuture(CommandAwareRpcDispatcher.constructMessage(buf, recipient, true, org.jgroups.blocks.ResponseMode.GET_ALL, false, false), sync));
} else {
RequestOptions async = new RequestOptions(org.jgroups.blocks.ResponseMode.GET_NONE, xsb.getTimeout());
dispatcher.sendMessage(CommandAwareRpcDispatcher.constructMessage(buf, recipient, false, org.jgroups.blocks.ResponseMode.GET_NONE, false, false), async);
}
}
return new JGroupsBackupResponse(syncBackupCalls, timeService);
}
private static org.jgroups.blocks.ResponseMode toJGroupsMode(ResponseMode mode) {
switch (mode) {
case ASYNCHRONOUS:
case ASYNCHRONOUS_WITH_SYNC_MARSHALLING:
return org.jgroups.blocks.ResponseMode.GET_NONE;
case SYNCHRONOUS:
case SYNCHRONOUS_IGNORE_LEAVERS:
return org.jgroups.blocks.ResponseMode.GET_ALL;
case WAIT_FOR_VALID_RESPONSE:
return org.jgroups.blocks.ResponseMode.GET_FIRST;
}
throw new CacheException("Unknown response mode " + mode);
}
private RspFilter toJGroupsFilter(ResponseFilter responseFilter) {
return responseFilter == null ? null : new JGroupsResponseFilterAdapter(responseFilter);
}
// ------------------------------------------------------------------------------------------------------------------
// Implementations of JGroups interfaces
// ------------------------------------------------------------------------------------------------------------------
private interface Notify {
void emitNotification(List<Address> oldMembers, View newView);
}
private class NotifyViewChange implements Notify {
@Override
public void emitNotification(List<Address> oldMembers, View newView) {
notifier.notifyViewChange(members, oldMembers, getAddress(), (int) newView.getVid().getId());
}
}
private class NotifyMerge implements Notify {
@Override
public void emitNotification(List<Address> oldMembers, View newView) {
MergeView mv = (MergeView) newView;
final Address address = getAddress();
final int viewId = (int) newView.getVid().getId();
notifier.notifyMerge(members, oldMembers, address, viewId, getSubgroups(mv.getSubgroups()));
}
private List<List<Address>> getSubgroups(List<View> subviews) {
List<List<Address>> l = new ArrayList<List<Address>>(subviews.size());
for (View v : subviews)
l.add(fromJGroupsAddressList(v.getMembers()));
return l;
}
}
@Override
public void viewAccepted(View newView) {
log.debugf("New view accepted: %s", newView);
List<org.jgroups.Address> newMembers = newView.getMembers();
if (newMembers == null || newMembers.isEmpty()) {
log.debugf("Received null or empty member list from JGroups channel: " + newView);
return;
}
List<Address> oldMembers = members;
// Update every view-related field while holding the lock so that waitForView only returns
// after everything was updated.
viewUpdateLock.lock();
try {
viewId = (int) newView.getViewId().getId();
// we need a defensive copy anyway
members = fromJGroupsAddressList(newMembers);
// Delta view debug log for large cluster
if (log.isDebugEnabled() && oldMembers != null) {
List<Address> joined = new ArrayList<Address>(members);
joined.removeAll(oldMembers);
List<Address> left = new ArrayList<Address>(oldMembers);
left.removeAll(members);
log.debugf("Joined: %s, Left: %s", joined, left);
}
// Now that we have a view, figure out if we are the isCoordinator
coordinator = fromJGroupsAddress(newView.getCreator());
isCoordinator = coordinator != null && coordinator.equals(getAddress());
// Wake up any threads that are waiting to know about who the isCoordinator is
// do it before the notifications, so if a listener throws an exception we can still start
viewUpdateCondition.signalAll();
} finally {
viewUpdateLock.unlock();
}
// now notify listeners - *after* updating the isCoordinator. - JBCACHE-662
boolean hasNotifier = notifier != null;
if (hasNotifier) {
String clusterName = configuration.transport().clusterName();
Notify n;
if (newView instanceof MergeView) {
log.receivedMergedView(clusterName, newView);
n = new NotifyMerge();
} else {
log.receivedClusterView(clusterName, newView);
n = new NotifyViewChange();
}
n.emitNotification(oldMembers, newView);
}
JGroupsAddressCache.pruneAddressCache();
}
@Override
public void suspect(org.jgroups.Address suspected_mbr) {
// no-op
}
@Override
public void block() {
// no-op since ISPN-83 has been resolved
}
@Override
public void unblock() {
// no-op since ISPN-83 has been resolved
}
// ------------------------------------------------------------------------------------------------------------------
// Helpers to convert between Address types
// ------------------------------------------------------------------------------------------------------------------
protected static org.jgroups.Address toJGroupsAddress(Address a) {
return ((JGroupsAddress) a).address;
}
static Address fromJGroupsAddress(final org.jgroups.Address addr) {
return JGroupsAddressCache.fromJGroupsAddress(addr);
}
private List<org.jgroups.Address> toJGroupsAddressListExcludingSelf(Collection<Address> list, boolean totalOrder) {
if (list == null)
return null;
if (list.isEmpty())
return InfinispanCollections.emptyList();
List<org.jgroups.Address> retval = new ArrayList<org.jgroups.Address>(list.size());
boolean ignoreSelf = !totalOrder; //in total order, we need to send the message to ourselves!
Address self = getAddress();
for (Address a : list) {
if (!ignoreSelf || !a.equals(self)) {
retval.add(toJGroupsAddress(a));
} else {
ignoreSelf = false; // short circuit address equality for future iterations
}
}
return retval;
}
private static List<Address> fromJGroupsAddressList(List<org.jgroups.Address> list) {
if (list == null || list.isEmpty())
return InfinispanCollections.emptyList();
List<Address> retval = new ArrayList<Address>(list.size());
for (org.jgroups.Address a : list)
retval.add(fromJGroupsAddress(a));
return Collections.unmodifiableList(retval);
}
// mainly for unit testing
public CommandAwareRpcDispatcher getCommandAwareRpcDispatcher() {
return dispatcher;
}
public Channel getChannel() {
return channel;
}
@Override
public final void checkTotalOrderSupported() {
//For replicated and distributed tx caches, we use TOA as total order protocol.
if (channel.getProtocolStack().findProtocol(TOA.class) == null) {
throw new CacheConfigurationException("In order to support total order based transaction, the TOA protocol " +
"must be present in the JGroups's config.");
}
}
}