P
- type of the peer's statuspublic class PubSubPeerMonitor<P> extends Object implements PeerMonitor<P>
PeerMonitor
that uses a
PublishSubscribeService
to distribute the status across the
cluster.
It is highly recommended to use a type unique to the application
for the peer's class (Create a class that represent the peer). If
two different applications created PubSubPeerMonitor
's for a common
peer type, they both would receive updates from both applications which
will mislead the peer status.
Implementation overview:
updateLocalPeer(Object)
is called a messaged is
send with such status so all remote peers received the update.PeerListener.onUpdate(Node, Object)
is notified.ClusterListener.onJoin(Node)
event is received, a
recurrent task is scheduled to send a Peer Request
message until
the status for such node is received. This is where
PublishSubscribeService
does not behaves well because all
PubSubPeerMonitor
instances on all nodes will receive the request
even though it is meant for a single PubSubPeerMonitor
.
PubSubPeerMonitor
's which are not the target of the request will
discard the message. The node that joined the cluster will receive multiple
requests (one for each active node) and thus it will reply several times
with the same status which will be received also several times for each
active node.ClusterListener.onLeave(Node)
event is received,
PeerListener.onBecomingUnknown(Node, Object)
is notified.Modifier and Type | Method and Description |
---|---|
static <P> Restrictable<PeerMonitor<P>,Stoppable> |
create(Class<P> peerClass,
CoordinationService coordinationService,
ThreadExecutor threadExecutor,
org.slf4j.Logger logger)
Crates a peer monitor service.
|
static <P> Restrictable<PeerMonitor<P>,Stoppable> |
create(Class<P> peerClass,
Serializer<P> peerSerializer,
CoordinationService coordinationService,
ThreadExecutor threadExecutor,
org.slf4j.Logger logger)
Crates a peer monitor service.
|
Collection<Pair<Node,P>> |
getKnownPeers()
Gets known peers (including the local peer if its initial state has
been reported via
PeerMonitor.updateLocalPeer(Object) ) which are the peers
for which the state is considered up to date. |
Collection<Pair<Node,P>> |
getUnknownPeers()
Gets unknown peers (including the local peer if its initial state has
not been reported via
PeerMonitor.updateLocalPeer(Object) ) which are the
peers for which the state is considered unknown or out of date: These
are peers that have never joined the cluster, peers that have not
reported status after they left, or peers that previously reported
status but the node they belong to are not part of
ClusterService.getActiveNodes() . |
void |
register(PeerListener<P> observer)
Registers an observer (listener).
|
void |
unregister(PeerListener<P> observer)
Unregisters an observer (listener).
|
void |
updateLocalPeer(P localPeer)
Updates the state of the local peer.
|
public static <P> Restrictable<PeerMonitor<P>,Stoppable> create(Class<P> peerClass, CoordinationService coordinationService, ThreadExecutor threadExecutor, org.slf4j.Logger logger) throws NullPointerException
peerClass
- peer's classcoordinationService
- coordination servicethreadExecutor
- thread executorlogger
- loggerPeerMonitor
NullPointerException
- if either coordinationService
or
threadExecutor
or logger
is null
public static <P> Restrictable<PeerMonitor<P>,Stoppable> create(Class<P> peerClass, Serializer<P> peerSerializer, CoordinationService coordinationService, ThreadExecutor threadExecutor, org.slf4j.Logger logger) throws NullPointerException
peerClass
- peer's classpeerSerializer
- peer serializercoordinationService
- coordination servicethreadExecutor
- thread executorlogger
- loggerPeerMonitor
NullPointerException
- if either coordinationService
or
threadExecutor
or logger
is null
public void register(PeerListener<P> observer)
Observable
register
in interface Observable<PeerListener<P>>
observer
- observer to registerpublic void unregister(PeerListener<P> observer)
Observable
unregister
in interface Observable<PeerListener<P>>
observer
- observer to unregisterpublic void updateLocalPeer(P localPeer)
PeerMonitor
This method should be called when the initial state of the local peer is available, and after that when such state changes.
updateLocalPeer
in interface PeerMonitor<P>
localPeer
- local peerpublic Collection<Pair<Node,P>> getKnownPeers()
PeerMonitor
PeerMonitor.updateLocalPeer(Object)
) which are the peers
for which the state is considered up to date. These are peers that have
reported status and for which the node they belong to are part of
ClusterService.getActiveNodes()
. Note that a peer is only known
if it has reported status after joining the cluster; Thus, a node that
is part of ClusterService.getActiveNodes()
is not guaranteed to
be known.getKnownPeers
in interface PeerMonitor<P>
public Collection<Pair<Node,P>> getUnknownPeers()
PeerMonitor
PeerMonitor.updateLocalPeer(Object)
) which are the
peers for which the state is considered unknown or out of date: These
are peers that have never joined the cluster, peers that have not
reported status after they left, or peers that previously reported
status but the node they belong to are not part of
ClusterService.getActiveNodes()
.
The peer's status included in the result is the last known if
available, otherwise null
.
getUnknownPeers
in interface PeerMonitor<P>
Copyright © 2015. All Rights Reserved.