Search in sources :

Example 1 with BigIntegerToken

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)));
}
Also used : BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) InetAddress(java.net.InetAddress)

Example 2 with BigIntegerToken

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);
}
Also used : Set(java.util.Set) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) Test(org.junit.Test)

Example 3 with BigIntegerToken

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);
}
Also used : Set(java.util.Set) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) Test(org.junit.Test)

Example 4 with BigIntegerToken

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);
}
Also used : Set(java.util.Set) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) Test(org.junit.Test)

Example 5 with BigIntegerToken

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;
}
Also used : BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken)

Aggregations

BigIntegerToken (org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken)37 Test (org.junit.Test)27 Token (org.apache.cassandra.dht.Token)15 BytesToken (org.apache.cassandra.dht.ByteOrderedPartitioner.BytesToken)11 Set (java.util.Set)6 Range (org.apache.cassandra.dht.Range)6 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)5 InetAddress (java.net.InetAddress)4 VersionedValue (org.apache.cassandra.gms.VersionedValue)4 EndpointsForToken (org.apache.cassandra.locator.EndpointsForToken)4 TokenMetadata (org.apache.cassandra.locator.TokenMetadata)4 RangesAtEndpoint (org.apache.cassandra.locator.RangesAtEndpoint)3 RangesByEndpoint (org.apache.cassandra.locator.RangesByEndpoint)3 HashMultimap (com.google.common.collect.HashMultimap)2 Multimap (com.google.common.collect.Multimap)2 ArrayList (java.util.ArrayList)2 IPartitioner (org.apache.cassandra.dht.IPartitioner)2 StringToken (org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken)2 AbstractReplicationStrategy (org.apache.cassandra.locator.AbstractReplicationStrategy)2 RowHash (org.apache.cassandra.utils.MerkleTree.RowHash)2