Class OpOrder

java.lang.Object
org.apache.cassandra.utils.concurrent.OpOrder

public class OpOrder extends 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();
            }
        }
    }
 
 
  • Constructor Details

    • OpOrder

      public OpOrder()
  • Method Details

    • start

      public OpOrder.Group 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

      public OpOrder.Barrier 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

      public OpOrder.Group getCurrent()
    • awaitNewBarrier

      public void awaitNewBarrier()