/
MessageDispatcher.java
628 lines (519 loc) · 22.8 KB
/
MessageDispatcher.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
package org.jgroups.blocks;
import org.jgroups.*;
import org.jgroups.blocks.mux.Muxer;
import org.jgroups.logging.Log;
import org.jgroups.logging.LogFactory;
import org.jgroups.protocols.TP;
import org.jgroups.protocols.relay.SiteAddress;
import org.jgroups.stack.DiagnosticsHandler;
import org.jgroups.stack.Protocol;
import org.jgroups.stack.StateTransferInfo;
import org.jgroups.util.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Provides synchronous and asynchronous message sending with request-response
* correlation; i.e., matching responses with the original request.
* It also offers push-style message reception (by internally using the PullPushAdapter).
* <p>
* Channels are simple patterns to asynchronously send a receive messages.
* However, a significant number of communication patterns in group communication
* require synchronous communication. For example, a sender would like to send a
* message to the group and wait for all responses. Or another application would
* like to send a message to the group and wait only until the majority of the
* receivers have sent a response, or until a timeout occurred. MessageDispatcher
* offers a combination of the above pattern with other patterns.
* <p>
* Used on top of channel to implement group requests. Client's <code>handle()</code>
* method is called when request is received. Is the equivalent of RpcProtocol on
* the application instead of protocol level.
*
* @author Bela Ban
*/
public class MessageDispatcher implements RequestHandler, ChannelListener {
protected Channel channel=null;
protected RequestCorrelator corr=null;
protected MessageListener msg_listener=null;
protected MembershipListener membership_listener=null;
protected RequestHandler req_handler=null;
protected ProtocolAdapter prot_adapter=null;
protected volatile Collection<Address> members=new HashSet<Address>();
protected Address local_addr=null;
protected final Log log=LogFactory.getLog(getClass());
protected boolean hardware_multicast_supported=false;
protected final AtomicInteger sync_unicasts=new AtomicInteger(0);
protected final AtomicInteger async_unicasts=new AtomicInteger(0);
protected final AtomicInteger sync_multicasts=new AtomicInteger(0);
protected final AtomicInteger async_multicasts=new AtomicInteger(0);
protected final AtomicInteger sync_anycasts=new AtomicInteger(0);
protected final AtomicInteger async_anycasts=new AtomicInteger(0);
protected final Set<ChannelListener> channel_listeners=new CopyOnWriteArraySet<ChannelListener>();
protected final DiagnosticsHandler.ProbeHandler probe_handler=new MyProbeHandler();
public MessageDispatcher() {
}
public MessageDispatcher(Channel channel, MessageListener l, MembershipListener l2) {
this.channel=channel;
prot_adapter=new ProtocolAdapter();
if(channel != null) {
local_addr=channel.getAddress();
channel.addChannelListener(this);
}
setMessageListener(l);
setMembershipListener(l2);
if(channel != null) {
installUpHandler(prot_adapter, true);
}
start();
}
public MessageDispatcher(Channel channel, MessageListener l, MembershipListener l2, RequestHandler req_handler) {
this(channel, l, l2);
setRequestHandler(req_handler);
}
public UpHandler getProtocolAdapter() {
return prot_adapter;
}
/**
* If this dispatcher is using a user-provided PullPushAdapter, then need to set the members from the adapter
* initially since viewChange has most likely already been called in PullPushAdapter.
*/
protected void setMembers(List<Address> new_mbrs) {
if(new_mbrs != null)
members=new HashSet<Address>(new_mbrs); // volatile write - seen by a subsequent read
}
/**
* Adds a new channel listener to be notified on the channel's state change.
*/
public void addChannelListener(ChannelListener l) {
if(l != null)
channel_listeners.add(l);
}
public void removeChannelListener(ChannelListener l) {
if(l != null)
channel_listeners.remove(l);
}
public void start() {
if(corr == null)
corr=createRequestCorrelator(prot_adapter, this, local_addr);
correlatorStarted();
corr.start();
if(channel != null) {
List<Address> tmp_mbrs=channel.getView() != null ? channel.getView().getMembers() : null;
setMembers(tmp_mbrs);
if(channel instanceof JChannel) {
TP transport=channel.getProtocolStack().getTransport();
corr.registerProbeHandler(transport);
}
TP transport=channel.getProtocolStack().getTransport();
hardware_multicast_supported=transport.supportsMulticasting();
transport.registerProbeHandler(probe_handler);
}
}
protected RequestCorrelator createRequestCorrelator(Protocol transport, RequestHandler handler, Address local_addr) {
return new RequestCorrelator(transport, handler, local_addr);
}
protected void correlatorStarted() {
;
}
public void stop() {
if(corr != null)
corr.stop();
if(channel instanceof JChannel) {
TP transport=channel.getProtocolStack().getTransport();
transport.unregisterProbeHandler(probe_handler);
corr.unregisterProbeHandler(transport);
}
}
public final void setMessageListener(MessageListener l) {
msg_listener=l;
}
public MessageListener getMessageListener() {
return msg_listener;
}
public final void setMembershipListener(MembershipListener l) {
membership_listener=l;
}
public final void setRequestHandler(RequestHandler rh) {
req_handler=rh;
}
public Channel getChannel() {
return channel;
}
public void setChannel(Channel ch) {
if(ch == null)
return;
this.channel=ch;
local_addr=channel.getAddress();
if(prot_adapter == null)
prot_adapter=new ProtocolAdapter();
// Don't force installing the UpHandler so subclasses can use this
// method and still integrate with a MuxUpHandler
installUpHandler(prot_adapter, false);
}
/**
* Sets the given UpHandler as the UpHandler for the channel, or, if the
* channel already has a Muxer installed as it's UpHandler, sets the given
* handler as the Muxer's {@link Muxer#setDefaultHandler(Object) default handler}.
* If the relevant handler is already installed, the <code>canReplace</code>
* controls whether this method replaces it (after logging a WARN) or simply
* leaves <code>handler</code> uninstalled.
* <p>
* Passing <code>false</code> as the <code>canReplace</code> value allows
* callers to use this method to install defaults without concern about
* inadvertently overriding
*
* @param handler the UpHandler to install
* @param canReplace <code>true</code> if an existing Channel upHandler or
* Muxer default upHandler can be replaced; <code>false</code>
* if this method shouldn't install
*/
protected void installUpHandler(UpHandler handler, boolean canReplace)
{
UpHandler existing = channel.getUpHandler();
if (existing == null) {
channel.setUpHandler(handler);
}
else if (existing instanceof Muxer<?>) {
@SuppressWarnings("unchecked")
Muxer<UpHandler> mux = (Muxer<UpHandler>) existing;
if (mux.getDefaultHandler() == null) {
mux.setDefaultHandler(handler);
}
else if (canReplace) {
log.warn("Channel Muxer already has a default up handler installed (" +
mux.getDefaultHandler() + ") but now it is being overridden");
mux.setDefaultHandler(handler);
}
}
else if (canReplace) {
log.warn("Channel already has an up handler installed (" + existing + ") but now it is being overridden");
channel.setUpHandler(handler);
}
}
/**
* Sends a message to the members listed in dests. If dests is null, the message is sent to all current group
* members.
* @param dests A list of group members to send the message to. The message is sent to all members of the current
* group if null
* @param msg The message to be sent
* @param options A set of options that govern the call. See {@link org.jgroups.blocks.RequestOptions} for details
* @return RspList A list of Rsp elements
* @throws Exception If the request cannot be sent
* @since 2.9
*/
public <T> RspList<T> castMessage(final Collection<Address> dests,
Message msg, RequestOptions options) throws Exception {
GroupRequest<T> req=cast(dests, msg, options, true);
return req != null? req.getResults() : new RspList();
}
/**
* Sends a message to the members listed in dests. If dests is null, the message is sent to all current group
* members.
* @param dests A list of group members to send the message to. The message is sent to all members of the current
* group if null
* @param msg The message to be sent
* @param options A set of options that govern the call. See {@link org.jgroups.blocks.RequestOptions} for details
* @return NotifyingFuture<T> A future from which the results (RspList) can be retrieved
* @throws Exception If the request cannot be sent
*/
public <T> NotifyingFuture<RspList<T>> castMessageWithFuture(final Collection<Address> dests,
Message msg,
RequestOptions options) throws Exception {
GroupRequest<T> req=cast(dests, msg, options, false);
return req != null? req : new NullFuture<RspList>(new RspList());
}
protected <T> GroupRequest<T> cast(final Collection<Address> dests, Message msg,
RequestOptions options,
boolean block_for_results) throws Exception {
List<Address> real_dests;
// we need to clone because we don't want to modify the original
if(dests != null) {
real_dests=new ArrayList<Address>();
for(Address dest: dests) {
if(dest instanceof SiteAddress || this.members.contains(dest)) {
if(!real_dests.contains(dest))
real_dests.add(dest);
}
}
}
else
real_dests=new ArrayList<Address>(members);
// if local delivery is off, then we should not wait for the message from the local member.
// therefore remove it from the membership
Channel tmp=channel;
if(tmp != null && tmp.getDiscardOwnMessages()) {
if(local_addr == null)
local_addr=tmp.getAddress();
if(local_addr != null)
real_dests.remove(local_addr);
}
if(options != null && options.hasExclusionList()) {
Collection<Address> exclusion_list=options.getExclusionList();
real_dests.removeAll(exclusion_list);
}
// don't even send the message if the destination list is empty
if(log.isTraceEnabled())
log.trace("real_dests=" + real_dests);
if(real_dests.isEmpty()) {
if(log.isTraceEnabled())
log.trace("destination list is empty, won't send message");
return null;
}
if(options != null) {
boolean async=options.getMode() == ResponseMode.GET_NONE;
if(options.getAnycasting()) {
if(async) async_anycasts.incrementAndGet();
else sync_anycasts.incrementAndGet();
}
else {
if(async) async_multicasts.incrementAndGet();
else sync_multicasts.incrementAndGet();
}
}
GroupRequest<T> req=new GroupRequest<T>(msg, corr, real_dests, options);
if(options != null) {
req.setResponseFilter(options.getRspFilter());
req.setAnycasting(options.getAnycasting());
msg.setFlag(options.getFlags());
if(options.getScope() > 0)
msg.setScope(options.getScope());
}
req.setBlockForResults(block_for_results);
req.execute();
return req;
}
public void done(long req_id) {
corr.done(req_id);
}
/**
* Sends a unicast message and - depending on the options - returns a result
* @param msg the message to be sent. The destination needs to be non-null
* @param opts the options to be used
* @return T the result
* @throws Exception If there was problem sending the request, processing it at the receiver, or processing
* it at the sender.
* @throws TimeoutException If the call didn't succeed within the timeout defined in options (if set)
*/
public <T> T sendMessage(Message msg, RequestOptions opts) throws Exception {
Address dest=msg.getDest();
if(dest == null)
throw new IllegalArgumentException("message destination is null, cannot send message");
if(opts != null) {
msg.setFlag(opts.getFlags());
if(opts.getScope() > 0)
msg.setScope(opts.getScope());
if(opts.getMode() == ResponseMode.GET_NONE)
async_unicasts.incrementAndGet();
else
sync_unicasts.incrementAndGet();
}
UnicastRequest<T> req=new UnicastRequest<T>(msg, corr, dest, opts);
req.execute();
if(opts != null && opts.getMode() == ResponseMode.GET_NONE)
return null;
Rsp<T> rsp=req.getResult();
if(rsp.wasSuspected())
throw new SuspectedException(dest);
Throwable exception=rsp.getException();
if(exception != null) {
if(exception instanceof Error) throw (Error)exception;
else if(exception instanceof RuntimeException) throw (RuntimeException)exception;
else if(exception instanceof Exception) throw (Exception)exception;
else throw new RuntimeException(exception);
}
if(rsp.wasUnreachable())
throw new UnreachableException(dest);
if(!rsp.wasReceived() && !req.responseReceived())
throw new TimeoutException("timeout sending message to " + dest);
return rsp.getValue();
}
/**
* Sends a unicast message to the target defined by msg.getDest() and returns a future
* @param msg The unicast message to be sent. msg.getDest() must not be null
* @param options
* @return NotifyingFuture<T> A future from which the result can be fetched
* @throws Exception If there was problem sending the request, processing it at the receiver, or processing
* it at the sender. {@link java.util.concurrent.Future#get()} will throw this exception
* @throws TimeoutException If the call didn't succeed within the timeout defined in options (if set)
*/
public <T> NotifyingFuture<T> sendMessageWithFuture(Message msg, RequestOptions options) throws Exception {
Address dest=msg.getDest();
if(dest == null)
throw new IllegalArgumentException("message destination is null, cannot send message");
if(options != null) {
msg.setFlag(options.getFlags());
if(options.getScope() > 0)
msg.setScope(options.getScope());
if(options.getMode() == ResponseMode.GET_NONE)
async_unicasts.incrementAndGet();
else
sync_unicasts.incrementAndGet();
}
UnicastRequest<T> req=new UnicastRequest<T>(msg, corr, dest, options);
req.setBlockForResults(false);
req.execute();
if(options != null && options.getMode() == ResponseMode.GET_NONE)
return new NullFuture<T>(null);
return req;
}
/* ------------------------ RequestHandler Interface ---------------------- */
public Object handle(Message msg) throws Exception {
if(req_handler != null)
return req_handler.handle(msg);
return null;
}
/* -------------------- End of RequestHandler Interface ------------------- */
/* --------------------- Interface ChannelListener ---------------------- */
public void channelConnected(Channel channel) {
for(ChannelListener l: channel_listeners) {
try {
l.channelConnected(channel);
}
catch(Throwable t) {
log.warn("notifying channel listener " + l + " failed", t);
}
}
}
public void channelDisconnected(Channel channel) {
stop();
for(ChannelListener l: channel_listeners) {
try {
l.channelDisconnected(channel);
}
catch(Throwable t) {
log.warn("notifying channel listener " + l + " failed", t);
}
}
}
public void channelClosed(Channel channel) {
stop();
for(ChannelListener l: channel_listeners) {
try {
l.channelClosed(channel);
}
catch(Throwable t) {
log.warn("notifying channel listener " + l + " failed", t);
}
}
}
/* ----------------------------------------------------------------------- */
protected Object handleUpEvent(Event evt) throws Exception {
switch(evt.getType()) {
case Event.MSG:
if(msg_listener != null)
msg_listener.receive((Message) evt.getArg());
break;
case Event.GET_APPLSTATE: // reply with GET_APPLSTATE_OK
byte[] tmp_state=null;
if(msg_listener != null) {
ByteArrayOutputStream output=new ByteArrayOutputStream(1024);
msg_listener.getState(output);
tmp_state=output.toByteArray();
}
return new StateTransferInfo(null, 0L, tmp_state);
case Event.GET_STATE_OK:
if(msg_listener != null) {
StateTransferResult result=(StateTransferResult)evt.getArg();
ByteArrayInputStream input=new ByteArrayInputStream(result.getBuffer());
msg_listener.setState(input);
}
break;
case Event.STATE_TRANSFER_OUTPUTSTREAM:
OutputStream os=(OutputStream)evt.getArg();
if(msg_listener != null && os != null) {
msg_listener.getState(os);
}
break;
case Event.STATE_TRANSFER_INPUTSTREAM:
InputStream is=(InputStream)evt.getArg();
if(msg_listener != null && is!=null)
msg_listener.setState(is);
break;
case Event.VIEW_CHANGE:
View v=(View) evt.getArg();
List<Address> new_mbrs=v.getMembers();
setMembers(new_mbrs);
if(membership_listener != null)
membership_listener.viewAccepted(v);
break;
case Event.SET_LOCAL_ADDRESS:
if(log.isTraceEnabled())
log.trace("setting local_addr (" + local_addr + ") to " + evt.getArg());
local_addr=(Address)evt.getArg();
break;
case Event.SUSPECT:
if(membership_listener != null)
membership_listener.suspect((Address) evt.getArg());
break;
case Event.BLOCK:
if(membership_listener != null)
membership_listener.block();
break;
case Event.UNBLOCK:
if(membership_listener != null)
membership_listener.unblock();
break;
}
return null;
}
class MyProbeHandler implements DiagnosticsHandler.ProbeHandler {
public Map<String,String> handleProbe(String... keys) {
Map<String,String> retval=new HashMap<String,String>();
for(String key: keys) {
if("rpcs".equals(key)) {
String channel_name = channel != null ? channel.getClusterName() : "";
retval.put(channel_name + ": sync unicast RPCs", sync_unicasts.toString());
retval.put(channel_name + ": sync multicast RPCs", sync_multicasts.toString());
retval.put(channel_name + ": async unicast RPCs", async_unicasts.toString());
retval.put(channel_name + ": async multicast RPCs", async_multicasts.toString());
retval.put(channel_name + ": sync anycast RPCs", sync_anycasts.toString());
retval.put(channel_name + ": async anycast RPCs", async_anycasts.toString());
}
if("rpcs-reset".equals(key)) {
sync_unicasts.set(0);
sync_multicasts.set(0);
async_unicasts.set(0);
async_multicasts.set(0);
sync_anycasts.set(0);
async_anycasts.set(0);
}
}
return retval;
}
public String[] supportedKeys() {
return new String[]{"rpcs", "rpcs-reset"};
}
}
class ProtocolAdapter extends Protocol implements UpHandler {
/* ------------------------- Protocol Interface --------------------------- */
public String getName() {
return "MessageDispatcher";
}
/**
* Called by channel (we registered before) when event is received. This is the UpHandler interface.
*/
public Object up(Event evt) {
if(corr != null) {
if(!corr.receive(evt)) {
try {
return handleUpEvent(evt);
}
catch(Throwable t) {
throw new RuntimeException(t);
}
}
}
return null;
}
public Object down(Event evt) {
if(channel != null) {
if(evt.getType() == Event.MSG && !channel.isConnected())
throw new IllegalStateException("channel is not connected");
return channel.down(evt);
}
return null;
}
/* ----------------------- End of Protocol Interface ------------------------ */
}
}