-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
GraphStoreFixture.java
592 lines (520 loc) · 20.8 KB
/
GraphStoreFixture.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
/*
* Copyright (c) 2002-2018 "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.consistency.checking;
import org.apache.commons.lang3.StringUtils;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collection;
import org.neo4j.consistency.statistics.AccessStatistics;
import org.neo4j.consistency.statistics.AccessStatsKeepingStoreAccess;
import org.neo4j.consistency.statistics.DefaultCounts;
import org.neo4j.consistency.statistics.Statistics;
import org.neo4j.consistency.statistics.VerboseStatistics;
import org.neo4j.graphdb.DependencyResolver;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.index.internal.gbptree.RecoveryCleanupWorkCollector;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.io.pagecache.tracing.cursor.context.EmptyVersionContextSupplier;
import org.neo4j.kernel.api.StatementConstants;
import org.neo4j.kernel.api.direct.DirectStoreAccess;
import org.neo4j.internal.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.kernel.api.labelscan.LabelScanStore;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.extension.KernelExtensions;
import org.neo4j.kernel.impl.api.TransactionRepresentationCommitProcess;
import org.neo4j.kernel.impl.api.TransactionToApply;
import org.neo4j.kernel.impl.api.index.IndexProviderMap;
import org.neo4j.kernel.impl.api.index.IndexStoreView;
import org.neo4j.kernel.impl.api.scan.FullLabelStream;
import org.neo4j.kernel.impl.factory.DatabaseInfo;
import org.neo4j.kernel.impl.index.labelscan.NativeLabelScanStore;
import org.neo4j.kernel.impl.locking.LockService;
import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.kernel.impl.logging.SimpleLogService;
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.NodeLabelsField;
import org.neo4j.kernel.impl.store.NodeStore;
import org.neo4j.kernel.impl.store.StoreAccess;
import org.neo4j.kernel.impl.store.StoreFactory;
import org.neo4j.kernel.impl.store.id.DefaultIdGeneratorFactory;
import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.NeoStoreRecord;
import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.kernel.impl.store.record.PropertyRecord;
import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.kernel.impl.transaction.TransactionRepresentation;
import org.neo4j.kernel.impl.transaction.log.TransactionAppender;
import org.neo4j.kernel.impl.transaction.log.TransactionIdStore;
import org.neo4j.kernel.impl.transaction.state.storeview.NeoStoreIndexStoreView;
import org.neo4j.kernel.impl.transaction.tracing.CommitEvent;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.kernel.lifecycle.LifeSupport;
import org.neo4j.kernel.monitoring.Monitors;
import org.neo4j.logging.LogProvider;
import org.neo4j.logging.NullLog;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.storageengine.api.StorageEngine;
import org.neo4j.storageengine.api.TransactionApplicationMode;
import org.neo4j.storageengine.api.schema.SchemaRule;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.ConfigurablePageCacheRule;
import org.neo4j.test.rule.TestDirectory;
import static java.lang.System.currentTimeMillis;
import static org.neo4j.consistency.ConsistencyCheckService.defaultConsistencyCheckThreadsNumber;
import static org.neo4j.consistency.internal.SchemaIndexExtensionLoader.instantiateKernelExtensions;
import static org.neo4j.consistency.internal.SchemaIndexExtensionLoader.loadIndexProviders;
public abstract class GraphStoreFixture extends ConfigurablePageCacheRule implements TestRule
{
private DirectStoreAccess directStoreAccess;
private Statistics statistics;
private final boolean keepStatistics;
private NeoStores neoStore;
private File directory;
private long schemaId;
private long nodeId;
private int labelId;
private long nodeLabelsId;
private long relId;
private long relGroupId;
private int propId;
private long stringPropId;
private long arrayPropId;
private int relTypeId;
private int propKeyId;
private DefaultFileSystemAbstraction fileSystem;
private final LifeSupport life = new LifeSupport();
/**
* Record format used to generate initial database.
*/
private String formatName = StringUtils.EMPTY;
private GraphStoreFixture( boolean keepStatistics, String formatName )
{
this.keepStatistics = keepStatistics;
this.formatName = formatName;
}
protected GraphStoreFixture( String formatName )
{
this( false, formatName );
}
@Override
protected void after( boolean success )
{
super.after( success );
life.shutdown();
if ( fileSystem != null )
{
try
{
fileSystem.close();
}
catch ( IOException e )
{
throw new AssertionError( "Failed to stop file system after test", e );
}
}
}
public void apply( Transaction transaction ) throws TransactionFailureException
{
applyTransaction( transaction );
}
public DirectStoreAccess directStoreAccess()
{
if ( directStoreAccess == null )
{
life.start();
fileSystem = new DefaultFileSystemAbstraction();
PageCache pageCache = getPageCache( fileSystem );
LogProvider logProvider = NullLogProvider.getInstance();
Config config = Config.defaults();
DefaultIdGeneratorFactory idGeneratorFactory = new DefaultIdGeneratorFactory( fileSystem );
StoreFactory storeFactory = new StoreFactory(
directory, config, idGeneratorFactory, pageCache, fileSystem, logProvider, EmptyVersionContextSupplier.EMPTY );
neoStore = storeFactory.openAllNeoStores();
StoreAccess nativeStores;
if ( keepStatistics )
{
AccessStatistics accessStatistics = new AccessStatistics();
statistics = new VerboseStatistics( accessStatistics,
new DefaultCounts( defaultConsistencyCheckThreadsNumber() ), NullLog.getInstance() );
nativeStores = new AccessStatsKeepingStoreAccess( neoStore, accessStatistics );
}
else
{
statistics = Statistics.NONE;
nativeStores = new StoreAccess( neoStore );
}
nativeStores.initialize();
IndexStoreView indexStoreView =
new NeoStoreIndexStoreView( LockService.NO_LOCK_SERVICE, nativeStores.getRawNeoStores() );
Monitors monitors = new Monitors();
LabelScanStore labelScanStore = startLabelScanStore( pageCache, indexStoreView, monitors );
IndexProviderMap indexes = createIndexes( pageCache, fileSystem, directory, config, logProvider, monitors);
directStoreAccess = new DirectStoreAccess( nativeStores, labelScanStore, indexes );
}
return directStoreAccess;
}
private LabelScanStore startLabelScanStore( PageCache pageCache, IndexStoreView indexStoreView, Monitors monitors )
{
NativeLabelScanStore labelScanStore =
new NativeLabelScanStore( pageCache, directory, new FullLabelStream( indexStoreView ), false, monitors,
RecoveryCleanupWorkCollector.IMMEDIATE );
try
{
labelScanStore.init();
labelScanStore.start();
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
return labelScanStore;
}
private IndexProviderMap createIndexes( PageCache pageCache, FileSystemAbstraction fileSystem, File storeDir,
Config config, LogProvider logProvider, Monitors monitors )
{
LogService logService = new SimpleLogService( logProvider, logProvider );
KernelExtensions extensions = life.add( instantiateKernelExtensions( storeDir, fileSystem, config, logService,
pageCache, RecoveryCleanupWorkCollector.IGNORE, DatabaseInfo.COMMUNITY, monitors ) );
return loadIndexProviders( extensions );
}
public File directory()
{
return directory;
}
public Statistics getAccessStatistics()
{
return statistics;
}
public abstract static class Transaction
{
final long startTimestamp = currentTimeMillis();
protected abstract void transactionData( TransactionDataBuilder tx, IdGenerator next );
public TransactionRepresentation representation( IdGenerator idGenerator, int masterId, int authorId,
long lastCommittedTx, NeoStores neoStores )
{
TransactionWriter writer = new TransactionWriter( neoStores );
transactionData( new TransactionDataBuilder( writer, neoStores.getNodeStore() ), idGenerator );
idGenerator.updateCorrespondingIdGenerators( neoStores );
return writer.representation( new byte[0], masterId, authorId, startTimestamp, lastCommittedTx,
currentTimeMillis() );
}
}
public IdGenerator idGenerator()
{
return new IdGenerator();
}
public class IdGenerator
{
public long schema()
{
return schemaId++;
}
public long node()
{
return nodeId++;
}
public int label()
{
return labelId++;
}
public long nodeLabel()
{
return nodeLabelsId++;
}
public long relationship()
{
return relId++;
}
public long relationshipGroup()
{
return relGroupId++;
}
public long property()
{
return propId++;
}
public long stringProperty()
{
return stringPropId++;
}
public long arrayProperty()
{
return arrayPropId++;
}
public int relationshipType()
{
return relTypeId++;
}
public int propertyKey()
{
return propKeyId++;
}
void updateCorrespondingIdGenerators( NeoStores neoStores )
{
neoStores.getNodeStore().setHighestPossibleIdInUse( nodeId );
neoStores.getRelationshipStore().setHighestPossibleIdInUse( relId );
neoStores.getRelationshipGroupStore().setHighestPossibleIdInUse( relGroupId );
}
}
public static final class TransactionDataBuilder
{
private final TransactionWriter writer;
private final NodeStore nodes;
TransactionDataBuilder( TransactionWriter writer, NodeStore nodes )
{
this.writer = writer;
this.nodes = nodes;
}
public void createSchema( Collection<DynamicRecord> beforeRecords, Collection<DynamicRecord> afterRecords,
SchemaRule rule )
{
writer.createSchema( beforeRecords, afterRecords, rule );
}
// In the following three methods there's an assumption that all tokens use one dynamic record
// and since the first record in a dynamic store the id starts at 1 instead of 0... hence the +1
public void propertyKey( int id, String key )
{
writer.propertyKey( id, key, id + 1 );
}
public void nodeLabel( int id, String name )
{
writer.label( id, name, id + 1 );
}
public void relationshipType( int id, String relationshipType )
{
writer.relationshipType( id, relationshipType, id + 1 );
}
public void update( NeoStoreRecord before, NeoStoreRecord after )
{
writer.update( before, after );
}
public void create( NodeRecord node )
{
updateCounts( node, 1 );
writer.create( node );
}
public void update( NodeRecord before, NodeRecord after )
{
updateCounts( before, -1 );
updateCounts( after, 1 );
writer.update( before, after );
}
public void delete( NodeRecord node )
{
updateCounts( node, -1 );
writer.delete( node );
}
public void create( RelationshipRecord relationship )
{
writer.create( relationship );
}
public void update( RelationshipRecord before, RelationshipRecord after )
{
writer.update( before, after );
}
public void delete( RelationshipRecord relationship )
{
writer.delete( relationship );
}
public void create( RelationshipGroupRecord group )
{
writer.create( group );
}
public void update( RelationshipGroupRecord before, RelationshipGroupRecord after )
{
writer.update( before, after );
}
public void delete( RelationshipGroupRecord group )
{
writer.delete( group );
}
public void create( PropertyRecord property )
{
writer.create( property );
}
public void update( PropertyRecord before, PropertyRecord property )
{
writer.update( before, property );
}
public void delete( PropertyRecord before, PropertyRecord property )
{
writer.delete( before, property );
}
private void updateCounts( NodeRecord node, int delta )
{
writer.incrementNodeCount( StatementConstants.ANY_LABEL, delta );
for ( long label : NodeLabelsField.parseLabelsField( node ).get( nodes ) )
{
writer.incrementNodeCount( (int)label, delta );
}
}
public void incrementNodeCount( int labelId, long delta )
{
writer.incrementNodeCount( labelId, delta );
}
public void incrementRelationshipCount( int startLabelId, int typeId, int endLabelId, long delta )
{
writer.incrementRelationshipCount( startLabelId, typeId, endLabelId, delta );
}
}
protected abstract void generateInitialData( GraphDatabaseService graphDb );
protected void start( @SuppressWarnings( "UnusedParameters" ) File storeDir )
{
// allow for override
}
protected void stop() throws Throwable
{
if ( directStoreAccess != null )
{
neoStore.close();
directStoreAccess.close();
directStoreAccess = null;
}
}
private int myId()
{
return 1;
}
private int masterId()
{
return -1;
}
public class Applier implements AutoCloseable
{
private final GraphDatabaseAPI database;
private final TransactionRepresentationCommitProcess commitProcess;
private final TransactionIdStore transactionIdStore;
private final NeoStores neoStores;
Applier()
{
database = (GraphDatabaseAPI) new TestGraphDatabaseFactory()
.newEmbeddedDatabaseBuilder( directory )
.setConfig( "dbms.backup.enabled", "false" )
.newGraphDatabase();
DependencyResolver dependencyResolver = database.getDependencyResolver();
commitProcess = new TransactionRepresentationCommitProcess(
dependencyResolver.resolveDependency( TransactionAppender.class ),
dependencyResolver.resolveDependency( StorageEngine.class ) );
transactionIdStore = database.getDependencyResolver().resolveDependency(
TransactionIdStore.class );
neoStores = database.getDependencyResolver().resolveDependency( RecordStorageEngine.class )
.testAccessNeoStores();
}
public void apply( Transaction transaction ) throws TransactionFailureException
{
TransactionRepresentation representation = transaction.representation( idGenerator(), masterId(), myId(),
transactionIdStore.getLastCommittedTransactionId(), neoStores );
commitProcess.commit( new TransactionToApply( representation ), CommitEvent.NULL,
TransactionApplicationMode.EXTERNAL );
}
@Override
public void close()
{
database.shutdown();
}
}
public Applier createApplier()
{
return new Applier();
}
private void applyTransaction( Transaction transaction ) throws TransactionFailureException
{
// TODO you know... we could have just appended the transaction representation to the log
// and the next startup of the store would do recovery where the transaction would have been
// applied and all would have been well.
try ( Applier applier = createApplier() )
{
applier.apply( transaction );
}
}
private void generateInitialData()
{
GraphDatabaseBuilder builder = new TestGraphDatabaseFactory().newEmbeddedDatabaseBuilder( directory );
GraphDatabaseAPI graphDb = (GraphDatabaseAPI) builder
.setConfig( GraphDatabaseSettings.record_format, formatName )
// Some tests using this fixture were written when the label_block_size was 60 and so hardcoded
// tests and records around that. Those tests could change, but the simpler option is to just
// keep the block size to 60 and let them be.
.setConfig( GraphDatabaseSettings.label_block_size, "60" )
.setConfig( "dbms.backup.enabled", "false" )
.newGraphDatabase();
try
{
generateInitialData( graphDb );
StoreAccess stores = new StoreAccess( graphDb.getDependencyResolver()
.resolveDependency( RecordStorageEngine.class ).testAccessNeoStores() ).initialize();
schemaId = stores.getSchemaStore().getHighId();
nodeId = stores.getNodeStore().getHighId();
labelId = (int) stores.getLabelTokenStore().getHighId();
nodeLabelsId = stores.getNodeDynamicLabelStore().getHighId();
relId = stores.getRelationshipStore().getHighId();
relGroupId = stores.getRelationshipGroupStore().getHighId();
propId = (int) stores.getPropertyStore().getHighId();
stringPropId = stores.getStringStore().getHighId();
arrayPropId = stores.getArrayStore().getHighId();
relTypeId = (int) stores.getRelationshipTypeTokenStore().getHighId();
propKeyId = (int) stores.getPropertyKeyNameStore().getHighId();
}
finally
{
graphDb.shutdown();
}
}
@Override
public Statement apply( final Statement base, Description description )
{
final TestDirectory directory = TestDirectory.testDirectory( description.getTestClass() );
return super.apply( directory.apply( new Statement()
{
@Override
public void evaluate() throws Throwable
{
GraphStoreFixture.this.directory = directory.graphDbDir();
try
{
generateInitialData();
start( GraphStoreFixture.this.directory );
try
{
base.evaluate();
}
finally
{
stop();
}
}
finally
{
GraphStoreFixture.this.directory = null;
}
}
}, description ), description );
}
}