Class TokenMetadata

java.lang.Object
org.apache.cassandra.locator.TokenMetadata

public class TokenMetadata extends Object
  • Field Details

  • Constructor Details

    • TokenMetadata

      public TokenMetadata()
    • TokenMetadata

      public TokenMetadata(IEndpointSnitch snitch)
  • Method Details

    • cloneWithNewPartitioner

      public TokenMetadata cloneWithNewPartitioner(IPartitioner newPartitioner)
    • pendingRangeChanges

      public int pendingRangeChanges(InetAddressAndPort source)
      Returns:
      the number of nodes bootstrapping into source's primary range
    • updateNormalToken

      public void updateNormalToken(Token token, InetAddressAndPort endpoint)
      Update token map with a single token/endpoint pair in normal state.
    • updateNormalTokens

      public void updateNormalTokens(Collection<Token> tokens, InetAddressAndPort endpoint)
    • updateNormalTokens

      public void updateNormalTokens(com.google.common.collect.Multimap<InetAddressAndPort,Token> endpointTokens)
      Update token map with a set of token/endpoint pairs in normal state. Prefer this whenever there are multiple pairs to update, as each update (whether a single or multiple) is expensive (CASSANDRA-3831).
    • updateHostId

      public void updateHostId(UUID hostId, InetAddressAndPort endpoint)
      Store an end-point to host ID mapping. Each ID must be unique, and cannot be changed after the fact.
    • updateHostIds

      public void updateHostIds(Map<UUID,InetAddressAndPort> hostIdToEndpointMap)
    • getHostId

      public UUID getHostId(InetAddressAndPort endpoint)
      Return the unique host ID for an end-point.
    • getEndpointForHostId

      public InetAddressAndPort getEndpointForHostId(UUID hostId)
      Return the end-point for a unique host ID
    • getEndpointToHostIdMapForReading

      public Map<InetAddressAndPort,UUID> getEndpointToHostIdMapForReading()
      Returns:
      a copy of the endpoint-to-id map for read-only operations
    • addBootstrapTokens

      public void addBootstrapTokens(Collection<Token> tokens, InetAddressAndPort endpoint)
    • addReplaceTokens

      public void addReplaceTokens(Collection<Token> replacingTokens, InetAddressAndPort newNode, InetAddressAndPort oldNode)
    • getReplacementNode

      public Optional<InetAddressAndPort> getReplacementNode(InetAddressAndPort endpoint)
    • getReplacingNode

      public Optional<InetAddressAndPort> getReplacingNode(InetAddressAndPort endpoint)
    • removeBootstrapTokens

      public void removeBootstrapTokens(Collection<Token> tokens)
    • addLeavingEndpoint

      public void addLeavingEndpoint(InetAddressAndPort endpoint)
    • addMovingEndpoint

      public void addMovingEndpoint(Token token, InetAddressAndPort endpoint)
      Add a new moving endpoint
      Parameters:
      token - token which is node moving to
      endpoint - address of the moving node
    • removeEndpoint

      public void removeEndpoint(InetAddressAndPort endpoint)
    • updateTopology

      public TokenMetadata.Topology updateTopology(InetAddressAndPort endpoint)
      This is called when the snitch properties for this endpoint are updated, see CASSANDRA-10238.
    • updateTopology

      public TokenMetadata.Topology updateTopology()
      This is called when the snitch properties for many endpoints are updated, it will update the topology mappings of any endpoints whose snitch has changed, see CASSANDRA-10238.
    • removeFromMoving

      public void removeFromMoving(InetAddressAndPort endpoint)
      Remove pair of token/address from moving endpoints
      Parameters:
      endpoint - address of the moving node
    • getTokens

      public Collection<Token> getTokens(InetAddressAndPort endpoint)
    • isMember

      public boolean isMember(InetAddressAndPort endpoint)
    • isLeaving

      public boolean isLeaving(InetAddressAndPort endpoint)
    • isMoving

      public boolean isMoving(InetAddressAndPort endpoint)
    • cloneOnlyTokenMap

      public TokenMetadata cloneOnlyTokenMap()
      Create a copy of TokenMetadata with only tokenToEndpointMap. That is, pending ranges, bootstrap tokens and leaving endpoints are not included in the copy.
    • cachedOnlyTokenMap

      public TokenMetadata cachedOnlyTokenMap()
      Return a cached TokenMetadata with only tokenToEndpointMap, i.e., the same as cloneOnlyTokenMap but uses a cached copy that is invalided when the ring changes, so in the common case no extra locking is required. Callers must *NOT* mutate the returned metadata object.
    • cloneAfterAllLeft

      public TokenMetadata cloneAfterAllLeft()
      Create a copy of TokenMetadata with tokenToEndpointMap reflecting situation after all current leave operations have finished.
      Returns:
      new token metadata
    • cloneAfterAllSettled

      public TokenMetadata cloneAfterAllSettled()
      Create a copy of TokenMetadata with tokenToEndpointMap reflecting situation after all current leave, and move operations have finished.
      Returns:
      new token metadata
    • getEndpoint

      public InetAddressAndPort getEndpoint(Token token)
    • getPrimaryRangesFor

      public Collection<Range<Token>> getPrimaryRangesFor(Collection<Token> tokens)
    • sortedTokens

      public ArrayList<Token> sortedTokens()
    • getPendingRangesMM

      public EndpointsByRange getPendingRangesMM(String keyspaceName)
    • getPendingRanges

      public PendingRangeMaps getPendingRanges(String keyspaceName)
      a mutable map may be returned but caller should not modify it
    • getPendingRanges

      public RangesAtEndpoint getPendingRanges(String keyspaceName, InetAddressAndPort endpoint)
    • setPendingRangesUnsafe

      public void setPendingRangesUnsafe(String keyspaceName, com.google.common.collect.Multimap<Range<Token>,Replica> rangeMap)
    • calculatePendingRanges

      public void calculatePendingRanges(AbstractReplicationStrategy strategy, String keyspaceName)
      Calculate pending ranges according to bootsrapping and leaving nodes. Reasoning is: (1) When in doubt, it is better to write too much to a node than too little. That is, if there are multiple nodes moving, calculate the biggest ranges a node could have. Cleaning up unneeded data afterwards is better than missing writes during movement. (2) When a node leaves, ranges for other nodes can only grow (a node might get additional ranges, but it will not lose any of its current ranges as a result of a leave). Therefore we will first remove _all_ leaving tokens for the sake of calculation and then check what ranges would go where if all nodes are to leave. This way we get the biggest possible ranges with regard current leave operations, covering all subsets of possible final range values. (3) When a node bootstraps, ranges of other nodes can only get smaller. Without doing complex calculations to see if multiple bootstraps overlap, we simply base calculations on the same token ring used before (reflecting situation after all leave operations have completed). Bootstrapping nodes will be added and removed one by one to that metadata and checked what their ranges would be. This will give us the biggest possible ranges the node could have. It might be that other bootstraps make our actual final ranges smaller, but it does not matter as we can clean up the data afterwards. NOTE: This is heavy and ineffective operation. This will be done only once when a node changes state in the cluster, so it should be manageable.
    • unsafeCalculatePendingRanges

      public void unsafeCalculatePendingRanges(AbstractReplicationStrategy strategy, String keyspaceName)
    • getPredecessor

      public Token getPredecessor(Token token)
    • getSuccessor

      public Token getSuccessor(Token token)
    • getBootstrapTokens

      public BiMultiValMap<Token,InetAddressAndPort> getBootstrapTokens()
      Returns:
      a copy of the bootstrapping tokens map
    • getAllEndpoints

      public Set<InetAddressAndPort> getAllEndpoints()
    • getSizeOfAllEndpoints

      public int getSizeOfAllEndpoints()
    • getAllMembers

      public Set<InetAddressAndPort> getAllMembers()
    • getLeavingEndpoints

      public Set<InetAddressAndPort> getLeavingEndpoints()
      caller should not modify leavingEndpoints
    • getSizeOfLeavingEndpoints

      public int getSizeOfLeavingEndpoints()
    • getMovingEndpoints

      public Set<Pair<Token,InetAddressAndPort>> getMovingEndpoints()
      Endpoints which are migrating to the new tokens
      Returns:
      set of addresses of moving endpoints
    • getSizeOfMovingEndpoints

      public int getSizeOfMovingEndpoints()
    • firstTokenIndex

      public static int firstTokenIndex(ArrayList<Token> ring, Token start, boolean insertMin)
    • firstToken

      public static Token firstToken(ArrayList<Token> ring, Token start)
    • ringIterator

      public static Iterator<Token> ringIterator(ArrayList<Token> ring, Token start, boolean includeMin)
      iterator over the Tokens in the given ring, starting with the token for the node owning start (which does not have to be a Token in the ring)
      Parameters:
      includeMin - True if the minimum token should be returned in the ring even if it has no owner.
    • clearUnsafe

      public void clearUnsafe()
      used by tests
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • pendingEndpointsForToken

      public EndpointsForToken pendingEndpointsForToken(Token token, String keyspaceName)
    • getWriteEndpoints

      @Deprecated(since="4.0") public EndpointsForToken getWriteEndpoints(Token token, String keyspaceName, EndpointsForToken natural)
      Deprecated.
      retained for benefit of old tests
    • getEndpointToTokenMapForReading

      public com.google.common.collect.Multimap<InetAddressAndPort,Token> getEndpointToTokenMapForReading()
      Returns:
      an endpoint to token multimap representation of tokenToEndpointMap (a copy)
    • getNormalAndBootstrappingTokenToEndpointMap

      public Map<Token,InetAddressAndPort> getNormalAndBootstrappingTokenToEndpointMap()
      Returns:
      a (stable copy, won't be modified) Token to Endpoint map for all the normal and bootstrapping nodes in the cluster.
    • getDC2AllEndpoints

      public com.google.common.collect.ImmutableMultimap<String,InetAddressAndPort> getDC2AllEndpoints(IEndpointSnitch snitch)
      Returns:
      a (stable copy, won't be modified) datacenter to Endpoint map for all the nodes in the cluster.
    • getTopology

      public TokenMetadata.Topology getTopology()
      Returns:
      the Topology map of nodes to DCs + Racks This is only allowed when a copy has been made of TokenMetadata, to avoid concurrent modifications when Topology methods are subsequently used by the caller.
    • getRingVersion

      public long getRingVersion()
    • invalidateCachedRings

      public void invalidateCachedRings()
    • decorateKey

      public DecoratedKey decorateKey(ByteBuffer key)
    • isTokenInLocalPendingRange

      public boolean isTokenInLocalPendingRange(String keyspaceName, Token token)