public abstract class CommitBarrier
extends java.lang.Object
CyclicBarrier
but
tailored to work with transactions. Based on this functionality, it is possible to create
a 2-phase commit for example.Modifier and Type | Class and Description |
---|---|
(package private) static class |
CommitBarrier.Status |
Modifier and Type | Field and Description |
---|---|
private static int |
corePoolSize |
private static java.util.concurrent.ScheduledThreadPoolExecutor |
EXECUTOR |
private java.util.concurrent.ScheduledExecutorService |
executorService |
protected java.util.concurrent.locks.Lock |
lock |
private int |
numberWaiting |
private java.util.List<java.lang.Runnable> |
onAbortTasks |
private java.util.List<java.lang.Runnable> |
onCommitTasks |
private static boolean |
runAsDaemon |
private CommitBarrier.Status |
status |
protected java.util.concurrent.locks.Condition |
statusCondition |
Constructor and Description |
---|
CommitBarrier(CommitBarrier.Status status,
boolean fair)
Creates a new CommitBarrier.
|
Modifier and Type | Method and Description |
---|---|
void |
abort()
Aborts this CommitBarrier.
|
protected void |
addJoiner()
Adds a waiters.
|
private long |
awaitNanosUninterruptible(long timeoutNs) |
void |
awaitOpen()
Awaits for this barrier to open (commit or abort).
|
void |
awaitOpenUninterruptibly()
Awaits for this barrier to open (commit or abort).
|
protected static void |
ensureNotDead(Txn tx,
java.lang.String operation)
Ensures that a transaction is not dead.
|
protected static void |
executeTasks(java.util.List<java.lang.Runnable> tasks)
Executes the tasks.
|
protected void |
finish(Txn tx)
Finishes a Txn.
|
int |
getNumberWaiting()
Returns the number of Transactions that have prepared and are waiting to commit.
|
protected CommitBarrier.Status |
getStatus() |
boolean |
isAborted()
Checks if this CommitBarrier already is aborted.
|
boolean |
isClosed()
Checks if this CommitBarrier is closed.
|
boolean |
isCommitted()
Checks if this CommitBarrier already is committed.
|
protected abstract boolean |
isLastParty() |
void |
joinCommit(Txn tx)
Joins this CommitBarrier with the provided transaction.
|
void |
joinCommitUninterruptibly(Txn tx)
Joins this CommitBarrier with the provided transaction.
|
void |
registerOnAbortTask(java.lang.Runnable task)
Registers a task that is executed once the CommitBarrier aborts.
|
void |
registerOnCommitTask(java.lang.Runnable task)
Registers a task that is executed once the CommitBarrier commits.
|
void |
setScheduledExecutorService(java.util.concurrent.ScheduledExecutorService executorService)
Sets the ScheduledExecutorService to be used by this CommitBarrier for the timeout.
|
void |
setTimeout(long timeout,
java.util.concurrent.TimeUnit unit)
Sets the timeout on this CommitBarrier.
|
protected java.util.List<java.lang.Runnable> |
signalAborted()
Only should be made when the lock is acquired.
|
protected java.util.List<java.lang.Runnable> |
signalCommit()
Only should be made when the lock is acquired.
|
boolean |
tryAwaitOpen(long timeout,
java.util.concurrent.TimeUnit unit)
Waits for this barrier to open (abort or commit).
|
boolean |
tryAwaitOpenUninterruptibly(long timeout,
java.util.concurrent.TimeUnit unit)
Tries to await the close of the barrier.
|
boolean |
tryJoinCommit(Txn tx)
Tries to joins this CommitBarrier with the provided transaction.
|
boolean |
tryJoinCommit(Txn tx,
long timeout,
java.util.concurrent.TimeUnit unit)
Tries to joins this CommitBarrier with the provided transaction.
|
boolean |
tryJoinCommitUninterruptibly(Txn tx,
long timeout,
java.util.concurrent.TimeUnit unit)
Tries to joins this CommitBarrier with the provided transaction.
|
private static int corePoolSize
private static boolean runAsDaemon
private static final java.util.concurrent.ScheduledThreadPoolExecutor EXECUTOR
private volatile java.util.concurrent.ScheduledExecutorService executorService
protected final java.util.concurrent.locks.Lock lock
protected final java.util.concurrent.locks.Condition statusCondition
private volatile CommitBarrier.Status status
private volatile int numberWaiting
private java.util.List<java.lang.Runnable> onAbortTasks
private java.util.List<java.lang.Runnable> onCommitTasks
public CommitBarrier(CommitBarrier.Status status, boolean fair)
status
- the initial status of the CommitBarrier.fair
- if waking up threads is going to be fair.java.lang.NullPointerException
- if status is null.protected final CommitBarrier.Status getStatus()
public final int getNumberWaiting()
public final boolean isClosed()
public final boolean isCommitted()
public final boolean isAborted()
protected final java.util.List<java.lang.Runnable> signalCommit()
protected final java.util.List<java.lang.Runnable> signalAborted()
public final void abort()
CommitBarrierOpenException
- if this CommitBarrier already is committed.protected static void executeTasks(java.util.List<java.lang.Runnable> tasks)
tasks
- the tasks to execute.public final void awaitOpen() throws java.lang.InterruptedException
java.lang.InterruptedException
- if the calling thread is interrupted while waiting.public final void awaitOpenUninterruptibly()
public final boolean tryAwaitOpen(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
timeout
- the maximum amount of time to wait for the barrier to close.unit
- the TimeUnit for the timeout argument.java.lang.InterruptedException
- if the thread is interrupted while waiting.java.lang.NullPointerException
- if unit is null.public final boolean tryAwaitOpenUninterruptibly(long timeout, java.util.concurrent.TimeUnit unit)
timeout
- the maximum amount of time to wait for the barrier to be closed.unit
- the timeunit for the timeout argument.private long awaitNanosUninterruptible(long timeoutNs)
public void setScheduledExecutorService(java.util.concurrent.ScheduledExecutorService executorService)
executorService
- the ScheduledExecutorService this CommitBarrier is going to use for timeout.java.lang.NullPointerException
- if executorService is null.public final void setTimeout(long timeout, java.util.concurrent.TimeUnit unit)
timeout
- the maximum amount of time this barrier is allowed to run.unit
- the TimeUnit of the timeout parameter.java.lang.NullPointerException
- if unit is null.CommitBarrierOpenException
- if the CommitBarrier already is aborted or committed.public final void registerOnAbortTask(java.lang.Runnable task)
task
- the task that is executed once the CommitBarrier commits.java.lang.NullPointerException
- if task is null.CommitBarrierOpenException
- if this CommitBarrier already is aborted or committed.public final void registerOnCommitTask(java.lang.Runnable task)
task
- the task that is executed once the CommitBarrier commits.java.lang.NullPointerException
- if task is null.CommitBarrierOpenException
- if this CommitBarrier already is aborted or committed.protected final void addJoiner()
java.lang.IllegalStateException
- if the transaction isn't closed.protected final void finish(Txn tx)
tx
- the transaction to finishprotected static void ensureNotDead(Txn tx, java.lang.String operation)
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.DeadTxnException
- if tx is dead.java.lang.NullPointerException
- if tx is null.public void joinCommit(Txn tx) throws java.lang.InterruptedException
tx
- the Txn to commit.java.lang.InterruptedException
- if the thread is interrupted while waiting.java.lang.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.public void joinCommitUninterruptibly(Txn tx)
tx
- the Txn to join in the commit.java.lang.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.public boolean tryJoinCommit(Txn tx)
tx
- the Txn that wants to join the other parties to commit with.CommitBarrierOpenException
- if tx or this CountDownCommitBarrier is aborted or committed.java.lang.NullPointerException
- if tx is null.public boolean tryJoinCommit(Txn tx, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
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.CommitBarrierOpenException
- if tx or this CountDownCommitBarrier is aborted or committed.java.lang.NullPointerException
- if tx or unit is null is null.java.lang.InterruptedException
- if the calling thread is interrupted while waiting.public boolean tryJoinCommitUninterruptibly(Txn tx, long timeout, java.util.concurrent.TimeUnit unit)
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.CommitBarrierOpenException
- if tx or this CountDownCommitBarrier is aborted or committed.java.lang.NullPointerException
- if tx or unit is null is null.protected abstract boolean isLastParty()