/
Relayer.java
279 lines (228 loc) · 9.96 KB
/
Relayer.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
package org.jgroups.protocols.relay;
import org.jgroups.*;
import org.jgroups.logging.Log;
import org.jgroups.protocols.relay.config.RelayConfig;
import org.jgroups.stack.AddressGenerator;
import org.jgroups.util.UUID;
import org.jgroups.util.Util;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
/**
* Maintains bridges and routing table. Does the routing of outgoing messages and dispatches incoming messages to
* the right members.<p/>
* A Relayer cannot be reused once it is stopped, but a new Relayer instance must be created.
* @author Bela Ban
* @since 3.2
*/
public class Relayer {
/** The routing table. Site IDs are the keys (e.g. "sfo", and list of routes are the values */
protected ConcurrentMap<String,List<Route>> routes=new ConcurrentHashMap<>(5);
/** The bridges which are used to connect to different sites */
protected final Queue<Bridge> bridges=new ConcurrentLinkedQueue<>();
protected final Log log;
protected final RELAY2 relay;
/** Flag set when stop() is called. Since a Relayer should not be used after stop() has been called, a new
* instance needs to be created */
protected volatile boolean done;
protected boolean stats;
// Used to store messages for a site with status UNKNOWN. Messages will be flushed when the status changes to UP, or
// a SITE-UNREACHABLE message will be sent to each member *once* when the status changes to DOWN
// protected final ConcurrentMap<String,BlockingQueue<Message>> fwd_queue=new ConcurrentHashMap<String,BlockingQueue<Message>>();
// /** Map to store tasks which set the status of a site from UNKNOWN to DOWN. These are started when a site is
// * set to UNKNOWN, but they need to be cancelled when the status goes from UNKNOWN back to UP <em>before</em>
// * they kick in.*/
// protected final ConcurrentMap<Short,Future<?>> down_tasks=new ConcurrentHashMap<Short,Future<?>>();
//
public Relayer(RELAY2 relay, Log log) {
this.relay=relay;
this.stats=relay.statsEnabled();
this.log=log;
}
public boolean done() {return done;}
/**
* Creates all bridges from site_config and connects them (joining the bridge clusters)
* @param bridge_configs A list of bridge configurations
* @param bridge_name The name of the local bridge channel, prefixed with '_'.
* @param my_site_id The ID of this site
* @throws Throwable
*/
public void start(List<RelayConfig.BridgeConfig> bridge_configs, String bridge_name, final String my_site_id)
throws Throwable {
if(done) {
log.trace(relay.getLocalAddress() + ": will not start the Relayer as stop() has been called");
return;
}
try {
for(RelayConfig.BridgeConfig bridge_config: bridge_configs) {
Bridge bridge=new Bridge(bridge_config.createChannel(), bridge_config.getClusterName(), bridge_name,
() -> new SiteUUID(UUID.randomUUID(), null, my_site_id));
bridges.add(bridge);
}
for(Bridge bridge: bridges)
bridge.start();
}
catch(Throwable t) {
stop();
throw t;
}
finally {
if(done) {
log.trace(relay.getLocalAddress() + ": stop() was called while starting the relayer; stopping the relayer now");
stop();
}
}
}
/**
* Disconnects and destroys all bridges
*/
public void stop() {
done=true;
bridges.forEach(Bridge::stop);
bridges.clear();
}
public synchronized String printRoutes() {
StringBuilder sb=new StringBuilder();
for(Map.Entry<String,List<Route>> entry: routes.entrySet()) {
List<Route> list=entry.getValue();
if(list != null && !list.isEmpty())
sb.append(entry.getKey() + " --> ").append(Util.print(list)).append("\n");
}
return sb.toString();
}
protected Route getRoute(String site) { return getRoute(site, null);}
protected synchronized Route getRoute(String site, Address sender) {
List<Route> list=routes.get(site);
if(list == null)
return null;
if(list.size() == 1)
return list.get(0);
return relay.site_master_picker.pickRoute(site, list, sender);
}
protected List<String> getSiteNames() {
return new ArrayList<>(routes.keySet());
}
protected synchronized List<Route> getRoutes(String ... excluded_sites) {
List<Route> retval=new ArrayList<>(routes.size());
for(List<Route> list: routes.values()) {
for(Route route: list) {
if(route != null && !isExcluded(route, excluded_sites)) {
retval.add(route);
break;
}
}
}
return retval;
}
protected View getBridgeView(String cluster_name) {
if(cluster_name == null || bridges == null)
return null;
for(Bridge bridge: bridges) {
if(Objects.equals(bridge.cluster_name, cluster_name))
return bridge.view;
}
return null;
}
protected static boolean isExcluded(Route route, String... excluded_sites) {
if(excluded_sites == null)
return false;
String site=((SiteUUID)route.site_master).getSite();
for(String excluded_site: excluded_sites)
if(site.equals(excluded_site))
return true;
return false;
}
protected class Bridge extends ReceiverAdapter {
protected JChannel channel;
protected final String cluster_name;
protected View view;
protected Bridge(final JChannel ch, final String cluster_name, String channel_name, AddressGenerator addr_generator) throws Exception {
this.channel=ch;
channel.setName(channel_name);
channel.setReceiver(this);
channel.addAddressGenerator(addr_generator);
this.cluster_name=cluster_name;
}
protected void start() throws Exception {
channel.connect(cluster_name);
log.info("%s: joined bridge cluster '%s'", channel.getAddress(), cluster_name);
}
protected void stop() {
log.info("%s: leaving bridge cluster '%s'", channel.getAddress(), channel.getClusterName());
Util.close(channel);
}
public void receive(Message msg) {
RELAY2.Relay2Header hdr=msg.getHeader(relay.getId());
if(hdr == null) {
log.warn("received a message without a relay header; discarding it");
return;
}
relay.handleRelayMessage(hdr, msg);
}
/** The view contains a list of SiteUUIDs. Adjust the routing table based on the SiteUUIDs UUID and site
*/
public void viewAccepted(View new_view) {
this.view=new_view;
log.trace("[Relayer " + channel.getAddress() + "] view: " + new_view);
RouteStatusListener listener=relay.getRouteStatusListener();
Map<String,List<Address>> tmp=extract(new_view);
Set<String> down=listener != null? new HashSet<>(routes.keySet()) : null;
Set<String> up=listener != null? new HashSet<>() : null;
if(listener != null)
down.removeAll(tmp.keySet());
routes.keySet().retainAll(tmp.keySet()); // remove all sites which are not in the view
for(Map.Entry<String,List<Address>> entry: tmp.entrySet()) {
String key=entry.getKey();
List<Address> val=entry.getValue();
if(!routes.containsKey(key)) {
routes.put(key, new ArrayList<>());
if(up != null)
up.add(key);
}
List<Route> list=routes.get(key);
// Remove routes not in the view anymore:
for(Iterator<Route> it=list.iterator(); it.hasNext();) {
Route route=it.next();
if(!val.contains(route.siteMaster()))
it.remove();
}
// Add routes that aren't yet in the routing table:
val.stream().filter(addr -> !contains(list, addr))
.forEach(addr -> list.add(new Route(addr, channel, relay, log).stats(stats)));
if(list.isEmpty()) {
routes.remove(key);
if(listener != null) {
down.add(key);
up.remove(key);
}
}
}
if(listener != null) {
if(!down.isEmpty())
listener.sitesDown(down.toArray(new String[down.size()]));
if(!up.isEmpty())
listener.sitesUp(up.toArray(new String[up.size()]));
}
}
protected boolean contains(List<Route> routes, Address addr) {
return routes.stream().anyMatch(route -> route.siteMaster().equals(addr));
}
/** Returns a map containing the site keys and addresses as values */
protected Map<String,List<Address>> extract(View view) {
Map<String,List<Address>> map=new HashMap<>(view.size());
for(Address mbr: view) {
SiteAddress member=(SiteAddress)mbr;
String key=member.getSite();
List<Address> list=map.get(key);
if(list == null) {
list=new ArrayList<>();
map.put(key, list);
}
if(!list.contains(member))
list.add(member);
}
return map;
}
}
}