-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
RecordStorageEngine.java
524 lines (478 loc) · 22.6 KB
/
RecordStorageEngine.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
/*
* Copyright (c) 2002-2016 "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.storageengine.impl.recordstorage;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.neo4j.concurrent.WorkSync;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.IOLimiter;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.IdReuseEligibility;
import org.neo4j.kernel.api.TokenNameLookup;
import org.neo4j.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.kernel.api.exceptions.schema.ConstraintValidationKernelException;
import org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException;
import org.neo4j.kernel.api.index.SchemaIndexProvider;
import org.neo4j.kernel.api.labelscan.LabelScanStore;
import org.neo4j.kernel.api.labelscan.LabelScanWriter;
import org.neo4j.kernel.api.txstate.TransactionCountingStateVisitor;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.api.BatchTransactionApplier;
import org.neo4j.kernel.impl.api.BatchTransactionApplierFacade;
import org.neo4j.kernel.impl.api.CountsRecordState;
import org.neo4j.kernel.impl.api.CountsStoreBatchTransactionApplier;
import org.neo4j.kernel.impl.api.IndexReaderFactory;
import org.neo4j.kernel.impl.api.KernelTransactionsSnapshot;
import org.neo4j.kernel.impl.api.LegacyBatchIndexApplier;
import org.neo4j.kernel.impl.api.LegacyIndexApplierLookup;
import org.neo4j.kernel.impl.api.LegacyIndexProviderLookup;
import org.neo4j.kernel.impl.api.TransactionApplier;
import org.neo4j.kernel.impl.api.TransactionApplierFacade;
import org.neo4j.kernel.impl.api.index.IndexingService;
import org.neo4j.kernel.impl.api.index.IndexingServiceFactory;
import org.neo4j.kernel.impl.api.index.PropertyPhysicalToLogicalConverter;
import org.neo4j.kernel.impl.api.scan.LabelScanStoreProvider;
import org.neo4j.kernel.impl.api.store.CacheLayer;
import org.neo4j.kernel.impl.api.store.DiskLayer;
import org.neo4j.kernel.impl.api.store.SchemaCache;
import org.neo4j.kernel.impl.api.store.StoreStatement;
import org.neo4j.kernel.impl.cache.BridgingCacheAccess;
import org.neo4j.kernel.impl.constraints.ConstraintSemantics;
import org.neo4j.kernel.impl.core.CacheAccessBackDoor;
import org.neo4j.kernel.impl.core.LabelTokenHolder;
import org.neo4j.kernel.impl.core.PropertyKeyTokenHolder;
import org.neo4j.kernel.impl.core.RelationshipTypeTokenHolder;
import org.neo4j.kernel.impl.index.IndexConfigStore;
import org.neo4j.kernel.impl.locking.LockGroup;
import org.neo4j.kernel.impl.locking.LockService;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.SchemaStorage;
import org.neo4j.kernel.impl.store.StoreFactory;
import org.neo4j.kernel.impl.store.id.BufferingIdGeneratorFactory;
import org.neo4j.kernel.impl.store.id.IdGeneratorFactory;
import org.neo4j.kernel.impl.transaction.command.CacheInvalidationBatchTransactionApplier;
import org.neo4j.kernel.impl.transaction.command.HighIdBatchTransactionApplier;
import org.neo4j.kernel.impl.transaction.command.IndexBatchTransactionApplier;
import org.neo4j.kernel.impl.transaction.command.IndexUpdatesWork;
import org.neo4j.kernel.impl.transaction.command.LabelUpdateWork;
import org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier;
import org.neo4j.kernel.impl.transaction.state.DefaultSchemaIndexProviderMap;
import org.neo4j.kernel.impl.transaction.state.IntegrityValidator;
import org.neo4j.kernel.impl.transaction.state.Loaders;
import org.neo4j.kernel.impl.transaction.state.NeoStoreIndexStoreView;
import org.neo4j.kernel.impl.transaction.state.PropertyCreator;
import org.neo4j.kernel.impl.transaction.state.PropertyDeleter;
import org.neo4j.kernel.impl.transaction.state.PropertyLoader;
import org.neo4j.kernel.impl.transaction.state.PropertyTraverser;
import org.neo4j.kernel.impl.transaction.state.RecordChangeSet;
import org.neo4j.kernel.impl.transaction.state.RelationshipCreator;
import org.neo4j.kernel.impl.transaction.state.RelationshipDeleter;
import org.neo4j.kernel.impl.transaction.state.RelationshipGroupGetter;
import org.neo4j.kernel.impl.transaction.state.TransactionRecordState;
import org.neo4j.kernel.impl.util.DependencySatisfier;
import org.neo4j.kernel.impl.util.IdOrderingQueue;
import org.neo4j.kernel.impl.util.JobScheduler;
import org.neo4j.kernel.info.DiagnosticsManager;
import org.neo4j.kernel.internal.DatabaseHealth;
import org.neo4j.kernel.lifecycle.Lifecycle;
import org.neo4j.kernel.spi.legacyindex.IndexImplementation;
import org.neo4j.logging.LogProvider;
import org.neo4j.storageengine.api.CommandReaderFactory;
import org.neo4j.storageengine.api.CommandsToApply;
import org.neo4j.storageengine.api.StorageCommand;
import org.neo4j.storageengine.api.StorageEngine;
import org.neo4j.storageengine.api.StorageStatement;
import org.neo4j.storageengine.api.StoreReadLayer;
import org.neo4j.storageengine.api.TransactionApplicationMode;
import org.neo4j.storageengine.api.lock.ResourceLocker;
import org.neo4j.storageengine.api.schema.SchemaRule;
import org.neo4j.storageengine.api.txstate.ReadableTransactionState;
import org.neo4j.storageengine.api.txstate.TxStateVisitor;
import org.neo4j.unsafe.impl.internal.dragons.FeatureToggles;
import static org.neo4j.kernel.impl.locking.LockService.NO_LOCK_SERVICE;
import static org.neo4j.unsafe.impl.internal.dragons.FeatureToggles.flag;
public class RecordStorageEngine implements StorageEngine, Lifecycle
{
private static final boolean safeIdBuffering = flag( RecordStorageEngine.class, "safeIdBuffering", false );
private final StoreReadLayer storeLayer;
private final IndexingService indexingService;
private final NeoStores neoStores;
private final PropertyKeyTokenHolder propertyKeyTokenHolder;
private final RelationshipTypeTokenHolder relationshipTypeTokenHolder;
private final LabelTokenHolder labelTokenHolder;
private final DatabaseHealth databaseHealth;
private final IndexConfigStore indexConfigStore;
private final SchemaCache schemaCache;
private final IntegrityValidator integrityValidator;
private final CacheAccessBackDoor cacheAccess;
private final LabelScanStore labelScanStore;
private final DefaultSchemaIndexProviderMap schemaIndexProviderMap;
private final LegacyIndexApplierLookup legacyIndexApplierLookup;
private final Runnable schemaStateChangeCallback;
private final SchemaStorage schemaStorage;
private final ConstraintSemantics constraintSemantics;
private final IdOrderingQueue legacyIndexTransactionOrdering;
private final JobScheduler scheduler;
private final LockService lockService;
private final WorkSync<Supplier<LabelScanWriter>,LabelUpdateWork> labelScanStoreSync;
private final CommandReaderFactory commandReaderFactory;
private final WorkSync<IndexingService,IndexUpdatesWork> indexUpdatesSync;
private final NeoStoreIndexStoreView indexStoreView;
private final LegacyIndexProviderLookup legacyIndexProviderLookup;
private final PropertyPhysicalToLogicalConverter indexUpdatesConverter;
private final Supplier<StorageStatement> storeStatementSupplier;
private BufferingIdGeneratorFactory bufferingIdGeneratorFactory;
private JobScheduler.JobHandle idBufferMaintenance;
// Immutable state for creating/applying commands
private final Loaders loaders;
private final RelationshipCreator relationshipCreator;
private final RelationshipDeleter relationshipDeleter;
private final PropertyCreator propertyCreator;
private final PropertyDeleter propertyDeleter;
public RecordStorageEngine(
File storeDir,
Config config,
IdGeneratorFactory idGeneratorFactory,
IdReuseEligibility eligibleForReuse,
PageCache pageCache,
FileSystemAbstraction fs,
LogProvider logProvider,
PropertyKeyTokenHolder propertyKeyTokenHolder,
LabelTokenHolder labelTokens,
RelationshipTypeTokenHolder relationshipTypeTokens,
Runnable schemaStateChangeCallback,
ConstraintSemantics constraintSemantics,
JobScheduler scheduler,
TokenNameLookup tokenNameLookup,
LockService lockService,
SchemaIndexProvider indexProvider,
IndexingService.Monitor indexingServiceMonitor,
DatabaseHealth databaseHealth,
LabelScanStoreProvider labelScanStoreProvider,
LegacyIndexProviderLookup legacyIndexProviderLookup,
IndexConfigStore indexConfigStore,
IdOrderingQueue legacyIndexTransactionOrdering,
Supplier<KernelTransactionsSnapshot> transactionsSnapshotSupplier )
{
this.propertyKeyTokenHolder = propertyKeyTokenHolder;
this.relationshipTypeTokenHolder = relationshipTypeTokens;
this.labelTokenHolder = labelTokens;
this.schemaStateChangeCallback = schemaStateChangeCallback;
this.scheduler = scheduler;
this.lockService = lockService;
this.databaseHealth = databaseHealth;
this.legacyIndexProviderLookup = legacyIndexProviderLookup;
this.indexConfigStore = indexConfigStore;
this.constraintSemantics = constraintSemantics;
this.legacyIndexTransactionOrdering = legacyIndexTransactionOrdering;
if ( safeIdBuffering )
{
// This buffering id generator factory will have properly buffering id generators injected into
// the stores. The buffering depends on knowledge about active transactions,
// so we'll initialize it below when all those components have been instantiated.
bufferingIdGeneratorFactory = new BufferingIdGeneratorFactory(
idGeneratorFactory, transactionsSnapshotSupplier, eligibleForReuse );
idGeneratorFactory = bufferingIdGeneratorFactory;
}
StoreFactory factory = new StoreFactory( storeDir, config, idGeneratorFactory, pageCache, fs, logProvider );
neoStores = factory.openAllNeoStores( true );
try
{
indexUpdatesConverter = new PropertyPhysicalToLogicalConverter( neoStores.getPropertyStore() );
schemaCache = new SchemaCache( constraintSemantics, Collections.emptyList() );
schemaStorage = new SchemaStorage( neoStores.getSchemaStore() );
schemaIndexProviderMap = new DefaultSchemaIndexProviderMap( indexProvider );
indexStoreView = new NeoStoreIndexStoreView( lockService, neoStores );
indexingService = IndexingServiceFactory.createIndexingService( config, scheduler, schemaIndexProviderMap,
indexStoreView, tokenNameLookup,
Iterators.asList( new SchemaStorage( neoStores.getSchemaStore() ).allIndexRules() ), logProvider,
indexingServiceMonitor, schemaStateChangeCallback );
integrityValidator = new IntegrityValidator( neoStores, indexingService );
cacheAccess = new BridgingCacheAccess( schemaCache, schemaStateChangeCallback,
propertyKeyTokenHolder, relationshipTypeTokens, labelTokens );
labelScanStore = labelScanStoreProvider.getLabelScanStore();
storeStatementSupplier = storeStatementSupplier( neoStores );
DiskLayer diskLayer = new DiskLayer(
propertyKeyTokenHolder, labelTokens, relationshipTypeTokens,
schemaStorage, neoStores, indexingService,
storeStatementSupplier );
storeLayer = new CacheLayer( diskLayer, schemaCache );
legacyIndexApplierLookup = new LegacyIndexApplierLookup.Direct( legacyIndexProviderLookup );
labelScanStoreSync = new WorkSync<>( labelScanStore::newWriter );
commandReaderFactory = new RecordStorageCommandReaderFactory();
indexUpdatesSync = new WorkSync<>( indexingService );
// Immutable state for creating/applying commands
loaders = new Loaders( neoStores );
RelationshipGroupGetter relationshipGroupGetter =
new RelationshipGroupGetter( neoStores.getRelationshipGroupStore() );
relationshipCreator = new RelationshipCreator( relationshipGroupGetter,
config.get( GraphDatabaseSettings.dense_node_threshold ) );
PropertyTraverser propertyTraverser = new PropertyTraverser();
propertyDeleter = new PropertyDeleter( propertyTraverser );
relationshipDeleter = new RelationshipDeleter( relationshipGroupGetter, propertyDeleter );
propertyCreator = new PropertyCreator( neoStores.getPropertyStore(), propertyTraverser );
}
catch ( Throwable failure )
{
neoStores.close();
throw failure;
}
}
private Supplier<StorageStatement> storeStatementSupplier( NeoStores neoStores )
{
Supplier<IndexReaderFactory> indexReaderFactory = () -> new IndexReaderFactory.Caching( indexingService );
LockService lockService =
FeatureToggles.flag( getClass(), "propertyReadLocks", true ) ? this.lockService : NO_LOCK_SERVICE;
return () -> new StoreStatement( neoStores, indexReaderFactory, labelScanStore::newReader, lockService );
}
@Override
public StoreReadLayer storeReadLayer()
{
return storeLayer;
}
@Override
public CommandReaderFactory commandReaderFactory()
{
return commandReaderFactory;
}
@SuppressWarnings( "resource" )
@Override
public void createCommands(
Collection<StorageCommand> commands,
ReadableTransactionState txState,
StorageStatement storageStatement,
ResourceLocker locks,
long lastTransactionIdWhenStarted )
throws TransactionFailureException, CreateConstraintFailureException, ConstraintValidationKernelException
{
if ( txState != null )
{
RecordChangeSet recordChangeSet = new RecordChangeSet( loaders );
TransactionRecordState recordState = new TransactionRecordState( neoStores, integrityValidator,
recordChangeSet, lastTransactionIdWhenStarted, locks,
relationshipCreator, relationshipDeleter, propertyCreator, propertyDeleter );
// Visit transaction state and populate these record state objects
TxStateVisitor txStateVisitor = new TransactionToRecordStateVisitor( recordState,
schemaStateChangeCallback, schemaStorage, constraintSemantics, schemaIndexProviderMap );
CountsRecordState countsRecordState = new CountsRecordState();
txStateVisitor = constraintSemantics.decorateTxStateVisitor(
storeLayer,
txState,
txStateVisitor );
txStateVisitor = new TransactionCountingStateVisitor(
txStateVisitor, storeLayer, storageStatement, txState, countsRecordState );
try ( TxStateVisitor visitor = txStateVisitor )
{
txState.accept( txStateVisitor );
}
// Convert record state into commands
recordState.extractCommands( commands );
countsRecordState.extractCommands( commands );
}
}
@Override
public void apply( CommandsToApply batch, TransactionApplicationMode mode ) throws Exception
{
// Have these command appliers as separate try-with-resource to have better control over
// point between closing this and the locks above
try ( BatchTransactionApplier batchApplier = applier( mode ) )
{
while ( batch != null )
{
try ( LockGroup locks = new LockGroup() )
{
try ( TransactionApplier txApplier = batchApplier.startTx( batch, locks ) )
{
batch.accept( txApplier );
}
batch = batch.next();
}
}
}
catch ( Throwable cause )
{
databaseHealth.panic( cause );
throw cause;
}
}
/**
* Creates a {@link BatchTransactionApplierFacade} that is to be used for all transactions
* in a batch. Each transaction is handled by a {@link TransactionApplierFacade} which wraps the
* individual {@link TransactionApplier}s returned by the wrapped {@link BatchTransactionApplier}s.
*
* After all transactions have been applied the appliers are closed.
*/
protected BatchTransactionApplierFacade applier( TransactionApplicationMode mode )
{
ArrayList<BatchTransactionApplier> appliers = new ArrayList<>();
// Graph store application. The order of the decorated store appliers is irrelevant
appliers.add( new NeoStoreBatchTransactionApplier( neoStores, cacheAccess, lockService ) );
if ( mode.needsHighIdTracking() )
{
appliers.add( new HighIdBatchTransactionApplier( neoStores ) );
}
if ( mode.needsCacheInvalidationOnUpdates() )
{
appliers.add( new CacheInvalidationBatchTransactionApplier( neoStores, cacheAccess ) );
}
// Schema index application
appliers.add( new IndexBatchTransactionApplier( indexingService, labelScanStoreSync, indexUpdatesSync,
neoStores.getNodeStore(), new PropertyLoader( neoStores ),
indexUpdatesConverter, mode ) );
// Legacy index application
appliers.add(
new LegacyBatchIndexApplier( indexConfigStore, legacyIndexApplierLookup, legacyIndexTransactionOrdering,
mode ) );
// Counts store application
appliers.add( new CountsStoreBatchTransactionApplier( neoStores.getCounts(), mode ) );
// Perform the application
return new BatchTransactionApplierFacade(
appliers.toArray( new BatchTransactionApplier[appliers.size()] ) );
}
public void satisfyDependencies( DependencySatisfier satisfier )
{
satisfier.satisfyDependency( legacyIndexApplierLookup );
satisfier.satisfyDependency( cacheAccess );
satisfier.satisfyDependency( indexStoreView );
satisfier.satisfyDependency( schemaIndexProviderMap );
satisfier.satisfyDependency( integrityValidator );
satisfier.satisfyDependency( labelScanStore );
satisfier.satisfyDependency( indexingService );
// providing TransactionIdStore, LogVersionRepository
satisfier.satisfyDependency( neoStores.getMetaDataStore() );
satisfier.satisfyDependency( indexStoreView );
}
@Override
public void init() throws Throwable
{
indexingService.init();
labelScanStore.init();
}
@Override
public void start() throws Throwable
{
neoStores.makeStoreOk();
propertyKeyTokenHolder.setInitialTokens(
neoStores.getPropertyKeyTokenStore().getTokens( Integer.MAX_VALUE ) );
relationshipTypeTokenHolder.setInitialTokens(
neoStores.getRelationshipTypeTokenStore().getTokens( Integer.MAX_VALUE ) );
labelTokenHolder.setInitialTokens(
neoStores.getLabelTokenStore().getTokens( Integer.MAX_VALUE ) );
neoStores.rebuildCountStoreIfNeeded(); // TODO: move this to counts store lifecycle
loadSchemaCache();
indexingService.start();
labelScanStore.start();
if ( safeIdBuffering )
{
Runnable maintenance = bufferingIdGeneratorFactory::maintenance;
idBufferMaintenance = scheduler.scheduleRecurring(
JobScheduler.Groups.storageMaintenance, maintenance, 1, TimeUnit.SECONDS );
}
}
@Override
public void loadSchemaCache()
{
List<SchemaRule> schemaRules = Iterators.asList( neoStores.getSchemaStore().loadAllSchemaRules() );
schemaCache.load( schemaRules );
}
@Override
public void clearBufferedIds()
{
if ( bufferingIdGeneratorFactory != null )
{
bufferingIdGeneratorFactory.clear();
}
}
@Override
public void maintenance()
{
if ( bufferingIdGeneratorFactory != null )
{
bufferingIdGeneratorFactory.maintenance();
}
}
@Override
public void stop() throws Throwable
{
labelScanStore.stop();
indexingService.stop();
if ( safeIdBuffering )
{
idBufferMaintenance.cancel( false );
}
}
@Override
public void shutdown() throws Throwable
{
labelScanStore.shutdown();
indexingService.shutdown();
neoStores.close();
}
@Override
public void flushAndForce( IOLimiter limiter )
{
indexingService.forceAll();
labelScanStore.force();
for ( IndexImplementation index : legacyIndexProviderLookup.all() )
{
index.force();
}
neoStores.flush( limiter );
}
@Override
public void registerDiagnostics( DiagnosticsManager diagnosticsManager )
{
neoStores.registerDiagnostics( diagnosticsManager );
}
@Override
public void forceClose()
{
try
{
shutdown();
}
catch ( Throwable e )
{
throw new RuntimeException( e );
}
}
@Override
public void prepareForRecoveryRequired()
{
neoStores.deleteIdGenerators();
}
/**
* @return the underlying {@link NeoStores} which should <strong>ONLY</strong> be accessed by tests
* until all tests are properly converted to not rely on access to {@link NeoStores}. Currently there
* are important tests which asserts details about the neo stores that are very important to test,
* but to convert all those tests might be a bigger piece of work.
*/
public NeoStores testAccessNeoStores()
{
return neoStores;
}
}