-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NativeSchemaNumberIndexPopulatorTest.java
610 lines (527 loc) · 17.1 KB
/
NativeSchemaNumberIndexPopulatorTest.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
/*
* 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.index.schema;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import org.neo4j.index.internal.gbptree.GBPTree;
import org.neo4j.index.internal.gbptree.Layout;
import org.neo4j.index.internal.gbptree.RecoveryCleanupWorkCollector;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.fs.OpenMode;
import org.neo4j.io.fs.StoreChannel;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.io.pagecache.PagedFile;
import org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException;
import org.neo4j.kernel.api.index.IndexEntryUpdate;
import org.neo4j.kernel.api.index.IndexUpdater;
import org.neo4j.kernel.api.index.PropertyAccessor;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig;
import org.neo4j.values.storable.Values;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.neo4j.index.internal.gbptree.GBPTree.NO_HEADER_WRITER;
import static org.neo4j.kernel.impl.index.schema.NativeSchemaNumberIndexPopulator.BYTE_FAILED;
import static org.neo4j.kernel.impl.index.schema.NativeSchemaNumberIndexPopulator.BYTE_ONLINE;
public abstract class NativeSchemaNumberIndexPopulatorTest<KEY extends SchemaNumberKey,VALUE extends SchemaNumberValue>
extends SchemaNumberIndexTestUtil<KEY,VALUE>
{
private static final int LARGE_AMOUNT_OF_UPDATES = 1_000;
static final PropertyAccessor null_property_accessor = ( nodeId, propKeyId ) ->
{
throw new RuntimeException( "Did not expect an attempt to go to store" );
};
NativeSchemaNumberIndexPopulator<KEY,VALUE> populator;
@Before
public void setupPopulator()
{
IndexSamplingConfig samplingConfig = new IndexSamplingConfig( Config.defaults() );
populator = createPopulator( pageCache, fs, indexFile, layout, samplingConfig );
}
abstract NativeSchemaNumberIndexPopulator<KEY,VALUE> createPopulator( PageCache pageCache, FileSystemAbstraction fs, File indexFile,
Layout<KEY,VALUE> layout, IndexSamplingConfig samplingConfig );
@Test
public void createShouldCreateFile() throws Exception
{
// given
assertFileNotPresent();
// when
populator.create();
// then
assertFilePresent();
populator.close( true );
}
@Test
public void createShouldClearExistingFile() throws Exception
{
// given
byte[] someBytes = fileWithContent();
// when
populator.create();
// then
try ( StoreChannel r = fs.open( indexFile, OpenMode.READ ) )
{
byte[] firstBytes = new byte[someBytes.length];
r.readAll( ByteBuffer.wrap( firstBytes ) );
assertNotEquals( "Expected previous file content to have been cleared but was still there",
someBytes, firstBytes );
}
populator.close( true );
}
@Test
public void dropShouldDeleteExistingFile() throws Exception
{
// given
populator.create();
// when
populator.drop();
// then
assertFileNotPresent();
}
@Test
public void dropShouldSucceedOnNonExistentFile() throws Exception
{
// given
assertFileNotPresent();
// when
populator.drop();
// then
assertFileNotPresent();
}
@Test
public void addShouldHandleEmptyCollection() throws Exception
{
// given
populator.create();
List<IndexEntryUpdate<?>> updates = Collections.emptyList();
// when
populator.add( updates );
// then
populator.close( true );
}
@Test
public void addShouldApplyAllUpdatesOnce() throws Exception
{
// given
populator.create();
@SuppressWarnings( "unchecked" )
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
// when
populator.add( Arrays.asList( updates ) );
// then
populator.close( true );
verifyUpdates( updates );
}
@Test
public void updaterShouldApplyUpdates() throws Exception
{
// given
populator.create();
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
try ( IndexUpdater updater = populator.newPopulatingUpdater( null_property_accessor ) )
{
// when
for ( IndexEntryUpdate<IndexDescriptor> update : updates )
{
updater.process( update );
}
}
// then
populator.close( true );
verifyUpdates( updates );
}
@Test
public void updaterMustThrowIfProcessAfterClose() throws Exception
{
// given
populator.create();
IndexUpdater updater = populator.newPopulatingUpdater( null_property_accessor );
// when
updater.close();
// then
try
{
updater.process( layoutUtil.add( 1, Values.of( Long.MAX_VALUE ) ) );
fail( "Expected process to throw on closed updater" );
}
catch ( IllegalStateException e )
{
// good
}
populator.close( true );
}
@Test
public void shouldApplyInterleavedUpdatesFromAddAndUpdater() throws Exception
{
// given
populator.create();
@SuppressWarnings( "unchecked" )
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
// when
applyInterleaved( updates, populator );
// then
populator.close( true );
verifyUpdates( updates );
}
@Test
public void successfulCloseMustCloseGBPTree() throws Exception
{
// given
populator.create();
Optional<PagedFile> existingMapping = pageCache.getExistingMapping( indexFile );
if ( existingMapping.isPresent() )
{
existingMapping.get().close();
}
else
{
fail( "Expected underlying GBPTree to have a mapping for this file" );
}
// when
populator.close( true );
// then
existingMapping = pageCache.getExistingMapping( indexFile );
assertFalse( existingMapping.isPresent() );
}
@Test
public void successfulCloseMustMarkIndexAsOnline() throws Exception
{
// given
populator.create();
// when
populator.close( true );
// then
assertHeader( true, null, false );
}
@Test
public void unsuccessfulCloseMustSucceedWithoutMarkAsFailed() throws Exception
{
// given
populator.create();
// then
populator.close( false );
}
@Test
public void unsuccessfulCloseMustCloseGBPTree() throws Exception
{
// given
populator.create();
Optional<PagedFile> existingMapping = pageCache.getExistingMapping( indexFile );
if ( existingMapping.isPresent() )
{
existingMapping.get().close();
}
else
{
fail( "Expected underlying GBPTree to have a mapping for this file" );
}
// when
populator.close( false );
// then
existingMapping = pageCache.getExistingMapping( indexFile );
assertFalse( existingMapping.isPresent() );
}
@Test
public void unsuccessfulCloseMustNotMarkIndexAsOnline() throws Exception
{
// given
populator.create();
// when
populator.close( false );
// then
assertHeader( false, "", false );
}
@Test
public void closeMustWriteFailureMessageAfterMarkedAsFailed() throws Exception
{
// given
populator.create();
// when
String failureMessage = "Fly, you fools!";
populator.markAsFailed( failureMessage );
populator.close( false );
// then
assertHeader( false, failureMessage, false );
}
@Test
public void closeMustWriteFailureMessageAfterMarkedAsFailedWithLongMessage() throws Exception
{
// given
populator.create();
// when
String failureMessage = longString( pageCache.pageSize() );
populator.markAsFailed( failureMessage );
populator.close( false );
// then
assertHeader( false, failureMessage, true );
}
@Test
public void successfulCloseMustThrowIfMarkedAsFailed() throws Exception
{
// given
populator.create();
// when
populator.markAsFailed( "" );
// then
try
{
populator.close( true );
fail( "Expected successful close to fail after markedAsFailed" );
}
catch ( IllegalStateException e )
{
// good
}
populator.close( false );
}
@Test
public void shouldApplyLargeAmountOfInterleavedRandomUpdates() throws Exception
{
// given
populator.create();
random.reset();
Random updaterRandom = new Random( random.seed() );
Iterator<IndexEntryUpdate<IndexDescriptor>> updates = layoutUtil.randomUpdateGenerator( random );
// when
int count = interleaveLargeAmountOfUpdates( updaterRandom, updates );
// then
populator.close( true );
random.reset();
verifyUpdates( layoutUtil.randomUpdateGenerator( random ), count );
}
@Test
public void dropMustSucceedAfterSuccessfulClose() throws Exception
{
// given
populator.create();
populator.close( true );
// when
populator.drop();
// then
assertFileNotPresent();
}
@Test
public void dropMustSucceedAfterUnsuccessfulClose() throws Exception
{
// given
populator.create();
populator.close( false );
// when
populator.drop();
// then
assertFileNotPresent();
}
@Test
public void successfulCloseMustThrowWithoutPriorSuccessfulCreate() throws Exception
{
// given
assertFileNotPresent();
// when
try
{
populator.close( true );
fail( "Should have failed" );
}
catch ( IllegalStateException e )
{
// then good
}
}
@Test
public void unsuccessfulCloseMustSucceedWithoutSuccessfulPriorCreate() throws Exception
{
// given
assertFileNotPresent();
String failureMessage = "There is no spoon";
populator.markAsFailed( failureMessage );
// when
populator.close( false );
// then
assertHeader( false, failureMessage, false );
}
@Test
public void successfulCloseMustThrowAfterDrop() throws Exception
{
// given
populator.create();
// when
populator.drop();
// then
try
{
populator.close( true );
fail( "Should have failed" );
}
catch ( IllegalStateException e )
{
// then good
}
}
@Test
public void unsuccessfulCloseMustThrowAfterDrop() throws Exception
{
// given
populator.create();
// when
populator.drop();
// then
try
{
populator.close( false );
fail( "Should have failed" );
}
catch ( IllegalStateException e )
{
// then good
}
}
private int interleaveLargeAmountOfUpdates( Random updaterRandom,
Iterator<IndexEntryUpdate<IndexDescriptor>> updates ) throws IOException, IndexEntryConflictException
{
int count = 0;
for ( int i = 0; i < LARGE_AMOUNT_OF_UPDATES; i++ )
{
if ( updaterRandom.nextFloat() < 0.1 )
{
try ( IndexUpdater indexUpdater = populator.newPopulatingUpdater( null_property_accessor ) )
{
int numberOfUpdaterUpdates = updaterRandom.nextInt( 100 );
for ( int j = 0; j < numberOfUpdaterUpdates; j++ )
{
indexUpdater.process( updates.next() );
count++;
}
}
}
populator.add( Collections.singletonList( updates.next() ) );
count++;
}
return count;
}
private void assertHeader( boolean online, String failureMessage, boolean messageTruncated ) throws IOException
{
NativeSchemaIndexHeaderReader headerReader = new NativeSchemaIndexHeaderReader();
try ( GBPTree<KEY,VALUE> ignored = new GBPTree<>( pageCache, indexFile, layout, 0, GBPTree.NO_MONITOR,
headerReader, NO_HEADER_WRITER, RecoveryCleanupWorkCollector.IMMEDIATE ) )
{
if ( online )
{
assertEquals( "Index was not marked as online when expected not to be.", BYTE_ONLINE, headerReader.state );
assertNull( "Expected failure message to be null when marked as online.", headerReader.failureMessage );
}
else
{
assertEquals( "Index was marked as online when expected not to be.", BYTE_FAILED, headerReader.state );
if ( messageTruncated )
{
assertTrue( headerReader.failureMessage.length() < failureMessage.length() );
assertTrue( failureMessage.startsWith( headerReader.failureMessage ) );
}
else
{
assertEquals( failureMessage, headerReader.failureMessage );
}
}
}
}
private String longString( int length )
{
return RandomStringUtils.random( length, true, true );
}
private void applyInterleaved( IndexEntryUpdate<IndexDescriptor>[] updates, NativeSchemaNumberIndexPopulator<KEY,VALUE> populator )
throws IOException, IndexEntryConflictException
{
boolean useUpdater = true;
Collection<IndexEntryUpdate<IndexDescriptor>> populatorBatch = new ArrayList<>();
IndexUpdater updater = populator.newPopulatingUpdater( null_property_accessor );
for ( IndexEntryUpdate<IndexDescriptor> update : updates )
{
if ( random.nextInt( 100 ) < 20 )
{
if ( useUpdater )
{
updater.close();
populatorBatch = new ArrayList<>();
}
else
{
populator.add( populatorBatch );
updater = populator.newPopulatingUpdater( null_property_accessor );
}
useUpdater = !useUpdater;
}
if ( useUpdater )
{
updater.process( update );
}
else
{
populatorBatch.add( update );
}
}
if ( useUpdater )
{
updater.close();
}
else
{
populator.add( populatorBatch );
}
}
private void verifyUpdates( Iterator<IndexEntryUpdate<IndexDescriptor>> indexEntryUpdateIterator, int count )
throws IOException
{
@SuppressWarnings( "unchecked" )
IndexEntryUpdate<IndexDescriptor>[] updates = new IndexEntryUpdate[count];
for ( int i = 0; i < count; i++ )
{
updates[i] = indexEntryUpdateIterator.next();
}
verifyUpdates( updates );
}
private byte[] fileWithContent() throws IOException
{
int size = 1000;
try ( StoreChannel storeChannel = fs.create( indexFile ) )
{
byte[] someBytes = new byte[size];
random.nextBytes( someBytes );
storeChannel.writeAll( ByteBuffer.wrap( someBytes ) );
return someBytes;
}
}
}