-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LabelScanStoreTest.java
632 lines (545 loc) · 20.9 KB
/
LabelScanStoreTest.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
/*
* 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.api.impl.labelscan;
import org.apache.lucene.index.IndexFileNames;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.RuleChain;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.neo4j.collection.primitive.PrimitiveLongCollections;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.helpers.collection.BoundedIterable;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.helpers.collection.PrefetchingIterator;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.IOLimiter;
import org.neo4j.kernel.api.labelscan.LabelScanStore;
import org.neo4j.kernel.api.labelscan.LabelScanWriter;
import org.neo4j.kernel.api.labelscan.NodeLabelRange;
import org.neo4j.kernel.api.labelscan.NodeLabelUpdate;
import org.neo4j.kernel.impl.api.scan.FullStoreChangeStream;
import org.neo4j.kernel.lifecycle.LifeSupport;
import org.neo4j.storageengine.api.schema.LabelScanReader;
import org.neo4j.test.rule.RandomRule;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.hamcrest.core.IsCollectionContaining.hasItems;
import static org.junit.Assert.assertArrayEquals;
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.neo4j.collection.primitive.PrimitiveLongCollections.EMPTY_LONG_ARRAY;
import static org.neo4j.helpers.collection.Iterators.iterator;
import static org.neo4j.helpers.collection.Iterators.single;
import static org.neo4j.io.fs.FileUtils.deleteRecursively;
import static org.neo4j.kernel.api.labelscan.NodeLabelUpdate.labelChanges;
public abstract class LabelScanStoreTest
{
private final TestDirectory testDirectory = TestDirectory.testDirectory();
private final ExpectedException expectedException = ExpectedException.none();
private final DefaultFileSystemRule fileSystemRule = new DefaultFileSystemRule();
final RandomRule random = new RandomRule();
@Rule
public final RuleChain ruleChain = RuleChain.outerRule( random ).around( testDirectory ).around( expectedException )
.around( fileSystemRule );
private static final long[] NO_LABELS = new long[0];
private LifeSupport life;
private TrackingMonitor monitor;
private LabelScanStore store;
private File dir;
@Before
public void clearDir() throws IOException
{
dir = testDirectory.directory( "lucene" );
if ( dir.exists() )
{
deleteRecursively( dir );
}
}
@After
public void shutdown()
{
life.shutdown();
}
protected abstract LabelScanStore createLabelScanStore( FileSystemAbstraction fileSystemAbstraction,
File rootFolder, List<NodeLabelUpdate> existingData, boolean usePersistentStore, boolean readOnly,
LabelScanStore.Monitor monitor );
@Test
public void failToRetrieveWriterOnReadOnlyScanStore() throws IOException
{
createAndStartReadOnly();
expectedException.expect( UnsupportedOperationException.class );
store.newWriter();
}
@Test
public void forceShouldNotForceWriterOnReadOnlyScanStore()
{
createAndStartReadOnly();
store.force( IOLimiter.unlimited() );
}
@Test
public void failToStartIfLabelScanStoreIndexDoesNotExistInReadOnlyMode()
{
expectedException.expectCause( Matchers.instanceOf( UnsupportedOperationException.class ) );
start( false, true );
}
@Test
public void snapshotReadOnlyLabelScanStore() throws IOException
{
prepareIndex();
createAndStartReadOnly();
try (ResourceIterator<File> indexFiles = store.snapshotStoreFiles())
{
List<String> filesNames = indexFiles.stream().map( File::getName ).collect( toList() );
assertThat( "Should have at least index segment file.", filesNames,
hasItem( startsWith( IndexFileNames.SEGMENTS ) ) );
}
}
@Test
public void shouldUpdateIndexOnLabelChange() throws Exception
{
// GIVEN
int labelId = 1;
long nodeId = 10;
start();
// WHEN
write( iterator( labelChanges( nodeId, NO_LABELS, new long[]{labelId} ) ) );
// THEN
assertNodesForLabel( labelId, nodeId );
}
@Test
public void shouldUpdateIndexOnAddedLabels() throws Exception
{
// GIVEN
int labelId1 = 1, labelId2 = 2;
long nodeId = 10;
start();
write( iterator( labelChanges( nodeId, NO_LABELS, new long[]{labelId1} ) ) );
assertNodesForLabel( labelId2 );
// WHEN
write( iterator( labelChanges( nodeId, NO_LABELS, new long[]{labelId1, labelId2} ) ) );
// THEN
assertNodesForLabel( labelId1, nodeId );
assertNodesForLabel( labelId2, nodeId );
}
@Test
public void shouldUpdateIndexOnRemovedLabels() throws Exception
{
// GIVEN
int labelId1 = 1, labelId2 = 2;
long nodeId = 10;
start();
write( iterator( labelChanges( nodeId, NO_LABELS, new long[]{labelId1, labelId2} ) ) );
assertNodesForLabel( labelId1, nodeId );
assertNodesForLabel( labelId2, nodeId );
// WHEN
write( iterator( labelChanges( nodeId, new long[]{labelId1, labelId2}, new long[]{labelId2} ) ) );
// THEN
assertNodesForLabel( labelId1 );
assertNodesForLabel( labelId2, nodeId );
}
@Test
public void shouldDeleteFromIndexWhenDeletedNode() throws Exception
{
// GIVEN
int labelId = 1;
long nodeId = 10;
start();
write( iterator( labelChanges( nodeId, NO_LABELS, new long[]{labelId} ) ) );
// WHEN
write( iterator( labelChanges( nodeId, new long[]{labelId}, NO_LABELS ) ) );
// THEN
assertNodesForLabel( labelId );
}
@Test
public void shouldScanSingleRange() throws Exception
{
// GIVEN
int labelId1 = 1, labelId2 = 2;
long nodeId1 = 10, nodeId2 = 11;
start( asList(
labelChanges( nodeId1, NO_LABELS, new long[]{labelId1} ),
labelChanges( nodeId2, NO_LABELS, new long[]{labelId1, labelId2} )
) );
// WHEN
BoundedIterable<NodeLabelRange> reader = store.allNodeLabelRanges();
NodeLabelRange range = single( reader.iterator() );
// THEN
assertArrayEquals( new long[]{nodeId1, nodeId2}, sorted( range.nodes() ) );
assertArrayEquals( new long[]{labelId1}, sorted( range.labels( nodeId1 ) ) );
assertArrayEquals( new long[]{labelId1, labelId2}, sorted( range.labels( nodeId2 ) ) );
}
@Test
public void shouldScanMultipleRanges() throws Exception
{
// GIVEN
int labelId1 = 1, labelId2 = 2;
long nodeId1 = 10, nodeId2 = 1280;
start( asList(
labelChanges( nodeId1, NO_LABELS, new long[]{labelId1} ),
labelChanges( nodeId2, NO_LABELS, new long[]{labelId1, labelId2} )
) );
// WHEN
BoundedIterable<NodeLabelRange> reader = store.allNodeLabelRanges();
Iterator<NodeLabelRange> iterator = reader.iterator();
NodeLabelRange range1 = iterator.next();
NodeLabelRange range2 = iterator.next();
assertFalse( iterator.hasNext() );
// THEN
assertArrayEquals( new long[]{nodeId1}, sorted( range1.nodes() ) );
assertArrayEquals( new long[]{nodeId2}, sorted( range2.nodes() ) );
assertArrayEquals( new long[]{labelId1}, sorted( range1.labels( nodeId1 ) ) );
assertArrayEquals( new long[]{labelId1, labelId2}, sorted( range2.labels( nodeId2 ) ) );
}
@Test
public void shouldWorkWithAFullRange() throws Exception
{
// given
long labelId = 0;
List<NodeLabelUpdate> updates = new ArrayList<>();
for ( int i = 0; i < 34; i++ )
{
updates.add( NodeLabelUpdate.labelChanges( i, new long[]{}, new long[]{labelId} ) );
}
start( updates );
// when
LabelScanReader reader = store.newReader();
Set<Long> nodesWithLabel = PrimitiveLongCollections.toSet( reader.nodesWithLabel( (int) labelId ) );
// then
for ( long i = 0; i < 34; i++ )
{
assertThat( nodesWithLabel, hasItem( i ) );
Set<Long> labels = PrimitiveLongCollections.toSet( reader.labelsForNode( i ) );
assertThat( labels, hasItem( labelId ) );
}
}
@Test
public void shouldUpdateAFullRange() throws Exception
{
// given
long label0Id = 0;
List<NodeLabelUpdate> label0Updates = new ArrayList<>();
for ( int i = 0; i < 34; i++ )
{
label0Updates.add( NodeLabelUpdate.labelChanges( i, new long[]{}, new long[]{label0Id} ) );
}
start( label0Updates );
// when
write( Collections.emptyIterator() );
// then
LabelScanReader reader = store.newReader();
Set<Long> nodesWithLabel0 = PrimitiveLongCollections.toSet( reader.nodesWithLabel( (int) label0Id ) );
for ( long i = 0; i < 34; i++ )
{
assertThat( nodesWithLabel0, hasItem( i ) );
Set<Long> labels = PrimitiveLongCollections.toSet( reader.labelsForNode( i ) );
assertThat( labels, hasItem( label0Id ) );
}
}
private void write( Iterator<NodeLabelUpdate> iterator ) throws IOException
{
try ( LabelScanWriter writer = store.newWriter() )
{
while ( iterator.hasNext() )
{
writer.write( iterator.next() );
}
}
}
private long[] sorted( long[] input )
{
Arrays.sort( input );
return input;
}
@Test
public void shouldRebuildFromScratchIfIndexMissing() throws Exception
{
// GIVEN a start of the store with existing data in it
start( asList(
labelChanges( 1, NO_LABELS, new long[]{1} ),
labelChanges( 2, NO_LABELS, new long[]{1, 2} )
) );
// THEN
assertTrue( "Didn't rebuild the store on startup",
monitor.noIndexCalled & monitor.rebuildingCalled & monitor.rebuiltCalled );
assertNodesForLabel( 1, 1, 2 );
assertNodesForLabel( 2, 2 );
}
@Test
public void rebuildCorruptedIndexIndexOnStartup() throws Exception
{
// GIVEN a start of the store with existing data in it
List<NodeLabelUpdate> data = asList(
labelChanges( 1, NO_LABELS, new long[]{1} ),
labelChanges( 2, NO_LABELS, new long[]{1, 2} ) );
start( data, true, false );
// WHEN the index is corrupted and then started again
scrambleIndexFilesAndRestart( data, true, false );
assertTrue( "Index corruption should be detected", monitor.corruptedIndex );
assertTrue( "Index should be rebuild", monitor.rebuildingCalled );
}
@Test
public void shouldFindDecentAmountOfNodesForALabel() throws Exception
{
// GIVEN
// 16 is the magic number of the page iterator
// 32 is the number of nodes in each lucene document
final int labelId = 1, nodeCount = 32 * 16 + 10;
start();
write( new PrefetchingIterator<NodeLabelUpdate>()
{
private int i = -1;
@Override
protected NodeLabelUpdate fetchNextOrNull()
{
return ++i < nodeCount ? labelChanges( i, NO_LABELS, new long[]{labelId} ) : null;
}
} );
// WHEN
Set<Long> nodeSet = new TreeSet<>();
LabelScanReader reader = store.newReader();
PrimitiveLongIterator nodes = reader.nodesWithLabel( labelId );
while ( nodes.hasNext() )
{
nodeSet.add( nodes.next() );
}
reader.close();
// THEN
assertEquals( "Found gaps in node id range: " + gaps( nodeSet, nodeCount ), nodeCount, nodeSet.size() );
}
@Test
public void shouldFindAllLabelsForGivenNode() throws Exception
{
// GIVEN
// 16 is the magic number of the page iterator
// 32 is the number of nodes in each lucene document
final long labelId1 = 1, labelId2 = 2, labelId3 = 87;
start();
int nodeId = 42;
write( Iterators.iterator( labelChanges( nodeId, NO_LABELS, new long[]{labelId1, labelId2} ) ) );
write( Iterators.iterator( labelChanges( 41, NO_LABELS, new long[]{labelId3, labelId2} ) ) );
// WHEN
LabelScanReader reader = store.newReader();
// THEN
assertThat( PrimitiveLongCollections.toSet( reader.labelsForNode( nodeId ) ), hasItems( labelId1, labelId2 ) );
reader.close();
}
@Test
public void shouldFindNodesWithAnyOfGivenLabels() throws Exception
{
// GIVEN
int labelId1 = 3, labelId2 = 5, labelId3 = 13;
start();
// WHEN
write( iterator(
labelChanges( 2, EMPTY_LONG_ARRAY, new long[] {labelId1, labelId2} ),
labelChanges( 1, EMPTY_LONG_ARRAY, new long[] {labelId1} ),
labelChanges( 4, EMPTY_LONG_ARRAY, new long[] {labelId1, labelId3} ),
labelChanges( 5, EMPTY_LONG_ARRAY, new long[] {labelId1, labelId2, labelId3} ),
labelChanges( 3, EMPTY_LONG_ARRAY, new long[] {labelId1} ),
labelChanges( 7, EMPTY_LONG_ARRAY, new long[] { labelId2} ),
labelChanges( 8, EMPTY_LONG_ARRAY, new long[] { labelId3} ),
labelChanges( 6, EMPTY_LONG_ARRAY, new long[] { labelId2} ),
labelChanges( 9, EMPTY_LONG_ARRAY, new long[] { labelId3} ) ) );
// THEN
try ( LabelScanReader reader = store.newReader() )
{
assertArrayEquals(
new long[] {1, 2, 3, 4, 5, 6, 7},
PrimitiveLongCollections.asArray( reader.nodesWithAnyOfLabels( labelId1, labelId2 ) ) );
assertArrayEquals(
new long[] {1, 2, 3, 4, 5, 8, 9},
PrimitiveLongCollections.asArray( reader.nodesWithAnyOfLabels( labelId1, labelId3 ) ) );
assertArrayEquals(
new long[] {1, 2, 3, 4, 5, 6, 7, 8, 9},
PrimitiveLongCollections.asArray( reader.nodesWithAnyOfLabels( labelId1, labelId2, labelId3 ) ) );
}
}
@Test
public void shouldFindNodesWithAllGivenLabels() throws Exception
{
// GIVEN
int labelId1 = 3, labelId2 = 5, labelId3 = 13;
start();
// WHEN
write( iterator(
labelChanges( 5, EMPTY_LONG_ARRAY, new long[] {labelId1, labelId2, labelId3} ),
labelChanges( 8, EMPTY_LONG_ARRAY, new long[] { labelId3} ),
labelChanges( 3, EMPTY_LONG_ARRAY, new long[] {labelId1} ),
labelChanges( 6, EMPTY_LONG_ARRAY, new long[] { labelId2} ),
labelChanges( 1, EMPTY_LONG_ARRAY, new long[] {labelId1} ),
labelChanges( 7, EMPTY_LONG_ARRAY, new long[] { labelId2} ),
labelChanges( 4, EMPTY_LONG_ARRAY, new long[] {labelId1, labelId3} ),
labelChanges( 2, EMPTY_LONG_ARRAY, new long[] {labelId1, labelId2} ),
labelChanges( 9, EMPTY_LONG_ARRAY, new long[] { labelId3} ) ) );
// THEN
try ( LabelScanReader reader = store.newReader() )
{
assertArrayEquals(
new long[] {2, 5},
PrimitiveLongCollections.asArray( reader.nodesWithAllLabels( labelId1, labelId2 ) ) );
assertArrayEquals(
new long[] {4, 5},
PrimitiveLongCollections.asArray( reader.nodesWithAllLabels( labelId1, labelId3 ) ) );
assertArrayEquals(
new long[] {5},
PrimitiveLongCollections.asArray( reader.nodesWithAllLabels( labelId1, labelId2, labelId3 ) ) );
}
}
private void prepareIndex() throws IOException
{
start();
try ( LabelScanWriter labelScanWriter = store.newWriter() )
{
labelScanWriter.write( NodeLabelUpdate.labelChanges( 1, new long[]{}, new long[]{1} ) );
}
store.shutdown();
}
private Set<Long> gaps( Set<Long> ids, int expectedCount )
{
Set<Long> gaps = new HashSet<>();
for ( long i = 0; i < expectedCount; i++ )
{
if ( !ids.contains( i ) )
{
gaps.add( i );
}
}
return gaps;
}
private void assertNodesForLabel( int labelId, long... expectedNodeIds )
{
Set<Long> nodeSet = new HashSet<>();
PrimitiveLongIterator nodes = store.newReader().nodesWithLabel( labelId );
while ( nodes.hasNext() )
{
nodeSet.add( nodes.next() );
}
for ( long expectedNodeId : expectedNodeIds )
{
assertTrue( "Expected node " + expectedNodeId + " not found in scan store",
nodeSet.remove( expectedNodeId ) );
}
assertTrue( "Unexpected nodes in scan store " + nodeSet, nodeSet.isEmpty() );
}
private void createAndStartReadOnly()
{
// create label scan store and shutdown it
start();
life.shutdown();
start( false, true );
}
private void start()
{
start( false, false );
}
private void start( boolean usePersistentStore, boolean readOnly )
{
start( Collections.emptyList(), usePersistentStore, readOnly );
}
private void start( List<NodeLabelUpdate> existingData )
{
start( existingData, false, false );
}
private void start( List<NodeLabelUpdate> existingData, boolean usePersistentStore,
boolean readOnly )
{
life = new LifeSupport();
monitor = new TrackingMonitor();
store = createLabelScanStore( fileSystemRule.get(), dir, existingData, usePersistentStore, readOnly, monitor );
life.add( store );
life.start();
assertTrue( monitor.initCalled );
}
FullStoreChangeStream asStream( final List<NodeLabelUpdate> existingData )
{
return writer ->
{
long count = 0;
for ( NodeLabelUpdate update : existingData )
{
writer.write( update );
count++;
}
return count;
};
}
private void scrambleIndexFilesAndRestart( List<NodeLabelUpdate> data,
boolean usePersistentStore, boolean readOnly ) throws IOException
{
shutdown();
corruptIndex();
start( data, usePersistentStore, readOnly );
}
protected abstract void corruptIndex() throws IOException;
private static class TrackingMonitor implements LabelScanStore.Monitor
{
boolean initCalled, rebuildingCalled, rebuiltCalled, noIndexCalled;
boolean corruptedIndex = false;
@Override
public void noIndex()
{
noIndexCalled = true;
}
@Override
public void lockedIndex( Exception e )
{
}
@Override
public void notValidIndex()
{
corruptedIndex = true;
}
@Override
public void rebuilding()
{
rebuildingCalled = true;
}
@Override
public void rebuilt( long roughNodeCount )
{
rebuiltCalled = true;
}
@Override
public void init()
{
initCalled = true;
}
}
}