-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LegacyDeadlockCompatibility.java
355 lines (335 loc) · 12.6 KB
/
LegacyDeadlockCompatibility.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
/*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.locking;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.util.Random;
import java.util.Stack;
import java.util.concurrent.CountDownLatch;
import org.neo4j.kernel.DeadlockDetectedException;
import static java.lang.System.currentTimeMillis;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@Ignore("Not a test, part of a compatibility suite.")
// This is the legacy deadlock detection tests
public class LegacyDeadlockCompatibility extends LockingCompatibilityTestSuite.Compatibility
{
public LegacyDeadlockCompatibility( LockingCompatibilityTestSuite suite )
{
super( suite );
}
@Test
@Ignore
// Because it makes assumptions about who will win deadlock scenarios that are not deterministic.
// This test has been shown to run successfully except in those cases where the "wrong" looser is picked, as such
// this test is to be dismantled, and it's individual assertions moved into DeadlockTest
public void testDeadlockDetection() throws Exception
{
long r1 = 1L;
long r2 = 2L;
long r3 = 3L;
long r4 = 4L;
LockWorker t1 = new LockWorker( "T1", locks );
LockWorker t2 = new LockWorker( "T2", locks );
LockWorker t3 = new LockWorker( "T3", locks );
LockWorker t4 = new LockWorker( "T4", locks );
try
{
t1.getReadLock( r1, true );
t1.getReadLock( r4, true );
t2.getReadLock( r2, true );
t2.getReadLock( r3, true );
t3.getReadLock( r3, true );
t3.getWriteLock( r1, false ); // t3-r1-t1 // T3
t2.getWriteLock( r4, false ); // t2-r4-t1
t1.getWriteLock( r2, true );
assertTrue( t1.isLastGetLockDeadLock() ); // t1-r2-t2-r4-t1
// resolve and try one more time
t1.releaseReadLock( r4 ); // will give r4 to t2
t1.getWriteLock( r2, false );
// t1-r2-t2
t2.releaseReadLock( r2 ); // will give r2 to t1
t1.getWriteLock( r4, false ); // t1-r4-t2 // T1
// dead lock
t2.getWriteLock( r2, true ); // T2
assertTrue( t2.isLastGetLockDeadLock() || t1.isLastGetLockDeadLock() );
// t2-r2-t3-r1-t1-r4-t2 or t2-r2-t1-r4-t2
t2.releaseWriteLock( r4 ); // give r4 to t1
t1.releaseWriteLock( r4 );
t2.getReadLock( r4, true );
t1.releaseWriteLock( r2 );
t1.getReadLock( r2, true );
t1.releaseReadLock( r1 ); // give r1 to t3
t3.getReadLock( r2, true );
t3.releaseWriteLock( r1 );
t1.getReadLock( r1, true ); // give r1->t1
t1.getWriteLock( r4, false );
t3.getWriteLock( r1, false );
t4.getReadLock( r2, true );
// deadlock
t2.getWriteLock( r2, true );
assertTrue( t2.isLastGetLockDeadLock() );
// t2-r2-t3-r1-t1-r4-t2
// resolve
t2.releaseReadLock( r4 );
t1.releaseWriteLock( r4 );
t1.releaseReadLock( r1 );
t2.getReadLock( r4, true ); // give r1 to t3
t3.releaseWriteLock( r1 );
t1.getReadLock( r1, true ); // give r1 to t1
t1.getWriteLock( r4, false );
t3.releaseReadLock( r2 );
t3.getWriteLock( r1, false );
// cleanup
t2.releaseReadLock( r4 ); // give r4 to t1
t1.releaseWriteLock( r4 );
t1.releaseReadLock( r1 ); // give r1 to t3
t3.releaseWriteLock( r1 );
t1.releaseReadLock( r2 );
t4.releaseReadLock( r2 );
t2.releaseReadLock( r3 );
t3.releaseReadLock( r3 );
// -- special case...
t1.getReadLock( r1, true );
t2.getReadLock( r1, true );
t1.getWriteLock( r1, false ); // t1->r1-t1&t2
t2.getWriteLock( r1, true );
assertTrue( t2.isLastGetLockDeadLock() );
// t2->r1->t1->r1->t2
t2.releaseReadLock( r1 );
t1.releaseReadLock( r1 );
t1.releaseWriteLock( r1 );
}
catch ( Exception e )
{
LockWorkFailureDump dumper = new LockWorkFailureDump( testDir.directory( getClass().getSimpleName() ) );
File file = dumper.dumpState( locks, t1, t2, t3, t4 );
throw new RuntimeException( "Failed, forensics information dumped to " + file.getAbsolutePath(), e );
}
finally
{
t1.close();
t2.close();
t3.close();
t4.close();
}
}
public static class StressThread extends Thread
{
private static final Object READ = new Object();
private static final Object WRITE = new Object();
private static long[] resources = new long[10];
private final Random rand = new Random( currentTimeMillis() );
static
{
for ( int i = 0; i < resources.length; i++ )
{
resources[i] = i;
}
}
private final CountDownLatch startSignal;
private final String name;
private final int numberOfIterations;
private final int depthCount;
private final float readWriteRatio;
private final Locks.Client lm;
private volatile Exception error;
public volatile Long startedWaiting = null;
StressThread( String name, int numberOfIterations, int depthCount,
float readWriteRatio, Locks.Client lm, CountDownLatch startSignal )
{
super();
this.name = name;
this.numberOfIterations = numberOfIterations;
this.depthCount = depthCount;
this.readWriteRatio = readWriteRatio;
this.lm = lm;
this.startSignal = startSignal;
}
@Override
public void run()
{
try
{
startSignal.await();
java.util.Stack<Object> lockStack = new java.util.Stack<Object>();
java.util.Stack<Long> resourceStack = new java.util.Stack<>();
for ( int i = 0; i < numberOfIterations; i++ )
{
try
{
int depth = depthCount;
do
{
float f = rand.nextFloat();
int n = rand.nextInt( resources.length );
if ( f < readWriteRatio )
{
startedWaiting = currentTimeMillis();
lm.acquireShared( ResourceTypes.NODE, resources[n] );
startedWaiting = null;
lockStack.push( READ );
}
else
{
startedWaiting = currentTimeMillis();
lm.acquireExclusive( ResourceTypes.NODE, resources[n] );
startedWaiting = null;
lockStack.push( WRITE );
}
resourceStack.push( resources[n] );
}
while ( --depth > 0 );
}
catch ( DeadlockDetectedException e )
{
// This is good
}
finally
{
releaseAllLocks( lockStack, resourceStack );
}
}
}
catch ( Exception e )
{
error = e;
}
}
private void releaseAllLocks( Stack<Object> lockStack, Stack<Long> resourceStack )
{
while ( !lockStack.isEmpty() )
{
if ( lockStack.pop() == READ )
{
lm.releaseShared( ResourceTypes.NODE, resourceStack.pop() );
}
else
{
lm.releaseExclusive( ResourceTypes.NODE, resourceStack.pop() );
}
}
}
@Override
public String toString()
{
return this.name;
}
}
@Test
public void testStressMultipleThreads() throws Exception
{
/*
This test starts a bunch of threads, and randomly takes read or write locks on random resources.
No thread should wait more than five seconds for a lock - if it does, we consider it a failure.
Successful outcomes are when threads either finish with all their lock taking and releasing, or
are terminated with a DeadlockDetectedException.
*/
for ( int i = 0; i < StressThread.resources.length; i++ )
{
StressThread.resources[i] = i;
}
StressThread[] stressThreads = new StressThread[50];
CountDownLatch startSignal = new CountDownLatch( 1 );
for ( int i = 0; i < stressThreads.length; i++ )
{
int numberOfIterations = 100;
int depthCount = 10;
float readWriteRatio = 0.80f;
stressThreads[i] = new StressThread( "T" + i, numberOfIterations, depthCount, readWriteRatio,
locks.newClient(),
startSignal );
}
for ( Thread thread : stressThreads )
{
thread.start();
}
startSignal.countDown();
while ( anyAliveAndAllWell( stressThreads ) )
{
throwErrorsIfAny( stressThreads );
sleepALittle();
}
}
private String diagnostics( StressThread culprit, StressThread[] stressThreads, long waited )
{
StringBuilder builder = new StringBuilder();
for ( StressThread stressThread : stressThreads )
{
if ( stressThread.isAlive() )
{
if ( stressThread == culprit )
{
builder.append( "This is the thread that waited too long. It waited: " ).append( waited ).append(
" milliseconds" );
}
for ( StackTraceElement element : stressThread.getStackTrace() )
{
builder.append( element.toString() ).append( "\n" );
}
}
builder.append( "\n" );
}
return builder.toString();
}
private void throwErrorsIfAny( StressThread[] stressThreads ) throws Exception
{
for ( StressThread stressThread : stressThreads )
{
if ( stressThread.error != null )
{
throw stressThread.error;
}
}
}
private void sleepALittle()
{
try
{
Thread.sleep( 1000 );
}
catch ( InterruptedException e )
{
Thread.interrupted();
}
}
private boolean anyAliveAndAllWell( StressThread[] stressThreads )
{
for ( StressThread stressThread : stressThreads )
{
if ( stressThread.isAlive() )
{
Long startedWaiting = stressThread.startedWaiting;
if ( startedWaiting != null )
{
long waitingTime = currentTimeMillis() - startedWaiting;
if ( waitingTime > 5000 )
{
fail( "One of the threads waited far too long. Diagnostics: \n" +
diagnostics( stressThread, stressThreads, waitingTime) );
}
}
return true;
}
}
return false;
}
}