-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SessionResetIT.java
398 lines (352 loc) · 13.5 KB
/
SessionResetIT.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
397
398
/*
* Copyright (c) 2002-2017 "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.bolt;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.nio.channels.ClosedChannelException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.neo4j.driver.v1.Config;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.Transaction;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
import org.neo4j.driver.v1.exceptions.TransientException;
import org.neo4j.graphdb.DependencyResolver;
import org.neo4j.graphdb.Result;
import org.neo4j.harness.junit.EnterpriseNeo4jRule;
import org.neo4j.harness.junit.Neo4jRule;
import org.neo4j.io.IOUtils;
import org.neo4j.kernel.configuration.Settings;
import org.neo4j.kernel.impl.api.KernelTransactions;
import org.neo4j.kernel.impl.enterprise.configuration.OnlineBackupSettings;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.test.ThreadTestUtils;
import org.neo4j.test.rule.VerboseTimeout;
import static java.util.Collections.newSetFromMap;
import static java.util.concurrent.CompletableFuture.runAsync;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.neo4j.driver.v1.Config.EncryptionLevel.NONE;
import static org.neo4j.function.Predicates.await;
import static org.neo4j.helpers.Exceptions.rootCause;
import static org.neo4j.helpers.NamedThreadFactory.daemon;
import static org.neo4j.helpers.collection.Iterators.single;
import static org.neo4j.test.assertion.Assert.assertEventually;
public class SessionResetIT
{
private static final String SHORT_QUERY_1 = "CREATE (n:Node {name: 'foo', occupation: 'bar'})";
private static final String SHORT_QUERY_2 = "MATCH (n:Node {name: 'foo'}) RETURN count(n)";
private static final String LONG_QUERY = "UNWIND range(0, 10000000) AS i CREATE (n:Node {idx: i}) DELETE n";
private static final String LONG_PERIODIC_COMMIT_QUERY = "USING PERIODIC COMMIT 1 " +
"LOAD CSV FROM '" + createTmpCsvFile() + "' AS l " +
"UNWIND range(0, 10) AS i " +
"CREATE (n:Node {name: l[0], occupation: l[1], idx: i}) " +
"DELETE n";
private static final int STRESS_IT_THREAD_COUNT = Runtime.getRuntime().availableProcessors() * 2;
private static final long STRESS_IT_DURATION_MS = SECONDS.toMillis( 5 );
private static final String[] STRESS_IT_QUERIES = {SHORT_QUERY_1, SHORT_QUERY_2, LONG_QUERY};
private final VerboseTimeout timeout = VerboseTimeout.builder().withTimeout( 3, MINUTES ).build();
private final Neo4jRule db = new EnterpriseNeo4jRule().withConfig( OnlineBackupSettings.online_backup_enabled, Settings.FALSE );
@Rule
public final RuleChain ruleChain = RuleChain.outerRule( timeout ).around( db );
private Driver driver;
@Before
public void setUp() throws Exception
{
driver = GraphDatabase.driver( db.boltURI(), Config.build().withEncryptionLevel( NONE ).toConfig() );
}
@After
public void tearDown() throws Exception
{
IOUtils.closeAllSilently( driver );
}
@Test
public void shouldTerminateAutoCommitQuery() throws Exception
{
testQueryTermination( LONG_QUERY, true );
}
@Test
public void shouldTerminateQueryInExplicitTransaction() throws Exception
{
testQueryTermination( LONG_QUERY, false );
}
/**
* It is currently unsafe to terminate periodic commit query because it'll then be half-committed.
*/
@Test
public void shouldNotTerminatePeriodicCommitQueries() throws Exception
{
// periodic commit query can't be terminated so but reset must fail the transaction
Future<Void> queryResult = runQueryInDifferentThreadAndResetSession( LONG_PERIODIC_COMMIT_QUERY, true );
try
{
assertNull( queryResult.get( 1, MINUTES ) );
}
catch ( TimeoutException e )
{
System.err.println( "Unable to get query result, dumping all stacktraces:" );
ThreadTestUtils.dumpAllStackTraces();
throw e;
}
assertDatabaseIsIdle();
// termination must cause transaction failure and no nodes should be committed
assertEquals( 0, countNodes() );
}
@Test
public void shouldTerminateAutoCommitQueriesRandomly() throws Exception
{
testRandomQueryTermination( true );
}
@Test
public void shouldTerminateQueriesInExplicitTransactionsRandomly() throws Exception
{
testRandomQueryTermination( false );
}
private void testRandomQueryTermination( boolean autoCommit ) throws Exception
{
ExecutorService executor = Executors.newFixedThreadPool( STRESS_IT_THREAD_COUNT, daemon( "test-worker" ) );
Set<Session> runningSessions = newSetFromMap( new ConcurrentHashMap<>() );
AtomicBoolean stop = new AtomicBoolean();
List<Future<?>> futures = new ArrayList<>();
for ( int i = 0; i < STRESS_IT_THREAD_COUNT; i++ )
{
futures.add( executor.submit( () ->
{
ThreadLocalRandom random = ThreadLocalRandom.current();
while ( !stop.get() )
{
runRandomQuery( autoCommit, random, runningSessions, stop );
}
} ) );
}
long deadline = System.currentTimeMillis() + STRESS_IT_DURATION_MS;
while ( !stop.get() )
{
if ( System.currentTimeMillis() > deadline )
{
stop.set( true );
}
resetAny( runningSessions );
MILLISECONDS.sleep( 30 );
}
driver.close();
awaitAll( futures );
assertDatabaseIsIdle();
}
private void runRandomQuery( boolean autoCommit, Random random, Set<Session> runningSessions, AtomicBoolean stop )
{
try
{
Session session = driver.session();
runningSessions.add( session );
try
{
String query = STRESS_IT_QUERIES[random.nextInt( STRESS_IT_QUERIES.length - 1 )];
runQuery( session, query, autoCommit );
}
finally
{
runningSessions.remove( session );
session.close();
}
}
catch ( Throwable error )
{
if ( !stop.get() && !isAcceptable( error ) )
{
stop.set( true );
throw error;
}
// else it is fine to receive some errors from the driver because
// sessions are being reset concurrently by the main thread, driver can also be closed concurrently
}
}
private void testQueryTermination( String query, boolean autoCommit ) throws Exception
{
Future<Void> queryResult = runQueryInDifferentThreadAndResetSession( query, autoCommit );
try
{
queryResult.get( 10, SECONDS );
fail( "Exception expected" );
}
catch ( Exception e )
{
assertThat( e, instanceOf( ExecutionException.class ) );
assertTrue( isTransactionTerminatedException( e.getCause() ) );
}
assertDatabaseIsIdle();
}
private Future<Void> runQueryInDifferentThreadAndResetSession( String query, boolean autoCommit ) throws Exception
{
AtomicReference<Session> sessionRef = new AtomicReference<>();
Future<Void> queryResult = runAsync( () ->
{
try ( Session session = driver.session() )
{
sessionRef.set( session );
runQuery( session, query, autoCommit );
}
} );
await( () -> activeQueriesCount() == 1, 10, SECONDS );
SECONDS.sleep( 1 ); // additionally wait a bit before resetting the session
Session session = sessionRef.get();
assertNotNull( session );
session.reset();
return queryResult;
}
private static void runQuery( Session session, String query, boolean autoCommit )
{
if ( autoCommit )
{
session.run( query ).consume();
}
else
{
try ( Transaction tx = session.beginTransaction() )
{
tx.run( query );
tx.success();
}
}
}
private void assertDatabaseIsIdle() throws InterruptedException
{
assertEventually( "Wrong number of active queries", this::activeQueriesCount, is( 0L ), 10, SECONDS );
assertEventually( "Wrong number of active transactions", this::activeTransactionsCount, is( 0L ), 10, SECONDS );
}
private long activeQueriesCount()
{
try ( Result result = db().execute( "CALL dbms.listQueries() YIELD queryId RETURN count(queryId) AS result" ) )
{
return (long) single( result ).get( "result" ) - 1; // do not count listQueries procedure invocation
}
}
private long activeTransactionsCount()
{
DependencyResolver resolver = db().getDependencyResolver();
KernelTransactions kernelTransactions = resolver.resolveDependency( KernelTransactions.class );
return kernelTransactions.activeTransactions().size();
}
private long countNodes()
{
try ( Result result = db().execute( "MATCH (n) RETURN count(n) AS result" ) )
{
return (long) single( result ).get( "result" );
}
}
private GraphDatabaseAPI db()
{
return (GraphDatabaseAPI) db.getGraphDatabaseService();
}
private static void resetAny( Set<Session> sessions )
{
sessions.stream().findAny().ifPresent( session ->
{
if ( sessions.remove( session ) )
{
resetSafely( session );
}
} );
}
private static void resetSafely( Session session )
{
try
{
if ( session.isOpen() )
{
session.reset();
}
}
catch ( ClientException e )
{
if ( session.isOpen() )
{
throw e;
}
// else this thread lost race with close and it's fine
}
}
private static boolean isAcceptable( Throwable error )
{
Throwable cause = rootCause( error );
return isTransactionTerminatedException( cause ) ||
cause instanceof ServiceUnavailableException ||
cause instanceof ClientException ||
cause instanceof ClosedChannelException;
}
private static boolean isTransactionTerminatedException( Throwable error )
{
return error instanceof TransientException &&
error.getMessage().startsWith( "The transaction has been terminated" );
}
private static URI createTmpCsvFile()
{
try
{
Path csvFile = Files.createTempFile( "test", ".csv" );
List<String> lines = range( 0, 50000 ).mapToObj( i -> "Foo-" + i + ", Bar-" + i ).collect( toList() );
return Files.write( csvFile, lines ).toAbsolutePath().toUri();
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
}
private static void awaitAll( List<Future<?>> futures ) throws Exception
{
for ( Future<?> future : futures )
{
assertNull( future.get( 1, MINUTES ) );
}
}
}