Class ActiveElectionManager
java.lang.Object
com.gigaspaces.cluster.activeelection.core.ActiveElectionManager
ActiveElectionManager based on distributed naming service
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();
}
- Since:
- 6.0
- Version:
- 1.0
- Author:
- Igor Goldenberg
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionActiveElectionManager(String nodeName, Object service, ServiceTemplate participantSrvTemplate, INamingService namingSrv, IActiveElectionListener activeElectionListener, IActiveElectionDecisionFilter decisionFilter, ActiveElectionConfig config, ClusterFailureDetector clusterFailureDetector, SplitBrainRecoveryHolder splitBrainRecoveryHolder, AtomicBoolean recoveryIndicator) Constructs an instance of this class that will addActiveElectionStateto the set of service attributes see:#addNamingAttributes(Object, Entry[]). -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanchangeState(ActiveElectionState.State oldState, ActiveElectionState.State newState, boolean force) Change the service state on naming service.protected booleanconfirmPendingStateRegistration(int retryCount) Confirmation PENDING state registration on naming service.voidelect()Elect the active service, this method will be blocked while the active service will be elected.protected ServiceItemFind an ACTIVE service with the election template.voidForcefully make this space to be primarygetState()protected booleanisAdvanceToStateAllowed(ActiveElectionState.State currentState, ActiveElectionState.State acquireState) Request to advance to the new state.protected booleanprotected booleanprotected List<ServiceItem>lookup(ActiveElectionState.State state, int maxMatches) Lookup the services with desired state.protected voidnotifyListenerOnActive(ServiceItem activeService) notify theIActiveElectionListenerabout elected ACTIVE service or an ACTIVE on split-brain occurrence.voidonActiveDiscoveryCheckExtraBackup(ServiceItem activeServiceItem) A backup Space verifies that it is a replication target of an active primary Space.protected voidThe callback method fromActiveFailureDetectoron active failure.protected voidonSplitBrain(List<ServiceItem> splitActives) The callback method fromSplitBrainControlleron 2 or more identified ACTIVE services.protected ServiceItempollStateUntilAvailable(ActiveElectionState.State pollingState) Poll the desired service state on naming service while available, when thepollingStateis not available make an attempt to find an ACTIVE state, if ACTIVE was found notify theIActiveElectionListenerand return the active service.voidreelect()Re-elect the primary space.protected voidStart the split-brain controller which controls the collision of 2 or more ACTIVE services.voidTerminate the ActiveElectionManager.toString()
-
Constructor Details
-
ActiveElectionManager
public ActiveElectionManager(String nodeName, Object service, ServiceTemplate participantSrvTemplate, INamingService namingSrv, IActiveElectionListener activeElectionListener, IActiveElectionDecisionFilter decisionFilter, ActiveElectionConfig config, ClusterFailureDetector clusterFailureDetector, SplitBrainRecoveryHolder splitBrainRecoveryHolder, AtomicBoolean recoveryIndicator) throws ActiveElectionException Constructs an instance of this class that will addActiveElectionStateto the set of service attributes see:#addNamingAttributes(Object, Entry[]).- Parameters:
service- the service candidate which a participate in active election.
NOTE: the service must provide consistentequals()method implementation or implementReferentUuidinterface.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.- Throws:
ActiveElectionException- Failed to initialize ActiveElectionManager, naming service communication problem.
-
-
Method Details
-
isSpaceInRecovery
protected boolean isSpaceInRecovery()- Returns:
- true if backup Space is still in recovery or false if recovery completed.
-
confirmPendingStateRegistration
Confirmation PENDING state registration on naming service.- Returns:
trueif registration confirmed, otherwisefalse.- Throws:
ActiveElectionException- failed to confirm, the process might be interrupted.
-
lookup
protected List<ServiceItem> lookup(ActiveElectionState.State state, int maxMatches) throws InterruptedException Lookup the services with desired state.- Parameters:
state- The state to find.maxMatches- maximum number of matched services.- Returns:
- The matched services or
nullif no service was found. - Throws:
InterruptedException
-
changeState
protected boolean changeState(ActiveElectionState.State oldState, ActiveElectionState.State newState, boolean force) throws RemoteException Change the service state on naming service.- Parameters:
oldState- oldState the old service state.newState- newState the new service state.force-trueif 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.- Throws:
RemoteException- Failed to change state on naming-service, the reason might be a network problem.
-
pollStateUntilAvailable
protected ServiceItem pollStateUntilAvailable(ActiveElectionState.State pollingState) throws InterruptedException Poll the desired service state on naming service while available, when thepollingStateis not available make an attempt to find an ACTIVE state, if ACTIVE was found notify theIActiveElectionListenerand return the active service.- Parameters:
pollingState- the polling service state.- Returns:
- The ACTIVE service or
nullif ACTIVE was not found and polling state is not available on naming service.. - Throws:
InterruptedException
-
isAdvanceToStateAllowed
protected boolean isAdvanceToStateAllowed(ActiveElectionState.State currentState, ActiveElectionState.State acquireState) throws RemoteException, InterruptedException Request to advance to the new state. This method collects all candidates with currentState and only ifIActiveElectionDecisionFilter.isAcceptable(com.gigaspaces.cluster.activeelection.core.ActiveElectionState.State, java.util.List<net.jini.core.lookup.ServiceItem>)returntruethis manage service can advance to theacquireStatestate.- Parameters:
currentState- the current state.acquireState- the requesting state to acquire.- Returns:
truethe manage service can advance the state from currentState to acquireState,falseanother candidate should advance first, or current state not in lookup.- Throws:
RemoteException- Failed to communicate with Naming Service.InterruptedException- current thread was interrupted.
-
findActive
Find an ACTIVE service with the election template. If an active was found this service:
1. Register to theActiveFailureDetector, so that to get notification on active failure.
2. Notify theIActiveElectionListenerabout 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.- Returns:
- the active service or
nullactive not found. - Throws:
InterruptedException- throws when operation was interrupted.
-
elect
Elect the active service, this method will be blocked while the active service will be elected.- Throws:
InterruptedException- thrown when operation was interrupted.ActiveElectionException- failed to elect active service, might beINamingServicecommunication.
-
forceMoveToPrimary
Forcefully make this space to be primary- Throws:
RemoteException
-
notifyListenerOnActive
notify theIActiveElectionListenerabout elected ACTIVE service or an ACTIVE on split-brain occurrence. -
onSplitBrain
protected void onSplitBrain(List<ServiceItem> splitActives) throws RemoteException, InterruptedException, ActiveElectionException The callback method fromSplitBrainControlleron 2 or more identified ACTIVE services. This method should resolve the split-brain and only 1 ACTIVE service should stay in the network.- Throws:
RemoteException- Failed to communicate with naming service.ActiveElectionException- Election failedInterruptedException- on split brain resolution was interrupted.
-
onActiveDiscoveryCheckExtraBackup
A backup Space verifies that it is a replication target of an active primary Space. If the primary Space is already connected to a backup by the same name, but a different service ID then this backup should move to stopped state and be declared 'unhealthy'.- Parameters:
activeServiceItem- the primary service received from the LUS
-
onActiveFailure
The callback method fromActiveFailureDetectoron active failure.- Throws:
ActiveElectionException- Failed to elect new Active.InterruptedException- operation was interrupted.
-
startSplitBrainController
Start the split-brain controller which controls the collision of 2 or more ACTIVE services.- Throws:
RemoteException
-
terminate
public void terminate()Terminate the ActiveElectionManager. -
getState
- Returns:
- the current state.
-
getConfig
- Returns:
- active election configuration.
-
isTerminate
protected boolean isTerminate()- Returns:
trueif the manager should terminate, otherwisefalse.
-
toString
-
reelect
Re-elect the primary space. The elector state is reset and election is performed -
getClusterFailureDetector
-