Package org.apache.cassandra.locator
Class TokenMetadata
- java.lang.Object
-
- org.apache.cassandra.locator.TokenMetadata
-
public class TokenMetadata extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TokenMetadata.Topology
Tracks the assignment of racks and endpoints in each datacenter for all the "normal" endpoints in this TokenMetadata.
-
Field Summary
Fields Modifier and Type Field Description IPartitioner
partitioner
-
Constructor Summary
Constructors Constructor Description TokenMetadata()
TokenMetadata(IEndpointSnitch snitch)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addBootstrapToken(Token token, InetAddressAndPort endpoint)
Deprecated.void
addBootstrapTokens(java.util.Collection<Token> tokens, InetAddressAndPort endpoint)
void
addLeavingEndpoint(InetAddressAndPort endpoint)
void
addMovingEndpoint(Token token, InetAddressAndPort endpoint)
Add a new moving endpointvoid
addReplaceTokens(java.util.Collection<Token> replacingTokens, InetAddressAndPort newNode, InetAddressAndPort oldNode)
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.void
calculatePendingRanges(AbstractReplicationStrategy strategy, java.lang.String keyspaceName)
Calculate pending ranges according to bootsrapping and leaving nodes.void
clearUnsafe()
used by testsTokenMetadata
cloneAfterAllLeft()
Create a copy of TokenMetadata with tokenToEndpointMap reflecting situation after all current leave operations have finished.TokenMetadata
cloneAfterAllSettled()
Create a copy of TokenMetadata with tokenToEndpointMap reflecting situation after all current leave, and move operations have finished.TokenMetadata
cloneOnlyTokenMap()
Create a copy of TokenMetadata with only tokenToEndpointMap.TokenMetadata
cloneWithNewPartitioner(IPartitioner newPartitioner)
To be used by tests only (viaStorageService.setPartitionerUnsafe(org.apache.cassandra.dht.IPartitioner)
).DecoratedKey
decorateKey(java.nio.ByteBuffer key)
static Token
firstToken(java.util.ArrayList<Token> ring, Token start)
static int
firstTokenIndex(java.util.ArrayList<Token> ring, Token start, boolean insertMin)
java.util.Set<InetAddressAndPort>
getAllEndpoints()
BiMultiValMap<Token,InetAddressAndPort>
getBootstrapTokens()
com.google.common.collect.ImmutableMultimap<java.lang.String,InetAddressAndPort>
getDC2AllEndpoints(IEndpointSnitch snitch)
InetAddressAndPort
getEndpoint(Token token)
InetAddressAndPort
getEndpointForHostId(java.util.UUID hostId)
Return the end-point for a unique host IDjava.util.Map<InetAddressAndPort,java.util.UUID>
getEndpointToHostIdMapForReading()
com.google.common.collect.Multimap<InetAddressAndPort,Token>
getEndpointToTokenMapForReading()
java.util.UUID
getHostId(InetAddressAndPort endpoint)
Return the unique host ID for an end-point.java.util.Set<InetAddressAndPort>
getLeavingEndpoints()
caller should not modify leavingEndpointsjava.util.Set<Pair<Token,InetAddressAndPort>>
getMovingEndpoints()
Endpoints which are migrating to the new tokensjava.util.Map<Token,InetAddressAndPort>
getNormalAndBootstrappingTokenToEndpointMap()
PendingRangeMaps
getPendingRanges(java.lang.String keyspaceName)
a mutable map may be returned but caller should not modify itRangesAtEndpoint
getPendingRanges(java.lang.String keyspaceName, InetAddressAndPort endpoint)
EndpointsByRange
getPendingRangesMM(java.lang.String keyspaceName)
Token
getPredecessor(Token token)
Range<Token>
getPrimaryRangeFor(Token right)
Deprecated.java.util.Collection<Range<Token>>
getPrimaryRangesFor(java.util.Collection<Token> tokens)
java.util.Optional<InetAddressAndPort>
getReplacementNode(InetAddressAndPort endpoint)
java.util.Optional<InetAddressAndPort>
getReplacingNode(InetAddressAndPort endpoint)
long
getRingVersion()
int
getSizeOfAllEndpoints()
int
getSizeOfLeavingEndpoints()
int
getSizeOfMovingEndpoints()
Token
getSuccessor(Token token)
Token
getToken(InetAddressAndPort endpoint)
Deprecated.java.util.Collection<Token>
getTokens(InetAddressAndPort endpoint)
TokenMetadata.Topology
getTopology()
EndpointsForToken
getWriteEndpoints(Token token, java.lang.String keyspaceName, EndpointsForToken natural)
Deprecated.retained for benefit of old testsvoid
invalidateCachedRings()
boolean
isLeaving(InetAddressAndPort endpoint)
boolean
isMember(InetAddressAndPort endpoint)
boolean
isMoving(InetAddressAndPort endpoint)
EndpointsForToken
pendingEndpointsForToken(Token token, java.lang.String keyspaceName)
int
pendingRangeChanges(InetAddressAndPort source)
void
removeBootstrapTokens(java.util.Collection<Token> tokens)
void
removeEndpoint(InetAddressAndPort endpoint)
void
removeFromMoving(InetAddressAndPort endpoint)
Remove pair of token/address from moving endpointsstatic java.util.Iterator<Token>
ringIterator(java.util.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)java.util.ArrayList<Token>
sortedTokens()
java.lang.String
toString()
void
updateHostId(java.util.UUID hostId, InetAddressAndPort endpoint)
Store an end-point to host ID mapping.void
updateNormalToken(Token token, InetAddressAndPort endpoint)
Update token map with a single token/endpoint pair in normal state.void
updateNormalTokens(com.google.common.collect.Multimap<InetAddressAndPort,Token> endpointTokens)
Update token map with a set of token/endpoint pairs in normal state.void
updateNormalTokens(java.util.Collection<Token> tokens, InetAddressAndPort endpoint)
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.TokenMetadata.Topology
updateTopology(InetAddressAndPort endpoint)
This is called when the snitch properties for this endpoint are updated, see CASSANDRA-10238.
-
-
-
Field Detail
-
partitioner
public final IPartitioner partitioner
-
-
Constructor Detail
-
TokenMetadata
public TokenMetadata()
-
TokenMetadata
public TokenMetadata(IEndpointSnitch snitch)
-
-
Method Detail
-
cloneWithNewPartitioner
public TokenMetadata cloneWithNewPartitioner(IPartitioner newPartitioner)
To be used by tests only (viaStorageService.setPartitionerUnsafe(org.apache.cassandra.dht.IPartitioner)
).
-
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(java.util.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(java.util.UUID hostId, InetAddressAndPort endpoint)
Store an end-point to host ID mapping. Each ID must be unique, and cannot be changed after the fact.
-
getHostId
public java.util.UUID getHostId(InetAddressAndPort endpoint)
Return the unique host ID for an end-point.
-
getEndpointForHostId
public InetAddressAndPort getEndpointForHostId(java.util.UUID hostId)
Return the end-point for a unique host ID
-
getEndpointToHostIdMapForReading
public java.util.Map<InetAddressAndPort,java.util.UUID> getEndpointToHostIdMapForReading()
- Returns:
- a copy of the endpoint-to-id map for read-only operations
-
addBootstrapToken
@Deprecated public void addBootstrapToken(Token token, InetAddressAndPort endpoint)
Deprecated.
-
addBootstrapTokens
public void addBootstrapTokens(java.util.Collection<Token> tokens, InetAddressAndPort endpoint)
-
addReplaceTokens
public void addReplaceTokens(java.util.Collection<Token> replacingTokens, InetAddressAndPort newNode, InetAddressAndPort oldNode)
-
getReplacementNode
public java.util.Optional<InetAddressAndPort> getReplacementNode(InetAddressAndPort endpoint)
-
getReplacingNode
public java.util.Optional<InetAddressAndPort> getReplacingNode(InetAddressAndPort endpoint)
-
removeBootstrapTokens
public void removeBootstrapTokens(java.util.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 toendpoint
- 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 java.util.Collection<Token> getTokens(InetAddressAndPort endpoint)
-
getToken
@Deprecated public Token getToken(InetAddressAndPort endpoint)
Deprecated.
-
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 java.util.Collection<Range<Token>> getPrimaryRangesFor(java.util.Collection<Token> tokens)
-
sortedTokens
public java.util.ArrayList<Token> sortedTokens()
-
getPendingRangesMM
public EndpointsByRange getPendingRangesMM(java.lang.String keyspaceName)
-
getPendingRanges
public PendingRangeMaps getPendingRanges(java.lang.String keyspaceName)
a mutable map may be returned but caller should not modify it
-
getPendingRanges
public RangesAtEndpoint getPendingRanges(java.lang.String keyspaceName, InetAddressAndPort endpoint)
-
calculatePendingRanges
public void calculatePendingRanges(AbstractReplicationStrategy strategy, java.lang.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.
-
getBootstrapTokens
public BiMultiValMap<Token,InetAddressAndPort> getBootstrapTokens()
- Returns:
- a copy of the bootstrapping tokens map
-
getAllEndpoints
public java.util.Set<InetAddressAndPort> getAllEndpoints()
-
getSizeOfAllEndpoints
public int getSizeOfAllEndpoints()
-
getLeavingEndpoints
public java.util.Set<InetAddressAndPort> getLeavingEndpoints()
caller should not modify leavingEndpoints
-
getSizeOfLeavingEndpoints
public int getSizeOfLeavingEndpoints()
-
getMovingEndpoints
public java.util.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(java.util.ArrayList<Token> ring, Token start, boolean insertMin)
-
ringIterator
public static java.util.Iterator<Token> ringIterator(java.util.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 java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
pendingEndpointsForToken
public EndpointsForToken pendingEndpointsForToken(Token token, java.lang.String keyspaceName)
-
getWriteEndpoints
@Deprecated public EndpointsForToken getWriteEndpoints(Token token, java.lang.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 java.util.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<java.lang.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(java.nio.ByteBuffer key)
-
-