forked from hazelcast/hazelcast
/
ClientHeartbeatTest.java
426 lines (357 loc) · 18 KB
/
ClientHeartbeatTest.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
/*
* Copyright (c) 2008-2018, 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.client.heartbeat;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.connection.ClientConnectionManager;
import com.hazelcast.client.impl.HazelcastClientInstanceImpl;
import com.hazelcast.client.impl.protocol.ClientMessage;
import com.hazelcast.client.impl.protocol.codec.ClientAddPartitionLostListenerCodec;
import com.hazelcast.client.impl.protocol.codec.ClientRemovePartitionLostListenerCodec;
import com.hazelcast.client.spi.ClientListenerService;
import com.hazelcast.client.spi.EventHandler;
import com.hazelcast.client.spi.impl.ConnectionHeartbeatListener;
import com.hazelcast.client.spi.impl.ListenerMessageCodec;
import com.hazelcast.client.spi.properties.ClientProperty;
import com.hazelcast.client.test.ClientTestSupport;
import com.hazelcast.client.test.TestHazelcastFactory;
import com.hazelcast.config.Config;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.core.LifecycleEvent;
import com.hazelcast.core.LifecycleListener;
import com.hazelcast.core.LifecycleService;
import com.hazelcast.core.Member;
import com.hazelcast.core.Partition;
import com.hazelcast.logging.Logger;
import com.hazelcast.nio.Connection;
import com.hazelcast.spi.exception.TargetDisconnectedException;
import com.hazelcast.spi.properties.GroupProperty;
import com.hazelcast.test.AssertTask;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.annotation.ParallelTest;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class, ParallelTest.class})
public class ClientHeartbeatTest extends ClientTestSupport {
private static final int HEARTBEAT_TIMEOUT_MILLIS = 10000;
private TestHazelcastFactory hazelcastFactory = new TestHazelcastFactory();
@Rule
public ExpectedException expectedException = ExpectedException.none();
@After
public void cleanup() {
hazelcastFactory.terminateAll();
}
@Test
public void testHeartbeatStoppedEvent() throws InterruptedException {
HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
HazelcastClientInstanceImpl clientImpl = getHazelcastClientInstanceImpl(client);
ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
final CountDownLatch countDownLatch = new CountDownLatch(1);
connectionManager.addConnectionHeartbeatListener(new ConnectionHeartbeatListener() {
@Override
public void heartbeatResumed(Connection connection) {
}
@Override
public void heartbeatStopped(Connection connection) {
countDownLatch.countDown();
}
});
blockMessagesFromInstance(instance, client);
assertOpenEventually(countDownLatch);
}
@Test
public void testHeartbeatResumedEvent() {
hazelcastFactory.newHazelcastInstance();
HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
final HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();
// make sure client is connected to instance2
String keyOwnedByInstance2 = generateKeyOwnedBy(instance2);
IMap<String, String> map = client.getMap(randomString());
map.put(keyOwnedByInstance2, randomString());
HazelcastClientInstanceImpl clientImpl = getHazelcastClientInstanceImpl(client);
final ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
final CountDownLatch countDownLatch = new CountDownLatch(1);
final AtomicLong stoppedCount = new AtomicLong();
final AtomicLong resumedCount = new AtomicLong();
connectionManager.addConnectionHeartbeatListener(new ConnectionHeartbeatListener() {
@Override
public void heartbeatResumed(Connection connection) {
assertEquals(instance2.getCluster().getLocalMember().getAddress(), connection.getEndPoint());
countDownLatch.countDown();
resumedCount.incrementAndGet();
}
@Override
public void heartbeatStopped(Connection connection) {
stoppedCount.incrementAndGet();
}
});
assertTrueEventually(new AssertTask() {
@Override
public void run() {
assertNotNull(connectionManager.getActiveConnection(instance2.getCluster().getLocalMember().getAddress()));
}
});
blockMessagesFromInstance(instance2, client);
sleepMillis(HEARTBEAT_TIMEOUT_MILLIS * 2);
unblockMessagesFromInstance(instance2, client);
assertOpenEventually(countDownLatch);
assertEquals(1, resumedCount.get());
assertEquals(1, stoppedCount.get());
}
@Test
public void testInvocation_whenHeartbeatStopped() throws InterruptedException {
hazelcastFactory.newHazelcastInstance();
final HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
final HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();
// Make sure that the partitions are updated as expected with the new member
assertTrueEventually(new AssertTask() {
@Override
public void run()
throws Exception {
Member instance2Member = instance2.getCluster().getLocalMember();
Set<Partition> partitions = client.getPartitionService().getPartitions();
boolean found = false;
for (Partition p : partitions) {
if (p.getOwner().equals(instance2Member)) {
found = true;
break;
}
}
assertTrue(found);
}
});
// make sure client is connected to instance2
String keyOwnedByInstance2 = generateKeyOwnedBy(instance2);
IMap<String, String> map = client.getMap(randomString());
map.put(keyOwnedByInstance2, randomString());
blockMessagesFromInstance(instance2, client);
expectedException.expect(TargetDisconnectedException.class);
expectedException.expectMessage(containsString("Heartbeat"));
map.put(keyOwnedByInstance2, randomString());
}
@Test
public void testAsyncInvocation_whenHeartbeatStopped() throws Throwable {
hazelcastFactory.newHazelcastInstance();
HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();
// make sure client is connected to instance2
IMap<String, String> map = client.getMap(randomString());
String keyOwnedByInstance2 = generateKeyOwnedBy(instance2);
map.put(keyOwnedByInstance2, randomString());
blockMessagesFromInstance(instance2, client);
expectedException.expect(TargetDisconnectedException.class);
expectedException.expectMessage(containsString("Heartbeat"));
try {
map.putAsync(keyOwnedByInstance2, randomString()).get();
} catch (ExecutionException e) {
//unwrap exception
throw e.getCause();
}
}
@Test
public void testInvocation_whenHeartbeatResumed() throws InterruptedException {
hazelcastFactory.newHazelcastInstance();
HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();
// make sure client is connected to instance2
String keyOwnedByInstance2 = generateKeyOwnedBy(instance2);
IMap<String, String> map = client.getMap(randomString());
map.put(keyOwnedByInstance2, randomString());
blockMessagesFromInstance(instance2, client);
sleepMillis(HEARTBEAT_TIMEOUT_MILLIS * 2);
unblockMessagesFromInstance(instance2, client);
map.put(keyOwnedByInstance2, randomString());
}
private static ClientConfig getClientConfig() {
ClientConfig clientConfig = new ClientConfig();
clientConfig.setProperty(ClientProperty.HEARTBEAT_TIMEOUT.getName(), String.valueOf(HEARTBEAT_TIMEOUT_MILLIS));
clientConfig.setProperty(ClientProperty.HEARTBEAT_INTERVAL.getName(), "500");
return clientConfig;
}
@Test
public void testAuthentication_whenHeartbeatResumed() throws Exception {
HazelcastInstance hazelcastInstance = hazelcastFactory.newHazelcastInstance();
ClientConfig config = new ClientConfig();
config.setProperty(ClientProperty.SHUFFLE_MEMBER_LIST.getName(), "false");
final HazelcastInstance client = hazelcastFactory.newHazelcastClient(config);
HazelcastClientInstanceImpl hazelcastClientInstanceImpl = getHazelcastClientInstanceImpl(client);
final ClientConnectionManager clientConnectionManager = hazelcastClientInstanceImpl.getConnectionManager();
final CountDownLatch countDownLatch = new CountDownLatch(2);
client.getLifecycleService().addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
countDownLatch.countDown();
}
});
final HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();
blockMessagesFromInstance(instance2, client);
final HazelcastInstance instance3 = hazelcastFactory.newHazelcastInstance();
hazelcastInstance.shutdown();
//wait for disconnect from instance1 since it is shutdown // CLIENT_DISCONNECTED event
//and wait for connect to from instance3 // CLIENT_CONNECTED event
assertOpenEventually(countDownLatch);
//verify and wait for authentication to 3 is complete
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
String uuid = instance3.getLocalEndpoint().getUuid();
assertEquals(uuid, getClientEngineImpl(instance3).getOwnerUuid(client.getLocalEndpoint().getUuid()));
assertEquals(uuid, getClientEngineImpl(instance2).getOwnerUuid(client.getLocalEndpoint().getUuid()));
assertEquals(uuid, clientConnectionManager.getPrincipal().getOwnerUuid());
assertEquals(instance3.getCluster().getLocalMember().getAddress(), clientConnectionManager.getOwnerConnectionAddress());
}
});
//unblock instance 2 for authentication response.
unblockMessagesFromInstance(instance2, client);
//late authentication response from instance2 should not be able to change state in both client and cluster
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
String uuid = instance3.getLocalEndpoint().getUuid();
assertEquals(uuid, getClientEngineImpl(instance3).getOwnerUuid(client.getLocalEndpoint().getUuid()));
assertEquals(uuid, getClientEngineImpl(instance2).getOwnerUuid(client.getLocalEndpoint().getUuid()));
assertEquals(uuid, clientConnectionManager.getPrincipal().getOwnerUuid());
assertEquals(instance3.getCluster().getLocalMember().getAddress(), clientConnectionManager.getOwnerConnectionAddress());
}
});
}
@Test
public void testClientEndpointsDelaySeconds_whenHeartbeatResumed() throws Exception {
int delaySeconds = 2;
Config config = new Config();
config.setProperty(GroupProperty.CLIENT_ENDPOINT_REMOVE_DELAY_SECONDS.getName(), String.valueOf(delaySeconds));
HazelcastInstance hazelcastInstance = hazelcastFactory.newHazelcastInstance(config);
HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
final CountDownLatch disconnectedLatch = new CountDownLatch(1);
LifecycleService lifecycleService = client.getLifecycleService();
lifecycleService.addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
if (LifecycleEvent.LifecycleState.CLIENT_DISCONNECTED == event.getState()) {
disconnectedLatch.countDown();
}
}
});
blockMessagesFromInstance(hazelcastInstance, client);
//Wait for client to disconnect because of hearBeat problem.
assertOpenEventually(disconnectedLatch);
final CountDownLatch connectedLatch = new CountDownLatch(1);
final AtomicLong stateChangeCount = new AtomicLong();
lifecycleService.addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
stateChangeCount.incrementAndGet();
Logger.getLogger(this.getClass()).info("state event: " + event);
if (LifecycleEvent.LifecycleState.CLIENT_CONNECTED == event.getState()) {
connectedLatch.countDown();
}
}
});
unblockMessagesFromInstance(hazelcastInstance, client);
//Wait for client to connect back after heartbeat issue is resolved
assertOpenEventually(connectedLatch);
//After client connected, there should not be further change in client state
//We are specifically testing for scheduled ClientDisconnectionOperation not to take action when run
assertTrueAllTheTime(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(1, stateChangeCount.get());
}
}, delaySeconds * 2);
}
@Test
public void testAddingListenerToNewConnectionFailedBecauseOfHeartbeat() throws Exception {
hazelcastFactory.newHazelcastInstance();
final HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
HazelcastClientInstanceImpl clientInstanceImpl = getHazelcastClientInstanceImpl(client);
final ClientListenerService clientListenerService = clientInstanceImpl.getListenerService();
final CountDownLatch blockIncoming = new CountDownLatch(1);
final CountDownLatch heartbeatStopped = new CountDownLatch(1);
final CountDownLatch onListenerRegister = new CountDownLatch(2);
clientInstanceImpl.getConnectionManager().addConnectionHeartbeatListener(new ConnectionHeartbeatListener() {
@Override
public void heartbeatResumed(Connection connection) {
}
@Override
public void heartbeatStopped(Connection connection) {
heartbeatStopped.countDown();
}
});
clientListenerService.registerListener(createPartitionLostListenerCodec(), new EventHandler() {
AtomicInteger count = new AtomicInteger(0);
@Override
public void handle(Object event) {
}
@Override
public void beforeListenerRegister() {
if (count.incrementAndGet() == 2) {
try {
blockIncoming.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void onListenerRegister() {
onListenerRegister.countDown();
}
});
HazelcastInstance hazelcastInstance2 = hazelcastFactory.newHazelcastInstance();
assertSizeEventually(2, clientInstanceImpl.getConnectionManager().getActiveConnections());
blockMessagesFromInstance(hazelcastInstance2, client);
assertOpenEventually(heartbeatStopped);
blockIncoming.countDown();
unblockMessagesFromInstance(hazelcastInstance2, client);
assertOpenEventually(onListenerRegister);
}
private ListenerMessageCodec createPartitionLostListenerCodec() {
return new ListenerMessageCodec() {
@Override
public ClientMessage encodeAddRequest(boolean localOnly) {
return ClientAddPartitionLostListenerCodec.encodeRequest(localOnly);
}
@Override
public String decodeAddResponse(ClientMessage clientMessage) {
return ClientAddPartitionLostListenerCodec.decodeResponse(clientMessage).response;
}
@Override
public ClientMessage encodeRemoveRequest(String realRegistrationId) {
return ClientRemovePartitionLostListenerCodec.encodeRequest(realRegistrationId);
}
@Override
public boolean decodeRemoveResponse(ClientMessage clientMessage) {
return ClientRemovePartitionLostListenerCodec.decodeResponse(clientMessage).response;
}
};
}
}