-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
KernelTransactionImplementation.java
614 lines (565 loc) · 21.8 KB
/
KernelTransactionImplementation.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
/*
* 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.api;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Supplier;
import org.neo4j.collection.pool.Pool;
import org.neo4j.helpers.Clock;
import org.neo4j.kernel.api.AccessMode;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.KeyReadTokenNameLookup;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.exceptions.ConstraintViolationTransactionFailureException;
import org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.kernel.api.exceptions.TransactionHookException;
import org.neo4j.kernel.api.exceptions.schema.ConstraintValidationKernelException;
import org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException;
import org.neo4j.kernel.api.exceptions.schema.DropIndexFailureException;
import org.neo4j.kernel.api.index.IndexDescriptor;
import org.neo4j.kernel.api.txstate.LegacyIndexTransactionState;
import org.neo4j.kernel.api.txstate.TransactionState;
import org.neo4j.kernel.api.txstate.TxStateHolder;
import org.neo4j.kernel.impl.api.state.ConstraintIndexCreator;
import org.neo4j.kernel.impl.api.state.TxState;
import org.neo4j.kernel.impl.locking.Locks;
import org.neo4j.kernel.impl.proc.Procedures;
import org.neo4j.kernel.impl.transaction.TransactionHeaderInformationFactory;
import org.neo4j.kernel.impl.transaction.TransactionMonitor;
import org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation;
import org.neo4j.kernel.impl.transaction.tracing.CommitEvent;
import org.neo4j.kernel.impl.transaction.tracing.TransactionEvent;
import org.neo4j.kernel.impl.transaction.tracing.TransactionTracer;
import org.neo4j.storageengine.api.StorageCommand;
import org.neo4j.storageengine.api.StorageEngine;
import org.neo4j.storageengine.api.StoreReadLayer;
import org.neo4j.storageengine.api.txstate.TxStateVisitor;
import static org.neo4j.storageengine.api.TransactionApplicationMode.INTERNAL;
/**
* This class should replace the {@link org.neo4j.kernel.api.KernelTransaction} interface, and take its name, as soon
* as
* {@code TransitionalTxManagementKernelTransaction} is gone from {@code server}.
*/
public class KernelTransactionImplementation implements KernelTransaction, TxStateHolder
{
/*
* IMPORTANT:
* This class is pooled and re-used. If you add *any* state to it, you *must* make sure that:
* - the #initialize() method resets that state for re-use
* - the #release() method releases resources acquired in #initialize() or during the transaction's life time
*/
private enum TransactionType
{
READ,
DATA
{
@Override
TransactionType upgradeToSchemaTransaction() throws InvalidTransactionTypeKernelException
{
throw new InvalidTransactionTypeKernelException(
"Cannot perform schema updates in a transaction that has performed data updates." );
}
},
SCHEMA
{
@Override
TransactionType upgradeToDataTransaction() throws InvalidTransactionTypeKernelException
{
throw new InvalidTransactionTypeKernelException(
"Cannot perform data updates in a transaction that has performed schema updates." );
}
};
TransactionType upgradeToDataTransaction() throws InvalidTransactionTypeKernelException
{
return DATA;
}
TransactionType upgradeToSchemaTransaction() throws InvalidTransactionTypeKernelException
{
return SCHEMA;
}
}
// Logic
private final SchemaWriteGuard schemaWriteGuard;
private final TransactionHooks hooks;
private final ConstraintIndexCreator constraintIndexCreator;
private final StatementOperationParts operations;
private final StorageEngine storageEngine;
private final Procedures procedures;
private final TransactionTracer tracer;
private final Pool<KernelTransactionImplementation> pool;
private final Supplier<LegacyIndexTransactionState> legacyIndexTxStateSupplier;
// For committing
private final TransactionHeaderInformationFactory headerInformationFactory;
private final TransactionCommitProcess commitProcess;
private final TransactionMonitor transactionMonitor;
private final StoreReadLayer storeLayer;
private final Clock clock;
// State that needs to be reset between uses. Most of these should be cleared or released in #release(),
// whereas others, such as timestamp or txId when transaction starts, even locks, needs to be set in #initialize().
private TransactionState txState;
private LegacyIndexTransactionState legacyIndexTransactionState;
private TransactionType transactionType; // Tracks current state of transaction, which will upgrade to WRITE or SCHEMA mode when necessary
private TransactionHooks.TransactionHooksState hooksState;
private KernelStatement currentStatement;
private CloseListener closeListener;
private AccessMode accessMode; // Defines whether a transaction is allowed to upgrade to WRITE or SCHEMA mode
private Locks.Client locks;
private boolean beforeHookInvoked;
private boolean closing, closed;
private boolean failure, success;
private volatile boolean terminated;
private long startTimeMillis;
private long lastTransactionIdWhenStarted;
private TransactionEvent transactionEvent;
public KernelTransactionImplementation( StatementOperationParts operations,
SchemaWriteGuard schemaWriteGuard,
TransactionHooks hooks,
ConstraintIndexCreator constraintIndexCreator,
Procedures procedures, TransactionHeaderInformationFactory headerInformationFactory,
TransactionCommitProcess commitProcess,
TransactionMonitor transactionMonitor,
Supplier<LegacyIndexTransactionState> legacyIndexTxStateSupplier,
Pool<KernelTransactionImplementation> pool,
Clock clock,
TransactionTracer tracer,
StorageEngine storageEngine )
{
this.operations = operations;
this.schemaWriteGuard = schemaWriteGuard;
this.hooks = hooks;
this.constraintIndexCreator = constraintIndexCreator;
this.procedures = procedures;
this.headerInformationFactory = headerInformationFactory;
this.commitProcess = commitProcess;
this.transactionMonitor = transactionMonitor;
this.storeLayer = storageEngine.storeReadLayer();
this.storageEngine = storageEngine;
this.legacyIndexTxStateSupplier = legacyIndexTxStateSupplier;
this.pool = pool;
this.clock = clock;
this.tracer = tracer;
}
/**
* Reset this transaction to a vanilla state, turning it into a logically new transaction.
*/
public KernelTransactionImplementation initialize( long lastCommittedTx, Locks.Client locks )
{
this.locks = locks;
this.closing = closed = failure = success = terminated = beforeHookInvoked = false;
this.transactionType = TransactionType.READ;
this.startTimeMillis = clock.currentTimeMillis();
this.lastTransactionIdWhenStarted = lastCommittedTx;
this.transactionEvent = tracer.beginTransaction();
assert transactionEvent != null : "transactionEvent was null!";
this.accessMode = AccessMode.FULL;
return this;
}
@Override
public void success()
{
this.success = true;
}
@Override
public void failure()
{
failure = true;
}
@Override
public boolean shouldBeTerminated()
{
return terminated;
}
@Override
public void markForTermination()
{
if ( !terminated )
{
failure = true;
terminated = true;
if ( !closed )
{
transactionMonitor.transactionTerminated( hasTxStateWithChanges() );
}
}
}
@Override
public boolean isOpen()
{
return !closed && !closing;
}
@Override
public AccessMode mode()
{
return accessMode;
}
@Override
public void setMode( AccessMode mode )
{
this.accessMode = mode;
}
@Override
public KernelStatement acquireStatement()
{
assertTransactionOpen();
if ( currentStatement == null )
{
currentStatement = new KernelStatement( this, this, locks, operations,
storageEngine.storeReadLayer().acquireStatement(), procedures );
}
currentStatement.acquire();
return currentStatement;
}
public void releaseStatement( Statement statement )
{
assert currentStatement == statement;
currentStatement = null;
}
public void upgradeToDataTransaction() throws InvalidTransactionTypeKernelException
{
if( !accessMode.allowsWrites() )
{
throw new InvalidTransactionTypeKernelException(
String.format( "Write operations are not allowed for `%s` transactions.", accessMode.name() ) );
}
transactionType = transactionType.upgradeToDataTransaction();
}
public void upgradeToSchemaTransaction() throws InvalidTransactionTypeKernelException
{
if( !accessMode.allowsSchemaWrites() )
{
throw new InvalidTransactionTypeKernelException(
String.format( "Schema write operations are not allowed for `%s` transactions.", accessMode.name() ) );
}
doUpgradeToSchemaTransaction();
transactionType = transactionType.upgradeToSchemaTransaction();
}
public void doUpgradeToSchemaTransaction() throws InvalidTransactionTypeKernelException
{
schemaWriteGuard.assertSchemaWritesAllowed();
}
private void dropCreatedConstraintIndexes() throws TransactionFailureException
{
if ( hasTxStateWithChanges() )
{
for ( IndexDescriptor createdConstraintIndex : txState().constraintIndexesCreatedInTx() )
{
try
{
// TODO logically, which statement should this operation be performed on?
constraintIndexCreator.dropUniquenessConstraintIndex( createdConstraintIndex );
}
catch ( DropIndexFailureException e )
{
throw new IllegalStateException( "Constraint index that was created in a transaction should be " +
"possible to drop during rollback of that transaction.", e );
}
}
}
}
@Override
public TransactionState txState()
{
if ( txState == null )
{
transactionMonitor.upgradeToWriteTransaction();
txState = new TxState();
}
return txState;
}
@Override
public LegacyIndexTransactionState legacyIndexTxState()
{
return legacyIndexTransactionState != null ? legacyIndexTransactionState :
(legacyIndexTransactionState = legacyIndexTxStateSupplier.get());
}
@Override
public boolean hasTxStateWithChanges()
{
return txState != null && txState.hasChanges();
}
private void closeTransaction()
{
assertTransactionOpen();
closed = true;
closeCurrentStatementIfAny();
if ( closeListener != null )
{
closeListener.notify( success );
}
}
private void closeCurrentStatementIfAny()
{
if ( currentStatement != null )
{
currentStatement.forceClose();
currentStatement = null;
}
}
private void assertTransactionNotClosing()
{
if ( closing )
{
throw new IllegalStateException( "This transaction is already being closed." );
}
}
private void assertTransactionOpen()
{
if ( closed )
{
throw new IllegalStateException( "This transaction has already been completed." );
}
}
private boolean hasChanges()
{
return hasTxStateWithChanges() || hasLegacyIndexChanges();
}
private boolean hasLegacyIndexChanges()
{
return legacyIndexTransactionState != null && legacyIndexTransactionState.hasChanges();
}
private boolean hasDataChanges()
{
return hasTxStateWithChanges() && txState.hasDataChanges();
}
@Override
public void close() throws TransactionFailureException
{
assertTransactionOpen();
assertTransactionNotClosing();
closeCurrentStatementIfAny();
closing = true;
try
{
if ( failure || !success )
{
rollback();
if ( success )
{
// Success was called, but also failure which means that the client code using this
// transaction passed through a happy path, but the transaction was still marked as
// failed for one or more reasons. Tell the user that although it looked happy it
// wasn't committed, but was instead rolled back.
throw new TransactionFailureException( Status.Transaction.MarkedAsFailed,
"Transaction rolled back even if marked as successful" );
}
}
else
{
commit();
}
}
finally
{
try
{
closed = true;
closing = false;
transactionEvent.setSuccess( success );
transactionEvent.setFailure( failure );
transactionEvent.setTransactionType( transactionType.name() );
transactionEvent.setReadOnly( txState == null || !txState.hasChanges() );
transactionEvent.close();
}
finally
{
release();
}
}
}
private void commit() throws TransactionFailureException
{
boolean success = false;
try ( CommitEvent commitEvent = transactionEvent.beginCommitEvent() )
{
// Trigger transaction "before" hooks.
if ( hasDataChanges() )
{
try
{
hooksState = hooks.beforeCommit( txState, this, storageEngine.storeReadLayer() );
if ( hooksState != null && hooksState.failed() )
{
TransactionHookException cause = hooksState.failure();
throw new TransactionFailureException( Status.Transaction.HookFailed, cause, "" );
}
}
finally
{
beforeHookInvoked = true;
}
}
// Convert changes into commands and commit
if ( hasChanges() )
{
// Gather up commands from the various sources
Collection<StorageCommand> extractedCommands = new ArrayList<>();
storageEngine.createCommands(
extractedCommands,
txState,
locks,
lastTransactionIdWhenStarted );
if ( hasLegacyIndexChanges() )
{
legacyIndexTransactionState.extractCommands( extractedCommands );
}
/* Here's the deal: we track a quick-to-access hasChanges in transaction state which is true
* if there are any changes imposed by this transaction. Some changes made inside a transaction undo
* previously made changes in that same transaction, and so at some point a transaction may have
* changes and at another point, after more changes seemingly,
* the transaction may not have any changes.
* However, to track that "undoing" of the changes is a bit tedious, intrusive and hard to maintain
* and get right.... So to really make sure the transaction has changes we re-check by looking if we
* have produced any commands to add to the logical log.
*/
if ( !extractedCommands.isEmpty() )
{
// Finish up the whole transaction representation
PhysicalTransactionRepresentation transactionRepresentation =
new PhysicalTransactionRepresentation( extractedCommands );
TransactionHeaderInformation headerInformation = headerInformationFactory.create();
transactionRepresentation.setHeader( headerInformation.getAdditionalHeader(),
headerInformation.getMasterId(),
headerInformation.getAuthorId(),
startTimeMillis, lastTransactionIdWhenStarted, clock.currentTimeMillis(),
locks.getLockSessionId() );
// Commit the transaction
commitProcess.commit( new TransactionToApply( transactionRepresentation ), commitEvent, INTERNAL );
}
}
success = true;
}
catch ( ConstraintValidationKernelException | CreateConstraintFailureException e )
{
throw new ConstraintViolationTransactionFailureException(
e.getUserMessage( new KeyReadTokenNameLookup( operations.keyReadOperations() ) ), e );
}
finally
{
if ( !success )
{
rollback();
}
else
{
afterCommit();
}
}
}
private void rollback() throws TransactionFailureException
{
try
{
try
{
dropCreatedConstraintIndexes();
}
catch ( IllegalStateException | SecurityException e )
{
throw new TransactionFailureException( Status.Transaction.CouldNotRollback, e,
"Could not drop created constraint indexes" );
}
// Free any acquired id's
if ( txState != null )
{
try
{
txState.accept( new TxStateVisitor.Adapter()
{
@Override
public void visitCreatedNode( long id )
{
storeLayer.releaseNode( id );
}
@Override
public void visitCreatedRelationship( long id, int type, long startNode, long endNode )
{
storeLayer.releaseRelationship( id );
}
} );
}
catch ( ConstraintValidationKernelException | CreateConstraintFailureException e )
{
throw new IllegalStateException(
"Releasing locks during rollback should perform no constraints checking.", e );
}
}
}
finally
{
afterRollback();
}
}
private void afterCommit()
{
try
{
closeTransaction();
if ( beforeHookInvoked )
{
hooks.afterCommit( txState, this, hooksState );
}
}
finally
{
transactionMonitor.transactionFinished( true, hasTxStateWithChanges() );
}
}
private void afterRollback()
{
try
{
closeTransaction();
if ( beforeHookInvoked )
{
hooks.afterRollback( txState, this, hooksState );
}
}
finally
{
transactionMonitor.transactionFinished( false, hasTxStateWithChanges() );
}
}
/**
* Release resources held up by this transaction & return it to the transaction pool.
*/
private void release()
{
locks.close();
transactionEvent = null;
legacyIndexTransactionState = null;
txState = null;
hooksState = null;
closeListener = null;
pool.release( this );
}
@Override
public void registerCloseListener( CloseListener listener )
{
assert closeListener == null;
closeListener = listener;
}
@Override
public String toString()
{
return "KernelTransaction[" + this.locks.getLockSessionId() + "]";
}
}