public class OpOrder
extends java.lang.Object
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();
}
}
}
Modifier and Type | Class and Description |
---|---|
class |
OpOrder.Barrier
This class represents a synchronisation point providing ordering guarantees on operations started
against the enclosing OpOrder.
|
static class |
OpOrder.Group
Represents a group of identically ordered operations, i.e.
|
Constructor and Description |
---|
OpOrder() |
Modifier and Type | Method and Description |
---|---|
void |
awaitNewBarrier() |
OpOrder.Group |
getCurrent() |
OpOrder.Barrier |
newBarrier()
Creates a new barrier.
|
OpOrder.Group |
start()
Start an operation against this OpOrder.
|
public OpOrder.Group start()
public OpOrder.Barrier newBarrier()
public OpOrder.Group getCurrent()
public void awaitNewBarrier()
Copyright © 2016 The Apache Software Foundation