public class RaftService extends Object implements ManagedService, SnapshotAwareService<MetadataRaftGroupSnapshot>, GracefulShutdownAwareService, MembershipAwareService, CPSubsystemManagementService, PreJoinAwareService, RaftNodeLifecycleAwareService, MigrationAwareService, DynamicMetricsProvider
RaftNode
instances that run the Raft consensus algorithm
for the created CP groups. Also implements CP Subsystem management methods.Modifier and Type | Field and Description |
---|---|
static String |
SERVICE_NAME |
Constructor and Description |
---|
RaftService(NodeEngine nodeEngine) |
Modifier and Type | Method and Description |
---|---|
void |
applyUnsafeModeState(int partitionId,
UnsafeModePartitionState state) |
boolean |
awaitUntilDiscoveryCompleted(long timeout,
TimeUnit timeUnit)
Blocks until the CP discovery process is completed, or the given
timeout occurs, or the current thread is interrupted, whichever
happens first.
|
void |
beforeMigration(PartitionMigrationEvent event)
Called before migration process starts, on both source and destination members.
|
void |
commitMigration(PartitionMigrationEvent event)
Commits the migration process for this service, on both source and destination members.
|
boolean |
completeFutures(CPGroupId groupId,
Collection<Long> indices,
Object result)
Completes all futures registered with
indices
in the CP group associated with groupId . |
boolean |
completeFutures(CPGroupId groupId,
Collection<Map.Entry<Long,Object>> results)
Completes all futures registered with
indices
in the CP group associated with groupId . |
RaftGroupId |
createRaftGroupForProxy(String name) |
InternalCompletableFuture<CPGroupId> |
createRaftGroupForProxyAsync(String name) |
void |
createRaftNode(CPGroupId groupId,
Collection<RaftEndpoint> members) |
void |
destroyRaftNode(CPGroupId groupId) |
InternalCompletableFuture<Void> |
forceDestroyCPGroup(String groupName)
this method is idempotent
|
InternalCompletableFuture<Collection<CPGroupId>> |
getAllCPGroupIds() |
Collection<RaftNode> |
getAllRaftNodes() |
CPSubsystemConfig |
getConfig() |
InternalCompletableFuture<CPGroup> |
getCPGroup(CPGroupId groupId) |
InternalCompletableFuture<CPGroup> |
getCPGroup(String name)
Returns the active CP group with the given name.
|
InternalCompletableFuture<Collection<CPGroupId>> |
getCPGroupIds()
Returns all active CP group ids.
|
int |
getCPGroupPartitionId(CPGroupId groupId) |
static int |
getCPGroupPartitionId(CPGroupId groupId,
int partitionCount) |
InternalCompletableFuture<Collection<CPMember>> |
getCPMembers()
Returns the current list of CP members
|
static String |
getGroupNameForProxy(String name) |
RaftInvocationManager |
getInvocationManager() |
Collection<CPGroupId> |
getLeadedGroups() |
RaftEndpoint |
getLocalCPEndpoint() |
CPMemberInfo |
getLocalCPMember()
Returns the local CP member if this Hazelcast member is part of
CP Subsystem, returns null otherwise.
|
RaftGroupId |
getMetadataGroupId() |
MetadataRaftGroupManager |
getMetadataGroupManager() |
static String |
getObjectNameForProxy(String name) |
RaftNode |
getOrInitRaftNode(CPGroupId groupId) |
Operation |
getPreJoinOperation()
An operation to be executed on the joining member before it is set as joined.
|
RaftNode |
getRaftNode(CPGroupId groupId) |
void |
handleActiveCPMembers(RaftGroupId receivedMetadataGroupId,
long membersCommitIndex,
Collection<CPMemberInfo> members) |
void |
handleAppendEntries(CPGroupId groupId,
AppendRequest request,
RaftEndpoint target) |
void |
handleAppendResponse(CPGroupId groupId,
AppendFailureResponse response,
RaftEndpoint target) |
void |
handleAppendResponse(CPGroupId groupId,
AppendSuccessResponse response,
RaftEndpoint target) |
void |
handlePreVoteRequest(CPGroupId groupId,
PreVoteRequest request,
RaftEndpoint target) |
void |
handlePreVoteResponse(CPGroupId groupId,
PreVoteResponse response,
RaftEndpoint target) |
void |
handleSnapshot(CPGroupId groupId,
InstallSnapshot request,
RaftEndpoint target) |
void |
handleTriggerLeaderElection(CPGroupId groupId,
TriggerLeaderElection request,
RaftEndpoint target) |
void |
handleVoteRequest(CPGroupId groupId,
VoteRequest request,
RaftEndpoint target) |
void |
handleVoteResponse(CPGroupId groupId,
VoteResponse response,
RaftEndpoint target) |
void |
init(NodeEngine nodeEngine,
Properties properties)
Initializes this service.
|
boolean |
isCpSubsystemEnabled() |
boolean |
isDiscoveryCompleted()
Returns whether the CP discovery process is completed or not.
|
boolean |
isRaftGroupDestroyed(CPGroupId groupId) |
void |
memberAdded(MembershipServiceEvent event)
Invoked when a new member is added to the cluster.
|
void |
memberAttributeChanged(MemberAttributeServiceEvent event)
Invoked when a member attribute is changed.
|
void |
memberRemoved(MembershipServiceEvent event)
Invoked when an existing member leaves the cluster.
|
long |
nextUnsafeModeCommitIndex(CPGroupId groupId) |
void |
onRaftGroupDestroyed(CPGroupId groupId)
Called on the thread of the Raft group when the given Raft node is
destroyed, either gracefully or forcefully.
|
void |
onRaftNodeSteppedDown(CPGroupId groupId)
Called on the thread of the Raft group when the given Raft node is
stepped down, either because it is shutting down, or it could not be
added to the Raft group
|
boolean |
onShutdown(long timeout,
TimeUnit unit)
A hook method that's called during graceful shutdown to provide safety for data managed by this service.
|
Operation |
prepareReplicationOperation(PartitionReplicationEvent event)
Returns an operation to replicate service data and/or state for a specific partition replica
on another cluster member.
|
InternalCompletableFuture<Void> |
promoteToCPMember()
Promotes the local Hazelcast member to the CP role.
|
void |
provideDynamicMetrics(MetricTaggerSupplier taggerSupplier,
MetricsExtractor extractor)
Metrics collection callback that is called in every metric collection
cycle.
|
void |
registerUnsafeWaitingOperation(CPGroupId groupId,
long commitIndex,
Operation op) |
InternalCompletableFuture<Void> |
removeCPMember(UUID cpMemberUuid)
Removes the given unreachable CP member from the active CP members list
and all CP groups it belongs to.
|
void |
reset()
Resets this service back to its initial state.
|
InternalCompletableFuture<Void> |
restart()
Wipes and resets the whole CP Subsystem state and initializes it
as if the Hazelcast cluster is starting up initially.
|
void |
restartLocal(long seed) |
RaftNodeImpl |
restoreRaftNode(RaftGroupId groupId,
RestoredRaftState restoredState,
LogFileStructure logFileStructure) |
void |
restoreSnapshot(CPGroupId groupId,
long commitIndex,
MetadataRaftGroupSnapshot snapshot)
Restores the snapshot for specified
CPGroupId . |
void |
rollbackMigration(PartitionMigrationEvent event)
Rollback the migration process for this service, on both source and destination members.
|
void |
shutdown(boolean terminate)
Shuts down this service.
|
void |
stepDownRaftNode(CPGroupId groupId) |
MetadataRaftGroupSnapshot |
takeSnapshot(CPGroupId groupId,
long commitIndex)
Creates a snapshot for specified
CPGroupId . |
InternalCompletableFuture |
transferLeadership(CPGroupId groupId,
CPMemberInfo destination) |
boolean |
updateInvocationManagerMembers(long groupIdSeed,
long membersCommitIndex,
Collection<CPMemberInfo> members) |
static String |
withoutDefaultGroupName(String name) |
public static final String SERVICE_NAME
public RaftService(NodeEngine nodeEngine)
public void init(NodeEngine nodeEngine, Properties properties)
ManagedService
init
in interface ManagedService
nodeEngine
- the NodeEngine that this service belongs toproperties
- the Properties (can be used to pass settings to the service)public void reset()
ManagedService
TODO: what is the purpose of reset
reset
in interface ManagedService
public void shutdown(boolean terminate)
ManagedService
TODO: what is the purpose of the terminate variable
shutdown
in interface ManagedService
terminate
- true
to shut down this servicepublic MetadataRaftGroupSnapshot takeSnapshot(CPGroupId groupId, long commitIndex)
SnapshotAwareService
CPGroupId
.
The snapshot object returned from this method will be stored among
the Raft log and it can be sent to other Raft nodes anytime. Therefore,
this method must not return a snapshot object that can mutate after
the takeSnapshot() call.takeSnapshot
in interface SnapshotAwareService<MetadataRaftGroupSnapshot>
groupId
- CPGroupId
which is snapshot requested forcommitIndex
- commitIndex of the taken snapshotCPGroupId
.public void restoreSnapshot(CPGroupId groupId, long commitIndex, MetadataRaftGroupSnapshot snapshot)
SnapshotAwareService
CPGroupId
.restoreSnapshot
in interface SnapshotAwareService<MetadataRaftGroupSnapshot>
groupId
- CPGroupId
of the snapshot to be restoredcommitIndex
- commitIndex of the restored snapshotsnapshot
- snapshot for specified CPGroupId
public InternalCompletableFuture<Collection<CPGroupId>> getAllCPGroupIds()
public InternalCompletableFuture<Collection<CPGroupId>> getCPGroupIds()
CPSubsystemManagementService
getCPGroupIds
in interface CPSubsystemManagementService
public InternalCompletableFuture<CPGroup> getCPGroup(CPGroupId groupId)
public InternalCompletableFuture<CPGroup> getCPGroup(String name)
CPSubsystemManagementService
getCPGroup
in interface CPSubsystemManagementService
public InternalCompletableFuture<Void> restart()
CPSubsystemManagementService
After this method is called, all CP state and data are wiped and CP members start with empty state.
This method can be invoked only from the Hazelcast master member, which is the first member in the Hazelcast cluster member list.
This method must not be called while there are membership changes in the Hazelcast cluster. Before calling this method, please make sure that there is no new member joining and all existing Hazelcast members have seen the same member list.
To be able to use this method, the initial CP member count of CP
Subsystem, which is defined by
CPSubsystemConfig.getCPMemberCount()
, must be satisfied. For
instance, if CPSubsystemConfig.getCPMemberCount()
is 5 and
only 1 CP member is alive, when this method is called, 4 additional AP
Hazelcast members should exist in the cluster, or new Hazelcast members
must be started.
This method also deletes all data written by CP Subsystem Persistence.
This method triggers a new CP discovery process round. However, if the new CP discovery round fails for any reason, Hazelcast members are not terminated, because Hazelcast members are likely to contain data for AP data structures and their termination can cause data loss. Hence, you need to observe the cluster and check if the CP discovery process completes successfully.
Use with caution: This method is NOT idempotent and multiple invocations can break the whole system! After calling this API, you must observe the system to see if the restart process is successfully completed or failed before making another call.
restart
in interface CPSubsystemManagementService
public void restartLocal(long seed)
public InternalCompletableFuture<Void> promoteToCPMember()
CPSubsystemManagementService
This method is idempotent. If the local member is already in the active CP members list, i.e., it is already a CP member, then this method has no effect. When the local member is promoted to the CP role, its member UUID is assigned as its CP member UUID.
Once the returned Future
object is completed, the promoted CP
member has been added to CP groups that have missing CP members, i.e.,
whose current size is smaller than
CPSubsystemConfig.getGroupSize()
.
If the local member is currently being removed from
the active CP members list, then the returned Future
object
will throw IllegalArgumentException
.
If there is an ongoing membership change in CP Subsystem when this
method is invoked, then the returned Future
object throws
IllegalStateException
.
If the CP discovery process has not completed yet when this method is
invoked, then the returned Future
object throws
IllegalStateException
.
If the local member is a lite member, the returned Future
object
throws IllegalStateException
.
promoteToCPMember
in interface CPSubsystemManagementService
public InternalCompletableFuture<Void> removeCPMember(UUID cpMemberUuid)
CPSubsystemManagementService
Before removing a CP member from CP Subsystem, please make sure that it is declared as unreachable by Hazelcast's failure detector and removed from Hazelcast's member list. The behavior is undefined when a running CP member is removed from CP Subsystem.
removeCPMember
in interface CPSubsystemManagementService
public InternalCompletableFuture<Void> forceDestroyCPGroup(String groupName)
forceDestroyCPGroup
in interface CPSubsystemManagementService
public InternalCompletableFuture<Collection<CPMember>> getCPMembers()
CPSubsystemManagementService
getCPMembers
in interface CPSubsystemManagementService
public boolean isDiscoveryCompleted()
CPSubsystemManagementService
isDiscoveryCompleted
in interface CPSubsystemManagementService
true
if the CP discovery process completed,
false
otherwiseCPSubsystemManagementService.awaitUntilDiscoveryCompleted(long, TimeUnit)
public boolean awaitUntilDiscoveryCompleted(long timeout, TimeUnit timeUnit) throws InterruptedException
CPSubsystemManagementService
awaitUntilDiscoveryCompleted
in interface CPSubsystemManagementService
timeout
- maximum time to waittimeUnit
- time unit of the timeouttrue
if CP discovery completed, false
otherwiseInterruptedException
- if interrupted while waitingCPSubsystemManagementService.isDiscoveryCompleted()
public boolean onShutdown(long timeout, TimeUnit unit)
GracefulShutdownAwareService
onShutdown
in interface GracefulShutdownAwareService
timeout
- timeout for graceful shutdownunit
- time unitpublic Operation getPreJoinOperation()
PreJoinAwareService
PostJoinAwareService.getPostJoinOperation()
s, no partition locks, no key-based locks, no service level
locks, no database interaction are allowed. Additionally, a pre-join operation is executed while the cluster
lock is being held on the joining member, so it is important that the operation finishes quickly and does not
interact with other locks.
The Operation.getPartitionId()
method should return a negative value.
This means that the operations should not implement PartitionAwareOperation
.
Pre join operations should return response, which may also be a null
response.
getPreJoinOperation
in interface PreJoinAwareService
null
.public void memberAdded(MembershipServiceEvent event)
MembershipAwareService
memberAdded
in interface MembershipAwareService
event
- the event for a new member added to the clusterpublic void memberRemoved(MembershipServiceEvent event)
MembershipAwareService
memberRemoved
in interface MembershipAwareService
event
- the event for an existing member leaving the clusterpublic void memberAttributeChanged(MemberAttributeServiceEvent event)
MembershipAwareService
memberAttributeChanged
in interface MembershipAwareService
event
- the event for a member attribute being changedpublic MetadataRaftGroupManager getMetadataGroupManager()
public RaftInvocationManager getInvocationManager()
public void handlePreVoteRequest(CPGroupId groupId, PreVoteRequest request, RaftEndpoint target)
public void handlePreVoteResponse(CPGroupId groupId, PreVoteResponse response, RaftEndpoint target)
public void handleVoteRequest(CPGroupId groupId, VoteRequest request, RaftEndpoint target)
public void handleVoteResponse(CPGroupId groupId, VoteResponse response, RaftEndpoint target)
public void handleAppendEntries(CPGroupId groupId, AppendRequest request, RaftEndpoint target)
public void handleAppendResponse(CPGroupId groupId, AppendSuccessResponse response, RaftEndpoint target)
public void handleAppendResponse(CPGroupId groupId, AppendFailureResponse response, RaftEndpoint target)
public void handleSnapshot(CPGroupId groupId, InstallSnapshot request, RaftEndpoint target)
public void handleTriggerLeaderElection(CPGroupId groupId, TriggerLeaderElection request, RaftEndpoint target)
public Collection<RaftNode> getAllRaftNodes()
public boolean isRaftGroupDestroyed(CPGroupId groupId)
public CPSubsystemConfig getConfig()
public CPMemberInfo getLocalCPMember()
CPSubsystemManagementService
This field is initialized when the local Hazelcast member is one of
the first CPSubsystemConfig.getCPMemberCount()
members
in the cluster and the CP discovery process is completed.
HazelcastException
if CP Subsystem is not
enabled.getLocalCPMember
in interface CPSubsystemManagementService
CPSubsystemManagementService.isDiscoveryCompleted()
,
CPSubsystemManagementService.awaitUntilDiscoveryCompleted(long, TimeUnit)
public RaftEndpoint getLocalCPEndpoint()
public void createRaftNode(CPGroupId groupId, Collection<RaftEndpoint> members)
public RaftNodeImpl restoreRaftNode(RaftGroupId groupId, RestoredRaftState restoredState, LogFileStructure logFileStructure)
public void provideDynamicMetrics(MetricTaggerSupplier taggerSupplier, MetricsExtractor extractor)
DynamicMetricsProvider
MetricsExtractor
passed in argument.provideDynamicMetrics
in interface DynamicMetricsProvider
extractor
- The consumer of the collected metricspublic boolean updateInvocationManagerMembers(long groupIdSeed, long membersCommitIndex, Collection<CPMemberInfo> members)
public void destroyRaftNode(CPGroupId groupId)
public void stepDownRaftNode(CPGroupId groupId)
public RaftGroupId createRaftGroupForProxy(String name)
public InternalCompletableFuture<CPGroupId> createRaftGroupForProxyAsync(String name)
public RaftGroupId getMetadataGroupId()
public boolean isCpSubsystemEnabled()
public void handleActiveCPMembers(RaftGroupId receivedMetadataGroupId, long membersCommitIndex, Collection<CPMemberInfo> members)
public void onRaftGroupDestroyed(CPGroupId groupId)
RaftNodeLifecycleAwareService
onRaftGroupDestroyed
in interface RaftNodeLifecycleAwareService
public void onRaftNodeSteppedDown(CPGroupId groupId)
RaftNodeLifecycleAwareService
onRaftNodeSteppedDown
in interface RaftNodeLifecycleAwareService
public Collection<CPGroupId> getLeadedGroups()
public InternalCompletableFuture transferLeadership(CPGroupId groupId, CPMemberInfo destination)
public int getCPGroupPartitionId(CPGroupId groupId)
public static int getCPGroupPartitionId(CPGroupId groupId, int partitionCount)
public long nextUnsafeModeCommitIndex(CPGroupId groupId)
public void registerUnsafeWaitingOperation(CPGroupId groupId, long commitIndex, Operation op)
public boolean completeFutures(CPGroupId groupId, Collection<Long> indices, Object result)
indices
in the CP group associated with groupId
.true
if the CP group exists, false
otherwise.public boolean completeFutures(CPGroupId groupId, Collection<Map.Entry<Long,Object>> results)
indices
in the CP group associated with groupId
.true
if the CP group exists, false
otherwise.public Operation prepareReplicationOperation(PartitionReplicationEvent event)
MigrationAwareService
This method will be called on source member whenever partitioning system requires to copy/replicate a partition replica. Returned operation will be executed on destination member. If operation fails by throwing exception, migration process will fail and will be rolled back.
Returning null is allowed and means service does not have anything to replicate.
prepareReplicationOperation
in interface MigrationAwareService
event
- replicationpublic void beforeMigration(PartitionMigrationEvent event)
MigrationAwareService
Service can take actions required before migration. Migration process will block until this method returns. If this method fails by throwing an exception, migration process for specific partition will fail and will be rolled back.
beforeMigration
in interface MigrationAwareService
event
- migration eventpublic void commitMigration(PartitionMigrationEvent event)
MigrationAwareService
Commit is not expected to fail at this point, all exceptions will be suppressed and logged. Implementations of this method must be thread safe as this method may be called concurrently for different migrations on different partitions.
commitMigration
in interface MigrationAwareService
event
- migration eventpublic void rollbackMigration(PartitionMigrationEvent event)
MigrationAwareService
Rollback is not expected to fail at this point, all exceptions will be suppressed and logged. Implementations of this method must be thread safe as this method may be called concurrently for different migrations on different partitions.
rollbackMigration
in interface MigrationAwareService
event
- migration eventpublic void applyUnsafeModeState(int partitionId, UnsafeModePartitionState state)
Copyright © 2019 Hazelcast, Inc.. All rights reserved.