Class PerKeyWriteSequencer<K,W>

java.lang.Object
io.github.flink.gcp.connector.base.sink.PerKeyWriteSequencer<K,W>
Type Parameters:
K - the key a write is ordered by
W - the write

@Internal public final class PerKeyWriteSequencer<K,W> extends Object
Admits one write per key at a time and holds the rest of that key's writes in arrival order.

A key is active from the admission of a write until the caller releases it with nothing held. A write for an active key is held behind the earlier ones, and a release hands back the oldest held write, which then occupies the key in its turn. Writes for other keys are not affected.

Nothing here knows what a write is or when it is answered; the writer decides both and calls this sequencer from its task thread only, so the sequencer is not thread-safe. The Firestore and Bigtable sinks share it (#1618); a key's toString appears in the invariant failures below, so a sink whose keys are user data supplies a key type that does not render them.

  • Constructor Details

    • PerKeyWriteSequencer

      public PerKeyWriteSequencer()
  • Method Details

    • tryAdmit

      public boolean tryAdmit(K key)
      Admits a write if its key is not active, making the key active.
      Returns:
      true if the write may be sent now; false if its key is active, when the caller must hold(Object, Object) it instead
    • hold

      public void hold(K key, W write)
      Holds a write behind its active key's earlier writes.
      Throws:
      IllegalStateException - if the key is not active
    • release

      @Nullable public W release(K key)
      Ends the turn of the key's current write.
      Returns:
      the key's oldest held write, which now occupies the key and may be sent, or null if none is held, which makes the key inactive
      Throws:
      IllegalStateException - if the key is not active: a write was released twice, or one never admitted was released
    • clear

      public void clear()
      Forgets every key and held write.