-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
RecordStorageEngine.java
509 lines (459 loc) · 20.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
/*
* Copyright (c) 2002-2015 "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.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import org.neo4j.concurrent.WorkSync;
import org.neo4j.function.Factory;
import org.neo4j.graphdb.config.Setting;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.IdGeneratorFactory;
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.txstate.LegacyIndexTransactionState;
import org.neo4j.kernel.api.txstate.TransactionCountingStateVisitor;
import org.neo4j.kernel.api.txstate.TransactionState;
import org.neo4j.kernel.api.txstate.TxStateVisitor;
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.LegacyBatchIndexApplier;
import org.neo4j.kernel.impl.api.LegacyIndexApplierLookup;
import org.neo4j.kernel.impl.api.LegacyIndexProviderLookup;
import org.neo4j.kernel.impl.api.StatementOperationParts;
import org.neo4j.kernel.impl.api.TransactionApplicationMode;
import org.neo4j.kernel.impl.api.TransactionApplier;
import org.neo4j.kernel.impl.api.TransactionApplierFacade;
import org.neo4j.kernel.impl.api.TransactionToApply;
import org.neo4j.kernel.impl.api.index.IndexUpdateMode;
import org.neo4j.kernel.impl.api.index.IndexUpdatesValidator;
import org.neo4j.kernel.impl.api.index.IndexingService;
import org.neo4j.kernel.impl.api.index.OnlineIndexUpdatesValidator;
import org.neo4j.kernel.impl.api.index.RecoveryIndexingUpdatesValidator;
import org.neo4j.kernel.impl.api.index.SchemaIndexProviderMap;
import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig;
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.ProcedureCache;
import org.neo4j.kernel.impl.api.store.SchemaCache;
import org.neo4j.kernel.impl.api.store.StoreReadLayer;
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.locking.Locks;
import org.neo4j.kernel.impl.storageengine.CommandReaderFactory;
import org.neo4j.kernel.impl.storageengine.StorageEngine;
import org.neo4j.kernel.impl.store.MetaDataStore;
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.record.SchemaRule;
import org.neo4j.kernel.impl.transaction.command.CacheInvalidationBatchTransactionApplier;
import org.neo4j.kernel.impl.transaction.command.Command;
import org.neo4j.kernel.impl.transaction.command.HighIdBatchTransactionApplier;
import org.neo4j.kernel.impl.transaction.command.IndexBatchTransactionApplier;
import org.neo4j.kernel.impl.transaction.command.LabelUpdateWork;
import org.neo4j.kernel.impl.transaction.command.NeoStoreBatchTransactionApplier;
import org.neo4j.kernel.impl.transaction.log.LogVersionRepository;
import org.neo4j.kernel.impl.transaction.log.TransactionIdStore;
import org.neo4j.kernel.impl.transaction.state.DefaultSchemaIndexProviderMap;
import org.neo4j.kernel.impl.transaction.state.IntegrityValidator;
import org.neo4j.kernel.impl.transaction.state.NeoStoreIndexStoreView;
import org.neo4j.kernel.impl.transaction.state.NeoStoreTransactionContext;
import org.neo4j.kernel.impl.transaction.state.PropertyLoader;
import org.neo4j.kernel.impl.transaction.state.TransactionRecordState;
import org.neo4j.kernel.impl.util.IdOrderingQueue;
import org.neo4j.kernel.impl.util.JobScheduler;
import org.neo4j.kernel.impl.util.SynchronizedArrayIdOrderingQueue;
import org.neo4j.kernel.internal.DatabaseHealth;
import org.neo4j.kernel.lifecycle.Lifecycle;
import org.neo4j.logging.LogProvider;
import org.neo4j.unsafe.batchinsert.LabelScanWriter;
import static org.neo4j.helpers.Settings.BOOLEAN;
import static org.neo4j.helpers.Settings.TRUE;
import static org.neo4j.helpers.Settings.setting;
import static org.neo4j.helpers.collection.Iterables.toList;
import static org.neo4j.kernel.impl.locking.LockService.NO_LOCK_SERVICE;
public class RecordStorageEngine implements StorageEngine, Lifecycle
{
/**
* This setting is hidden to the user and is here merely for making it easier to back out of
* a change where reading property chains incurs read locks on {@link LockService}.
*/
private static final Setting<Boolean> use_read_locks_on_property_reads =
setting( "experimental.use_read_locks_on_property_reads", BOOLEAN, TRUE );
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 IndexUpdatesValidator indexUpdatesValidator;
private final CacheAccessBackDoor cacheAccess;
private final LabelScanStore labelScanStore;
private final DefaultSchemaIndexProviderMap providerMap;
private final ProcedureCache procedureCache;
private final LegacyIndexApplierLookup legacyIndexApplierLookup;
private final Runnable schemaStateChangeCallback;
private final SchemaStorage schemaStorage;
private final ConstraintSemantics constraintSemantics;
private final RecoveryIndexingUpdatesValidator indexUpdatesValidatorForRecovery;
private final IdOrderingQueue legacyIndexTransactionOrdering;
private final LockService lockService;
private final WorkSync<Supplier<LabelScanWriter>,LabelUpdateWork> labelScanStoreSync;
private final CommandReaderFactory commandReaderFactory;
public RecordStorageEngine(
File storeDir,
Config config,
IdGeneratorFactory idGeneratorFactory,
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 )
{
this.propertyKeyTokenHolder = propertyKeyTokenHolder;
this.relationshipTypeTokenHolder = relationshipTypeTokens;
this.labelTokenHolder = labelTokens;
this.schemaStateChangeCallback = schemaStateChangeCallback;
this.constraintSemantics = constraintSemantics;
this.lockService = lockService;
this.databaseHealth = databaseHealth;
this.indexConfigStore = indexConfigStore;
final StoreFactory storeFactory = new StoreFactory( storeDir, config, idGeneratorFactory, pageCache, fs, logProvider );
neoStores = storeFactory.openAllNeoStores( true );
try
{
schemaCache = new SchemaCache( constraintSemantics, Collections.<SchemaRule>emptyList() );
schemaStorage = new SchemaStorage( neoStores.getSchemaStore() );
providerMap = new DefaultSchemaIndexProviderMap( indexProvider );
indexingService = IndexingService.create(
new IndexSamplingConfig( config ), scheduler, providerMap,
new NeoStoreIndexStoreView( lockService, neoStores ), tokenNameLookup,
toList( new SchemaStorage( neoStores.getSchemaStore() ).allIndexRules() ), logProvider,
indexingServiceMonitor, schemaStateChangeCallback );
integrityValidator = new IntegrityValidator( neoStores, indexingService );
indexUpdatesValidator = new OnlineIndexUpdatesValidator(
neoStores, databaseHealth, new PropertyLoader( neoStores ),
indexingService, IndexUpdateMode.BATCHED );
cacheAccess = new BridgingCacheAccess( schemaCache, schemaStateChangeCallback,
propertyKeyTokenHolder, relationshipTypeTokens, labelTokens );
DiskLayer diskLayer = new DiskLayer( propertyKeyTokenHolder, labelTokens, relationshipTypeTokens,
schemaStorage,
neoStores, indexingService, storeStatementFactory( neoStores, config, lockService ) );
procedureCache = new ProcedureCache();
storeLayer = new CacheLayer( diskLayer, schemaCache, procedureCache );
labelScanStore = labelScanStoreProvider.getLabelScanStore();
legacyIndexApplierLookup = new LegacyIndexApplierLookup.Direct( legacyIndexProviderLookup );
legacyIndexTransactionOrdering = new SynchronizedArrayIdOrderingQueue( 20 );
indexUpdatesValidatorForRecovery = new RecoveryIndexingUpdatesValidator( indexingService );
labelScanStoreSync = new WorkSync<>( labelScanStore::newWriter );
this.commandReaderFactory = new RecordStorageCommandReaderFactory();
}
catch ( Throwable failure )
{
neoStores.close();
throw failure;
}
}
private static Factory<StoreStatement> storeStatementFactory(
NeoStores neoStores, Config config, LockService lockService )
{
final LockService currentLockService =
config.get( use_read_locks_on_property_reads ) ? lockService : NO_LOCK_SERVICE;
return () -> new StoreStatement( neoStores, currentLockService );
}
@Override
public StoreReadLayer storeReadLayer()
{
return storeLayer;
}
@Override
public CommandReaderFactory commandReaderFactory()
{
return commandReaderFactory;
}
@SuppressWarnings( "resource" )
@Override
public Collection<Command> createCommands(
TransactionState txState,
LegacyIndexTransactionState legacyIndexTransactionState,
Locks.Client locks,
StatementOperationParts operations,
StoreStatement storeStatement,
long lastTransactionIdWhenStarted )
throws TransactionFailureException, CreateConstraintFailureException, ConstraintValidationKernelException
{
// Create objects to be populated with command-friendly changes
Collection<Command> commands = new ArrayList<>();
if ( txState == null )
{
// Bypass creating commands for record transaction state if we have no transaction state
legacyIndexTransactionState.extractCommands( commands );
return commands;
}
NeoStoreTransactionContext context = new NeoStoreTransactionContext( neoStores, locks );
TransactionRecordState recordState = new TransactionRecordState( neoStores, integrityValidator, context );
recordState.initialize( lastTransactionIdWhenStarted );
// Visit transaction state and populate these record state objects
TxStateVisitor txStateVisitor = new TransactionToRecordStateVisitor( recordState,
schemaStateChangeCallback, schemaStorage, constraintSemantics, providerMap,
legacyIndexTransactionState, procedureCache );
CountsRecordState countsRecordState = new CountsRecordState();
txStateVisitor = constraintSemantics.decorateTxStateVisitor(
operations,
storeStatement,
storeLayer,
new DirectTxStateHolder( txState, legacyIndexTransactionState ),
txStateVisitor );
txStateVisitor = new TransactionCountingStateVisitor(
txStateVisitor, storeLayer, txState, countsRecordState );
try ( TxStateVisitor visitor = txStateVisitor )
{
txState.accept( txStateVisitor );
}
// Convert record state into commands
recordState.extractCommands( commands );
legacyIndexTransactionState.extractCommands( commands );
countsRecordState.extractCommands( commands );
return commands;
}
@Override
public void apply( TransactionToApply 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 ) )
{
TransactionToApply tx = batch;
while ( tx != null )
{
try ( LockGroup locks = new LockGroup() )
{
ensureValidatedIndexUpdates( tx );
try ( TransactionApplier txApplier = batchApplier.startTx( tx, locks ) )
{
tx.transactionRepresentation().accept( txApplier );
}
tx = tx.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.
*/
private 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 ) );
// 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()] ) );
}
private void ensureValidatedIndexUpdates( TransactionToApply tx ) throws IOException
{
// This method is a hack needed because
// - we derive logical index updates from physical record changes + reading from store.
// In a batching scenario we must do this validation before each transaction gets applied,
// because data might rely on data applied by the previous transaction.
// - we do index validation up front like this and will be removed once we bust the lucene limits.
if ( tx.validatedIndexUpdates() == null )
{
tx.validatedIndexUpdates( indexUpdatesValidator.validate( tx.transactionRepresentation() ) );
}
}
@Override
public IndexUpdatesValidator indexUpdatesValidatorForRecovery()
{
return indexUpdatesValidatorForRecovery;
}
@Override
public TransactionIdStore transactionIdStore()
{
return neoStores.getMetaDataStore();
}
@Override
public LogVersionRepository logVersionRepository()
{
return neoStores.getMetaDataStore();
}
@Override
public ProcedureCache procedureCache()
{
return procedureCache;
}
@Override
public NeoStores neoStores()
{
return neoStores;
}
@Override
public MetaDataStore metaDataStore()
{
return neoStores.getMetaDataStore();
}
@Override
public IndexingService indexingService()
{
return indexingService;
}
@Override
public IndexUpdatesValidator indexUpdatesValidator()
{
return indexUpdatesValidator;
}
@Override
public LabelScanStore labelScanStore()
{
return labelScanStore;
}
@Override
public IntegrityValidator integrityValidator()
{
return integrityValidator;
}
@Override
public SchemaIndexProviderMap schemaIndexProviderMap()
{
return providerMap;
}
@Override
public CacheAccessBackDoor cacheAccess()
{
return cacheAccess;
}
@Override
public LegacyIndexApplierLookup legacyIndexApplierLookup()
{
return legacyIndexApplierLookup;
}
@Override
public IndexConfigStore indexConfigStore()
{
return indexConfigStore;
}
@Override
public IdOrderingQueue legacyIndexTransactionOrdering()
{
return legacyIndexTransactionOrdering;
}
@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();
}
@Override
public void loadSchemaCache()
{
List<SchemaRule> schemaRules = toList( neoStores.getSchemaStore().loadAllSchemaRules() );
schemaCache.load( schemaRules );
}
@Override
public void stop() throws Throwable
{
labelScanStore.stop();
indexingService.stop();
}
@Override
public void shutdown() throws Throwable
{
labelScanStore.shutdown();
indexingService.shutdown();
}
}