-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
KernelIT.java
660 lines (570 loc) · 22 KB
/
KernelIT.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
* 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 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.api.integrationtest;
import org.junit.Assert;
import org.junit.Test;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import org.neo4j.collection.primitive.PrimitiveIntCollections;
import org.neo4j.collection.primitive.PrimitiveIntIterator;
import org.neo4j.collection.primitive.PrimitiveLongCollections;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.cursor.Cursor;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.TransactionFailureException;
import org.neo4j.graphdb.TransactionTerminatedException;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.SchemaWriteOperations;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.exceptions.EntityNotFoundException;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.api.exceptions.schema.SchemaKernelException;
import org.neo4j.kernel.api.index.IndexDescriptor;
import org.neo4j.kernel.api.security.AnonymousContext;
import org.neo4j.kernel.impl.api.Kernel;
import org.neo4j.kernel.impl.transaction.log.TransactionIdStore;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.storageengine.api.NodeItem;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeThat;
import static org.neo4j.graphdb.Label.label;
import static org.neo4j.helpers.collection.Iterators.asSet;
import static org.neo4j.helpers.collection.Iterators.emptySetOf;
import static org.neo4j.kernel.api.security.SecurityContext.AUTH_DISABLED;
public class KernelIT extends KernelIntegrationTest
{
@Test
public void mixingBeansApiWithKernelAPI() throws Exception
{
// 1: Start your transactions through the Beans API
Transaction transaction = db.beginTx();
// 2: Get a hold of a KernelAPI statement context for the *current* transaction this way:
Statement statement = statementContextSupplier.get();
// 3: Now you can interact through both the statement context and the kernel API to manipulate the
// same transaction.
Node node = db.createNode();
int labelId = statement.dataWriteOperations().labelGetOrCreateForName( "labello" );
statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId );
// 4: Close the StatementContext
statement.close();
// 5: Commit through the beans API
transaction.success();
transaction.close();
}
@Test
public void mixingBeansApiWithKernelAPIForNestedTransaction() throws Exception
{
// GIVEN
Transaction outerTx = db.beginTx();
Statement statement = statementContextSupplier.get();
// WHEN
Node node = db.createNode();
int labelId = statement.dataWriteOperations().labelGetOrCreateForName( "labello" );
statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId );
statement.close();
outerTx.close();
}
@Test
public void changesInTransactionContextShouldBeRolledBackWhenTxIsRolledBack() throws Exception
{
// GIVEN
Node node;
int labelId;
try ( Transaction tx = db.beginTx() )
{
Statement statement = statementContextSupplier.get();
// WHEN
node = db.createNode();
labelId = statement.dataWriteOperations().labelGetOrCreateForName( "labello" );
statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId );
statement.close();
}
// THEN
try ( Transaction tx = db.beginTx() )
{
try ( Statement statement = statementContextSupplier.get() )
{
statement.readOperations().nodeHasLabel( node.getId(), labelId );
fail( "should have thrown exception" );
}
catch ( EntityNotFoundException e )
{
// Yay!
}
}
}
@Test
public void shouldNotBeAbleToCommitIfFailedTransactionContext() throws Exception
{
// WHEN
Node node = null;
int labelId = -1;
TransactionFailureException expectedException = null;
try ( Transaction transaction = db.beginTx() )
{
Statement statement = statementContextSupplier.get();
node = db.createNode();
labelId = statement.dataWriteOperations().labelGetOrCreateForName( "labello" );
statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId );
statement.close();
transaction.failure();
transaction.success();
}
catch ( TransactionFailureException e )
{
expectedException = e;
}
finally
{
Assert.assertNotNull( "Should have failed", expectedException );
}
// THEN
try (Transaction tx = db.beginTx())
{
Statement statement = statementContextSupplier.get();
try
{
statement.readOperations().nodeHasLabel( node.getId(), labelId );
fail( "should have thrown exception" );
}
catch ( EntityNotFoundException e )
{
// Yay!
}
}
}
@Test
public void transactionStateShouldRemovePreviouslyAddedLabel() throws Exception
{
Transaction tx = db.beginTx();
Statement statement = statementContextSupplier.get();
// WHEN
Node node = db.createNode();
int labelId1 = statement.dataWriteOperations().labelGetOrCreateForName( "labello1" );
int labelId2 = statement.dataWriteOperations().labelGetOrCreateForName( "labello2" );
statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId1 );
statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId2 );
statement.dataWriteOperations().nodeRemoveLabel( node.getId(), labelId2 );
statement.close();
tx.success();
tx.close();
// THEN
tx = db.beginTx();
statement = statementContextSupplier.get();
assertEquals( asSet( labelId1 ),
PrimitiveIntCollections.toSet( statement.readOperations().nodeGetLabels( node.getId() ) ) );
tx.close();
}
@Test
public void transactionStateShouldReflectRemovingAddedLabelImmediately() throws Exception
{
Transaction tx = db.beginTx();
Statement statement = statementContextSupplier.get();
// WHEN
Node node = db.createNode();
int labelId1 = statement.dataWriteOperations().labelGetOrCreateForName( "labello1" );
int labelId2 = statement.dataWriteOperations().labelGetOrCreateForName( "labello2" );
statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId1 );
statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId2 );
statement.dataWriteOperations().nodeRemoveLabel( node.getId(), labelId2 );
// THEN
assertFalse( statement.readOperations().nodeHasLabel( node.getId(), labelId2 ) );
assertEquals( asSet( labelId1 ),
PrimitiveIntCollections.toSet( statement.readOperations().nodeGetLabels( node.getId() ) ) );
statement.close();
tx.success();
tx.close();
}
@Test
public void transactionStateShouldReflectRemovingLabelImmediately() throws Exception
{
// GIVEN
Transaction tx = db.beginTx();
Statement statement = statementContextSupplier.get();
Node node = db.createNode();
int labelId1 = statement.dataWriteOperations().labelGetOrCreateForName( "labello1" );
int labelId2 = statement.dataWriteOperations().labelGetOrCreateForName( "labello2" );
statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId1 );
statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId2 );
statement.close();
tx.success();
tx.close();
tx = db.beginTx();
statement = statementContextSupplier.get();
// WHEN
statement.dataWriteOperations().nodeRemoveLabel( node.getId(), labelId2 );
// THEN
PrimitiveIntIterator labelsIterator = statement.readOperations().nodeGetLabels( node.getId() );
Set<Integer> labels = PrimitiveIntCollections.toSet( labelsIterator );
assertFalse( statement.readOperations().nodeHasLabel( node.getId(), labelId2 ) );
assertEquals( asSet( labelId1 ), labels );
statement.close();
tx.success();
tx.close();
}
@Test
public void labelShouldBeRemovedAfterCommit() throws Exception
{
// GIVEN
Transaction tx = db.beginTx();
Statement statement = statementContextSupplier.get();
Node node = db.createNode();
int labelId1 = statement.dataWriteOperations().labelGetOrCreateForName( "labello1" );
statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId1 );
statement.close();
tx.success();
tx.close();
// WHEN
tx = db.beginTx();
statement = statementContextSupplier.get();
statement.dataWriteOperations().nodeRemoveLabel( node.getId(), labelId1 );
statement.close();
tx.success();
tx.close();
// THEN
tx = db.beginTx();
statement = statementContextSupplier.get();
PrimitiveIntIterator labels = statement.readOperations().nodeGetLabels( node.getId() );
statement.close();
tx.success();
tx.close();
assertThat( PrimitiveIntCollections.toSet( labels ), equalTo( Collections.<Integer>emptySet() ) );
}
@Test
public void addingNewLabelToNodeShouldRespondTrue() throws Exception
{
// GIVEN
Transaction tx = db.beginTx();
Node node = db.createNode();
Statement statement = statementContextSupplier.get();
int labelId = statement.dataWriteOperations().labelGetOrCreateForName( "mylabel" );
statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId );
statement.close();
tx.success();
tx.close();
// WHEN
tx = db.beginTx();
statement = statementContextSupplier.get();
boolean added = statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId );
tx.close();
// THEN
assertFalse( "Shouldn't have been added now", added );
}
@Test
public void addingExistingLabelToNodeShouldRespondFalse() throws Exception
{
// GIVEN
Transaction tx = db.beginTx();
Node node = db.createNode();
Statement statement = statementContextSupplier.get();
int labelId = statement.dataWriteOperations().labelGetOrCreateForName( "mylabel" );
statement.close();
tx.success();
tx.close();
// WHEN
tx = db.beginTx();
statement = statementContextSupplier.get();
boolean added = statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId );
tx.close();
// THEN
assertTrue( "Should have been added now", added );
}
@Test
public void removingExistingLabelFromNodeShouldRespondTrue() throws Exception
{
// GIVEN
Transaction tx = db.beginTx();
Node node = db.createNode();
Statement statement = statementContextSupplier.get();
int labelId = statement.dataWriteOperations().labelGetOrCreateForName( "mylabel" );
statement.dataWriteOperations().nodeAddLabel( node.getId(), labelId );
statement.close();
tx.success();
tx.close();
// WHEN
tx = db.beginTx();
statement = statementContextSupplier.get();
boolean removed = statement.dataWriteOperations().nodeRemoveLabel( node.getId(), labelId );
// THEN
assertTrue( "Should have been removed now", removed );
tx.close();
}
@Test
public void removingNonExistentLabelFromNodeShouldRespondFalse() throws Exception
{
// GIVEN
Transaction tx = db.beginTx();
Node node = db.createNode();
Statement statement = statementContextSupplier.get();
int labelId = statement.dataWriteOperations().labelGetOrCreateForName( "mylabel" );
statement.close();
tx.success();
tx.close();
// WHEN
tx = db.beginTx();
statement = statementContextSupplier.get();
boolean removed = statement.dataWriteOperations().nodeRemoveLabel( node.getId(), labelId );
// THEN
assertFalse( "Shouldn't have been removed now", removed );
tx.close();
}
@Test
public void deletingNodeWithLabelsShouldHaveThoseLabelRemovalsReflectedInTransaction() throws Exception
{
// GIVEN
Transaction tx = db.beginTx();
Label label = label( "labello" );
Node node = db.createNode( label );
tx.success();
tx.close();
tx = db.beginTx();
Statement statement = statementContextSupplier.get();
// WHEN
statement.dataWriteOperations().nodeDelete( node.getId() );
// Then
int labelId = statement.readOperations().labelGetForName( label.name() );
try
{
statement.readOperations().nodeGetLabels( node.getId() );
fail();
}
catch ( EntityNotFoundException e )
{
// Ok
}
try
{
statement.readOperations().nodeHasLabel( node.getId(), labelId );
fail();
}
catch ( EntityNotFoundException e )
{
// Ok
}
Set<Long> nodes = PrimitiveLongCollections.toSet( statement.readOperations().nodesGetForLabel( labelId ) );
statement.close();
tx.success();
tx.close();
assertEquals( emptySetOf( Long.class ), nodes );
}
@Test
public void deletingNodeWithLabelsShouldHaveRemovalReflectedInLabelScans() throws Exception
{
// GIVEN
Transaction tx = db.beginTx();
Label label = label( "labello" );
Node node = db.createNode( label );
tx.success();
tx.close();
// AND GIVEN I DELETE IT
tx = db.beginTx();
node.delete();
tx.success();
tx.close();
// WHEN
tx = db.beginTx();
Statement statement = statementContextSupplier.get();
int labelId = statement.readOperations().labelGetForName( label.name() );
PrimitiveLongIterator nodes = statement.readOperations().nodesGetForLabel( labelId );
Set<Long> nodeSet = PrimitiveLongCollections.toSet( nodes );
tx.success();
tx.close();
// THEN
assertThat( nodeSet, equalTo( Collections.<Long>emptySet() ) );
}
@Test
public void schemaStateShouldBeEvictedOnIndexComingOnline() throws Exception
{
// GIVEN
schemaWriteOperationsInNewTransaction();
getOrCreateSchemaState( "my key", "my state" );
commit();
// WHEN
createIndex( schemaWriteOperationsInNewTransaction() );
commit();
try ( Transaction tx = db.beginTx() )
{
db.schema().awaitIndexOnline( db.schema().getIndexes().iterator().next(), 20, SECONDS );
tx.success();
}
// THEN
assertFalse( schemaStateContains( "my key" ) );
}
@Test
public void schemaStateShouldBeEvictedOnIndexDropped() throws Exception
{
// GIVEN
IndexDescriptor idx = createIndex( schemaWriteOperationsInNewTransaction() );
commit();
try ( Transaction tx = db.beginTx() )
{
db.schema().awaitIndexOnline( db.schema().getIndexes().iterator().next(), 20, SECONDS );
getOrCreateSchemaState( "my key", "some state" );
tx.success();
}
// WHEN
schemaWriteOperationsInNewTransaction().indexDrop( idx );
commit();
// THEN
assertFalse( schemaStateContains("my key") );
}
@Test
public void shouldKillTransactionsOnShutdown() throws Throwable
{
// Given
assumeThat(kernel, instanceOf( Kernel.class ));
// Then
try ( KernelTransaction tx = kernel.newTransaction( KernelTransaction.Type.implicit, AnonymousContext.read() ) )
{
((Kernel)kernel).stop();
tx.acquireStatement().readOperations().nodeExists( 0L );
fail("Should have been terminated.");
}
catch( TransactionTerminatedException e )
{
// Success
}
}
@Test
public void txReturnsCorrectIdWhenCommitted() throws Exception
{
executeDummyTxs( db, 42 );
KernelTransaction tx = kernel.newTransaction( KernelTransaction.Type.implicit, AUTH_DISABLED );
try ( Statement statement = tx.acquireStatement() )
{
statement.dataWriteOperations().nodeCreate();
}
tx.success();
long previousCommittedTxId = lastCommittedTxId( db );
assertEquals( previousCommittedTxId + 1, tx.closeTransaction() );
assertFalse( tx.isOpen() );
}
@Test
public void txReturnsCorrectIdWhenRolledBack() throws Exception
{
executeDummyTxs( db, 42 );
KernelTransaction tx = kernel.newTransaction( KernelTransaction.Type.implicit, AUTH_DISABLED );
try ( Statement statement = tx.acquireStatement() )
{
statement.dataWriteOperations().nodeCreate();
}
tx.failure();
assertEquals( KernelTransaction.ROLLBACK, tx.closeTransaction() );
assertFalse( tx.isOpen() );
}
@Test
public void txReturnsCorrectIdWhenMarkedForTermination() throws Exception
{
executeDummyTxs( db, 42 );
KernelTransaction tx = kernel.newTransaction( KernelTransaction.Type.implicit, AUTH_DISABLED );
try ( Statement statement = tx.acquireStatement() )
{
statement.dataWriteOperations().nodeCreate();
}
tx.markForTermination( Status.Transaction.Terminated );
assertEquals( KernelTransaction.ROLLBACK, tx.closeTransaction() );
assertFalse( tx.isOpen() );
}
@Test
public void txReturnsCorrectIdWhenFailedlAndMarkedForTermination() throws Exception
{
executeDummyTxs( db, 42 );
KernelTransaction tx = kernel.newTransaction( KernelTransaction.Type.implicit, AUTH_DISABLED );
try ( Statement statement = tx.acquireStatement() )
{
statement.dataWriteOperations().nodeCreate();
}
tx.failure();
tx.markForTermination( Status.Transaction.Terminated );
assertEquals( KernelTransaction.ROLLBACK, tx.closeTransaction() );
assertFalse( tx.isOpen() );
}
@Test
public void txReturnsCorrectIdWhenReadOnly() throws Exception
{
executeDummyTxs( db, 42 );
KernelTransaction tx = kernel.newTransaction( KernelTransaction.Type.implicit, AUTH_DISABLED );
try ( Statement statement = tx.acquireStatement();
Cursor<NodeItem> cursor = statement.readOperations().nodeCursor( 1 ) )
{
assertTrue( cursor.next() );
cursor.close();
}
tx.success();
assertEquals( KernelTransaction.READ_ONLY, tx.closeTransaction() );
assertFalse( tx.isOpen() );
}
private static void executeDummyTxs( GraphDatabaseService db, int count )
{
for ( int i = 0; i < count; i++ )
{
try ( Transaction tx = db.beginTx() )
{
db.createNode();
tx.success();
}
}
}
private static long lastCommittedTxId( GraphDatabaseAPI db )
{
TransactionIdStore txIdStore = db.getDependencyResolver().resolveDependency( TransactionIdStore.class );
return txIdStore.getLastCommittedTransactionId();
}
private IndexDescriptor createIndex( SchemaWriteOperations schemaWriteOperations ) throws SchemaKernelException
{
return schemaWriteOperations.indexCreate( schemaWriteOperations.labelGetOrCreateForName( "hello" ),
schemaWriteOperations.propertyKeyGetOrCreateForName( "hepp" ) );
}
private String getOrCreateSchemaState( String key, final String maybeSetThisState )
{
try ( Transaction tx = db.beginTx() )
{
Statement statement = statementContextSupplier.get();
String state = statement.readOperations().schemaStateGetOrCreate( key, s -> maybeSetThisState );
tx.success();
return state;
}
}
private boolean schemaStateContains( String key )
{
try ( Transaction tx = db.beginTx() )
{
Statement statement = statementContextSupplier.get();
final AtomicBoolean result = new AtomicBoolean( true );
statement.readOperations().schemaStateGetOrCreate( key, s -> {
result.set( false );
return null;
} );
tx.success();
return result.get();
}
}
}