-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
IgniteClientReconnectAbstractTest.java
468 lines (373 loc) · 14.3 KB
/
IgniteClientReconnectAbstractTest.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.ignite.internal;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.cache.CacheException;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteClientDisconnectedException;
import org.apache.ignite.IgniteException;
import org.apache.ignite.IgniteLogger;
import org.apache.ignite.cluster.ClusterNode;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.events.Event;
import org.apache.ignite.internal.managers.communication.GridIoMessage;
import org.apache.ignite.internal.util.typedef.G;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.lang.IgniteFuture;
import org.apache.ignite.lang.IgniteInClosure;
import org.apache.ignite.lang.IgnitePredicate;
import org.apache.ignite.plugin.extensions.communication.Message;
import org.apache.ignite.resources.LoggerResource;
import org.apache.ignite.spi.IgniteSpiException;
import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode;
import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
import org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryAbstractMessage;
import org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryJoinRequestMessage;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.jetbrains.annotations.Nullable;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.apache.ignite.events.EventType.EVT_CLIENT_NODE_DISCONNECTED;
import static org.apache.ignite.events.EventType.EVT_CLIENT_NODE_RECONNECTED;
/**
*
*/
public abstract class IgniteClientReconnectAbstractTest extends GridCommonAbstractTest {
/** */
private static TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true);
/** */
private static final long RECONNECT_TIMEOUT = 10_000;
/** */
protected boolean clientMode;
/** {@inheritDoc} */
@Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
IgniteConfiguration cfg = super.getConfiguration(gridName);
TestTcpDiscoverySpi disco = new TestTcpDiscoverySpi();
disco.setIpFinder(ipFinder);
disco.setJoinTimeout(2 * 60_000);
disco.setSocketTimeout(1000);
disco.setNetworkTimeout(2000);
cfg.setDiscoverySpi(disco);
BlockTcpCommunicationSpi commSpi = new BlockTcpCommunicationSpi();
commSpi.setSharedMemoryPort(-1);
cfg.setCommunicationSpi(commSpi);
if (clientMode)
cfg.setClientMode(true);
return cfg;
}
/**
* @param latch Latch.
* @throws Exception If failed.
*/
protected void waitReconnectEvent(CountDownLatch latch) throws Exception {
waitReconnectEvent(log, latch);
}
/**
* @param log Logger.
* @param latch Latch.
* @throws Exception If failed.
*/
protected static void waitReconnectEvent(IgniteLogger log, CountDownLatch latch) throws Exception {
if (!latch.await(RECONNECT_TIMEOUT, MILLISECONDS)) {
log.error("Failed to wait for reconnect event, will dump threads, latch count: " + latch.getCount());
U.dumpThreads(log);
fail("Failed to wait for disconnect/reconnect event.");
}
}
/**
* @return Number of server nodes started before tests.
*/
protected abstract int serverCount();
/**
* @return Number of client nodes started before tests.
*/
protected int clientCount() {
return 0;
}
/**
* @param ignite Node.
* @return Discovery SPI.
*/
protected static TestTcpDiscoverySpi spi(Ignite ignite) {
return ((TestTcpDiscoverySpi)ignite.configuration().getDiscoverySpi());
}
/**
* @param ignite Node.
* @return Communication SPI.
*/
protected BlockTcpCommunicationSpi commSpi(Ignite ignite) {
return ((BlockTcpCommunicationSpi)ignite.configuration().getCommunicationSpi());
}
/** {@inheritDoc} */
@Override protected void beforeTestsStarted() throws Exception {
super.beforeTestsStarted();
int srvs = serverCount();
if (srvs > 0)
startGrids(srvs);
int clients = clientCount();
if (clients > 0) {
clientMode = true;
startGridsMultiThreaded(srvs, clients);
clientMode = false;
}
}
/** {@inheritDoc} */
@Override protected void afterTestsStopped() throws Exception {
super.afterTestsStopped();
stopAllGrids();
}
/**
* @param client Client.
* @return Server node client connected to.
*/
protected Ignite clientRouter(Ignite client) {
TcpDiscoveryNode node = (TcpDiscoveryNode)client.cluster().localNode();
assertTrue(node.isClient());
assertNotNull(node.clientRouterNodeId());
Ignite srv = G.ignite(node.clientRouterNodeId());
assertNotNull(srv);
return srv;
}
/**
* @param fut Future.
* @throws Exception If failed.
*/
protected void assertNotDone(IgniteInternalFuture<?> fut) throws Exception {
assertNotNull(fut);
if (fut.isDone())
fail("Future completed with result: " + fut.get());
}
/**
* Reconnect client node.
*
* @param client Client.
* @param srv Server.
* @param disconnectedC Closure which will be run when client node disconnected.
* @throws Exception If failed.
*/
protected void reconnectClientNode(Ignite client, Ignite srv, @Nullable Runnable disconnectedC)
throws Exception {
reconnectClientNodes(log, Collections.singletonList(client), srv, disconnectedC);
}
/**
* Reconnect client node.
*
* @param log Logger.
* @param client Client.
* @param srv Server.
* @param disconnectedC Closure which will be run when client node disconnected.
* @throws Exception If failed.
*/
public static void reconnectClientNode(IgniteLogger log,
Ignite client,
Ignite srv,
@Nullable Runnable disconnectedC)
throws Exception {
reconnectClientNodes(log, Collections.singletonList(client), srv, disconnectedC);
}
/**
* Reconnect client node.
*
* @param log Logger.
* @param clients Clients.
* @param srv Server.
* @param disconnectedC Closure which will be run when client node disconnected.
* @throws Exception If failed.
*/
protected static void reconnectClientNodes(final IgniteLogger log,
List<Ignite> clients, Ignite srv,
@Nullable Runnable disconnectedC)
throws Exception {
final TestTcpDiscoverySpi srvSpi = spi(srv);
final CountDownLatch disconnectLatch = new CountDownLatch(clients.size());
final CountDownLatch reconnectLatch = new CountDownLatch(clients.size());
log.info("Block reconnect.");
for (Ignite client : clients)
spi(client).writeLatch = new CountDownLatch(1);
IgnitePredicate<Event> p = new IgnitePredicate<Event>() {
@Override public boolean apply(Event evt) {
if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
log.info("Disconnected: " + evt);
disconnectLatch.countDown();
}
else if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
log.info("Reconnected: " + evt);
reconnectLatch.countDown();
}
return true;
}
};
for (Ignite client : clients)
client.events().localListen(p, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
for (Ignite client : clients)
srvSpi.failNode(client.cluster().localNode().id(), null);
waitReconnectEvent(log, disconnectLatch);
if (disconnectedC != null)
disconnectedC.run();
log.info("Allow reconnect.");
for (Ignite client : clients)
spi(client).writeLatch.countDown();
waitReconnectEvent(log, reconnectLatch);
for (Ignite client : clients)
client.events().stopLocalListen(p);
}
/**
* @param log Logger.
* @param client Client node.
* @param srvs Server nodes to stop.
* @param srvStartC Closure starting server nodes.
* @throws Exception If failed.
* @return Restarted servers.
*/
public static Collection<Ignite> reconnectServersRestart(final IgniteLogger log,
Ignite client,
Collection<Ignite> srvs,
Callable<Collection<Ignite>> srvStartC)
throws Exception {
final CountDownLatch disconnectLatch = new CountDownLatch(1);
final CountDownLatch reconnectLatch = new CountDownLatch(1);
client.events().localListen(new IgnitePredicate<Event>() {
@Override public boolean apply(Event evt) {
if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
log.info("Disconnected: " + evt);
disconnectLatch.countDown();
}
else if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
log.info("Reconnected: " + evt);
reconnectLatch.countDown();
}
return true;
}
}, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
for (Ignite srv : srvs)
srv.close();
assertTrue(disconnectLatch.await(30_000, MILLISECONDS));
Collection<Ignite> startedSrvs = srvStartC.call();
assertTrue(reconnectLatch.await(10_000, MILLISECONDS));
return startedSrvs;
}
/**
* @param e Client disconnected exception.
* @return Reconnect future.
*/
protected IgniteFuture<?> check(CacheException e) {
log.info("Expected exception: " + e);
if (!(e.getCause() instanceof IgniteClientDisconnectedException))
log.error("Unexpected cause: " + e.getCause(), e);
assertTrue("Unexpected cause: " + e.getCause(), e.getCause() instanceof IgniteClientDisconnectedException);
IgniteClientDisconnectedException e0 = (IgniteClientDisconnectedException)e.getCause();
assertNotNull(e0.reconnectFuture());
return e0.reconnectFuture();
}
/**
* @param e Client disconnected exception.
*/
protected void checkAndWait(CacheException e) {
check(e).get();
}
/**
* @param e Client disconnected exception.
*/
protected void checkAndWait(IgniteClientDisconnectedException e) {
log.info("Expected exception: " + e);
assertNotNull(e.reconnectFuture());
e.reconnectFuture().get();
}
/**
*
*/
public static class TestTcpDiscoverySpi extends TcpDiscoverySpi {
/** */
volatile CountDownLatch writeLatch;
/** {@inheritDoc} */
@Override protected void writeToSocket(Socket sock, OutputStream out, TcpDiscoveryAbstractMessage msg, long timeout)
throws IOException, IgniteCheckedException {
if (msg instanceof TcpDiscoveryJoinRequestMessage) {
CountDownLatch writeLatch0 = writeLatch;
if (writeLatch0 != null) {
log.info("Block join request send: " + msg);
U.await(writeLatch0);
}
}
super.writeToSocket(sock, out, msg, timeout);
}
}
/**
*
*/
protected static class BlockTcpCommunicationSpi extends TcpCommunicationSpi {
/** */
volatile Class msgCls;
/** */
AtomicBoolean collectStart = new AtomicBoolean(false);
/** */
ConcurrentHashMap<String, ClusterNode> classes = new ConcurrentHashMap<>();
/** */
@LoggerResource
private IgniteLogger log;
/** {@inheritDoc} */
@Override public void sendMessage(ClusterNode node, Message msg, IgniteInClosure<IgniteException> ackC)
throws IgniteSpiException {
Class msgCls0 = msgCls;
if (collectStart.get() && msg instanceof GridIoMessage)
classes.put(((GridIoMessage)msg).message().getClass().getName(), node);
if (msgCls0 != null && msg instanceof GridIoMessage
&& ((GridIoMessage)msg).message().getClass().equals(msgCls)) {
log.info("Block message: " + msg);
return;
}
super.sendMessage(node, msg, ackC);
}
/**
* @param clazz Class of messages which will be block.
*/
public void blockMessage(Class clazz) {
msgCls = clazz;
}
/**
* Unlock all message.
*/
public void unblockMessage() {
msgCls = null;
}
/**
* Start collect messages.
*/
public void start() {
collectStart.set(true);
}
/**
* Print collected messages.
*/
public void print() {
for (String s : classes.keySet())
log.error(s);
}
}
}