/** * if initialtoken was specified, use that (split on comma). * otherwise, if num_tokens == 1, pick a token to assume half the load of the most-loaded node. * else choose num_tokens tokens at random */ public static Collection<Token> getBootstrapTokens(final TokenMetadata metadata) throws ConfigurationException { Collection<String> initialTokens = DatabaseDescriptor.getInitialTokens(); // if user specified tokens, use those if (initialTokens.size() > 0) { logger.debug("tokens manually specified as {}", initialTokens); List<Token> tokens = new ArrayList<Token>(initialTokens.size()); for (String tokenString : initialTokens) { Token token = StorageService.getPartitioner().getTokenFactory().fromString(tokenString); if (metadata.getEndpoint(token) != null) throw new ConfigurationException("Bootstrapping to existing token " + tokenString + " is not allowed (decommission/removenode the old node first)."); tokens.add(token); } return tokens; } int numTokens = DatabaseDescriptor.getNumTokens(); if (numTokens < 1) throw new ConfigurationException("num_tokens must be >= 1"); if (numTokens == 1) logger.warn("Picking random token for a single vnode. You should probably add more vnodes; failing that, you should probably specify the token manually"); return getRandomTokens(metadata, numTokens); }
public static Collection<Token> getRandomTokens(TokenMetadata metadata, int numTokens) { Set<Token> tokens = new HashSet<Token>(numTokens); while (tokens.size() < numTokens) { Token token = StorageService.getPartitioner().getRandomToken(); if (metadata.getEndpoint(token) == null) tokens.add(token); } return tokens; }