Search in sources :

Example 76 with Token

use of org.apache.cassandra.dht.Token in project eiger by wlloyd.

the class MerkleTree method splitHelper.

private Hashable splitHelper(Hashable hashable, Token pleft, Token pright, byte depth, Token t) throws StopRecursion.TooDeep {
    if (depth >= hashdepth)
        throw new StopRecursion.TooDeep();
    if (hashable instanceof Leaf) {
        Token midpoint = partitioner.midpoint(pleft, pright);
        // start exclusive). Note that we shouldn't hit that unless the full range is very small or we are fairly deep
        if (midpoint.equals(pleft) || midpoint.equals(pright))
            throw new StopRecursion.TooDeep();
        // split
        size++;
        return new Inner(midpoint, new Leaf(), new Leaf());
    }
    // else: node.
    // recurse on the matching child
    Inner node = (Inner) hashable;
    if (Range.contains(pleft, node.token, t))
        // left child contains token
        node.lchild(splitHelper(node.lchild, pleft, node.token, inc(depth), t));
    else
        // else: right child contains token
        node.rchild(splitHelper(node.rchild, node.token, pright, inc(depth), t));
    return node;
}
Also used : Token(org.apache.cassandra.dht.Token)

Example 77 with Token

use of org.apache.cassandra.dht.Token in project eiger by wlloyd.

the class MerkleTree method initHelper.

private Hashable initHelper(Token left, Token right, byte depth, byte max) {
    if (depth == max)
        // we've reached the leaves
        return new Leaf();
    Token midpoint = partitioner.midpoint(left, right);
    if (midpoint.equals(left) || midpoint.equals(right))
        return new Leaf();
    Hashable lchild = initHelper(left, midpoint, inc(depth), max);
    Hashable rchild = initHelper(midpoint, right, inc(depth), max);
    return new Inner(midpoint, lchild, rchild);
}
Also used : Token(org.apache.cassandra.dht.Token)

Example 78 with Token

use of org.apache.cassandra.dht.Token in project eiger by wlloyd.

the class MerkleTree method differenceHelper.

/**
     * TODO: This function could be optimized into a depth first traversal of
     * the two trees in parallel.
     *
     * Takes two trees and a range for which they have hashes, but are inconsistent.
     * @return FULLY_INCONSISTENT if active is inconsistent, PARTIALLY_INCONSISTENT if only a subrange is inconsistent.
     */
static int differenceHelper(MerkleTree ltree, MerkleTree rtree, List<TreeRange> diff, TreeRange active) {
    Token midpoint = ltree.partitioner().midpoint(active.left, active.right);
    TreeRange left = new TreeRange(null, active.left, midpoint, inc(active.depth), null);
    TreeRange right = new TreeRange(null, midpoint, active.right, inc(active.depth), null);
    byte[] lhash;
    byte[] rhash;
    // see if we should recurse left
    lhash = ltree.hash(left);
    rhash = rtree.hash(left);
    int ldiff = CONSISTENT;
    boolean lreso = lhash != null && rhash != null;
    if (lreso && !Arrays.equals(lhash, rhash))
        ldiff = differenceHelper(ltree, rtree, diff, left);
    else if (!lreso)
        ldiff = FULLY_INCONSISTENT;
    // see if we should recurse right
    lhash = ltree.hash(right);
    rhash = rtree.hash(right);
    int rdiff = CONSISTENT;
    boolean rreso = lhash != null && rhash != null;
    if (rreso && !Arrays.equals(lhash, rhash))
        rdiff = differenceHelper(ltree, rtree, diff, right);
    else if (!rreso)
        rdiff = FULLY_INCONSISTENT;
    if (ldiff == FULLY_INCONSISTENT && rdiff == FULLY_INCONSISTENT) {
        // both children are fully inconsistent
        return FULLY_INCONSISTENT;
    } else if (ldiff == FULLY_INCONSISTENT) {
        diff.add(left);
        return PARTIALLY_INCONSISTENT;
    } else if (rdiff == FULLY_INCONSISTENT) {
        diff.add(right);
        return PARTIALLY_INCONSISTENT;
    }
    return PARTIALLY_INCONSISTENT;
}
Also used : Token(org.apache.cassandra.dht.Token)

Example 79 with Token

use of org.apache.cassandra.dht.Token in project eiger by wlloyd.

the class SSTableReaderTest method testGetPositionsForRanges.

@Test
public void testGetPositionsForRanges() throws IOException, ExecutionException, InterruptedException {
    Table table = Table.open("Keyspace1");
    ColumnFamilyStore store = table.getColumnFamilyStore("Standard2");
    // insert data and compact to a single sstable
    CompactionManager.instance.disableAutoCompaction();
    for (int j = 0; j < 10; j++) {
        ByteBuffer key = ByteBufferUtil.bytes(String.valueOf(j));
        RowMutation rm = new RowMutation("Keyspace1", key);
        rm.add(new QueryPath("Standard2", null, ByteBufferUtil.bytes("0")), ByteBufferUtil.EMPTY_BYTE_BUFFER, j);
        rm.apply();
    }
    store.forceBlockingFlush();
    CompactionManager.instance.performMaximal(store);
    List<Range<Token>> ranges = new ArrayList<Range<Token>>();
    // 1 key
    ranges.add(new Range<Token>(t(0), t(1)));
    // 2 keys
    ranges.add(new Range<Token>(t(2), t(4)));
    // wrapping range from key to end
    ranges.add(new Range<Token>(t(6), StorageService.getPartitioner().getMinimumToken()));
    // empty range (should be ignored)
    ranges.add(new Range<Token>(t(9), t(91)));
    // confirm that positions increase continuously
    SSTableReader sstable = store.getSSTables().iterator().next();
    long previous = -1;
    for (Pair<Long, Long> section : sstable.getPositionsForRanges(ranges)) {
        assert previous <= section.left : previous + " ! < " + section.left;
        assert section.left < section.right : section.left + " ! < " + section.right;
        previous = section.right;
    }
}
Also used : ArrayList(java.util.ArrayList) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) ByteBuffer(java.nio.ByteBuffer) QueryPath(org.apache.cassandra.db.filter.QueryPath) Test(org.junit.Test)

Example 80 with Token

use of org.apache.cassandra.dht.Token in project eiger by wlloyd.

the class OldNetworkTopologyStrategyTest method testGetEndpoints.

private void testGetEndpoints(AbstractReplicationStrategy strategy, Token[] keyTokens) throws UnknownHostException {
    for (Token keyToken : keyTokens) {
        List<InetAddress> endpoints = strategy.getNaturalEndpoints(keyToken);
        for (int j = 0; j < endpoints.size(); j++) {
            ArrayList<InetAddress> hostsExpected = expectedResults.get(keyToken.toString());
            assertEquals(endpoints.get(j), hostsExpected.get(j));
        }
    }
}
Also used : Token(org.apache.cassandra.dht.Token) BigIntegerToken(org.apache.cassandra.dht.BigIntegerToken) InetAddress(java.net.InetAddress)

Aggregations

Token (org.apache.cassandra.dht.Token)173 Range (org.apache.cassandra.dht.Range)73 InetAddress (java.net.InetAddress)66 Test (org.junit.Test)65 BigIntegerToken (org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken)27 TokenMetadata (org.apache.cassandra.locator.TokenMetadata)27 IPartitioner (org.apache.cassandra.dht.IPartitioner)26 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)23 ArrayList (java.util.ArrayList)16 UUID (java.util.UUID)16 VersionedValue (org.apache.cassandra.gms.VersionedValue)15 StringToken (org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken)14 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)9 IOException (java.io.IOException)8 ByteBuffer (java.nio.ByteBuffer)8 BytesToken (org.apache.cassandra.dht.ByteOrderedPartitioner.BytesToken)8 AbstractReplicationStrategy (org.apache.cassandra.locator.AbstractReplicationStrategy)8 Set (java.util.Set)7 LongToken (org.apache.cassandra.dht.Murmur3Partitioner.LongToken)7 HashSet (java.util.HashSet)6