forked from eclipse-vertx/vert.x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClusteredEventBus.java
397 lines (359 loc) · 13.5 KB
/
ClusteredEventBus.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
/*
* Copyright (c) 2011-2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.eventbus.impl.clustered;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.Promise;
import io.vertx.core.VertxOptions;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.eventbus.AddressHelper;
import io.vertx.core.eventbus.EventBusOptions;
import io.vertx.core.eventbus.MessageCodec;
import io.vertx.core.eventbus.impl.CodecManager;
import io.vertx.core.eventbus.impl.EventBusImpl;
import io.vertx.core.eventbus.impl.HandlerHolder;
import io.vertx.core.eventbus.impl.HandlerRegistration;
import io.vertx.core.eventbus.impl.MessageImpl;
import io.vertx.core.eventbus.impl.OutboundDeliveryContext;
import io.vertx.core.impl.CloseFuture;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.EventLoopContext;
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.impl.utils.ConcurrentCyclicSequence;
import io.vertx.core.net.NetClient;
import io.vertx.core.net.NetClientOptions;
import io.vertx.core.net.NetServer;
import io.vertx.core.net.NetServerOptions;
import io.vertx.core.net.NetSocket;
import io.vertx.core.net.impl.NetClientBuilder;
import io.vertx.core.parsetools.RecordParser;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.core.spi.cluster.NodeInfo;
import io.vertx.core.spi.cluster.NodeSelector;
import io.vertx.core.spi.cluster.RegistrationInfo;
import io.vertx.core.spi.metrics.VertxMetrics;
import java.util.Iterator;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;
/**
* An event bus implementation that clusters with other Vert.x nodes
*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class ClusteredEventBus extends EventBusImpl {
private static final Logger log = LoggerFactory.getLogger(ClusteredEventBus.class);
private static final Buffer PONG = Buffer.buffer(new byte[]{(byte) 1});
private final EventBusOptions options;
private final ClusterManager clusterManager;
private final NodeSelector nodeSelector;
private final AtomicLong handlerSequence = new AtomicLong(0);
private final NetClient client;
private final ConcurrentMap<String, ConnectionHolder> connections = new ConcurrentHashMap<>();
private final CloseFuture closeFuture;
private final EventLoopContext ebContext;
private NodeInfo nodeInfo;
private String nodeId;
private NetServer server;
public ClusteredEventBus(VertxInternal vertx, VertxOptions options, ClusterManager clusterManager, NodeSelector nodeSelector) {
super(vertx);
this.options = options.getEventBusOptions();
this.clusterManager = clusterManager;
this.nodeSelector = nodeSelector;
closeFuture = new CloseFuture(log);
ebContext = vertx.createEventLoopContext(null, closeFuture, null, Thread.currentThread().getContextClassLoader());
this.client = createNetClient(vertx, new NetClientOptions(this.options.toJson()), closeFuture);
}
private NetClient createNetClient(VertxInternal vertx, NetClientOptions clientOptions, CloseFuture closeFuture) {
NetClientBuilder builder = new NetClientBuilder(vertx, clientOptions);
VertxMetrics metricsSPI = vertx.metricsSPI();
if (metricsSPI != null) {
builder.metrics(metricsSPI.createNetClientMetrics(clientOptions));
}
builder.closeFuture(closeFuture);
return builder.build();
}
NetClient client() {
return client;
}
private NetServerOptions getServerOptions() {
return new NetServerOptions(this.options.toJson());
}
@Override
public void start(Promise<Void> promise) {
NetServerOptions serverOptions = getServerOptions();
server = vertx.createNetServer(serverOptions);
server.connectHandler(getServerHandler());
int port = getClusterPort();
String host = getClusterHost();
ebContext.runOnContext(v -> {
server.listen(port, host).flatMap(v2 -> {
int publicPort = getClusterPublicPort(server.actualPort());
String publicHost = getClusterPublicHost(host);
nodeInfo = new NodeInfo(publicHost, publicPort, options.getClusterNodeMetadata());
nodeId = clusterManager.getNodeId();
Promise<Void> setPromise = Promise.promise();
clusterManager.setNodeInfo(nodeInfo, setPromise);
return setPromise.future();
}).andThen(ar -> {
if (ar.succeeded()) {
started = true;
nodeSelector.eventBusStarted();
}
}).onComplete(promise);
});
}
@Override
public void close(Promise<Void> promise) {
Promise<Void> parentClose = Promise.promise();
super.close(parentClose);
parentClose.future()
.transform(ar -> closeFuture.close())
.andThen(ar -> {
if (server != null) {
// Close all outbound connections explicitly - don't rely on context hooks
for (ConnectionHolder holder : connections.values()) {
holder.close();
}
}
})
.onComplete(promise);
}
@Override
public MessageImpl createMessage(boolean send, String address, MultiMap headers, Object body, String codecName) {
Objects.requireNonNull(address, "no null address accepted");
MessageCodec codec = codecManager.lookupCodec(body, codecName, false);
@SuppressWarnings("unchecked")
ClusteredMessage msg = new ClusteredMessage(nodeId, address, headers, body, codec, send, this);
return msg;
}
@Override
protected <T> void onLocalRegistration(HandlerHolder<T> handlerHolder, Promise<Void> promise) {
if (!handlerHolder.isReplyHandler()) {
RegistrationInfo registrationInfo = new RegistrationInfo(
nodeId,
handlerHolder.getSeq(),
handlerHolder.isLocalOnly()
);
clusterManager.addRegistration(handlerHolder.getHandler().address, registrationInfo, Objects.requireNonNull(promise));
} else if (promise != null) {
promise.complete();
}
}
@Override
protected <T> HandlerHolder<T> createHandlerHolder(HandlerRegistration<T> registration, boolean replyHandler, boolean localOnly, ContextInternal context) {
return new ClusteredHandlerHolder<>(registration, replyHandler, localOnly, context, handlerSequence.getAndIncrement());
}
@Override
protected <T> void onLocalUnregistration(HandlerHolder<T> handlerHolder, Promise<Void> completionHandler) {
if (!handlerHolder.isReplyHandler()) {
RegistrationInfo registrationInfo = new RegistrationInfo(
nodeId,
handlerHolder.getSeq(),
handlerHolder.isLocalOnly()
);
Promise<Void> promise = Promise.promise();
clusterManager.removeRegistration(handlerHolder.getHandler().address, registrationInfo, promise);
promise.future().onComplete(completionHandler);
} else {
completionHandler.complete();
}
}
@Override
protected <T> void sendOrPub(OutboundDeliveryContext<T> sendContext) {
if (((ClusteredMessage) sendContext.message).getRepliedTo() != null) {
clusteredSendReply(((ClusteredMessage) sendContext.message).getRepliedTo(), sendContext);
} else if (sendContext.options.isLocalOnly()) {
super.sendOrPub(sendContext);
} else {
Serializer serializer = Serializer.get(sendContext.ctx);
if (sendContext.message.isSend()) {
Promise<String> promise = sendContext.ctx.promise();
serializer.queue(sendContext.message, nodeSelector::selectForSend, promise);
promise.future().onComplete(ar -> {
if (ar.succeeded()) {
sendToNode(sendContext, ar.result());
} else {
sendOrPublishFailed(sendContext, ar.cause());
}
});
} else {
Promise<Iterable<String>> promise = sendContext.ctx.promise();
serializer.queue(sendContext.message, nodeSelector::selectForPublish, promise);
promise.future().onComplete(ar -> {
if (ar.succeeded()) {
sendToNodes(sendContext, ar.result());
} else {
sendOrPublishFailed(sendContext, ar.cause());
}
});
}
}
}
private void sendOrPublishFailed(OutboundDeliveryContext<?> sendContext, Throwable cause) {
if (log.isDebugEnabled()) {
log.error("Failed to send message", cause);
}
sendContext.written(cause);
}
@Override
protected String generateReplyAddress() {
// The address is a cryptographically secure id that can't be guessed
return "__vertx.reply." + UUID.randomUUID().toString();
}
@Override
protected boolean isMessageLocal(MessageImpl msg) {
ClusteredMessage clusteredMessage = (ClusteredMessage) msg;
return !clusteredMessage.isFromWire();
}
@Override
protected HandlerHolder nextHandler(ConcurrentCyclicSequence<HandlerHolder> handlers, boolean messageLocal) {
HandlerHolder handlerHolder = null;
if (messageLocal) {
handlerHolder = handlers.next();
} else {
Iterator<HandlerHolder> iterator = handlers.iterator(false);
while (iterator.hasNext()) {
HandlerHolder next = iterator.next();
if (next.isReplyHandler() || !next.isLocalOnly()) {
handlerHolder = next;
break;
}
}
}
return handlerHolder;
}
private int getClusterPort() {
return options.getPort();
}
private String getClusterHost() {
String host;
if ((host = options.getHost()) != null) {
return host;
}
if ((host = clusterManager.clusterHost()) != null) {
return host;
}
return AddressHelper.defaultAddress();
}
private int getClusterPublicPort(int actualPort) {
int publicPort = options.getClusterPublicPort();
return publicPort > 0 ? publicPort : actualPort;
}
private String getClusterPublicHost(String host) {
String publicHost;
if ((publicHost = options.getClusterPublicHost()) != null) {
return publicHost;
}
if ((publicHost = options.getHost()) != null) {
return publicHost;
}
if ((publicHost = clusterManager.clusterPublicHost()) != null) {
return publicHost;
}
return host;
}
private Handler<NetSocket> getServerHandler() {
return socket -> {
RecordParser parser = RecordParser.newFixed(4);
Handler<Buffer> handler = new Handler<Buffer>() {
int size = -1;
public void handle(Buffer buff) {
if (size == -1) {
size = buff.getInt(0);
parser.fixedSizeMode(size);
} else {
ClusteredMessage received = new ClusteredMessage(ClusteredEventBus.this);
received.readFromWire(buff, codecManager);
if (metrics != null) {
metrics.messageRead(received.address(), buff.length());
}
parser.fixedSizeMode(4);
size = -1;
if (received.hasFailure()) {
received.internalError();
} else if (received.codec() == CodecManager.PING_MESSAGE_CODEC) {
// Just send back pong directly on connection
socket.write(PONG);
} else {
deliverMessageLocally(received);
}
}
}
};
parser.setOutput(handler);
socket.handler(parser);
};
}
private <T> void sendToNode(OutboundDeliveryContext<T> sendContext, String nodeId) {
if (nodeId != null && !nodeId.equals(this.nodeId)) {
sendRemote(sendContext, nodeId, sendContext.message);
} else {
super.sendOrPub(sendContext);
}
}
private <T> void sendToNodes(OutboundDeliveryContext<T> sendContext, Iterable<String> nodeIds) {
boolean sentRemote = false;
if (nodeIds != null) {
for (String nid : nodeIds) {
if (!sentRemote) {
sentRemote = true;
}
sendToNode(sendContext, nid);
}
}
if (!sentRemote) {
super.sendOrPub(sendContext);
}
}
private <T> void clusteredSendReply(String replyDest, OutboundDeliveryContext<T> sendContext) {
MessageImpl message = sendContext.message;
if (!replyDest.equals(nodeId)) {
sendRemote(sendContext, replyDest, message);
} else {
super.sendOrPub(sendContext);
}
}
private void sendRemote(OutboundDeliveryContext<?> sendContext, String remoteNodeId, MessageImpl message) {
// We need to deal with the fact that connecting can take some time and is async, and we cannot
// block to wait for it. So we add any sends to a pending list if not connected yet.
// Once we connect we send them.
// This can also be invoked concurrently from different threads, so it gets a little
// tricky
ConnectionHolder holder = connections.get(remoteNodeId);
if (holder == null) {
// When process is creating a lot of connections this can take some time
// so increase the timeout
holder = new ConnectionHolder(this, remoteNodeId);
ConnectionHolder prevHolder = connections.putIfAbsent(remoteNodeId, holder);
if (prevHolder != null) {
// Another one sneaked in
holder = prevHolder;
} else {
holder.connect();
}
}
holder.writeMessage(sendContext);
}
ConcurrentMap<String, ConnectionHolder> connections() {
return connections;
}
VertxInternal vertx() {
return vertx;
}
EventBusOptions options() {
return options;
}
}