Class CommitBarrier
java.lang.Object
org.multiverse.commitbarriers.CommitBarrier
- Direct Known Subclasses:
CountDownCommitBarrier, VetoCommitBarrier
A CommitBarrier is a blocking structure like the
CyclicBarrier but
tailored to work with transactions. Based on this functionality, it is possible to create
a 2-phase commit for example.-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static intprivate static final ScheduledThreadPoolExecutorprivate ScheduledExecutorServiceprotected final Lockprivate intprivate static booleanprivate CommitBarrier.Statusprotected final Condition -
Constructor Summary
ConstructorsConstructorDescriptionCommitBarrier(CommitBarrier.Status status, boolean fair) Creates a new CommitBarrier. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidabort()Aborts this CommitBarrier.protected final voidAdds a waiters.private longawaitNanosUninterruptible(long timeoutNs) final voidAwaits for this barrier to open (commit or abort).final voidAwaits for this barrier to open (commit or abort).protected static voidensureNotDead(Txn tx, String operation) Ensures that a transaction is not dead.protected static voidexecuteTasks(List<Runnable> tasks) Executes the tasks.protected final voidFinishes a Txn.final intReturns the number of Transactions that have prepared and are waiting to commit.protected final CommitBarrier.Statusfinal booleanChecks if this CommitBarrier already is aborted.final booleanisClosed()Checks if this CommitBarrier is closed.final booleanChecks if this CommitBarrier already is committed.protected abstract booleanvoidjoinCommit(Txn tx) Joins this CommitBarrier with the provided transaction.voidJoins this CommitBarrier with the provided transaction.final voidregisterOnAbortTask(Runnable task) Registers a task that is executed once the CommitBarrier aborts.final voidregisterOnCommitTask(Runnable task) Registers a task that is executed once the CommitBarrier commits.voidsetScheduledExecutorService(ScheduledExecutorService executorService) Sets the ScheduledExecutorService to be used by this CommitBarrier for the timeout.final voidsetTimeout(long timeout, TimeUnit unit) Sets the timeout on this CommitBarrier.Only should be made when the lock is acquired.Only should be made when the lock is acquired.final booleantryAwaitOpen(long timeout, TimeUnit unit) Waits for this barrier to open (abort or commit).final booleantryAwaitOpenUninterruptibly(long timeout, TimeUnit unit) Tries to await the close of the barrier.booleantryJoinCommit(Txn tx) Tries to joins this CommitBarrier with the provided transaction.booleantryJoinCommit(Txn tx, long timeout, TimeUnit unit) Tries to joins this CommitBarrier with the provided transaction.booleantryJoinCommitUninterruptibly(Txn tx, long timeout, TimeUnit unit) Tries to joins this CommitBarrier with the provided transaction.
-
Field Details
-
corePoolSize
private static int corePoolSize -
runAsDaemon
private static boolean runAsDaemon -
EXECUTOR
-
executorService
-
lock
-
statusCondition
-
status
-
numberWaiting
private volatile int numberWaiting -
onAbortTasks
-
onCommitTasks
-
-
Constructor Details
-
CommitBarrier
Creates a new CommitBarrier.- Parameters:
status- the initial status of the CommitBarrier.fair- if waking up threads is going to be fair.- Throws:
NullPointerException- if status is null.
-
-
Method Details
-
getStatus
-
getNumberWaiting
public final int getNumberWaiting()Returns the number of Transactions that have prepared and are waiting to commit. Value eventually becomes null after a commit or abort.- Returns:
- the number of transactions prepared.
-
isClosed
public final boolean isClosed()Checks if this CommitBarrier is closed. This is the initial status of the barrier.- Returns:
- true if closed, false otherwise.
-
isCommitted
public final boolean isCommitted()Checks if this CommitBarrier already is committed.- Returns:
- true if committed, false otherwise.
-
isAborted
public final boolean isAborted()Checks if this CommitBarrier already is aborted.- Returns:
- true if aborted, false otherwise.
-
signalCommit
-
signalAborted
-
abort
public final void abort()Aborts this CommitBarrier. If there are any prepared transactions that are waiting for this CommitBarrier to complete, they are aborted as well. If the CommitBarrier already is aborted, this call is ignored.- Throws:
CommitBarrierOpenException- if this CommitBarrier already is committed.
-
executeTasks
-
awaitOpen
Awaits for this barrier to open (commit or abort). This call doesn't influence the state of this CommitBarrier.- Throws:
InterruptedException- if the calling thread is interrupted while waiting.
-
awaitOpenUninterruptibly
public final void awaitOpenUninterruptibly()Awaits for this barrier to open (commit or abort). This call doesn't influence the state of this CommitBarrier. This call is not responsive to interrupts. -
tryAwaitOpen
Waits for this barrier to open (abort or commit). This call doesn't influence the state of this CommitBarrier.- Parameters:
timeout- the maximum amount of time to wait for the barrier to close.unit- the TimeUnit for the timeout argument.- Returns:
- true if the wait was a success, false if the barrier still is closed.
- Throws:
InterruptedException- if the thread is interrupted while waiting.NullPointerException- if unit is null.
-
tryAwaitOpenUninterruptibly
Tries to await the close of the barrier. This call doesn't influence the state of this CommitBarrier. This call is not responsive to interrupts.- Parameters:
timeout- the maximum amount of time to wait for the barrier to be closed.unit- the timeunit for the timeout argument.- Returns:
- true if the wait was a success, false otherwise.
-
awaitNanosUninterruptible
private long awaitNanosUninterruptible(long timeoutNs) -
setScheduledExecutorService
Sets the ScheduledExecutorService to be used by this CommitBarrier for the timeout. This method can always be called no matter the state of the CommitBarrier.- Parameters:
executorService- the ScheduledExecutorService this CommitBarrier is going to use for timeout.- Throws:
NullPointerException- if executorService is null.
-
setTimeout
Sets the timeout on this CommitBarrier. If the barrier hasn't committed/aborted before the timeout it automatically is aborted. This is a function that typically is used when initializing the CommitBarrier. The timeout starts running when this method is called.- Parameters:
timeout- the maximum amount of time this barrier is allowed to run.unit- the TimeUnit of the timeout parameter.- Throws:
NullPointerException- if unit is null.CommitBarrierOpenException- if the CommitBarrier already is aborted or committed.
-
registerOnAbortTask
Registers a task that is executed once the CommitBarrier aborts. The task will be executed after the abort and will be executed by the thread that does the actual abort. The tasks will be executed in the order they are registered and will be executed at most once. If one of the tasks throws a RuntimeException, the following will not be executed.- Parameters:
task- the task that is executed once the CommitBarrier commits.- Throws:
NullPointerException- if task is null.CommitBarrierOpenException- if this CommitBarrier already is aborted or committed.
-
registerOnCommitTask
Registers a task that is executed once the CommitBarrier commits. The task will be executed after the commit and will be executed by the thread that does the actual commit. The tasks will be executed in the order they are registered and will be executed at most once. If one of the tasks throws a RuntimeException, the following will not be executed.- Parameters:
task- the task that is executed once the CommitBarrier commits.- Throws:
NullPointerException- if task is null.CommitBarrierOpenException- if this CommitBarrier already is aborted or committed.
-
addJoiner
protected final void addJoiner()Adds a waiters. Should only be called when the main lock is acquired.- Throws:
IllegalStateException- if the transaction isn't closed.
-
finish
Finishes a Txn. Can be called without the mainlock is acquired.- Parameters:
tx- the transaction to finish
-
ensureNotDead
Ensures that a transaction is not dead. Can be called without the mainlock is acquired.- Parameters:
tx- the transaction to check.operation- the name of the operation to checks if this transaction is not dead. Needed to provide a useful message.- Throws:
DeadTxnException- if tx is dead.NullPointerException- if tx is null.
-
joinCommit
Joins this CommitBarrier with the provided transaction. If the CommitBarrier can't commit yet, the method will block. If the CommitBarrier already is aborted or committed, the transaction is aborted. This method is responsive to interrupts. If the waiting thread is interrupted, it will abort itself and this CommitGroup.- Parameters:
tx- the Txn to commit.- Throws:
InterruptedException- if the thread is interrupted while waiting.NullPointerException- if tx is null.IllegalTxnStateException- if the tx is no in the correct state for this operation.CommitBarrierOpenException- if this VetoCommitBarrier is committed or aborted.
-
joinCommitUninterruptibly
Joins this CommitBarrier with the provided transaction. If the CommitBarrier can't commit yet, this method will block without being interruptible. If the CommitBarrier already is aborted or committed, the transaction is aborted.- Parameters:
tx- the Txn to join in the commit.- Throws:
NullPointerException- if tx is null.IllegalTxnStateException- if the tx is not in the correct state for the operation.CommitBarrierOpenException- if this VetoCommitBarrier is committed or aborted.
-
tryJoinCommit
Tries to joins this CommitBarrier with the provided transaction. If the CommitBarrier can't commit yet, the transaction and CommitBarrier will be aborted. So this method will not block (for a long period). If the CommitBarrier already is aborted or committed, the transaction is aborted.- Parameters:
tx- the Txn that wants to join the other parties to commit with.- Returns:
- true if CountDownCommitBarrier was committed, false if aborted.
- Throws:
CommitBarrierOpenException- if tx or this CountDownCommitBarrier is aborted or committed.NullPointerException- if tx is null.
-
tryJoinCommit
Tries to joins this CommitBarrier with the provided transaction. If the CommitBarrier can't commit yet, this call will block until one of the following things happens:- the CommitBarrier is committed before timeing out: the transaction also is committed
- the CommitBarrier is aborted before timeing out: the transaction also is aborted
- the thread is interrupted: the transaction and commit barrier also is aborted
- the thread times out: the transaction and commit barrier are aborted
- Parameters:
tx- the Txn that wants to join the other parties to commit with.timeout- the maximum time to wait.unit- the TimeUnit for the timeout argument.- Returns:
- true if CountDownCommitBarrier was committed, false if aborted.
- Throws:
CommitBarrierOpenException- if tx or this CountDownCommitBarrier is aborted or committed.NullPointerException- if tx or unit is null is null.InterruptedException- if the calling thread is interrupted while waiting.
-
tryJoinCommitUninterruptibly
Tries to joins this CommitBarrier with the provided transaction. If the CommitBarrier can't commit yet, this call will block until one of the following things happens:- the CommitBarrier is committed
- the CommitBarrier is aborted
- Parameters:
tx- the Txn that wants to join the other parties to commit with.timeout- the maximum time to wait.unit- the TimeUnit for the timeout argument.- Returns:
- true if CountDownCommitBarrier was committed, false if aborted.
- Throws:
CommitBarrierOpenException- if tx or this CountDownCommitBarrier is aborted or committed.NullPointerException- if tx or unit is null is null.
-
isLastParty
protected abstract boolean isLastParty()
-