-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ZookeeperIpFinderTest.java
396 lines (309 loc) · 13.4 KB
/
ZookeeperIpFinderTest.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
/*
* 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.spi.discovery.tcp.ipfinder.zk;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryNTimes;
import org.apache.curator.test.InstanceSpec;
import org.apache.curator.test.TestingCluster;
import org.apache.curator.utils.CloseableUtils;
import org.apache.ignite.Ignite;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.events.Event;
import org.apache.ignite.events.EventType;
import org.apache.ignite.internal.util.lang.GridAbsPredicate;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.lang.IgniteBiPredicate;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.testframework.GridTestUtils;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
/**
* Test for {@link TcpDiscoveryZookeeperIpFinder}.
*
* @author Raul Kripalani
*/
public class ZookeeperIpFinderTest extends GridCommonAbstractTest {
/** ZK Cluster size. */
private static final int ZK_CLUSTER_SIZE = 3;
/** ZK Path size. */
private static final String SERVICES_IGNITE_ZK_PATH = "/services/ignite";
/** The ZK cluster instance, from curator-test. */
private TestingCluster zkCluster;
/** A Curator client to perform assertions on the embedded ZK instances. */
private CuratorFramework zkCurator;
/** Whether to allow duplicate registrations for the current test method or not. */
private boolean allowDuplicateRegistrations = false;
/** Constructor that does not start any grids. */
public ZookeeperIpFinderTest() {
super(false);
}
/**
* Before test.
*
* @throws Exception
*/
@Override public void beforeTest() throws Exception {
super.beforeTest();
// remove stale system properties
System.getProperties().remove(TcpDiscoveryZookeeperIpFinder.PROP_ZK_CONNECTION_STRING);
// disable JMX for tests
System.setProperty("zookeeper.jmx.log4j.disable", "true");
// start the ZK cluster
zkCluster = new TestingCluster(ZK_CLUSTER_SIZE);
zkCluster.start();
// start the Curator client so we can perform assertions on the ZK state later
zkCurator = CuratorFrameworkFactory.newClient(zkCluster.getConnectString(), new RetryNTimes(10, 1000));
zkCurator.start();
}
/**
* After test.
*
* @throws Exception
*/
@Override public void afterTest() throws Exception {
super.afterTest();
if (zkCurator != null)
CloseableUtils.closeQuietly(zkCurator);
if (zkCluster != null)
CloseableUtils.closeQuietly(zkCluster);
stopAllGrids();
}
/**
* Enhances the default configuration with the {#TcpDiscoveryZookeeperIpFinder}.
*
* @param igniteInstanceName Ignite instance name.
* @return Ignite configuration.
* @throws Exception If failed.
*/
@Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
IgniteConfiguration configuration = super.getConfiguration(igniteInstanceName);
TcpDiscoverySpi tcpDisco = (TcpDiscoverySpi)configuration.getDiscoverySpi();
TcpDiscoveryZookeeperIpFinder zkIpFinder = new TcpDiscoveryZookeeperIpFinder();
zkIpFinder.setAllowDuplicateRegistrations(allowDuplicateRegistrations);
// first node => configure with zkUrl; second node => configure with CuratorFramework; third and subsequent
// shall be configured through system property
if (igniteInstanceName.equals(getTestIgniteInstanceName(0)))
zkIpFinder.setZkConnectionString(zkCluster.getConnectString());
else if (igniteInstanceName.equals(getTestIgniteInstanceName(1))) {
zkIpFinder.setCurator(CuratorFrameworkFactory.newClient(zkCluster.getConnectString(),
new ExponentialBackoffRetry(100, 5)));
}
tcpDisco.setIpFinder(zkIpFinder);
return configuration;
}
/**
* @throws Exception If failed.
*/
public void testOneIgniteNodeIsAlone() throws Exception {
startGrid(0);
assertEquals(1, grid(0).cluster().metrics().getTotalNodes());
stopAllGrids();
}
/**
* @throws Exception If failed.
*/
public void testTwoIgniteNodesFindEachOther() throws Exception {
// start one node
startGrid(0);
// set up an event listener to expect one NODE_JOINED event
CountDownLatch latch = expectJoinEvents(grid(0), 1);
// start the other node
startGrid(1);
// assert the nodes see each other
assertEquals(2, grid(0).cluster().metrics().getTotalNodes());
assertEquals(2, grid(1).cluster().metrics().getTotalNodes());
// assert the event listener got as many events as expected
latch.await(1, TimeUnit.SECONDS);
stopAllGrids();
}
/**
* @throws Exception If failed.
*/
public void testThreeNodesWithThreeDifferentConfigMethods() throws Exception {
// start one node
startGrid(0);
// set up an event listener to expect one NODE_JOINED event
CountDownLatch latch = expectJoinEvents(grid(0), 2);
// start the 2nd node
startGrid(1);
// start the 3rd node, first setting the system property
System.setProperty(TcpDiscoveryZookeeperIpFinder.PROP_ZK_CONNECTION_STRING, zkCluster.getConnectString());
startGrid(2);
// wait until all grids are started
waitForRemoteNodes(grid(0), 2);
// assert the nodes see each other
assertEquals(3, grid(0).cluster().metrics().getTotalNodes());
assertEquals(3, grid(1).cluster().metrics().getTotalNodes());
assertEquals(3, grid(2).cluster().metrics().getTotalNodes());
// assert the event listener got as many events as expected
latch.await(1, TimeUnit.SECONDS);
stopAllGrids();
}
/**
* @throws Exception If failed.
*/
public void testFourNodesStartingAndStopping() throws Exception {
// start one node
startGrid(0);
// set up an event listener to expect one NODE_JOINED event
CountDownLatch latch = expectJoinEvents(grid(0), 3);
// start the 2nd node
startGrid(1);
// start the 3rd & 4th nodes, first setting the system property
System.setProperty(TcpDiscoveryZookeeperIpFinder.PROP_ZK_CONNECTION_STRING, zkCluster.getConnectString());
startGrid(2);
startGrid(3);
// wait until all grids are started
waitForRemoteNodes(grid(0), 3);
// assert the nodes see each other
assertEquals(4, grid(0).cluster().metrics().getTotalNodes());
assertEquals(4, grid(1).cluster().metrics().getTotalNodes());
assertEquals(4, grid(2).cluster().metrics().getTotalNodes());
assertEquals(4, grid(3).cluster().metrics().getTotalNodes());
// assert the event listener got as many events as expected
latch.await(1, TimeUnit.SECONDS);
// stop the first grid
stopGrid(0);
// make sure that nodes were synchronized; they should only see 3 now
assertEquals(3, grid(1).cluster().metrics().getTotalNodes());
assertEquals(3, grid(2).cluster().metrics().getTotalNodes());
assertEquals(3, grid(3).cluster().metrics().getTotalNodes());
// stop all remaining grids
stopGrid(1);
stopGrid(2);
stopGrid(3);
// check that the nodes are gone in ZK
assertEquals(0, zkCurator.getChildren().forPath(SERVICES_IGNITE_ZK_PATH).size());
}
/**
* @throws Exception If failed.
*/
public void testFourNodesWithDuplicateRegistrations() throws Exception {
allowDuplicateRegistrations = true;
// start 4 nodes
System.setProperty(TcpDiscoveryZookeeperIpFinder.PROP_ZK_CONNECTION_STRING, zkCluster.getConnectString());
startGrids(4);
// wait until all grids are started
waitForRemoteNodes(grid(0), 3);
// each node will register itself + the node that it connected to to join the cluster
assertEquals(7, zkCurator.getChildren().forPath(SERVICES_IGNITE_ZK_PATH).size());
// stop all grids
stopAllGrids();
// check that all nodes are gone in ZK
assertEquals(0, zkCurator.getChildren().forPath(SERVICES_IGNITE_ZK_PATH).size());
}
/**
* @throws Exception If failed.
*/
public void testFourNodesWithNoDuplicateRegistrations() throws Exception {
allowDuplicateRegistrations = false;
// start 4 nodes
System.setProperty(TcpDiscoveryZookeeperIpFinder.PROP_ZK_CONNECTION_STRING, zkCluster.getConnectString());
startGrids(4);
// wait until all grids are started
waitForRemoteNodes(grid(0), 3);
// each node will only register itself
assertEquals(4, zkCurator.getChildren().forPath(SERVICES_IGNITE_ZK_PATH).size());
// stop all grids
stopAllGrids();
// check that all nodes are gone in ZK
assertEquals(0, zkCurator.getChildren().forPath(SERVICES_IGNITE_ZK_PATH).size());
}
/**
* @throws Exception If failed.
*/
public void testFourNodesRestartLastSeveralTimes() throws Exception {
allowDuplicateRegistrations = false;
// start 4 nodes
System.setProperty(TcpDiscoveryZookeeperIpFinder.PROP_ZK_CONNECTION_STRING, zkCluster.getConnectString());
startGrids(4);
// wait until all grids are started
waitForRemoteNodes(grid(0), 3);
// each node will only register itself
assertEquals(4, zkCurator.getChildren().forPath(SERVICES_IGNITE_ZK_PATH).size());
// repeat 5 times
for (int i = 0; i < 5; i++) {
// stop last grid
stopGrid(2);
// check that the node has unregistered itself and its party
assertEquals(3, zkCurator.getChildren().forPath(SERVICES_IGNITE_ZK_PATH).size());
// start the node again
startGrid(2);
// check that the node back in ZK
assertEquals(4, zkCurator.getChildren().forPath(SERVICES_IGNITE_ZK_PATH).size());
}
stopAllGrids();
assertEquals(0, zkCurator.getChildren().forPath(SERVICES_IGNITE_ZK_PATH).size());
}
/**
* @throws Exception If failed.
*/
public void testFourNodesKillRestartZookeeper() throws Exception {
allowDuplicateRegistrations = false;
// start 4 nodes
System.setProperty(TcpDiscoveryZookeeperIpFinder.PROP_ZK_CONNECTION_STRING, zkCluster.getConnectString());
startGrids(4);
// wait until all grids are started
waitForRemoteNodes(grid(0), 3);
// each node will only register itself
assertEquals(4, zkCurator.getChildren().forPath(SERVICES_IGNITE_ZK_PATH).size());
// remember ZK server configuration and stop the cluster
Collection<InstanceSpec> instances = zkCluster.getInstances();
zkCluster.stop();
Thread.sleep(1000);
// start the cluster with the previous configuration
zkCluster = new TestingCluster(instances);
zkCluster.start();
// block the client until connected
zkCurator.blockUntilConnected();
// check that the nodes have registered again
assertEquals(4, zkCurator.getChildren().forPath(SERVICES_IGNITE_ZK_PATH).size());
// stop all grids
stopAllGrids();
assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override public boolean apply() {
try {
return 0 == zkCurator.getChildren().forPath(SERVICES_IGNITE_ZK_PATH).size();
}
catch (Exception e) {
U.error(log, "Failed to wait for zk condition", e);
return false;
}
}
}, 20000));
}
/**
* @param ignite Node.
* @param joinEvtCnt Expected events number.
* @return Events latch.
*/
private CountDownLatch expectJoinEvents(Ignite ignite, int joinEvtCnt) {
final CountDownLatch latch = new CountDownLatch(joinEvtCnt);
ignite.events().remoteListen(new IgniteBiPredicate<UUID, Event>() {
@Override public boolean apply(UUID uuid, Event evt) {
latch.countDown();
return true;
}
}, null, EventType.EVT_NODE_JOINED);
return latch;
}
}