Class ReplicationTracker

  • All Implemented Interfaces:
    java.util.function.LongSupplier, IndexShardComponent

    public class ReplicationTracker
    extends AbstractIndexShardComponent
    implements java.util.function.LongSupplier
    This class is responsible for tracking the replication group with its progress and safety markers (local and global checkpoints). The global checkpoint is the highest sequence number for which all lower (or equal) sequence number have been processed on all shards that are currently active. Since shards count as "active" when the master starts them, and before this primary shard has been notified of this fact, we also include shards that have completed recovery. These shards have received all old operations via the recovery mechanism and are kept up to date by the various replications actions. The set of shards that are taken into account for the global checkpoint calculation are called the "in-sync shards".

    The global checkpoint is maintained by the primary shard and is replicated to all the replicas (via GlobalCheckpointSyncAction).

    • Constructor Detail

      • ReplicationTracker

        public ReplicationTracker​(ShardId shardId,
                                  java.lang.String allocationId,
                                  IndexSettings indexSettings,
                                  long globalCheckpoint,
                                  java.util.function.LongConsumer onGlobalCheckpointUpdated)
        Initialize the global checkpoint service. The specified global checkpoint should be set to the last known global checkpoint, or SequenceNumbers.UNASSIGNED_SEQ_NO.
        shardId - the shard ID
        allocationId - the allocation ID
        indexSettings - the index settings
        globalCheckpoint - the last known global checkpoint for this shard, or SequenceNumbers.UNASSIGNED_SEQ_NO
    • Method Detail

      • getInSyncGlobalCheckpoints

        public com.carrotsearch.hppc.ObjectLongMap<java.lang.String> getInSyncGlobalCheckpoints()
        Get the local knowledge of the global checkpoints for all in-sync allocation IDs.
        a map from allocation ID to the local knowledge of the global checkpoint for that allocation ID
      • isPrimaryMode

        public boolean isPrimaryMode()
        Returns whether the replication tracker is in primary mode, i.e., whether the current shard is acting as primary from the point of view of replication.
      • isRelocated

        public boolean isRelocated()
        Returns whether the replication tracker has relocated away to another shard copy.
      • getReplicationGroup

        public ReplicationGroup getReplicationGroup()
        Returns the current replication group for the shard.
        the replication group
      • getGlobalCheckpoint

        public long getGlobalCheckpoint()
        Returns the global checkpoint for the shard.
        the global checkpoint
      • getAsLong

        public long getAsLong()
        Specified by:
        getAsLong in interface java.util.function.LongSupplier
      • updateGlobalCheckpointOnReplica

        public void updateGlobalCheckpointOnReplica​(long globalCheckpoint,
                                                    java.lang.String reason)
        Updates the global checkpoint on a replica shard after it has been updated by the primary.
        globalCheckpoint - the global checkpoint
        reason - the reason the global checkpoint was updated
      • updateGlobalCheckpointForShard

        public void updateGlobalCheckpointForShard​(java.lang.String allocationId,
                                                   long globalCheckpoint)
        Update the local knowledge of the global checkpoint for the specified allocation ID.
        allocationId - the allocation ID to update the global checkpoint for
        globalCheckpoint - the global checkpoint
      • activatePrimaryMode

        public void activatePrimaryMode​(long localCheckpoint)
        Initializes the global checkpoint tracker in primary mode (see primaryMode. Called on primary activation or promotion.
      • updateFromMaster

        public void updateFromMaster​(long applyingClusterStateVersion,
                                     java.util.Set<java.lang.String> inSyncAllocationIds,
                                     IndexShardRoutingTable routingTable,
                                     java.util.Set<java.lang.String> pre60AllocationIds)
        Notifies the tracker of the current allocation IDs in the cluster state.
        applyingClusterStateVersion - the cluster state version being applied when updating the allocation IDs from the master
        inSyncAllocationIds - the allocation IDs of the currently in-sync shard copies
        routingTable - the shard routing table
        pre60AllocationIds - the allocation IDs of shards that are allocated to pre-6.0 nodes
      • initiateTracking

        public void initiateTracking​(java.lang.String allocationId)
        Called when the recovery process for a shard has opened the engine on the target shard. Ensures that the right data structures have been set up locally to track local checkpoint information for the shard and that the shard is added to the replication group.
        allocationId - the allocation ID of the shard for which recovery was initiated
      • markAllocationIdAsInSync

        public void markAllocationIdAsInSync​(java.lang.String allocationId,
                                             long localCheckpoint)
                                      throws java.lang.InterruptedException
        Marks the shard with the provided allocation ID as in-sync with the primary shard. This method will block until the local checkpoint on the specified shard advances above the current global checkpoint.
        allocationId - the allocation ID of the shard to mark as in-sync
        localCheckpoint - the current local checkpoint on the shard
      • updateLocalCheckpoint

        public void updateLocalCheckpoint​(java.lang.String allocationId,
                                          long localCheckpoint)
        Notifies the service to update the local checkpoint for the shard with the provided allocation ID. If the checkpoint is lower than the currently known one, this is a no-op. If the allocation ID is not tracked, it is ignored.
        allocationId - the allocation ID of the shard to update the local checkpoint for
        localCheckpoint - the local checkpoint for the shard
      • startRelocationHandoff

        public ReplicationTracker.PrimaryContext startRelocationHandoff()
        Initiates a relocation handoff and returns the corresponding primary context.
      • abortRelocationHandoff

        public void abortRelocationHandoff()
        Fails a relocation handoff attempt.
      • completeRelocationHandoff

        public void completeRelocationHandoff()
        Marks a relocation handoff attempt as successful. Moves the tracker into replica mode.
      • activateWithPrimaryContext

        public void activateWithPrimaryContext​(ReplicationTracker.PrimaryContext primaryContext)
        Activates the global checkpoint tracker in primary mode (see primaryMode. Called on primary relocation target during primary relocation handoff.
        primaryContext - the primary context used to initialize the state
      • pendingInSync

        public boolean pendingInSync()
        Whether the are shards blocking global checkpoint advancement. Used by tests.
      • getTrackedLocalCheckpointForShard

        public ReplicationTracker.CheckpointState getTrackedLocalCheckpointForShard​(java.lang.String allocationId)
        Returns the local checkpoint information tracked for a specific shard. Used by tests.