-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ClusterPartitionIT.java
378 lines (308 loc) · 14.6 KB
/
ClusterPartitionIT.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
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.ha;
import org.junit.Rule;
import org.junit.Test;
import org.neo4j.cluster.ClusterSettings;
import org.neo4j.cluster.InstanceId;
import org.neo4j.function.Predicate;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.TransientDatabaseFailureException;
import org.neo4j.kernel.impl.ha.ClusterManager;
import org.neo4j.kernel.impl.ha.ClusterManager.NetworkFlag;
import org.neo4j.test.LoggerRule;
import org.neo4j.test.TargetDirectory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.neo4j.helpers.collection.MapUtil.stringMap;
import static org.neo4j.kernel.ha.cluster.HighAvailabilityMemberState.PENDING;
import static org.neo4j.kernel.impl.ha.ClusterManager.allSeesAllAsAvailable;
import static org.neo4j.kernel.impl.ha.ClusterManager.masterAvailable;
import static org.neo4j.kernel.impl.ha.ClusterManager.masterSeesSlavesAsAvailable;
import static org.neo4j.kernel.impl.ha.ClusterManager.memberSeesOtherMemberAsFailed;
public class ClusterPartitionIT
{
@Rule
public LoggerRule logger = new LoggerRule();
@Rule
public TargetDirectory.TestDirectory dir = TargetDirectory.testDirForTest( getClass() );
private final String testPropKey = "testPropKey";
private final String testPropValue = "testPropValue";
private long testNodeId;
@Test
public void isolatedMasterShouldRemoveSelfFromClusterAndBecomeReadOnly() throws Throwable
{
int clusterSize = 3;
ClusterManager manager = new ClusterManager.Builder().withRootDirectory( dir.cleanDirectory( "testcluster" ) )
.withProvider( ClusterManager.clusterOfSize( clusterSize ) )
.withSharedConfig( stringMap(
ClusterSettings.heartbeat_interval.name(), "1" ) )
.build();
try
{
manager.start();
ClusterManager.ManagedCluster cluster = manager.getDefaultCluster();
cluster.await( allSeesAllAsAvailable() );
cluster.await( masterAvailable() );
HighlyAvailableGraphDatabase oldMaster = cluster.getMaster();
addSomeData( oldMaster );
ClusterManager.RepairKit fail = cluster.fail( oldMaster, NetworkFlag.values() );
cluster.await( instanceEvicted( oldMaster ), 20 );
ensureInstanceIsReadOnlyInPendingState( oldMaster );
fail.repair();
cluster.await( allSeesAllAsAvailable() );
ensureInstanceIsWritable( oldMaster );
}
finally
{
manager.safeShutdown();
}
}
@Test
public void isolatedSlaveShouldRemoveSelfFromClusterAndBecomeReadOnly() throws Throwable
{
int clusterSize = 3;
ClusterManager manager = new ClusterManager.Builder().withRootDirectory( dir.cleanDirectory( "testcluster" ) )
.withProvider( ClusterManager.clusterOfSize( clusterSize ) )
.withSharedConfig( stringMap(
ClusterSettings.heartbeat_interval.name(), "1" ) )
.build();
try
{
manager.start();
ClusterManager.ManagedCluster cluster = manager.getDefaultCluster();
cluster.await( allSeesAllAsAvailable() );
cluster.await( masterAvailable() );
HighlyAvailableGraphDatabase slave = cluster.getAnySlave();
addSomeData( slave );
ClusterManager.RepairKit fail = cluster.fail( slave, NetworkFlag.values() );
cluster.await( instanceEvicted( slave ), 20 );
ensureInstanceIsReadOnlyInPendingState( slave );
fail.repair();
cluster.await( allSeesAllAsAvailable() );
ensureInstanceIsWritable( slave );
}
finally
{
manager.safeShutdown();
}
}
@Test
public void losingQuorumIncrementallyShouldMakeAllInstancesPendingAndReadOnly() throws Throwable
{
int clusterSize = 5; // we need 5 to differentiate between all other instances gone and just quorum being gone
ClusterManager manager = new ClusterManager.Builder().withRootDirectory( dir.cleanDirectory( "testcluster" ) )
.withProvider( ClusterManager.clusterOfSize( clusterSize ) )
.withSharedConfig( stringMap(
ClusterSettings.heartbeat_interval.name(), "1",
ClusterSettings.heartbeat_timeout.name(), "3",
HaSettings.tx_push_factor.name(), "4" ) ) // so we know the initial data made it everywhere
.build();
try
{
manager.start();
ClusterManager.ManagedCluster cluster = manager.getDefaultCluster();
cluster.await( allSeesAllAsAvailable() );
cluster.await( masterAvailable() );
HighlyAvailableGraphDatabase master = cluster.getMaster();
addSomeData( master );
/*
* we need 3 failures. We'll end up with the old master and a slave connected. They should both be in
* PENDING state, allowing reads but not writes. Repairing just one of the removed instances should
* result in a master being elected and all instances being read and writable.
* The instances we remove do not need additional verification for their state. Their behaviour is already
* known by other tests.
*/
HighlyAvailableGraphDatabase failed1;
ClusterManager.RepairKit rk1;
HighlyAvailableGraphDatabase failed2;
HighlyAvailableGraphDatabase failed3;
HighlyAvailableGraphDatabase remainingSlave;
failed1 = cluster.getAnySlave();
failed2 = cluster.getAnySlave( failed1 );
failed3 = cluster.getAnySlave( failed1, failed2 );
remainingSlave = cluster.getAnySlave( failed1, failed2, failed3 );
rk1 = killIncrementally( cluster, failed1, failed2, failed3 );
cluster.await( memberSeesOtherMemberAsFailed( remainingSlave, failed1 ) );
cluster.await( memberSeesOtherMemberAsFailed( remainingSlave, failed2 ) );
cluster.await( memberSeesOtherMemberAsFailed( remainingSlave, failed3 ) );
cluster.await( memberSeesOtherMemberAsFailed( master, failed1 ) );
cluster.await( memberSeesOtherMemberAsFailed( master, failed2 ) );
cluster.await( memberSeesOtherMemberAsFailed( master, failed3 ) );
ensureInstanceIsReadOnlyInPendingState( master );
ensureInstanceIsReadOnlyInPendingState( remainingSlave );
rk1.repair();
cluster.await( masterAvailable( failed2, failed3 ) );
cluster.await( masterSeesSlavesAsAvailable( 2 ) );
ensureInstanceIsWritable( master );
ensureInstanceIsWritable( remainingSlave );
ensureInstanceIsWritable( failed1 );
}
finally
{
manager.shutdown();
}
}
@Test
public void losingQuorumAbruptlyShouldMakeAllInstancesPendingAndReadOnly() throws Throwable
{
int clusterSize = 5; // we need 5 to differentiate between all other instances gone and just quorum being gone
ClusterManager manager = new ClusterManager.Builder().withRootDirectory( dir.cleanDirectory( "testcluster" ) )
.withProvider( ClusterManager.clusterOfSize( clusterSize ) )
.withSharedConfig( stringMap(
ClusterSettings.heartbeat_interval.name(), "1",
ClusterSettings.heartbeat_timeout.name(), "3",
HaSettings.tx_push_factor.name(), "4" ) ) // so we know the initial data made it everywhere
.build();
try
{
manager.start();
ClusterManager.ManagedCluster cluster = manager.getDefaultCluster();
cluster.await( allSeesAllAsAvailable() );
cluster.await( masterAvailable() );
HighlyAvailableGraphDatabase master = cluster.getMaster();
addSomeData( master );
/*
* we need 3 failures. We'll end up with the old master and a slave connected. They should both be in
* PENDING state, allowing reads but not writes. Repairing just one of the removed instances should
* result in a master being elected and all instances being read and writable.
* The instances we remove do not need additional verification for their state. Their behaviour is already
* known by other tests.
*/
HighlyAvailableGraphDatabase failed1;
ClusterManager.RepairKit rk1;
HighlyAvailableGraphDatabase failed2;
HighlyAvailableGraphDatabase failed3;
HighlyAvailableGraphDatabase remainingSlave;
failed1 = cluster.getAnySlave();
failed2 = cluster.getAnySlave( failed1 );
failed3 = cluster.getAnySlave( failed1, failed2 );
remainingSlave = cluster.getAnySlave( failed1, failed2, failed3 );
rk1 = killAbruptly( cluster, failed1, failed2, failed3 );
cluster.await( memberSeesOtherMemberAsFailed( remainingSlave, failed1 ) );
cluster.await( memberSeesOtherMemberAsFailed( remainingSlave, failed2 ) );
cluster.await( memberSeesOtherMemberAsFailed( remainingSlave, failed3 ) );
cluster.await( memberSeesOtherMemberAsFailed( master, failed1 ) );
cluster.await( memberSeesOtherMemberAsFailed( master, failed2 ) );
cluster.await( memberSeesOtherMemberAsFailed( master, failed3 ) );
ensureInstanceIsReadOnlyInPendingState( master );
ensureInstanceIsReadOnlyInPendingState( remainingSlave );
rk1.repair();
cluster.await( masterAvailable( failed2, failed3 ) );
cluster.await( masterSeesSlavesAsAvailable( 2 ) );
ensureInstanceIsWritable( master );
ensureInstanceIsWritable( remainingSlave );
ensureInstanceIsWritable( failed1 );
}
finally
{
manager.shutdown();
}
}
private ClusterManager.RepairKit killAbruptly( ClusterManager.ManagedCluster cluster,
HighlyAvailableGraphDatabase failed1,
HighlyAvailableGraphDatabase failed2,
HighlyAvailableGraphDatabase failed3 ) throws Throwable
{
ClusterManager.RepairKit firstFailure = cluster.fail( failed1 );
cluster.fail( failed2 );
cluster.fail( failed3 );
cluster.await( instanceEvicted( failed1 ) );
cluster.await( instanceEvicted( failed2 ) );
cluster.await( instanceEvicted( failed3 ) );
return firstFailure;
}
private ClusterManager.RepairKit killIncrementally( ClusterManager.ManagedCluster cluster,
HighlyAvailableGraphDatabase failed1,
HighlyAvailableGraphDatabase failed2,
HighlyAvailableGraphDatabase failed3 ) throws Throwable
{
ClusterManager.RepairKit firstFailure = cluster.fail( failed1 );
cluster.await( instanceEvicted( failed1 ) );
cluster.fail( failed2 );
cluster.await( instanceEvicted( failed2 ) );
cluster.fail( failed3 );
cluster.await( instanceEvicted( failed3 ) );
return firstFailure;
}
private void addSomeData( HighlyAvailableGraphDatabase instance )
{
try ( Transaction tx = instance.beginTx() )
{
Node testNode = instance.createNode();
testNodeId = testNode.getId();
testNode.setProperty( testPropKey, testPropValue );
tx.success();
}
}
/*
* This method must be called on an instance that has had addSomeData() called on it.
*/
private void ensureInstanceIsReadOnlyInPendingState( HighlyAvailableGraphDatabase instance )
{
assertEquals( PENDING, instance.getInstanceState() );
try ( Transaction tx = instance.beginTx() )
{
assertEquals( testPropValue, instance.getNodeById( testNodeId ).getProperty( testPropKey ) );
tx.success();
}
try ( Transaction ignored = instance.beginTx() )
{
instance.getNodeById( testNodeId ).delete();
fail( "Should not be able to do write transactions when detached" );
}
catch ( TransientDatabaseFailureException e )
{
// expected
}
}
private void ensureInstanceIsWritable( HighlyAvailableGraphDatabase instance )
{
try ( Transaction tx = instance.beginTx() )
{
instance.createNode().setProperty( testPropKey, testPropValue );
tx.success();
}
}
private Predicate<ClusterManager.ManagedCluster> instanceEvicted( final HighlyAvailableGraphDatabase instance )
{
return new Predicate<ClusterManager.ManagedCluster>()
{
@Override
public boolean test( ClusterManager.ManagedCluster managedCluster )
{
InstanceId instanceId = managedCluster.getServerId( instance );
Iterable<HighlyAvailableGraphDatabase> members = managedCluster.getAllMembers();
for ( HighlyAvailableGraphDatabase member : members )
{
if ( instanceId.equals( managedCluster.getServerId( member ) ) )
{
if ( member.role().equals( "UNKNOWN" ) )
{
return true;
}
}
}
return false;
}
};
}
}