Skip to content

Commit

Permalink
Add KernelTransactionType
Browse files Browse the repository at this point in the history
A transaction should know if it has been implictly created by Neo4j or
explicitly created by the end user
  • Loading branch information
davidegrohmann committed Feb 26, 2016
1 parent e153316 commit 7cf94d5
Show file tree
Hide file tree
Showing 30 changed files with 208 additions and 188 deletions.
Expand Up @@ -28,7 +28,7 @@
* and write operations are supported as well as creating transactions.
*
* Changes to the graph (i.e. write operations) are performed via a
* {@link #newTransaction() transaction context} where changes done
* {@link #newTransaction(KernelTransaction.Type) transaction context} where changes done
* inside the transaction are visible in read operations for {@link Statement statements}
* executed within that transaction context.
*/
Expand All @@ -37,8 +37,10 @@ public interface KernelAPI
/**
* Creates and returns a new {@link KernelTransaction} capable of modifying the
* underlying graph.
*
* @param type the type of the new transaction: implicit (internally created) or explicit (created by the user)
*/
KernelTransaction newTransaction() throws TransactionFailureException;
KernelTransaction newTransaction( KernelTransaction.Type type ) throws TransactionFailureException;

/**
* Registers a {@link TransactionHook} that will receive notifications about committing transactions
Expand Down
Expand Up @@ -73,6 +73,11 @@
*/
public interface KernelTransaction extends AutoCloseable
{
enum Type {
implicit,
explicit
}

interface CloseListener
{
void notify( boolean success );
Expand Down Expand Up @@ -140,4 +145,14 @@ interface CloseListener
* @param listener {@link CloseListener} to get these notifications.
*/
void registerCloseListener( CloseListener listener );

/**
* Kernel transaction type
*
* Implicit if created internally in the database
* Explicit if created by the end user
*
* @return the transaction type: implicit or explicit
*/
Type transactionType();
}
Expand Up @@ -81,10 +81,10 @@ public Kernel( KernelTransactions transactionFactory,
}

@Override
public KernelTransaction newTransaction() throws TransactionFailureException
public KernelTransaction newTransaction( KernelTransaction.Type type ) throws TransactionFailureException
{
health.assertHealthy( TransactionFailureException.class );
KernelTransaction transaction = transactions.newInstance();
KernelTransaction transaction = transactions.newInstance( type );
transactionMonitor.transactionStarted();
return transaction;
}
Expand Down
Expand Up @@ -140,6 +140,7 @@ TransactionType upgradeToSchemaTransaction() throws InvalidTransactionTypeKernel
private long startTimeMillis;
private long lastTransactionIdWhenStarted;
private TransactionEvent transactionEvent;
private Type type;

public KernelTransactionImplementation( StatementOperationParts operations,
SchemaWriteGuard schemaWriteGuard,
Expand Down Expand Up @@ -173,8 +174,9 @@ public KernelTransactionImplementation( StatementOperationParts operations,
/**
* Reset this transaction to a vanilla state, turning it into a logically new transaction.
*/
public KernelTransactionImplementation initialize( long lastCommittedTx, Locks.Client locks )
public KernelTransactionImplementation initialize( long lastCommittedTx, Locks.Client locks, Type type )
{
this.type = type;
this.locks = locks;
this.closing = closed = failure = success = terminated = beforeHookInvoked = false;
this.transactionType = TransactionType.READ;
Expand Down Expand Up @@ -591,6 +593,7 @@ private void afterRollback()
private void release()
{
locks.close();
type = null;
transactionEvent = null;
legacyIndexTransactionState = null;
txState = null;
Expand All @@ -606,6 +609,12 @@ public void registerCloseListener( CloseListener listener )
closeListener = listener;
}

@Override
public Type transactionType()
{
return type;
}

@Override
public String toString()
{
Expand Down
Expand Up @@ -54,7 +54,7 @@
* for enumerating all running transactions. During normal operation, acquiring new transactions and enumerating live
* ones requires no synchronization (although the live list is not guaranteed to be exact).
*/
public class KernelTransactions extends LifecycleAdapter implements Factory<KernelTransaction>
public class KernelTransactions extends LifecycleAdapter
{
// Transaction dependencies

Expand Down Expand Up @@ -86,8 +86,7 @@ public class KernelTransactions extends LifecycleAdapter implements Factory<Kern
* As such, it provides a good mechanism for listing all transactions without requiring synchronization when
* starting and committing transactions.
*/
private final Set<KernelTransactionImplementation> allTransactions = newSetFromMap(
new ConcurrentHashMap<>() );
private final Set<KernelTransactionImplementation> allTransactions = newSetFromMap( new ConcurrentHashMap<>() );

public KernelTransactions( Locks locks,
ConstraintIndexCreator constraintIndexCreator,
Expand Down Expand Up @@ -141,11 +140,11 @@ public KernelTransactionImplementation newInstance()
}
};

@Override
public KernelTransaction newInstance()
public KernelTransaction newInstance( KernelTransaction.Type type )
{
assertDatabaseIsRunning();
return localTxPool.acquire().initialize( transactionIdStore.getLastCommittedTransactionId(), locks.newClient() );
return localTxPool.acquire()
.initialize( transactionIdStore.getLastCommittedTransactionId(), locks.newClient(), type );
}

/**
Expand Down
Expand Up @@ -47,7 +47,7 @@ public RemoveOrphanConstraintIndexesOnStartup( KernelAPI kernel, LogProvider log

public void perform()
{
try ( KernelTransaction transaction = kernel.newTransaction();
try ( KernelTransaction transaction = kernel.newTransaction( KernelTransaction.Type.implicit );
Statement statement = transaction.acquireStatement() )
{
for ( Iterator<IndexDescriptor> indexes = statement.readOperations().uniqueIndexesGetAll();
Expand Down
Expand Up @@ -60,7 +60,7 @@ public ConstraintIndexCreator( Supplier<KernelAPI> kernelSupplier, IndexingServi
public long createUniquenessConstraintIndex( KernelStatement state, SchemaReadOperations schema,
int labelId, int propertyKeyId )
throws ConstraintVerificationFailedKernelException, TransactionFailureException,
CreateConstraintFailureException, DropIndexFailureException
CreateConstraintFailureException, DropIndexFailureException
{
IndexDescriptor descriptor = createConstraintIndex( labelId, propertyKeyId );
UniquenessConstraint constraint = new UniquenessConstraint( labelId, propertyKeyId );
Expand Down Expand Up @@ -97,8 +97,8 @@ public long createUniquenessConstraintIndex( KernelStatement state, SchemaReadOp
public void dropUniquenessConstraintIndex( IndexDescriptor descriptor )
throws TransactionFailureException, DropIndexFailureException
{
try ( KernelTransaction transaction = kernelSupplier.get().newTransaction();
Statement statement = transaction.acquireStatement() )
try ( KernelTransaction transaction = kernelSupplier.get().newTransaction( KernelTransaction.Type.implicit );
Statement statement = transaction.acquireStatement() )
{
// NOTE: This creates the index (obviously) but it DOES NOT grab a schema
// write lock. It is assumed that the transaction that invoked this "inner" transaction
Expand Down Expand Up @@ -141,7 +141,7 @@ private void awaitIndexPopulation( UniquenessConstraint constraint, long indexId

public IndexDescriptor createConstraintIndex( final int labelId, final int propertyKeyId )
{
try ( KernelTransaction transaction = kernelSupplier.get().newTransaction();
try ( KernelTransaction transaction = kernelSupplier.get().newTransaction( KernelTransaction.Type.implicit );
Statement statement = transaction.acquireStatement() )
{
// NOTE: This creates the index (obviously) but it DOES NOT grab a schema
Expand Down
Expand Up @@ -39,7 +39,7 @@ public abstract class IsolatedTransactionTokenCreator implements TokenCreator
private final Supplier<KernelAPI> kernelSupplier;

public IsolatedTransactionTokenCreator( Supplier<KernelAPI> kernelSupplier,
IdGeneratorFactory idGeneratorFactory )
IdGeneratorFactory idGeneratorFactory )
{
this.kernelSupplier = kernelSupplier;
this.idGeneratorFactory = idGeneratorFactory;
Expand All @@ -49,7 +49,7 @@ public IsolatedTransactionTokenCreator( Supplier<KernelAPI> kernelSupplier,
public synchronized int getOrCreate( String name ) throws org.neo4j.kernel.api.exceptions.KernelException
{
KernelAPI kernel = kernelSupplier.get();
try ( KernelTransaction transaction = kernel.newTransaction() )
try ( KernelTransaction transaction = kernel.newTransaction( KernelTransaction.Type.implicit ) )
{
try ( Statement statement = transaction.acquireStatement() )
{
Expand Down
Expand Up @@ -157,12 +157,12 @@ public void shutdown()
}

@Override
public KernelTransaction beginTransaction()
public KernelTransaction beginTransaction( KernelTransaction.Type type )
{
try
{
availability.assertDatabaseAvailable();
KernelTransaction kernelTx = dataSource.kernelAPI.get().newTransaction();
KernelTransaction kernelTx = dataSource.kernelAPI.get().newTransaction( type );
kernelTx.registerCloseListener( (s) -> dataSource.threadToTransactionBridge.unbindTransactionFromCurrentThread() );
dataSource.threadToTransactionBridge.bindTransactionToCurrentThread( kernelTx );
return kernelTx;
Expand Down

0 comments on commit 7cf94d5

Please sign in to comment.