-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
ClientReconnectTest.java
346 lines (298 loc) · 13.9 KB
/
ClientReconnectTest.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
/*
* 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;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.spi.properties.ClientProperty;
import com.hazelcast.client.test.TestHazelcastFactory;
import com.hazelcast.config.Config;
import com.hazelcast.config.SerializationConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.HazelcastInstanceNotActiveException;
import com.hazelcast.core.IMap;
import com.hazelcast.core.LifecycleEvent;
import com.hazelcast.core.LifecycleListener;
import com.hazelcast.core.Member;
import com.hazelcast.core.MembershipAdapter;
import com.hazelcast.core.MembershipEvent;
import com.hazelcast.nio.Address;
import com.hazelcast.nio.serialization.Portable;
import com.hazelcast.nio.serialization.PortableFactory;
import com.hazelcast.nio.serialization.PortableReader;
import com.hazelcast.security.UsernamePasswordCredentials;
import com.hazelcast.test.AssertTask;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.ParallelTest;
import com.hazelcast.test.annotation.QuickTest;
import com.hazelcast.test.annotation.SlowTest;
import org.junit.After;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import java.io.IOException;
import java.util.Iterator;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class, ParallelTest.class})
public class ClientReconnectTest extends HazelcastTestSupport {
private final TestHazelcastFactory hazelcastFactory = new TestHazelcastFactory();
@After
public void cleanup() {
hazelcastFactory.terminateAll();
}
@Test
public void testClientReconnectOnClusterDown() throws Exception {
final HazelcastInstance h1 = hazelcastFactory.newHazelcastInstance();
ClientConfig clientConfig = new ClientConfig();
clientConfig.getNetworkConfig().setConnectionAttemptLimit(Integer.MAX_VALUE);
final HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
final CountDownLatch connectedLatch = new CountDownLatch(2);
client.getLifecycleService().addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
connectedLatch.countDown();
}
});
IMap<String, String> m = client.getMap("default");
h1.shutdown();
hazelcastFactory.newHazelcastInstance();
assertOpenEventually(connectedLatch);
assertNull(m.put("test", "test"));
assertEquals("test", m.get("test"));
}
@Test
public void testReconnectToNewInstanceAtSameAddress() throws InterruptedException {
HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
Address localAddress = instance.getCluster().getLocalMember().getAddress();
ClientConfig clientConfig = new ClientConfig();
clientConfig.getNetworkConfig().setConnectionAttemptLimit(Integer.MAX_VALUE);
final HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
final CountDownLatch memberRemovedLatch = new CountDownLatch(1);
client.getCluster().addMembershipListener(new MembershipAdapter() {
@Override
public void memberRemoved(MembershipEvent membershipEvent) {
memberRemovedLatch.countDown();
}
});
instance.shutdown();
final HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance(localAddress);
assertOpenEventually(memberRemovedLatch);
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertClusterSize(1, client);
Iterator<Member> iterator = client.getCluster().getMembers().iterator();
Member member = iterator.next();
assertEquals(instance2.getCluster().getLocalMember(), member);
}
});
}
@Test
public void testClientShutdownIfReconnectionNotPossible() {
HazelcastInstance server = hazelcastFactory.newHazelcastInstance();
ClientConfig clientConfig = new ClientConfig();
clientConfig.getNetworkConfig().setConnectionAttemptLimit(1);
HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
final CountDownLatch shutdownLatch = new CountDownLatch(1);
client.getLifecycleService().addLifecycleListener(new LifecycleListener() {
@Override
public void stateChanged(LifecycleEvent event) {
if (event.getState() == LifecycleEvent.LifecycleState.SHUTDOWN) {
shutdownLatch.countDown();
}
}
});
server.shutdown();
assertOpenEventually(shutdownLatch);
}
@Test(expected = HazelcastClientNotActiveException.class)
public void testRequestShouldFailOnShutdown() {
final HazelcastInstance server = hazelcastFactory.newHazelcastInstance();
ClientConfig clientConfig = new ClientConfig();
clientConfig.getNetworkConfig().setConnectionAttemptLimit(1);
HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
IMap<Object, Object> test = client.getMap("test");
test.put("key", "value");
server.shutdown();
test.get("key");
}
@Test(expected = HazelcastClientNotActiveException.class)
public void testExceptionAfterClientShutdown() {
hazelcastFactory.newHazelcastInstance();
ClientConfig clientConfig = new ClientConfig();
HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
IMap<Object, Object> test = client.getMap("test");
test.put("key", "value");
client.shutdown();
//to force weak references to be cleaned and get not active exception from serialization service
System.gc();
test.get("key");
}
@Test(expected = HazelcastClientNotActiveException.class)
public void testExceptionAfterClientShutdown_fromClientConnectionManager() {
hazelcastFactory.newHazelcastInstance();
ClientConfig clientConfig = new ClientConfig();
HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
IMap<Object, Object> test = client.getMap("test");
test.put("key", "value");
client.shutdown();
test.size();
}
@Test
public void testShutdownClient_whenThereIsNoCluster() {
ClientConfig clientConfig = new ClientConfig();
clientConfig.getConnectionStrategyConfig().setAsyncStart(true);
clientConfig.getNetworkConfig()
.setConnectionAttemptLimit(Integer.MAX_VALUE);
HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
client.shutdown();
}
public abstract static class CustomCredentials extends UsernamePasswordCredentials {
CustomCredentials() {
}
CustomCredentials(String username, String password) {
super(username, password);
}
@Override
public int getFactoryId() {
return 1;
}
@Override
public int getClassId() {
return 1;
}
}
public static class CustomCredentials_takesLong extends CustomCredentials {
private AtomicInteger count;
private long sleepMillis;
CustomCredentials_takesLong(AtomicInteger count, long sleepMillis) {
this.count = count;
this.sleepMillis = sleepMillis;
}
CustomCredentials_takesLong(String username, String password) {
super(username, password);
}
@Override
protected void readPortableInternal(PortableReader reader) throws IOException {
if (count.incrementAndGet() == 1) {
try {
Thread.sleep(sleepMillis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
super.readPortableInternal(reader);
}
}
@Test
public void testClientConnected_withFirstAuthenticationTakingLong() throws InterruptedException {
final AtomicInteger customCredentialsRunCount = new AtomicInteger();
final long customCredentialsSleepMillis = 30000;
SerializationConfig serializationConfig = new SerializationConfig();
serializationConfig.addPortableFactory(1, new PortableFactory() {
@Override
public Portable create(int classId) {
return new CustomCredentials_takesLong(customCredentialsRunCount, customCredentialsSleepMillis);
}
});
Config config = new Config();
config.setSerializationConfig(serializationConfig);
hazelcastFactory.newHazelcastInstance(config);
ClientConfig clientConfig = new ClientConfig();
clientConfig.setCredentials(new CustomCredentials_takesLong("dev", "dev-pass"));
clientConfig.setSerializationConfig(serializationConfig);
hazelcastFactory.newHazelcastClient(clientConfig);
}
@Test
@Category(SlowTest.class)
public void testEndpointCleanup_withFirstAuthenticationTakingLong() throws InterruptedException {
final AtomicInteger customCredentialsRunCount = new AtomicInteger();
ClientConfig clientConfig = new ClientConfig();
int heartbeatTimeoutMillis = 5000;
int authenticationTimeout = heartbeatTimeoutMillis;
clientConfig.setProperty(ClientProperty.HEARTBEAT_TIMEOUT.getName(), String.valueOf(heartbeatTimeoutMillis));
clientConfig.setProperty(ClientProperty.HEARTBEAT_INTERVAL.getName(), "1000");
//Credentials will take longer than connection timeout so that client will close the connection
final long customCredentialsSleepMillis = authenticationTimeout * 2;
SerializationConfig serializationConfig = new SerializationConfig();
serializationConfig.addPortableFactory(1, new PortableFactory() {
@Override
public Portable create(int classId) {
return new CustomCredentials_takesLong(customCredentialsRunCount, customCredentialsSleepMillis);
}
});
Config config = new Config();
config.setSerializationConfig(serializationConfig);
final HazelcastInstance instance = hazelcastFactory.newHazelcastInstance(config);
//make sure credentials that takes long is used
clientConfig.setCredentials(new CustomCredentials_takesLong("dev", "dev-pass"));
clientConfig.setSerializationConfig(serializationConfig);
//make sure that client is not connected back after first attempt
clientConfig.getNetworkConfig().setConnectionAttemptLimit(1);
//since client will not be connected, don't block the current test thread
clientConfig.getConnectionStrategyConfig().setAsyncStart(true);
hazelcastFactory.newHazelcastClient(clientConfig);
//Wait for custom credentials to wake up and register endpoint for timed out connection
Thread.sleep(customCredentialsSleepMillis + 3000);
// registered endpoint should be removed eventually
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
int size = instance.getClientService().getConnectedClients().size();
assertEquals(0, size);
}
});
}
public static class CustomCredentials_retried extends CustomCredentials {
private static AtomicInteger count = new AtomicInteger();
public CustomCredentials_retried() {
}
CustomCredentials_retried(String username, String password) {
super(username, password);
}
@Override
public String getPassword() {
if (count.incrementAndGet() == 1) {
throw new HazelcastInstanceNotActiveException();
}
return super.getPassword();
}
}
@Test
public void testClientConnected_withFirstAuthenticationRetried() throws InterruptedException {
SerializationConfig serializationConfig = new SerializationConfig();
serializationConfig.addPortableFactory(1, new PortableFactory() {
@Override
public Portable create(int classId) {
return new CustomCredentials_retried();
}
});
Config config = new Config();
config.setSerializationConfig(serializationConfig);
hazelcastFactory.newHazelcastInstance(config);
ClientConfig clientConfig = new ClientConfig();
//first authentication should be able to retried by invocation system. No need to do a second invocation.
//By setting attempt limit to one, we are expecting client to connect in first attempt.
clientConfig.getNetworkConfig().setConnectionTimeout(30000);
clientConfig.getNetworkConfig().setConnectionAttemptLimit(1);
clientConfig.setCredentials(new CustomCredentials_retried("dev", "dev-pass"));
clientConfig.setSerializationConfig(serializationConfig);
hazelcastFactory.newHazelcastClient(clientConfig);
}
}