use of org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken in project cassandra by apache.
the class Util method createInitialRing.
/**
* Creates initial set of nodes and tokens. Nodes are added to StorageService as 'normal'
*/
public static void createInitialRing(StorageService ss, IPartitioner partitioner, List<Token> endpointTokens, List<Token> keyTokens, List<InetAddress> hosts, List<UUID> hostIds, int howMany) throws UnknownHostException {
// Expand pool of host IDs as necessary
for (int i = hostIdPool.size(); i < howMany; i++) hostIdPool.add(UUID.randomUUID());
boolean endpointTokenPrefilled = endpointTokens != null && !endpointTokens.isEmpty();
for (int i = 0; i < howMany; i++) {
if (!endpointTokenPrefilled)
endpointTokens.add(new BigIntegerToken(String.valueOf(10 * i)));
keyTokens.add(new BigIntegerToken(String.valueOf(10 * i + 5)));
hostIds.add(hostIdPool.get(i));
}
for (int i = 0; i < endpointTokens.size(); i++) {
InetAddress ep = InetAddress.getByName("127.0.0." + String.valueOf(i + 1));
Gossiper.instance.initializeNodeUnsafe(ep, hostIds.get(i), 1);
Gossiper.instance.injectApplicationState(ep, ApplicationState.TOKENS, new VersionedValue.VersionedValueFactory(partitioner).tokens(Collections.singleton(endpointTokens.get(i))));
ss.onChange(ep, ApplicationState.STATUS, new VersionedValue.VersionedValueFactory(partitioner).normal(Collections.singleton(endpointTokens.get(i))));
hosts.add(ep);
}
// check that all nodes are in token metadata
for (int i = 0; i < endpointTokens.size(); ++i) assertTrue(ss.getTokenMetadata().isMember(hosts.get(i)));
}
use of org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken in project cassandra by apache.
the class OldNetworkTopologyStrategyTest method testMoveMiddleOfRing.
@SuppressWarnings("unchecked")
@Test
public void testMoveMiddleOfRing() throws UnknownHostException {
// moves to another position in the middle of the ring : should stream all its data, and fetch all its new data
int movingNodeIdx = 1;
int movingNodeIdxAfterMove = 4;
BigIntegerToken newToken = new BigIntegerToken("90070591730234615865843651857942052864");
BigIntegerToken[] tokens = initTokens();
BigIntegerToken[] tokensAfterMove = initTokensAfterMove(tokens, movingNodeIdx, newToken);
Pair<Set<Range<Token>>, Set<Range<Token>>> ranges = calculateStreamAndFetchRanges(tokens, tokensAfterMove, movingNodeIdx);
// sort the results, so they can be compared
Range<Token>[] toStream = ranges.left.toArray(new Range[0]);
Range<Token>[] toFetch = ranges.right.toArray(new Range[0]);
Arrays.sort(toStream);
Arrays.sort(toFetch);
// build expected ranges
Range<Token>[] toStreamExpected = new Range[2];
toStreamExpected[0] = new Range<Token>(getToken(movingNodeIdx - 2, tokens), getToken(movingNodeIdx - 1, tokens));
toStreamExpected[1] = new Range<Token>(getToken(movingNodeIdx - 1, tokens), getToken(movingNodeIdx, tokens));
Arrays.sort(toStreamExpected);
Range<Token>[] toFetchExpected = new Range[2];
toFetchExpected[0] = new Range<Token>(getToken(movingNodeIdxAfterMove - 1, tokens), getToken(movingNodeIdxAfterMove, tokens));
toFetchExpected[1] = new Range<Token>(getToken(movingNodeIdxAfterMove, tokensAfterMove), getToken(movingNodeIdx, tokensAfterMove));
Arrays.sort(toFetchExpected);
assertEquals(Arrays.equals(toStream, toStreamExpected), true);
assertEquals(Arrays.equals(toFetch, toFetchExpected), true);
}
use of org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken in project cassandra by apache.
the class OldNetworkTopologyStrategyTest method testMoveAfterNextNeighbors.
@SuppressWarnings("unchecked")
@Test
public void testMoveAfterNextNeighbors() throws UnknownHostException {
// moves after its next neighbor in the ring
int movingNodeIdx = 1;
int movingNodeIdxAfterMove = 2;
BigIntegerToken newToken = new BigIntegerToken("52535295865117307932921825928971026432");
BigIntegerToken[] tokens = initTokens();
BigIntegerToken[] tokensAfterMove = initTokensAfterMove(tokens, movingNodeIdx, newToken);
Pair<Set<Range<Token>>, Set<Range<Token>>> ranges = calculateStreamAndFetchRanges(tokens, tokensAfterMove, movingNodeIdx);
// sort the results, so they can be compared
Range<Token>[] toStream = ranges.left.toArray(new Range[0]);
Range<Token>[] toFetch = ranges.right.toArray(new Range[0]);
Arrays.sort(toStream);
Arrays.sort(toFetch);
// build expected ranges
Range<Token>[] toStreamExpected = new Range[1];
toStreamExpected[0] = new Range<Token>(getToken(movingNodeIdx - 2, tokens), getToken(movingNodeIdx - 1, tokens));
Arrays.sort(toStreamExpected);
Range<Token>[] toFetchExpected = new Range[2];
toFetchExpected[0] = new Range<Token>(getToken(movingNodeIdxAfterMove - 1, tokens), getToken(movingNodeIdxAfterMove, tokens));
toFetchExpected[1] = new Range<Token>(getToken(movingNodeIdxAfterMove, tokensAfterMove), getToken(movingNodeIdx, tokensAfterMove));
Arrays.sort(toFetchExpected);
assertEquals(Arrays.equals(toStream, toStreamExpected), true);
assertEquals(Arrays.equals(toFetch, toFetchExpected), true);
}
use of org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken in project cassandra by apache.
the class OldNetworkTopologyStrategyTest method testMoveLeft.
/**
* test basic methods to move a node. For sure, it's not the best place, but it's easy to test
*
* @throws java.net.UnknownHostException
*/
@Test
public void testMoveLeft() throws UnknownHostException {
// Moves to the left : nothing to fetch, last part to stream
int movingNodeIdx = 1;
BigIntegerToken newToken = new BigIntegerToken("21267647932558653966460912964485513216");
BigIntegerToken[] tokens = initTokens();
BigIntegerToken[] tokensAfterMove = initTokensAfterMove(tokens, movingNodeIdx, newToken);
Pair<Set<Range<Token>>, Set<Range<Token>>> ranges = calculateStreamAndFetchRanges(tokens, tokensAfterMove, movingNodeIdx);
assertEquals(ranges.left.iterator().next().left, tokensAfterMove[movingNodeIdx]);
assertEquals(ranges.left.iterator().next().right, tokens[movingNodeIdx]);
assertEquals("No data should be fetched", ranges.right.size(), 0);
}
use of org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken in project cassandra by apache.
the class OldNetworkTopologyStrategyTest method initTokenMetadata.
private TokenMetadata initTokenMetadata(BigIntegerToken[] tokens) throws UnknownHostException {
TokenMetadata tokenMetadataCurrent = new TokenMetadata();
int lastIPPart = 1;
for (BigIntegerToken token : tokens) tokenMetadataCurrent.updateNormalToken(token, InetAddress.getByName("254.0.0." + Integer.toString(lastIPPart++)));
return tokenMetadataCurrent;
}
Aggregations