forked from hazelcast/hazelcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpIpJoinTest.java
334 lines (289 loc) · 14.6 KB
/
TcpIpJoinTest.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
/*
* Copyright (c) 2008-2020, 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.cluster;
import com.hazelcast.cluster.impl.TcpIpJoiner;
import com.hazelcast.config.Config;
import com.hazelcast.config.InterfacesConfig;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.NetworkConfig;
import com.hazelcast.config.PartitionGroupConfig;
import com.hazelcast.config.TcpIpConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.instance.HazelcastInstanceFactory;
import com.hazelcast.instance.TestUtil;
import com.hazelcast.nio.Address;
import com.hazelcast.spi.properties.GroupProperty;
import com.hazelcast.test.AssertTask;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@RunWith(HazelcastSerialClassRunner.class)
@Category(QuickTest.class)
public class TcpIpJoinTest extends AbstractJoinTest {
@Before
@After
public void killAllHazelcastInstances() throws IOException {
HazelcastInstanceFactory.terminateAll();
}
@Test
public void test_whenNoExplicitPortConfigured() throws Exception {
Config config = new Config();
NetworkConfig networkConfig = config.getNetworkConfig();
JoinConfig join = networkConfig.getJoin();
join.getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
tcpIpConfig.setEnabled(true);
tcpIpConfig.addMember("127.0.0.1");
testJoin(config);
}
@Test
public void test_whenExplicitPortConfigured() throws Exception {
Config config = new Config();
NetworkConfig networkConfig = config.getNetworkConfig();
JoinConfig join = networkConfig.getJoin();
join.getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
tcpIpConfig.setEnabled(true);
tcpIpConfig.addMember("127.0.0.1:5701");
tcpIpConfig.addMember("127.0.0.1:5702");
testJoin(config);
}
@Test
public void test_whenPortAndInterfacesConfigured() throws Exception {
Config config = new Config();
NetworkConfig networkConfig = config.getNetworkConfig();
JoinConfig join = networkConfig.getJoin();
join.getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
tcpIpConfig.setEnabled(true);
tcpIpConfig.addMember("127.0.0.1:5701");
tcpIpConfig.addMember("127.0.0.1:5702");
InterfacesConfig interfaces = networkConfig.getInterfaces();
interfaces.setEnabled(true);
interfaces.addInterface("127.0.0.1");
testJoin(config);
}
@Test
public void test_whenHostAndInterfacesConfigured() throws Exception {
System.clearProperty("hazelcast.local.localAddress");
final Config config = new Config();
config.setProperty("hazelcast.socket.bind.any", "false");
final NetworkConfig networkConfig = config.getNetworkConfig();
networkConfig.setPort(5701).setPortAutoIncrement(true)
.getInterfaces().addInterface("127.0.0.1").setEnabled(true);
final JoinConfig joinConfig = networkConfig.getJoin();
joinConfig.getMulticastConfig().setEnabled(false);
joinConfig.getAwsConfig().setEnabled(false);
joinConfig.getTcpIpConfig().addMember("localhost:5701").addMember("localhost:5702").setEnabled(true);
testJoin(config);
}
@Test
public void test_whenDifferentBuildNumber() {
Config config = new Config();
NetworkConfig networkConfig = config.getNetworkConfig();
JoinConfig join = networkConfig.getJoin();
join.getMulticastConfig().setEnabled(false);
join.getTcpIpConfig().setEnabled(true);
join.getTcpIpConfig().addMember("127.0.0.1");
testJoin_With_DifferentBuildNumber(config);
}
@Test
public void test_whenHostUnresolvable() {
Config config = new Config();
NetworkConfig networkConfig = config.getNetworkConfig();
JoinConfig join = networkConfig.getJoin();
join.getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
tcpIpConfig.setEnabled(true);
tcpIpConfig.addMember("localhost");
tcpIpConfig.addMember("nonexistinghost");
HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
assertClusterSize(1, hz);
}
@Test
public void test_whenIncompatibleGroups() throws Exception {
Config config1 = new Config();
config1.setProperty(GroupProperty.WAIT_SECONDS_BEFORE_JOIN.getName(), "0");
config1.setProperty(GroupProperty.MAX_JOIN_SECONDS.getName(), "3");
config1.getGroupConfig().setName("group1");
config1.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
config1.getNetworkConfig().getJoin().getTcpIpConfig()
.setEnabled(true).setConnectionTimeoutSeconds(3).addMember("127.0.0.1");
Config config2 = new Config();
config2.setProperty(GroupProperty.WAIT_SECONDS_BEFORE_JOIN.getName(), "0");
config2.setProperty(GroupProperty.MAX_JOIN_SECONDS.getName(), "3");
config2.getGroupConfig().setName("group2");
config2.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
config2.getNetworkConfig().getJoin().getTcpIpConfig()
.setEnabled(true).setConnectionTimeoutSeconds(3).addMember("127.0.0.1");
assertIndependentClusters(config1, config2);
}
@Test
public void test_whenSameGroupNamesButDifferentPassword()
throws Exception {
Config config1 = new Config();
config1.setProperty(GroupProperty.WAIT_SECONDS_BEFORE_JOIN.getName(), "0");
config1.setProperty(GroupProperty.MAX_JOIN_SECONDS.getName(), "3");
config1.getGroupConfig().setPassword("pass1");
config1.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
config1.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true).setConnectionTimeoutSeconds(3)
.addMember("127.0.0.1");
Config config2 = new Config();
config2.setProperty(GroupProperty.WAIT_SECONDS_BEFORE_JOIN.getName(), "0");
config2.setProperty(GroupProperty.MAX_JOIN_SECONDS.getName(), "3");
config2.getGroupConfig().setPassword("pass2");
config2.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
config2.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true).setConnectionTimeoutSeconds(3)
.addMember("127.0.0.1");
HazelcastInstance hz1 = Hazelcast.newHazelcastInstance(config1);
HazelcastInstance hz2 = Hazelcast.newHazelcastInstance(config2);
assertClusterSize(2, hz1);
assertClusterSize(2, hz2);
}
@Test
public void test_whenIncompatiblePartitionGroups() throws Exception {
Config config1 = new Config();
config1.setProperty(GroupProperty.WAIT_SECONDS_BEFORE_JOIN.getName(), "0");
config1.setProperty(GroupProperty.MAX_JOIN_SECONDS.getName(), "3");
config1.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
config1.getNetworkConfig().getJoin().getTcpIpConfig()
.setEnabled(true).setConnectionTimeoutSeconds(3).addMember("127.0.0.1");
config1.getPartitionGroupConfig().setEnabled(true)
.setGroupType(PartitionGroupConfig.MemberGroupType.CUSTOM);
Config config2 = new Config();
config2.setProperty(GroupProperty.WAIT_SECONDS_BEFORE_JOIN.getName(), "0");
config2.setProperty(GroupProperty.MAX_JOIN_SECONDS.getName(), "3");
config2.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
config2.getNetworkConfig().getJoin().getTcpIpConfig()
.setEnabled(true).setConnectionTimeoutSeconds(3).addMember("127.0.0.1");
config2.getPartitionGroupConfig().setEnabled(true)
.setGroupType(PartitionGroupConfig.MemberGroupType.HOST_AWARE);
assertIncompatible(config1, config2);
}
@Test
public void test_whenIncompatibleJoiners() throws Exception {
Config config1 = new Config();
config1.setProperty(GroupProperty.WAIT_SECONDS_BEFORE_JOIN.getName(), "0");
config1.setProperty(GroupProperty.MAX_JOIN_SECONDS.getName(), "3");
config1.getNetworkConfig().getJoin().getMulticastConfig().setMulticastTimeoutSeconds(3);
config1.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(false);
Config config2 = new Config();
config2.setProperty(GroupProperty.WAIT_SECONDS_BEFORE_JOIN.getName(), "0");
config2.setProperty(GroupProperty.MAX_JOIN_SECONDS.getName(), "3");
config2.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
config2.getNetworkConfig().getJoin().getTcpIpConfig().setConnectionTimeoutSeconds(3)
.setEnabled(true).addMember("127.0.0.1");
assertIncompatible(config1, config2);
}
@Test
public void test_tcpIpJoinerRemembersJoinedMemberAddresses() {
Config config1 = smallInstanceConfig()
.setProperty("hazelcast.merge.first.run.delay.seconds", "10")
.setProperty("hazelcast.merge.next.run.delay.seconds", "10");
JoinConfig join1 = config1.getNetworkConfig().getJoin();
join1.getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig1 = join1.getTcpIpConfig();
tcpIpConfig1.setEnabled(true);
Config config2 = smallInstanceConfig();
JoinConfig join2 = config2.getNetworkConfig().setPort(8899).getJoin();
join2.getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig2 = join2.getTcpIpConfig();
tcpIpConfig2.setEnabled(true).addMember("127.0.0.1:5701");
Config config3 = smallInstanceConfig();
JoinConfig join3 = config3.getNetworkConfig().setPort(9099).getJoin();
join3.getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig3 = join3.getTcpIpConfig();
tcpIpConfig3.setEnabled(true).addMember("127.0.0.1:5701");
HazelcastInstance hz1 = Hazelcast.newHazelcastInstance(config1);
HazelcastInstance hz2 = Hazelcast.newHazelcastInstance(config2);
HazelcastInstance hz3 = Hazelcast.newHazelcastInstance(config3);
Address hz2Address = hz2.getCluster().getLocalMember().getAddress();
Address hz3Address = hz3.getCluster().getLocalMember().getAddress();
TcpIpJoiner tcpJoiner1 = (TcpIpJoiner) TestUtil.getNode(hz1).getJoiner();
assertTrueEventually(new AssertTask() {
@Override
public void run() {
assertContainsAll(tcpJoiner1.getKnownMemberAddresses().keySet(), Arrays.asList(hz2Address, hz3Address));
}
});
hz2.shutdown();
hz3.shutdown();
assertTrueAllTheTime(new AssertTask() {
@Override
public void run() {
assertContainsAll(tcpJoiner1.getKnownMemberAddresses().keySet(), Arrays.asList(hz2Address, hz3Address));
}
}, 15);
}
@Test
public void test_tcpIpJoinerCleanupAddressesAfterAddressRetentionPeriodIsPassed() throws Exception {
Config config1 = smallInstanceConfig()
.setProperty("hazelcast.merge.first.run.delay.seconds", "10")
.setProperty("hazelcast.merge.next.run.delay.seconds", "10");
JoinConfig join1 = config1.getNetworkConfig().getJoin();
join1.getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig1 = join1.getTcpIpConfig();
tcpIpConfig1.setEnabled(true);
Config config2 = smallInstanceConfig();
JoinConfig join2 = config2.getNetworkConfig().setPort(8899).getJoin();
join2.getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig2 = join2.getTcpIpConfig();
tcpIpConfig2.setEnabled(true).addMember("127.0.0.1:5701");
Config config3 = smallInstanceConfig();
JoinConfig join3 = config3.getNetworkConfig().setPort(9099).getJoin();
join3.getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig3 = join3.getTcpIpConfig();
tcpIpConfig3.setEnabled(true).addMember("127.0.0.1:5701");
HazelcastInstance hz1 = Hazelcast.newHazelcastInstance(config1);
TcpIpJoiner tcpJoiner1 = (TcpIpJoiner) TestUtil.getNode(hz1).getJoiner();
overrideAddressRetentionDuration(tcpJoiner1, TimeUnit.SECONDS.toMillis(10));
HazelcastInstance hz2 = Hazelcast.newHazelcastInstance(config2);
HazelcastInstance hz3 = Hazelcast.newHazelcastInstance(config3);
Address hz2Address = hz2.getCluster().getLocalMember().getAddress();
Address hz3Address = hz3.getCluster().getLocalMember().getAddress();
assertTrueEventually(new AssertTask() {
@Override
public void run() {
assertContainsAll(tcpJoiner1.getKnownMemberAddresses().keySet(), Arrays.asList(hz2Address, hz3Address));
}
});
hz3.shutdown();
// Timeout below must be greater than both hazelcast.merge.next.run.delay.seconds and previouslyJoinedMemberAddressRetentionDuration
assertTrueEventually(new AssertTask() {
@Override
public void run() {
Set<Address> rememberedAddresses = tcpJoiner1.getKnownMemberAddresses().keySet();
assertContains(rememberedAddresses, hz2Address);
assertNotContains(rememberedAddresses, hz3Address);
}
}, 20);
}
private static void overrideAddressRetentionDuration(TcpIpJoiner tcpIpJoiner, long addressRetentionDuration) throws Exception {
Field privateField = TcpIpJoiner.class.getDeclaredField("previouslyJoinedMemberAddressRetentionDuration");
privateField.setAccessible(true);
privateField.set(tcpIpJoiner, addressRetentionDuration);
}
}