-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
UniqueConstraintStressIT.java
394 lines (343 loc) · 13.5 KB
/
UniqueConstraintStressIT.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
/*
* Copyright (c) 2002-2015 "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.Before;
import org.junit.Rule;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.neo4j.com.ComException;
import org.neo4j.graphdb.ConstraintViolationException;
import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.TransactionFailureException;
import org.neo4j.graphdb.TransientTransactionFailureException;
import org.neo4j.helpers.Exceptions;
import org.neo4j.kernel.impl.ha.ClusterManager;
import org.neo4j.test.OtherThreadExecutor;
import org.neo4j.test.OtherThreadRule;
import org.neo4j.test.RepeatRule;
import org.neo4j.test.ha.ClusterRule;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertThat;
import static org.neo4j.helpers.collection.IteratorUtil.loop;
/**
* This test stress tests unique constraints in a setup where writes are being issued against a slave.
*/
public class UniqueConstraintStressIT
{
@Rule
public final ClusterRule clusterRule = new ClusterRule(getClass());
protected ClusterManager.ManagedCluster cluster;
@Before
public void setup() throws Exception
{
cluster = clusterRule.config(HaSettings.pull_interval, "0").startCluster( );
}
private final int REPETITIONS = 1;
public final @Rule RepeatRule repeater = new RepeatRule();
@Rule
public OtherThreadRule<Object> slaveWork = new OtherThreadRule<>();
@Rule
public OtherThreadRule<Object> masterWork = new OtherThreadRule<>();
/** Configure how long to run the test. */
private static long runtime = Long.getLong( "neo4j.UniqueConstraintStressIT.runtime", TimeUnit.SECONDS.toMillis( 10 ) );
/** Label to constrain for the current iteration of the test. */
private volatile Label label = DynamicLabel.label( "User" );
/** Property key to constrain for the current iteration of the test. */
private volatile String property;
private final AtomicInteger roundNo = new AtomicInteger(0);
private abstract class Operation
{
HighlyAvailableGraphDatabase master = cluster.getMaster();
HighlyAvailableGraphDatabase slave = cluster.getAnySlave();
Future<Boolean> constraintCreation = null;
Future<Integer> constraintViolation = null;
abstract void perform();
}
/* The different orders and delays in the below variations try to stress all known scenarios, as well as
* of course stress for unknown concurrency issues. See the exception handling structure further below
* for explanations. */
@RepeatRule.Repeat ( times = REPETITIONS )
@Test
public void shouldNotAllowUniquenessViolationsUnderStress_A() throws Exception
{
shouldNotAllowUniquenessViolationsUnderStress( new Operation()
{
@Override
void perform()
{
constraintCreation = masterWork.execute( createConstraint( master ) );
constraintViolation = slaveWork.execute( performInsert( slave ) );
}
} );
}
@RepeatRule.Repeat ( times = REPETITIONS )
@Test
public void shouldNotAllowUniquenessViolationsUnderStress_B() throws Exception
{
shouldNotAllowUniquenessViolationsUnderStress( new Operation()
{
@Override
void perform()
{
constraintViolation = slaveWork.execute( performInsert( slave ) );
constraintCreation = masterWork.execute( createConstraint( master ) );
}
} );
}
@RepeatRule.Repeat ( times = REPETITIONS )
@Test
public void shouldNotAllowUniquenessViolationsUnderStress_C() throws Exception
{
shouldNotAllowUniquenessViolationsUnderStress( new Operation()
{
@Override
void perform()
{
constraintCreation = masterWork.execute( createConstraint( master ) );
try
{
Thread.sleep( 10 );
}
catch ( InterruptedException e )
{
}
constraintViolation = slaveWork.execute( performInsert( slave ) );
}
} );
}
@RepeatRule.Repeat ( times = REPETITIONS )
@Test
public void shouldNotAllowUniquenessViolationsUnderStress_D() throws Exception
{
shouldNotAllowUniquenessViolationsUnderStress( new Operation()
{
@Override
void perform()
{
constraintViolation = slaveWork.execute( performInsert( slave ) );
try
{
Thread.sleep( 10 );
}
catch ( InterruptedException e )
{
}
constraintCreation = masterWork.execute( createConstraint( master ) );
}
} );
}
@RepeatRule.Repeat ( times = REPETITIONS )
@Test
public void shouldNotAllowUniquenessViolationsUnderStress_E() throws Exception
{
shouldNotAllowUniquenessViolationsUnderStress( new Operation()
{
@Override
void perform()
{
constraintCreation = masterWork.execute( createConstraint( master ) );
try
{
Thread.sleep( 2000 );
}
catch ( InterruptedException e )
{
}
constraintViolation = slaveWork.execute( performInsert( slave ) );
}
} );
}
@RepeatRule.Repeat ( times = REPETITIONS )
@Test
public void shouldNotAllowUniquenessViolationsUnderStress_F() throws Exception
{
shouldNotAllowUniquenessViolationsUnderStress( new Operation()
{
@Override
void perform()
{
constraintViolation = slaveWork.execute( performInsert( slave ) );
try
{
Thread.sleep( 2000 );
}
catch ( InterruptedException e )
{
}
constraintCreation = masterWork.execute( createConstraint( master ) );
}
} );
}
public void shouldNotAllowUniquenessViolationsUnderStress(Operation ops) throws Exception
{
// Given
HighlyAvailableGraphDatabase master = cluster.getMaster();
HighlyAvailableGraphDatabase slave = cluster.getAnySlave();
// Because this is a brute-force test, we run for a user-specified time, and consider ourselves successful if no failure is found.
long end = System.currentTimeMillis() + runtime;
int successfulAttempts = 0;
while ( end > System.currentTimeMillis() )
{
// Each round of this loop:
// - on a slave, creates a set of nodes with a label/property combo with all-unique values
// - on the master, creates a unique constraint
// - on a slave, while the master is creating the constraint, starts issuing transactions that will violate the constraint
setup :
{
// Set a target property for the constraint for this round
setLabelAndPropertyForNextRound();
// Ensure slave has constraints from previous round
cluster.sync();
// Create the initial data
try
{
slaveWork.execute( performInsert( slave ) ).get();
}
catch ( ExecutionException e )
{
if ( Exceptions.contains( e, "could not connect", ComException.class ) )
{ // We're in a weird cluster state. The slave should be able to succeed if trying again
continue;
}
}
}
stress :
{
ops.perform();
// And then ensure all is well
assertConstraintsNotViolated( ops.constraintCreation, ops.constraintViolation, master );
successfulAttempts++;
}
}
assertThat( successfulAttempts, greaterThan( 0 ) );
}
private void assertConstraintsNotViolated( Future<Boolean> constraintCreation, Future<Integer> constraintViolation,
HighlyAvailableGraphDatabase master ) throws InterruptedException, ExecutionException
{
boolean constraintCreationFailed = constraintCreation.get();
int txSuccessCount = constraintViolation.get();
if ( constraintCreationFailed )
{
// Constraint creation failed, some of the violating operations should have been successful.
assertThat( txSuccessCount, greaterThan( 0 ) );
// And the graph should contain some duplicates.
assertThat( allPropertiesAreUnique( master, label, property ), equalTo( false ) );
}
else
{
// Constraint creation was successful, all the violating operations should have failed.
assertThat( txSuccessCount, equalTo( 0 ) );
// And the graph should not contain duplicates.
assertThat( allPropertiesAreUnique( master, label, property ), equalTo( true ) );
}
}
private boolean allPropertiesAreUnique( HighlyAvailableGraphDatabase master, Label label, String property )
{
try( Transaction tx = master.beginTx() )
{
Set<Object> values = new HashSet<>();
for ( Node node : loop( master.findNodes( label ) ) )
{
Object value = node.getProperty( property );
if ( values.contains( value ) )
{
return false;
}
values.add( value );
}
tx.success();
}
return true;
}
private OtherThreadExecutor.WorkerCommand<Object, Boolean> createConstraint( final HighlyAvailableGraphDatabase master )
{
return new OtherThreadExecutor.WorkerCommand<Object, Boolean>()
{
@Override
public Boolean doWork( Object state ) throws Exception
{
boolean constraintCreationFailed = false;
try( Transaction tx = master.beginTx() )
{
master.schema().constraintFor( label ).assertPropertyIsUnique( property ).create();
tx.success();
}
catch( ConstraintViolationException e )
{
/* Unable to create constraint since it is not consistent with existing data. */
constraintCreationFailed = true;
}
return constraintCreationFailed;
}
};
}
/**
* Inserts a bunch of new nodes with the label and property key currently set in the fields in this class, where
* running this method twice will insert nodes with duplicate property values, assuming propertykey or label has not
* changed.
*/
private OtherThreadExecutor.WorkerCommand<Object, Integer> performInsert( final HighlyAvailableGraphDatabase slave )
{
return new OtherThreadExecutor.WorkerCommand<Object, Integer>()
{
@Override
public Integer doWork( Object state ) throws Exception
{
int i = 0;
try
{
for ( ; i < 100; i++ )
{
try( Transaction tx = slave.beginTx() )
{
slave.createNode( label ).setProperty( property, "value-" + i );
tx.success();
}
}
}
catch ( TransactionFailureException | TransientTransactionFailureException e )
{
// Swallowed on purpose, we except it to fail sometimes due to either
// - constraint violation on master
// - concurrent schema operation on master
}
catch ( ConstraintViolationException e )
{
// Constraint violation detected on slave while building transaction
}
return i;
}
};
}
private void setLabelAndPropertyForNextRound()
{
property = "Key-" + roundNo.incrementAndGet();
label = DynamicLabel.label("Label-" + roundNo.get());
}
}