-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
MockServer.java
411 lines (343 loc) · 15.2 KB
/
MockServer.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
/*
* Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.test.mocknetwork;
import com.hazelcast.cluster.Address;
import com.hazelcast.cluster.Member;
import com.hazelcast.instance.EndpointQualifier;
import com.hazelcast.instance.impl.Node;
import com.hazelcast.instance.impl.NodeState;
import com.hazelcast.internal.nio.Connection;
import com.hazelcast.internal.nio.ConnectionLifecycleListener;
import com.hazelcast.internal.nio.ConnectionListener;
import com.hazelcast.internal.nio.Packet;
import com.hazelcast.internal.server.NetworkStats;
import com.hazelcast.internal.server.Server;
import com.hazelcast.internal.server.ServerConnection;
import com.hazelcast.internal.server.ServerConnectionManager;
import com.hazelcast.internal.server.ServerContext;
import com.hazelcast.internal.util.concurrent.ThreadFactoryImpl;
import com.hazelcast.internal.util.executor.StripedRunnable;
import com.hazelcast.logging.ILogger;
import com.hazelcast.spi.impl.executionservice.ExecutionService;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import static com.hazelcast.internal.util.ThreadUtil.createThreadPoolName;
import static com.hazelcast.test.HazelcastTestSupport.suspectMember;
import static java.util.Collections.singletonMap;
class MockServer implements Server {
private static final int RETRY_NUMBER = 5;
private static final int DELAY_FACTOR = 100;
private final ConcurrentMap<Address, MockServerConnection> connectionMap = new ConcurrentHashMap<>(10);
private final TestNodeRegistry nodeRegistry;
private final Node node;
private final ScheduledExecutorService scheduler;
private final ServerContext serverContext;
private final ILogger logger;
private final ServerConnectionManager connectionManager;
private volatile boolean live;
MockServer(ServerContext serverContext, Node node, TestNodeRegistry testNodeRegistry) {
this.serverContext = serverContext;
this.nodeRegistry = testNodeRegistry;
this.node = node;
this.connectionManager = new MockServerConnectionManager(this);
this.scheduler = new ScheduledThreadPoolExecutor(4,
new ThreadFactoryImpl(createThreadPoolName(serverContext.getHazelcastName(), "MockConnectionManager")));
this.logger = serverContext.getLoggingService().getLogger(MockServer.class);
}
static class MockServerConnectionManager
implements ServerConnectionManager {
private final MockServer server;
private final ConnectionLifecycleListener lifecycleListener = new MockServerConnectionManager.MockConnLifecycleListener();
private final Set<ConnectionListener> connectionListeners = new CopyOnWriteArraySet<>();
MockServerConnectionManager(MockServer server) {
this.server = server;
}
@Override
public Server getServer() {
return server;
}
@Override
public MockServerConnection get(Address address, int stream) {
return server.connectionMap.get(address);
}
@Override
public MockServerConnection getOrConnect(Address address, int stream) {
MockServerConnection conn = server.connectionMap.get(address);
if (conn != null && conn.isAlive()) {
return conn;
}
if (!server.live) {
return null;
}
Node targetNode = server.nodeRegistry.getNode(address);
if (targetNode == null || isTargetLeft(targetNode)) {
suspectAddress(address);
return null;
}
return createConnection(targetNode);
}
@Override
public void accept(Packet packet) {
}
private void suspectAddress(final Address address) {
// see ServerContext#removeEndpoint()
server.node.getNodeEngine().getExecutionService().execute(ExecutionService.IO_EXECUTOR,
() -> server.node.getClusterService().suspectAddressIfNotConnected(address));
}
public static boolean isTargetLeft(Node targetNode) {
return !targetNode.isRunning() && !targetNode.getClusterService().isJoined();
}
private synchronized MockServerConnection createConnection(Node targetNode) {
if (!server.live) {
throw new IllegalStateException("connection manager is not live!");
}
Node node = server.node;
Address local = node.getThisAddress();
Address remote = targetNode.getThisAddress();
MockServerConnection thisConnection = new MockServerConnection(lifecycleListener, remote, local,
node.getNodeEngine(), targetNode.getServer().getConnectionManager(EndpointQualifier.MEMBER));
MockServerConnection remoteConnection = new MockServerConnection(lifecycleListener, local, remote,
targetNode.getNodeEngine(), node.getServer().getConnectionManager(EndpointQualifier.MEMBER));
remoteConnection.localConnection = thisConnection;
thisConnection.localConnection = remoteConnection;
if (!remoteConnection.isAlive()) {
// targetNode is not alive anymore.
suspectAddress(remote);
return null;
}
server.connectionMap.put(remote, remoteConnection);
server.logger.info("Created connection to endpoint: " + remote + ", connection: " + remoteConnection);
if (!remoteConnection.isAlive()) {
// If connection is not alive after inserting it into connection map,
// that means remote node is being stopping during connection creation.
suspectAddress(remote);
}
return remoteConnection;
}
@Override
public MockServerConnection getOrConnect(Address address, boolean silent, int stream) {
return getOrConnect(address, stream);
}
@Override
public synchronized boolean register(final Address remoteAddress, final ServerConnection c, int streamId) {
MockServerConnection connection = (MockServerConnection) c;
if (!server.live) {
throw new IllegalStateException("connection manager is not live!");
}
if (!connection.isAlive()) {
return false;
}
connection.setLifecycleListener(lifecycleListener);
server.connectionMap.put(remoteAddress, connection);
server.serverContext.getEventService().executeEventCallback(new StripedRunnable() {
@Override
public void run() {
for (ConnectionListener listener : connectionListeners) {
listener.connectionAdded(connection);
}
}
@Override
public int getKey() {
return remoteAddress.hashCode();
}
});
return true;
}
@Override
public void addConnectionListener(ConnectionListener connectionListener) {
connectionListeners.add(connectionListener);
}
private void fireConnectionRemovedEvent(final MockServerConnection connection, final Address endPoint) {
if (server.live) {
server.serverContext.getEventService().executeEventCallback(new StripedRunnable() {
@Override
public void run() {
connectionListeners.forEach(listener -> listener.connectionRemoved(connection));
}
@Override
public int getKey() {
return endPoint.hashCode();
}
});
}
}
@Override
public @Nonnull Collection getConnections() {
return server.connectionMap.values();
}
@Override
public int connectionCount(Predicate<ServerConnection> predicate) {
return (int) server.connectionMap.values().stream().filter(predicate).count();
}
/**
* Retries sending packet maximum 5 times until connection to target becomes available.
*/
@Override
public boolean transmit(Packet packet, Address target, int streamId) {
return send(packet, target, null);
}
private boolean send(Packet packet, Address target, SendTask sendTask) {
MockServerConnection connection = get(target, 0);
if (connection != null) {
return connection.write(packet);
}
if (sendTask == null) {
sendTask = new SendTask(packet, target);
}
int retries = sendTask.retries.get();
if (retries < RETRY_NUMBER && server.serverContext.isNodeActive()) {
getOrConnect(target, true);
// TODO: Caution: may break the order guarantee of the packets sent from the same thread!
try {
server.scheduler.schedule(sendTask, (retries + 1) * DELAY_FACTOR, TimeUnit.MILLISECONDS);
} catch (RejectedExecutionException e) {
if (server.live) {
throw e;
}
if (server.logger.isFinestEnabled()) {
server.logger.finest("Packet send task is rejected. Packet cannot be sent to " + target);
}
}
return true;
}
return false;
}
@Override
public NetworkStats getNetworkStats() {
return new MockNetworkStats();
}
private class MockConnLifecycleListener
implements ConnectionLifecycleListener<MockServerConnection> {
@Override
public void onConnectionClose(MockServerConnection connection, Throwable t, boolean silent) {
final Address endPoint = connection.getRemoteAddress();
if (!server.connectionMap.remove(endPoint, connection)) {
return;
}
Server server = connection.remoteNodeEngine.getNode().getServer();
// all mock implementations of networking service ignore the provided endpoint qualifier
// so we pass in null. Once they are changed to use the parameter, we should be notified
// and this parameter can be changed
Connection remoteConnection = server.getConnectionManager(null)
.get(connection.localAddress);
if (remoteConnection != null) {
remoteConnection.close("Connection closed by the other side", null);
}
MockServerConnectionManager.this.server.logger.info("Removed connection to endpoint: " + endPoint + ", connection: " + connection);
fireConnectionRemovedEvent(connection, endPoint);
}
}
private final class SendTask implements Runnable {
private final AtomicInteger retries = new AtomicInteger();
private final Packet packet;
private final Address target;
private SendTask(Packet packet, Address target) {
this.packet = packet;
this.target = target;
}
@Override
public void run() {
int actualRetries = retries.incrementAndGet();
if (server.logger.isFinestEnabled()) {
server.logger.finest("Retrying[" + actualRetries + "] packet send operation to: " + target);
}
send(packet, target, this);
}
}
private static class MockNetworkStats implements NetworkStats {
@Override
public long getBytesReceived() {
return 0;
}
@Override
public long getBytesSent() {
return 0;
}
}
}
@Override
public ServerContext getContext() {
return serverContext;
}
@Override
public @Nonnull Collection<ServerConnection> getConnections() {
return connectionManager.getConnections();
}
@Override
public Map<EndpointQualifier, NetworkStats> getNetworkStats() {
return singletonMap(EndpointQualifier.MEMBER, connectionManager.getNetworkStats());
}
@Override
public void addConnectionListener(ConnectionListener<ServerConnection> listener) {
connectionManager.addConnectionListener(listener);
}
@Override
public ServerConnectionManager getConnectionManager(EndpointQualifier qualifier) {
return connectionManager;
}
@Override
public boolean isLive() {
return live;
}
@Override
public synchronized void start() {
logger.fine("Starting connection manager");
live = true;
}
@Override
public synchronized void stop() {
if (!live) {
return;
}
logger.fine("Stopping connection manager");
live = false;
connectionMap.values().forEach(connection -> connection.close(null, null));
connectionMap.clear();
final Member localMember = node.getLocalMember();
final Address thisAddress = localMember.getAddress();
for (Address address : nodeRegistry.getAddresses()) {
if (address.equals(thisAddress)) {
continue;
}
Node otherNode = nodeRegistry.getNode(address);
if (otherNode != null && otherNode.getState() != NodeState.SHUT_DOWN) {
logger.fine(otherNode.getThisAddress() + " is instructed to suspect from " + thisAddress);
try {
suspectMember(otherNode, node, "Connection manager is stopped on " + localMember);
} catch (Throwable e) {
ILogger otherLogger = otherNode.getLogger(MockServer.class);
otherLogger.warning("While removing " + thisAddress, e);
}
}
}
}
@Override
public synchronized void shutdown() {
stop();
scheduler.shutdownNow();
}
}