Class ServiceStateTracker
- java.lang.Object
-
- de.aristaflow.adept2.core.registry.cluster.ServiceStateTracker
-
public final class ServiceStateTracker extends Object
This class resembles the service state tracking ofClientService
. 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 theClientService
may have a different lifecycle and keep on running while the platform restarts. When restarting the platform and thus theClientService
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 theClientService
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 theURI[]
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).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
ServiceStateTracker.PlatformAvailabilityNotifier
A callable retrieving all listeners registered for platform state and submitting the corresponding notifications about the (un-)availability of the platform.protected class
ServiceStateTracker.PlatformAvailabilityTracker
A thread tracking the availability of the platform.static interface
ServiceStateTracker.PlatformStateListener
Listener for events that the availability of the platform within the cluster node has changed.class
ServiceStateTracker.ServiceAvailabilityTracker
A tracker for the availability of a specificADEPT2Service
identified by itsURI[]
.
-
Field Summary
Fields Modifier and Type Field Description protected Map<ArrayKey<URI>,ServiceStateTracker.ServiceAvailabilityTracker>
availTracker
The listeners registered for the availability of anADEPT2Service
and the corresponding service availability tracker.protected ClusterNodeState
cns
The cluster node state for the platform availability tracker.protected Logger
logger
The logger for problems occurring with the asynchronous notifications.protected LimitedPipelineExecutor<Object>
platformAvailabilityNotifier
The executor for notifying the registered platform state listeners.protected Thread
platformAvailabilityTracker
The thread tracking the availability of the platform by polling the cluster node state.protected AbstractAuthenticatedService
platformService
The service used in the injected part of the cluster node/ClusterService
.protected long
pollRate
The rate in milliseconds at which to poll the availability of servicesserviceAvailibilityChecker
.protected int
poolSize
The amount of threads used for theserviceAvailibilityChecker
.protected Collection<ServiceStateTracker.PlatformStateListener>
psListeners
The listeners registered for changes in the platform availability.protected HashSet<Status>
relevantStatuses
The statuses which are relevant for the (un-)availability of the platform.protected ServiceAccess
servAccess
The access for services within the platform used to retrieve the polled services.protected ScheduledExecutorService
serviceAvailibilityChecker
The thread pool for checking the availability of services and notifying changes to the registered listeners.
-
Constructor Summary
Constructors Constructor Description ServiceStateTracker(int servAvailPoolSize, long servAvailPollRate, ClusterNodeState cns)
Creates a new service state tracker for a cluster node.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addPlatformStateListener(ServiceStateTracker.PlatformStateListener psl)
Registers the designated listener for changes in the availability of the platform.boolean
addServiceStateListener(URI[] service, Class<? extends ADEPT2Service> servClass, ClientService.ServiceStateListener listener)
This is similar toClientService.addServiceStateListener(ADEPT2Service, ServiceStateListener)
except that it uses theURI[]
of the service and the service class instead of the service instance.void
join()
Joins the thread tracking the platform availability.void
removePlatformStateListener(ServiceStateTracker.PlatformStateListener psl)
Removes the designated listener from changes in the availability of the platform.void
removeServiceStateListener(URI[] service, ClientService.ServiceStateListener listener)
This is similar toClientService.removeServiceStateListener(ADEPT2Service, ServiceStateListener)
except that it uses theURI[]
of the service instead of the service instance.void
shutdown()
Signals the shutting down of the platform of the cluster node.void
start(AbstractAuthenticatedService pService, ServiceAccess sAccess)
Starts this service state tracker each time the platform becomes available (again).
-
-
-
Field Detail
-
logger
protected final Logger logger
The logger for problems occurring with the asynchronous notifications.
-
poolSize
protected final int poolSize
The amount of threads used for theserviceAvailibilityChecker
.
-
pollRate
protected final long pollRate
The rate in milliseconds at which to poll the availability of servicesserviceAvailibilityChecker
.
-
availTracker
protected final Map<ArrayKey<URI>,ServiceStateTracker.ServiceAvailabilityTracker> availTracker
The listeners registered for the availability of anADEPT2Service
and the corresponding service availability tracker.
-
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.
-
psListeners
protected final Collection<ServiceStateTracker.PlatformStateListener> psListeners
The listeners registered for changes in the platform availability.
-
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 theserviceAvailibilityChecker
and the #platformAvailabilityNotifier
.servAvailPollRate
- The rate in milliseconds at which to poll the availability of servicesserviceAvailibilityChecker
.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 toClientService.addServiceStateListener(ADEPT2Service, ServiceStateListener)
except that it uses theURI[]
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
- TheURI[]
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.
-
removeServiceStateListener
public void removeServiceStateListener(URI[] service, ClientService.ServiceStateListener listener)
This is similar toClientService.removeServiceStateListener(ADEPT2Service, ServiceStateListener)
except that it uses theURI[]
of the service instead of the service instance.- Parameters:
service
- TheURI[]
of the service which to no longer poll.listener
- The listener which to no longer notify in case of service availability changes.
-
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, anInterruptedException
will be thrown and the thread will not be joined.
-
-