public class ActiveElectionManager extends Object
INamingService
and provides a distributed active election algorithm to select only one active service
between all candidates. NOTE: The manage service must provide consistent
equals()
method implementation or implement ReferentUuid
interface.
Example:
public class ServiceIDOrderFilter
implements IActiveElectionDecisionFilter
{
ServiceID _srvID;
public boolean isAcceptable(ActiveElectionState.State advanceState, List
candidateSrv )
{
for( ServiceItem srvItem : candidateSrv )
{
// return true if _srvID is the greater from all candidateSrv.serviceID;
}
}
}
public class ElectionListener
implements IActiveElectionListener
{
ActiveElectionManager _electManager;
public void onActive( ActiveElectionEvent event )
{
System.out.println("OnActive -> Elected Active: " + event.getActiveServiceItem() );
if ( _electManager.getState() == ActiveElectionState.State.ACTIVE )
System.out.println("The manage service is an ACTIVE");
else
System.out.println("The manage service is an NOT an ACTIVE");
}
public void onSplitBrain( ActiveElectionEvent event )
{
System.out.println("OnSplit -> Elected Active: " + event.getActiveServiceItem() ); *
}
}
public static void main( String[] args )
{
IJSpace space;
ServiceTemplate srvTemplate;
INamingService lookupSrv = new LookupNamingService();
ElectionListener listener = new ElectionListener();
ServiceIDOrderFilter filter = new ServiceIDOrderFilter();
ActiveElectionManager electManager = new ActiveElectionManager( space, srvTemplate,
lookupSrv, listener, filter );
electManager.elect();
}
IActiveElectionDecisionFilter
Constructor and Description |
---|
ActiveElectionManager(String nodeName,
Object service,
ServiceTemplate participantSrvTemplate,
INamingService namingSrv,
IActiveElectionListener activeElectionListener,
IActiveElectionDecisionFilter decisionFilter,
ActiveElectionConfig config,
ClusterFailureDetector clusterFailureDetector,
SplitBrainRecoveryHolder splitBrainRecoveryHolder)
Constructs an instance of this class that will add
ActiveElectionState
to the set of service attributes see: #addNamingAttributes(Object, Entry[]) . |
Modifier and Type | Method and Description |
---|---|
protected boolean |
changeState(ActiveElectionState.State oldState,
ActiveElectionState.State newState,
boolean force)
Change the service state on naming service.
|
protected boolean |
confirmPendingStateRegistration(int retryCount)
Confirmation PENDING state registration on naming service.
|
void |
elect()
Elect the active service, this method will be blocked while the active service will be
elected.
|
protected ServiceItem |
findActive()
Find an ACTIVE service with the election template.
|
void |
forceMoveToPrimary()
Forcefully make this space to be primary
|
ClusterFailureDetector |
getClusterFailureDetector() |
ActiveElectionConfig |
getConfig() |
ActiveElectionState.State |
getState() |
protected boolean |
isAdvanceToStateAllowed(ActiveElectionState.State currentState,
ActiveElectionState.State acquireState)
Request to advance to the new state.
|
protected boolean |
isTerminate() |
protected List<ServiceItem> |
lookup(ActiveElectionState.State state,
int maxMatches)
Lookup the services with desired state.
|
protected void |
notifyListenerOnActive(ServiceItem activeService)
notify the
IActiveElectionListener about
elected ACTIVE service or an ACTIVE on split-brain occurrence. |
void |
onActiveDiscoveryCheckExtraBackup(ServiceItem activeServiceItem)
A backup Space verifies that it is a replication target of an active primary Space.
|
protected void |
onActiveFailure()
The callback method from
ActiveFailureDetector
on active failure. |
protected void |
onSplitBrain(List<ServiceItem> splitActives)
The callback method from
SplitBrainController
on 2 or more identified ACTIVE services. |
protected ServiceItem |
pollStateUntilAvailable(ActiveElectionState.State pollingState)
Poll the desired service state on naming service while available, when the
pollingState is not available make an attempt to find an ACTIVE state, if ACTIVE
was found notify the IActiveElectionListener
and return the active service. |
void |
reelect()
Re-elect the primary space.
|
protected void |
startSplitBrainController()
Start the split-brain controller which controls the collision of 2 or more ACTIVE services.
|
void |
terminate()
Terminate the ActiveElectionManager.
|
String |
toString() |
public ActiveElectionManager(String nodeName, Object service, ServiceTemplate participantSrvTemplate, INamingService namingSrv, IActiveElectionListener activeElectionListener, IActiveElectionDecisionFilter decisionFilter, ActiveElectionConfig config, ClusterFailureDetector clusterFailureDetector, SplitBrainRecoveryHolder splitBrainRecoveryHolder) throws ActiveElectionException
ActiveElectionState
to the set of service attributes see: #addNamingAttributes(Object, Entry[])
.service
- the service candidate which a participate in active
election.equals()
method implementation or implement ReferentUuid
interface.participantSrvTemplate
- the template to match participant services with match
attributes set.namingSrv
- the naming service provider to lookup services, change states
and etc...activeElectionListener
- the listener implementation to receive notification about new
active candidate.decisionFilter
- the decision filter is call to get acceptance to advance new
state.ActiveElectionException
- Failed to initialize
ActiveElectionManager,
naming service
communication
problem.protected boolean confirmPendingStateRegistration(int retryCount) throws ActiveElectionException
true
if registration confirmed, otherwise false
.ActiveElectionException
- failed to confirm,
the process might
be interrupted.protected List<ServiceItem> lookup(ActiveElectionState.State state, int maxMatches) throws InterruptedException
state
- The state to find.maxMatches
- maximum number of matched services.null
if no service was found.InterruptedException
protected boolean changeState(ActiveElectionState.State oldState, ActiveElectionState.State newState, boolean force) throws RemoteException
oldState
- oldState the old service state.newState
- newState the new service state.force
- true
if the newState must be changed without throwing
RemoteException. This operation will continue with retrying until the change
state operation will be succeed or election manager was terminated.RemoteException
- Failed to change state on naming-service, the reason might
be a network problem.protected ServiceItem pollStateUntilAvailable(ActiveElectionState.State pollingState) throws InterruptedException
pollingState
is not available make an attempt to find an ACTIVE state, if ACTIVE
was found notify the IActiveElectionListener
and return the active service.pollingState
- the polling service state.null
if ACTIVE was not found and polling
state is not available on naming service..InterruptedException
protected boolean isAdvanceToStateAllowed(ActiveElectionState.State currentState, ActiveElectionState.State acquireState) throws RemoteException, InterruptedException
IActiveElectionDecisionFilter.isAcceptable(com.gigaspaces.cluster.activeelection.core.ActiveElectionState.State, java.util.List<net.jini.core.lookup.ServiceItem>)
return true
this manage service can advance to the acquireState
state.currentState
- the current state.acquireState
- the requesting state to acquire.true
the manage service can advance the state from currentState to
acquireState, false
another candidate should advance first, or current state not
in lookup.RemoteException
- Failed to communicate with Naming Service.InterruptedException
- current thread was interrupted.protected ServiceItem findActive() throws InterruptedException
ActiveFailureDetector
,
so that to get notification on active failure.IActiveElectionListener
about new active
service.
If split-brain has been identified( two or more active services were found), lookup again the
active while the split-bran will be resolved.null
active not found.InterruptedException
- throws when operation was interrupted.public void elect() throws InterruptedException, ActiveElectionException
InterruptedException
- thrown when operation
was interrupted.ActiveElectionException
- failed to elect
active service,
might be INamingService
communication.public void forceMoveToPrimary() throws RemoteException
RemoteException
protected void notifyListenerOnActive(ServiceItem activeService)
IActiveElectionListener
about
elected ACTIVE service or an ACTIVE on split-brain occurrence.protected void onSplitBrain(List<ServiceItem> splitActives) throws RemoteException, InterruptedException, ActiveElectionException
SplitBrainController
on 2 or more identified ACTIVE services. This method should resolve the split-brain and only
1 ACTIVE service should stay in the network.RemoteException
- Failed to communicate
with naming service.ActiveElectionException
- Election failedInterruptedException
- on split brain
resolution was
interrupted.public void onActiveDiscoveryCheckExtraBackup(ServiceItem activeServiceItem)
activeServiceItem
- the primary service received from the LUSprotected void onActiveFailure() throws InterruptedException, ActiveElectionException
ActiveFailureDetector
on active failure.ActiveElectionException
- Failed to elect
new Active.InterruptedException
- operation was
interrupted.protected void startSplitBrainController() throws RemoteException
RemoteException
public void terminate()
public ActiveElectionState.State getState()
public ActiveElectionConfig getConfig()
protected boolean isTerminate()
true
if the manager should terminate, otherwise false
.public void reelect() throws InterruptedException, ActiveElectionException, RemoteException
public ClusterFailureDetector getClusterFailureDetector()
Copyright © GigaSpaces.