Class PresenceKvWatcher

java.lang.Object
io.deephaven.enterprise.etcdclient.presence.PresenceKvWatcher

public class PresenceKvWatcher extends Object

This class monitors an etcd KV via an etcd watcher. The KV is expected to have, as a value, a string where every line (seprated by a '\n' character) represents an entry in a String to String map: each line is expected to be of the form "key=value" Any space leading or trailing the line, or any space around the equal sign, is ignored.

Users of PresenceKvWatcher pass an etcd key and a listener and receive updates when the KV changes, or the KV is deleted. The update takes the form of a Map<String, String>; in the case the KV is deleted the map is empty.

  • Constructor Details

    • PresenceKvWatcher

      public PresenceKvWatcher(@NotNull @NotNull String who, @NotNull @NotNull Consumer<String> logConsumer, @NotNull @NotNull io.deephaven.shadow.jetcd.io.etcd.jetcd.Client client, @NotNull @NotNull io.deephaven.shadow.jetcd.io.etcd.jetcd.ByteSequence key, @NotNull @NotNull PresenceKvWatcher.PresenceKvListener presenceKvListener, boolean isPrefix, long activeWatchDeadlinePeriodMillis)

      Create a PresenceKvWatcher object without starting its watcher yet.

      Note: We take a log consumer instead of having a Logger because this class may be used from clients with different logging backends, eg, a Core+ worker.

      Note the updates to the provided PresenceKvListener are executed over shared threads (either underlying etcd infrastructure threads or a shared timer thread). As such, is critical that the listener update call returns quickly; anything minimally computationally expensive should be done to the side, as work dispatched in a separate thread while the update call itself can return quickly.

      Parameters:
      who - String identifying the caller, used for logging actions on this watcher
      logConsumer - a method to use for logging
      client - a jetcd Client where the watcher will be created
      key - the etcd key to watch
      presenceKvListener - the listener that will receive updates once this object is started.
      activeWatchDeadlinePeriodMillis - time in milliseconds to wait for the successful (re)creation of the watch. This is the deadline time for the initial etcd GET request. The caller will be notified of update events with a delete when the watcher is being restarted, and should have its own timeout for an update after a delete to consider it failed. Similarly, the caller should have a check with a timer for the initial update.
  • Method Details

    • key

      public io.deephaven.shadow.jetcd.io.etcd.jetcd.ByteSequence key()
    • startWatch

      public void startWatch()
      Start the watcher for the KV of interest. This method will either return and an initial update would have been dispatched, or an exception would have been thrown. The associated map will contain data for the current value or will be empty if the KV does not exist at this time.
      Throws:
      IllegalStateException - if this object is already closed or a watcher has been already started.
      RuntimeException - if the initial get failed or it failed to obtain a value after activeWatchDeadlinePeriodMillis
    • closeAsync

      public void closeAsync()

      Close this watcher. Any associated active etcd watcher will be closed, but that may happen asynchronously with respect to this call.

      The object becomes invalidated; calling close more than once is legal and has no ill effects, but no other methods should be called.

      Note since updates are delivered asynchronously, it is possible to receive updates after close is called.

    • stringValueToMap

      public static Map<String,String> stringValueToMap(@Nullable @Nullable String valueStr, @NotNull @NotNull Consumer<String> log)
      Converts lines of the form "key=value" into a Map<String, String>. Lines starting with '#' are ignored as comments, and lines not conforming to the expected format are logged and ignored.
      Parameters:
      valueStr -
      log -
      Returns: