Class ViewStateManagerImpl

java.lang.Object
org.apache.sling.discovery.commons.providers.base.ViewStateManagerImpl
All Implemented Interfaces:
ViewStateManager

public class ViewStateManagerImpl extends Object implements ViewStateManager
The ViewStateManager is at the core of managing TopologyEventListeners, the 'view state' (changing vs changed) and sending out the appropriate and according TopologyEvents to the registered listeners.

Note re synchronization: this class rquires a lock object to be passed in the constructor - this will be applied to all public methods appropriately. Additionally, the ClusterSyncService callback will also be locked using the provided lock object.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Lock
    The lock object with which all public methods are guarded - to be provided in the constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    bind(TopologyEventListener eventListener)
    Binds the given eventListener, sending it an INIT event if applicable.
    protected boolean
    Checks if the previouesView is equal to the newView, ignoring the syncToken (but only if the newView has partially started instances).
    protected boolean
     
    void
    Handles activation - ie marks this manager as activated thus the TOPOLOGY_INIT event can be sent to already bound listeners and subsequent calls to handleChanging/handleNewView will result in according/appropriate TOPOLOGY_CHANGING/ TOPOLOGY_CHANGED events.
    void
    Handles the fact that some (possibly early) indicator of a change in a topology has been detected and that a new view is being agreed upon (whatever that means, be it voting or similar).
    void
    Must be called when the corresponding service (typically a DiscoveryService implementation) is deactivated.
    void
    Handles the fact that a new view became true/established and sends out TOPOLOGY_INIT to uninitialized listeners and TOPOLOGY_CHANGED to already initialized listeners (in the latter case, also sends a TOPOLOGY_CHANGING if that has not yet been done)
    void
    installMinEventDelayHandler(DiscoveryService discoveryService, Scheduler scheduler, long minEventDelaySecs)
    Installs an optional 'min event delay handler' which, using the given scheduler, delays sending TOPOLOGY_CHANGED event after receiving a handleNewView - with the idea as to limit the number of toggling between view states.
    protected boolean
     
    boolean
    Unbinds the given eventListener, returning whether or not it was bound at all.
    protected boolean
     
    int
    waitForAsyncEvents(long timeout)
    for testing only: wait for any potentially queued async events to be delivered before returning.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • lock

      protected final Lock lock
      The lock object with which all public methods are guarded - to be provided in the constructor.
  • Method Details

    • installMinEventDelayHandler

      public void installMinEventDelayHandler(DiscoveryService discoveryService, Scheduler scheduler, long minEventDelaySecs)
      Description copied from interface: ViewStateManager
      Installs an optional 'min event delay handler' which, using the given scheduler, delays sending TOPOLOGY_CHANGED event after receiving a handleNewView - with the idea as to limit the number of toggling between view states.
      Specified by:
      installMinEventDelayHandler in interface ViewStateManager
    • hadPreviousView

      protected boolean hadPreviousView()
    • unchanged

      protected boolean unchanged(BaseTopologyView newView)
    • bind

      public void bind(TopologyEventListener eventListener)
      Description copied from interface: ViewStateManager
      Binds the given eventListener, sending it an INIT event if applicable.
      Specified by:
      bind in interface ViewStateManager
      Parameters:
      eventListener - the eventListener that is to bind
    • unbind

      public boolean unbind(TopologyEventListener eventListener)
      Description copied from interface: ViewStateManager
      Unbinds the given eventListener, returning whether or not it was bound at all.
      Specified by:
      unbind in interface ViewStateManager
      Parameters:
      eventListener - the eventListner that is to unbind
      Returns:
      whether or not the listener was added in the first place
    • handleActivated

      public void handleActivated()
      Description copied from interface: ViewStateManager
      Handles activation - ie marks this manager as activated thus the TOPOLOGY_INIT event can be sent to already bound listeners and subsequent calls to handleChanging/handleNewView will result in according/appropriate TOPOLOGY_CHANGING/ TOPOLOGY_CHANGED events.
      Specified by:
      handleActivated in interface ViewStateManager
    • handleDeactivated

      public void handleDeactivated()
      Description copied from interface: ViewStateManager
      Must be called when the corresponding service (typically a DiscoveryService implementation) is deactivated.

      Will mark this manager as deactivated and flags the last available view as not current.

      Specified by:
      handleDeactivated in interface ViewStateManager
    • handleChanging

      public void handleChanging()
      Description copied from interface: ViewStateManager
      Handles the fact that some (possibly early) indicator of a change in a topology has been detected and that a new view is being agreed upon (whatever that means, be it voting or similar).

      Will send out TOPOLOGY_CHANGING to all initialized listeners.

      Specified by:
      handleChanging in interface ViewStateManager
    • handleNewView

      public void handleNewView(BaseTopologyView newView)
      Description copied from interface: ViewStateManager
      Handles the fact that a new view became true/established and sends out TOPOLOGY_INIT to uninitialized listeners and TOPOLOGY_CHANGED to already initialized listeners (in the latter case, also sends a TOPOLOGY_CHANGING if that has not yet been done)
      Specified by:
      handleNewView in interface ViewStateManager
      Parameters:
      newView - the new, established view true if we were either in changing mode or the newView was different from the previous one.
    • equalsIgnoreSyncToken

      protected boolean equalsIgnoreSyncToken(BaseTopologyView newView)
      Checks if the previouesView is equal to the newView, ignoring the syncToken (but only if the newView has partially started instances).

      This caller of this method must ensure to be in a lock.lock() block

    • onlyDiffersInProperties

      protected boolean onlyDiffersInProperties(BaseTopologyView newView)
    • waitForAsyncEvents

      public int waitForAsyncEvents(long timeout)
      Description copied from interface: ViewStateManager
      for testing only: wait for any potentially queued async events to be delivered before returning.

      Specified by:
      waitForAsyncEvents in interface ViewStateManager
      Parameters:
      timeout - time in millis to wait for at max - 0 to not wait at all - -1 to wait indefinitely
      Returns:
      0 if no more async events exist, or the number of queued or in-flight (being sent) events if the timeout hit early