Class OpOrder
java.lang.Object
org.apache.cassandra.utils.concurrent.OpOrder
A class for providing synchronization between producers and consumers that do not communicate directly with each other, but where the consumers need to process their work in contiguous batches. In particular this is useful for both CommitLog and Memtable where the producers (writing threads) are modifying a structure that the consumer (flush executor) only batch syncs, but needs to know what 'position' the work is at for co-ordination with other processes,
The typical usage is something like:
public final class ExampleShared
{
final OpOrder order = new OpOrder();
volatile SharedState state;
static class SharedState
{
volatile Barrier barrier;
// ...
}
public void consume()
{
SharedState state = this.state;
state.setReplacement(new State())
state.doSomethingToPrepareForBarrier();
state.barrier = order.newBarrier();
// seal() MUST be called after newBarrier() else barrier.isAfter()
// will always return true, and barrier.await() will fail
state.barrier.issue();
// wait for all producer work started prior to the barrier to complete
state.barrier.await();
// change the shared state to its replacement, as the current state will no longer be used by producers
this.state = state.getReplacement();
state.doSomethingWithExclusiveAccess();
}
public void produce()
{
try (Group opGroup = order.start())
{
SharedState s = state;
while (s.barrier != null && !s.barrier.isAfter(opGroup))
s = s.getReplacement();
s.doProduceWork();
}
}
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classThis class represents a synchronisation point providing ordering guarantees on operations started against the enclosing OpOrder.static final classRepresents a group of identically ordered operations, i.e. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCreates a new barrier.start()Start an operation against this OpOrder.
-
Constructor Details
-
OpOrder
public OpOrder()
-
-
Method Details
-
start
Start an operation against this OpOrder. Once the operation is completed Ordered.close() MUST be called EXACTLY once for this operation.- Returns:
- the Ordered instance that manages this OpOrder
-
newBarrier
Creates a new barrier. The barrier is only a placeholder until barrier.issue() is called on it, after which all new operations will start against a new Group that will not be accepted by barrier.isAfter(), and barrier.await() will return only once all operations started prior to the issue have completed. -
getCurrent
-
awaitNewBarrier
public void awaitNewBarrier()
-