Class ShardRouting

All Implemented Interfaces:
Writeable, ToXContent, ToXContentObject

public final class ShardRouting extends Object implements Writeable, ToXContentObject
ShardRouting immutably encapsulates information about shard indexRoutings like id, state, version, etc.
  • Field Details


      public static final long UNAVAILABLE_EXPECTED_SHARD_SIZE
      Used if shard size is not available
      See Also:
  • Constructor Details

  • Method Details

    • newUnassigned

      public static ShardRouting newUnassigned(ShardId shardId, boolean primary, RecoverySource recoverySource, UnassignedInfo unassignedInfo)
      Creates a new unassigned shard.
    • index

      public Index index()
    • getIndexName

      public String getIndexName()
      The index name.
    • id

      public int id()
      The shard id.
    • getId

      public int getId()
      The shard id.
    • unassigned

      public boolean unassigned()
      The shard is unassigned (not allocated to any node).
    • initializing

      public boolean initializing()
      The shard is initializing (usually recovering either from peer shard or from gateway).
    • active

      public boolean active()
      Returns true iff the this shard is currently started or relocating to another node. Otherwise false
    • started

      public boolean started()
      The shard is in started mode.
    • relocating

      public boolean relocating()
      Returns true iff the this shard is currently relocating to another node. Otherwise false
      See Also:
    • assignedToNode

      public boolean assignedToNode()
      Returns true iff this shard is assigned to a node ie. not unassigned. Otherwise false
    • currentNodeId

      public String currentNodeId()
      The current node id the shard is allocated on.
    • relocatingNodeId

      public String relocatingNodeId()
      The relocating node id the shard is either relocating to or relocating from.
    • getTargetRelocatingShard

      public ShardRouting getTargetRelocatingShard()
      Returns a shard routing representing the target shard. The target shard routing will be the INITIALIZING state and have relocatingNodeId set to the source node.
    • unassignedInfo

      @Nullable public UnassignedInfo unassignedInfo()
      Additional metadata on why the shard is/was unassigned. The metadata is kept around until the shard moves to STARTED.
    • allocationId

      @Nullable public AllocationId allocationId()
      An id that uniquely identifies an allocation.
    • primary

      public boolean primary()
      Returns true iff this shard is a primary.
    • state

      public ShardRoutingState state()
      The shard state.
    • shardId

      public ShardId shardId()
      The shard id.
    • shardsIt

      public ShardIterator shardsIt()
      A shard iterator with just this shard in it.
    • writeToThin

      public void writeToThin(StreamOutput out) throws IOException
      Writes shard information to StreamOutput without writing index name and shard id
      out - StreamOutput to write shard information to
      IOException - if something happens during write
    • writeTo

      public void writeTo(StreamOutput out) throws IOException
      Description copied from interface: Writeable
      Write this into the StreamOutput.
      Specified by:
      writeTo in interface Writeable
    • updateUnassigned

      public ShardRouting updateUnassigned(UnassignedInfo unassignedInfo, RecoverySource recoverySource)
    • moveToUnassigned

      public ShardRouting moveToUnassigned(UnassignedInfo unassignedInfo)
      Moves the shard to unassigned state.
    • initialize

      public ShardRouting initialize(String nodeId, @Nullable String existingAllocationId, long expectedShardSize)
      Initializes an unassigned shard on a node.
      existingAllocationId - allocation id to use. If null, a fresh allocation id is generated.
    • relocate

      public ShardRouting relocate(String relocatingNodeId, long expectedShardSize)
      Relocate the shard to another node.
      relocatingNodeId - id of the node to relocate the shard
    • cancelRelocation

      public ShardRouting cancelRelocation()
      Cancel relocation of a shard. The shards state must be set to RELOCATING.
    • removeRelocationSource

      public ShardRouting removeRelocationSource()
      Removes relocation source of a non-primary shard. The shard state must be INITIALIZING. This allows the non-primary shard to continue recovery from the primary even though its non-primary relocation source has failed.
    • reinitializeReplicaShard

      public ShardRouting reinitializeReplicaShard()
      Reinitializes a replica shard, giving it a fresh allocation id
    • moveToStarted

      public ShardRouting moveToStarted()
      Set the shards state to STARTED. The shards state must be INITIALIZING or RELOCATING. Any relocation will be canceled.
    • moveActiveReplicaToPrimary

      public ShardRouting moveActiveReplicaToPrimary()
      Make the active shard primary unless it's not primary
      IllegalShardRoutingStateException - if shard is already a primary
    • moveUnassignedFromPrimary

      public ShardRouting moveUnassignedFromPrimary()
      Set the unassigned primary shard to non-primary
      IllegalShardRoutingStateException - if shard is already a replica
    • isSameAllocation

      public boolean isSameAllocation(ShardRouting other)
      returns true if this routing has the same allocation ID as another.

      Note: if both shard routing has a null as their allocationId(), this method returns false as the routing describe no allocation at all..

    • isRelocationTarget

      public boolean isRelocationTarget()
      Returns true if this shard is a relocation target for another shard (i.e., was created with initializeTargetRelocatingShard()
    • isRelocationTargetOf

      public boolean isRelocationTargetOf(ShardRouting other)
      returns true if the routing is the relocation target of the given routing
    • isRelocationSourceOf

      public boolean isRelocationSourceOf(ShardRouting other)
      returns true if the routing is the relocation source for the given routing
    • equalsIgnoringMetadata

      public boolean equalsIgnoringMetadata(ShardRouting other)
      returns true if the current routing is identical to the other routing in all but meta fields, i.e., unassigned info
    • equals

      public boolean equals(Object o)
      equals in class Object
    • hashCode

      public int hashCode()
      hashCode in class Object
    • toString

      public String toString()
      toString in class Object
    • shortSummary

      public String shortSummary()
      A short description of the shard.
    • toXContent

      public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException
      Specified by:
      toXContent in interface ToXContent
    • getExpectedShardSize

      public long getExpectedShardSize()
      Returns the expected shard size for ShardRoutingState.RELOCATING and ShardRoutingState.INITIALIZING shards. If it's size is not available -1L will be returned.
    • recoverySource

      @Nullable public RecoverySource recoverySource()
      Returns recovery source for the given shard. Replica shards always recover from the primary RecoverySource.PeerRecoverySource.
      recovery source or null if shard is active()