Skip to content

Commit

Permalink
fix unsynchronized use of TokenMetadata.entrySet
Browse files Browse the repository at this point in the history
patch by Peter Schuller; reviewed by jbellis for CASSANDRA-3417
  • Loading branch information
jbellis committed Feb 13, 2012
1 parent cb0efd0 commit b55ab4f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
Expand Up @@ -88,7 +88,7 @@ public List<InetAddress> calculateNaturalEndpoints(Token searchToken, TokenMetad

// collect endpoints in this DC
TokenMetadata dcTokens = new TokenMetadata();
for (Entry<Token, InetAddress> tokenEntry : tokenMetadata.entrySet())
for (Entry<Token, InetAddress> tokenEntry : tokenMetadata.getTokenToEndpointMapForReading().entrySet())
{
if (snitch.getDatacenter(tokenEntry.getValue()).equals(dcName))
dcTokens.updateNormalToken(tokenEntry.getKey(), tokenEntry.getValue());
Expand Down
28 changes: 21 additions & 7 deletions src/java/org/apache/cassandra/locator/TokenMetadata.java
Expand Up @@ -408,11 +408,6 @@ public TokenMetadata cloneAfterAllSettled()
}
}

public Set<Map.Entry<Token,InetAddress>> entrySet()
{
return tokenToEndpointMap.entrySet();
}

public InetAddress getEndpoint(Token token)
{
lock.readLock().lock();
Expand Down Expand Up @@ -713,9 +708,28 @@ public Collection<InetAddress> getWriteEndpoints(Token token, String table, Coll
}

/**
* Return the Token to Endpoint map for all the node in the cluster, including bootstrapping ones.
* @return a token to endpoint map to consider for read operations on the cluster.
*/
public Map<Token, InetAddress> getTokenToEndpointMapForReading()
{
lock.readLock().lock();
try
{
Map<Token, InetAddress> map = new HashMap<Token, InetAddress>(tokenToEndpointMap.size());
map.putAll(tokenToEndpointMap);
return map;
}
finally
{
lock.readLock().unlock();
}
}

/**
* @return a (stable copy, won't be modified) Token to Endpoint map for all the normal and bootstrapping nodes
* in the cluster.
*/
public Map<Token, InetAddress> getTokenToEndpointMap()
public Map<Token, InetAddress> getNormalAndBootstrappingTokenToEndpointMap()
{
lock.readLock().lock();
try
Expand Down
4 changes: 2 additions & 2 deletions src/java/org/apache/cassandra/service/StorageService.java
Expand Up @@ -854,7 +854,7 @@ public List<TokenRange> describeRing(String keyspace) throws InvalidRequestExcep

public Map<Token, String> getTokenToEndpointMap()
{
Map<Token, InetAddress> mapInetAddress = tokenMetadata_.getTokenToEndpointMap();
Map<Token, InetAddress> mapInetAddress = tokenMetadata_.getNormalAndBootstrappingTokenToEndpointMap();
Map<Token, String> mapString = new HashMap<Token, String>(mapInetAddress.size());
for (Map.Entry<Token, InetAddress> entry : mapInetAddress.entrySet())
{
Expand Down Expand Up @@ -2074,7 +2074,7 @@ public Token getBootstrapToken()
if (token instanceof StringToken)
{
token = new StringToken(((String)token.token).replaceAll(VersionedValue.DELIMITER_STR, ""));
if (tokenMetadata_.getTokenToEndpointMap().containsKey(token))
if (tokenMetadata_.getNormalAndBootstrappingTokenToEndpointMap().containsKey(token))
throw new RuntimeException("Unable to compute unique token for new node -- specify one manually with initial_token");
}
return token;
Expand Down

0 comments on commit b55ab4f

Please sign in to comment.