-
Notifications
You must be signed in to change notification settings - Fork 470
/
DAISYCHAIN.java
214 lines (170 loc) · 6.63 KB
/
DAISYCHAIN.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
package org.jgroups.protocols;
import org.jgroups.*;
import org.jgroups.annotations.Experimental;
import org.jgroups.annotations.MBean;
import org.jgroups.annotations.ManagedAttribute;
import org.jgroups.annotations.Property;
import org.jgroups.stack.Protocol;
import org.jgroups.util.MessageBatch;
import org.jgroups.util.Util;
import java.io.DataInput;
import java.io.DataOutput;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
/**
* Implementation of daisy chaining. Multicast messages are sent to our neighbor, which sends them to its neighbor etc.
* A TTL restricts the number of times a message is forwarded. The advantage of daisy chaining is that - for
* point-to-point transports such as TCP - we can avoid the N-1 issue: when A sends a multicast message to 10
* members, it needs to send it 9 times. With daisy chaining, it sends it 1 time, and in the next round, can already
* send another message. This leads to much better throughput, see the ref in the JIRA.<p/>
* Should be inserted just above MERGE3, in TCP based configurations.
* JIRA: https://jira.jboss.org/browse/JGRP-1021
* @author Bela Ban
* @since 2.11
*/
@Experimental
@MBean(description="Protocol just above the transport which disseminates multicasts via daisy chaining")
public class DAISYCHAIN extends Protocol {
/* ----------------------------------------- Properties -------------------------------------------------- */
@Property(description="Loop back multicast messages")
boolean loopback=true;
/* --------------------------------------------- Fields ------------------------------------------------------ */
protected volatile Address local_addr, next;
protected int view_size=0;
protected Executor default_pool=null;
protected volatile boolean running=true;
@ManagedAttribute
public int msgs_forwarded=0;
@ManagedAttribute
public int msgs_sent=0;
public void resetStats() {
super.resetStats();
msgs_forwarded=msgs_sent=0;
}
public void init() throws Exception {
default_pool=getTransport().getThreadPool();
}
public void start() throws Exception {
super.start();
running=true;
}
public void stop() {
super.stop();
running=false;
}
public Object down(final Event evt) {
switch(evt.getType()) {
case Event.VIEW_CHANGE:
handleView(evt.getArg());
break;
case Event.TMP_VIEW:
view_size=((View)evt.getArg()).size();
break;
case Event.SET_LOCAL_ADDRESS:
local_addr=evt.getArg();
break;
}
return down_prot.down(evt);
}
public Object down(Message msg) {
if(msg.getDest() != null)
return down_prot.down(msg); // only process multicast messages
if(next == null) // view hasn'<></> been received yet, use the normal transport
return down_prot.down(msg);
// we need to copy the message, as we cannot do a msg.setSrc(next): the next retransmission
// would use 'next' as destination !
Message copy=msg.copy(true);
short hdr_ttl=(short)(loopback? view_size -1 : view_size);
DaisyHeader hdr=new DaisyHeader(hdr_ttl);
copy.setDest(next);
copy.putHeader(getId(), hdr);
msgs_sent++;
if(loopback) {
if(log.isTraceEnabled()) log.trace(new StringBuilder("looping back message ").append(msg));
if(msg.getSrc() == null)
msg.setSrc(local_addr);
default_pool.execute(() -> up_prot.up(msg));
}
return down_prot.down(copy);
}
public Object up(Message msg) {
DaisyHeader hdr=msg.getHeader(getId());
if(hdr == null)
return up_prot.up(msg);
// 1. forward the message to the next in line if ttl > 0
short ttl=hdr.getTTL();
if(log.isTraceEnabled())
log.trace(local_addr + ": received message from " + msg.getSrc() + " with ttl=" + ttl);
if(--ttl > 0) {
Message copy=msg.copy(true);
copy.setDest(next);
copy.putHeader(getId(), new DaisyHeader(ttl));
msgs_forwarded++;
if(log.isTraceEnabled())
log.trace(local_addr + ": forwarding message to " + next + " with ttl=" + ttl);
down_prot.down(copy);
}
// 2. Pass up
msg.setDest(null);
return up_prot.up(msg);
}
public void up(MessageBatch batch) {
for(Message msg: batch) {
DaisyHeader hdr=msg.getHeader(getId());
if(hdr != null) {
// 1. forward the message to the next in line if ttl > 0
short ttl=hdr.getTTL();
if(log.isTraceEnabled())
log.trace(local_addr + ": received message from " + msg.getSrc() + " with ttl=" + ttl);
if(--ttl > 0) {
Message copy=msg.copy(true);
copy.setDest(next);
copy.putHeader(getId(), new DaisyHeader(ttl));
msgs_forwarded++;
if(log.isTraceEnabled())
log.trace(local_addr + ": forwarding message to " + next + " with ttl=" + ttl);
down_prot.down(copy);
}
// 2. Pass up
msg.setDest(null);
}
}
if(!batch.isEmpty())
up_prot.up(batch);
}
protected void handleView(View view) {
view_size=view.size();
Address tmp=Util.pickNext(view.getMembers(), local_addr);
if(tmp != null && !tmp.equals(local_addr)) {
next=tmp;
if(log.isDebugEnabled())
log.debug("next=" + next);
}
}
public static class DaisyHeader extends Header {
private short ttl;
public DaisyHeader() {
}
public DaisyHeader(short ttl) {
this.ttl=ttl;
}
public short getMagicId() {return 69;}
public short getTTL() {return ttl;}
public void setTTL(short ttl) {
this.ttl=ttl;
}
public Supplier<? extends Header> create() {return DaisyHeader::new;}
public int serializedSize() {
return Global.SHORT_SIZE;
}
public void writeTo(DataOutput out) throws Exception {
out.writeShort(ttl);
}
public void readFrom(DataInput in) throws Exception {
ttl=in.readShort();
}
public String toString() {
return "ttl=" + ttl;
}
}
}