Class ServiceStateTracker


  • public final class ServiceStateTracker
    extends Object
    This class resembles the service state tracking of ClientService. It also tracks the availability of the platform by polling the cluster node state. Both issues are separately implemented.

    The lifecycle of ClientService is coupled to the platform running on a cluster node. This platform may be restarted and may change between client and server role. However, service state listeners registered at the ClientService may have a different lifecycle and keep on running while the platform restarts. When restarting the platform and thus the ClientService the listeners will get lost and they will no longer receive notifications about a service. Additionally, they will not get notified about the restart of the ClientService where they would need to re-register.

    This class solves this issue by being coupled to the lifecycle of ClusterNode. Additionally, listeners do not register with a service object (which is also coupled to the lifecycle of the platform and not of the one of a cluster node) but with the URI[] of a service. This is usually the global service URI which is resolved transparently considering all cluster nodes. Therefore the service may change its providing cluster node without the service state listener needing to change.

    The availability of the platform will be tracked by a separate thread polling the cluster node state for changes. As soon as a changes occurs, it submits a runnable for notifying the registered listeners and immediately polls for a cluster node change again. Since this has to be outside of the platform lifecycle, the thread keeps on running while the cluster node exists. It stops as soon as the cluster node has terminated. The code bootstrapping the cluster node should also join() this thread. While the service state tracker may ignore the platform being unavailable, platform state listeners will be notified about every relevant change of the cluster node state. This may lead to consecutive notifications of the platform being unavailable (but the cluster node changing states while preparing the platform restart).

    • Field Detail

      • logger

        protected final Logger logger
        The logger for problems occurring with the asynchronous notifications.
      • pollRate

        protected final long pollRate
        The rate in milliseconds at which to poll the availability of services serviceAvailibilityChecker.
      • platformService

        protected AbstractAuthenticatedService platformService
        The service used in the injected part of the cluster node/ClusterService. This is used to track the state of the platform within the cluster node.
      • servAccess

        protected ServiceAccess servAccess
        The access for services within the platform used to retrieve the polled services.
      • serviceAvailibilityChecker

        protected volatile ScheduledExecutorService serviceAvailibilityChecker
        The thread pool for checking the availability of services and notifying changes to the registered listeners.
      • cns

        protected final ClusterNodeState cns
        The cluster node state for the platform availability tracker.
      • relevantStatuses

        protected final HashSet<Status> relevantStatuses
        The statuses which are relevant for the (un-)availability of the platform. Some intermediate statuses are not relevant.
      • platformAvailabilityNotifier

        protected final LimitedPipelineExecutor<Object> platformAvailabilityNotifier
        The executor for notifying the registered platform state listeners. This is a pipeline executor to avoid later state notifications to overtake older state notifications.
      • platformAvailabilityTracker

        protected final Thread platformAvailabilityTracker
        The thread tracking the availability of the platform by polling the cluster node state.
    • Constructor Detail

      • ServiceStateTracker

        public ServiceStateTracker​(int servAvailPoolSize,
                                   long servAvailPollRate,
                                   ClusterNodeState cns)
        Creates a new service state tracker for a cluster node.
        Parameters:
        servAvailPoolSize - The amount of threads used for the serviceAvailibilityChecker and the #platformAvailabilityNotifier.
        servAvailPollRate - The rate in milliseconds at which to poll the availability of services serviceAvailibilityChecker.
        cns - The cluster node state for the platform availability tracker.
    • Method Detail

      • start

        public void start​(AbstractAuthenticatedService pService,
                          ServiceAccess sAccess)
        Starts this service state tracker each time the platform becomes available (again).
        Parameters:
        pService - The service used in the injected part of the cluster node/ClusterService. This is used to track the state of the platform within the cluster node.
        sAccess - The access for services within the platform used to retrieve the polled services.
      • shutdown

        public void shutdown()
        Signals the shutting down of the platform of the cluster node. This removes access to the platform and terminates the executor service.
      • addServiceStateListener

        public boolean addServiceStateListener​(URI[] service,
                                               Class<? extends ADEPT2Service> servClass,
                                               ClientService.ServiceStateListener listener)
        This is similar to ClientService.addServiceStateListener(ADEPT2Service, ServiceStateListener) except that it uses the URI[] of the service and the service class instead of the service instance. This is due to the service instance to be subject of change in case the platform within the cluster node changes.
        Parameters:
        service - The URI[] of the service to poll.
        servClass - The class of the service to poll.
        listener - The listener which to notify in case of service availability changes.
        Returns:
        Whether the designated service is currently available.
      • addPlatformStateListener

        public boolean addPlatformStateListener​(ServiceStateTracker.PlatformStateListener psl)
        Registers the designated listener for changes in the availability of the platform.
        Parameters:
        psl - The listener notified of changes in the availability of the platform.
        Returns:
        Whether the platform is currently available.
      • removePlatformStateListener

        public void removePlatformStateListener​(ServiceStateTracker.PlatformStateListener psl)
        Removes the designated listener from changes in the availability of the platform.
        Parameters:
        psl - The listener which no longer to notify of changes in the availability of the platform.
      • join

        public void join()
                  throws InterruptedException
        Joins the thread tracking the platform availability. The shutdown procedure should call this to prevent the platform availability tracker from surviving the final termination.
        Throws:
        InterruptedException - If the current thread is interrupted while waiting for the platform availability tracker, an InterruptedException will be thrown and the thread will not be joined.