Search in sources :

Example 6 with TreeRange

use of org.apache.cassandra.utils.MerkleTree.TreeRange in project eiger by wlloyd.

the class MerkleTreeTest method testHashRandom.

@Test
public void testHashRandom() {
    int max = 1000000;
    TOKEN_SCALE = new BigInteger("" + max);
    mt = new MerkleTree(partitioner, fullRange(), RECOMMENDED_DEPTH, 32);
    Random random = new Random();
    while (true) {
        if (!mt.split(tok(random.nextInt(max))))
            break;
    }
    // validate the tree
    TreeRangeIterator ranges = mt.invalids();
    for (TreeRange range : ranges) range.addHash(new RowHash(range.right, new byte[0]));
    assert null != mt.hash(new Range(tok(-1), tok(-1))) : "Could not hash tree " + mt;
}
Also used : RowHash(org.apache.cassandra.utils.MerkleTree.RowHash) Random(java.util.Random) TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) BigInteger(java.math.BigInteger) Range(org.apache.cassandra.dht.Range) TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) TreeRangeIterator(org.apache.cassandra.utils.MerkleTree.TreeRangeIterator) Test(org.junit.Test)

Example 7 with TreeRange

use of org.apache.cassandra.utils.MerkleTree.TreeRange in project eiger by wlloyd.

the class MerkleTreeTest method testDifference.

@Test
public void testDifference() {
    Range full = new Range(tok(-1), tok(-1));
    int maxsize = 16;
    mt.maxsize(maxsize);
    MerkleTree mt2 = new MerkleTree(partitioner, fullRange(), RECOMMENDED_DEPTH, maxsize);
    mt.init();
    mt2.init();
    // add dummy hashes to both trees
    for (TreeRange range : mt.invalids()) range.addAll(new HIterator(range.right));
    for (TreeRange range : mt2.invalids()) range.addAll(new HIterator(range.right));
    TreeRange leftmost = null;
    TreeRange middle = null;
    // give some room for splitting
    mt.maxsize(maxsize + 2);
    // split the leftmost
    Iterator<TreeRange> ranges = mt.invalids();
    leftmost = ranges.next();
    mt.split(leftmost.right);
    // set the hashes for the leaf of the created split
    middle = mt.get(leftmost.right);
    middle.hash("arbitrary!".getBytes());
    mt.get(partitioner.midpoint(leftmost.left, leftmost.right)).hash("even more arbitrary!".getBytes());
    // trees should disagree for (leftmost.left, middle.right]
    List<TreeRange> diffs = MerkleTree.difference(mt, mt2);
    assertEquals(diffs + " contains wrong number of differences:", 1, diffs.size());
    assertTrue(diffs.contains(new Range(leftmost.left, middle.right)));
}
Also used : TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) Range(org.apache.cassandra.dht.Range) TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) Test(org.junit.Test)

Example 8 with TreeRange

use of org.apache.cassandra.utils.MerkleTree.TreeRange in project cassandra by apache.

the class MerkleTreeTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    Range<Token> full = new Range<>(tok(-1), tok(-1));
    // populate and validate the tree
    mt.maxsize(256);
    mt.init();
    for (TreeRange range : mt.invalids()) range.addAll(new HIterator(range.right));
    byte[] initialhash = mt.hash(full);
    DataOutputBuffer out = new DataOutputBuffer();
    MerkleTree.serializer.serialize(mt, out, MessagingService.current_version);
    byte[] serialized = out.toByteArray();
    DataInputPlus in = new DataInputBuffer(serialized);
    MerkleTree restored = MerkleTree.serializer.deserialize(in, MessagingService.current_version);
    assertHashEquals(initialhash, restored.hash(full));
}
Also used : DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) Range(org.apache.cassandra.dht.Range) TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) Test(org.junit.Test)

Example 9 with TreeRange

use of org.apache.cassandra.utils.MerkleTree.TreeRange in project cassandra by apache.

the class MerkleTreeTest method testDifference.

@Test
public void testDifference() {
    int maxsize = 16;
    mt.maxsize(maxsize);
    MerkleTree mt2 = new MerkleTree(partitioner, fullRange(), RECOMMENDED_DEPTH, maxsize);
    mt.init();
    mt2.init();
    // add dummy hashes to both trees
    for (TreeRange range : mt.invalids()) range.addAll(new HIterator(range.right));
    for (TreeRange range : mt2.invalids()) range.addAll(new HIterator(range.right));
    TreeRange leftmost = null;
    TreeRange middle = null;
    // give some room for splitting
    mt.maxsize(maxsize + 2);
    // split the leftmost
    Iterator<TreeRange> ranges = mt.invalids();
    leftmost = ranges.next();
    mt.split(leftmost.right);
    // set the hashes for the leaf of the created split
    middle = mt.get(leftmost.right);
    middle.hash("arbitrary!".getBytes());
    mt.get(partitioner.midpoint(leftmost.left, leftmost.right)).hash("even more arbitrary!".getBytes());
    // trees should disagree for (leftmost.left, middle.right]
    List<TreeRange> diffs = MerkleTree.difference(mt, mt2);
    assertEquals(diffs + " contains wrong number of differences:", 1, diffs.size());
    assertTrue(diffs.contains(new Range<>(leftmost.left, middle.right)));
}
Also used : TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) Range(org.apache.cassandra.dht.Range) TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) Test(org.junit.Test)

Example 10 with TreeRange

use of org.apache.cassandra.utils.MerkleTree.TreeRange in project cassandra by apache.

the class MerkleTreeTest method testValidateTree.

/**
     * Generate two trees with different splits, but containing the same keys, and
     * check that they compare equally.
     *
     * The set of keys used in this test is: #{2,4,6,8,12,14,0}
     */
@Test
public void testValidateTree() {
    // this test needs slightly more resolution
    TOKEN_SCALE = new BigInteger("16");
    Range<Token> full = new Range<>(tok(-1), tok(-1));
    Iterator<TreeRange> ranges;
    MerkleTree mt2 = new MerkleTree(partitioner, fullRange(), RECOMMENDED_DEPTH, Integer.MAX_VALUE);
    mt.split(tok(8));
    mt.split(tok(4));
    mt.split(tok(12));
    mt.split(tok(6));
    mt.split(tok(10));
    ranges = mt.invalids();
    // (-1,4]: depth 2
    ranges.next().addAll(new HIterator(2, 4));
    // (4,6]
    ranges.next().addAll(new HIterator(6));
    // (6,8]
    ranges.next().addAll(new HIterator(8));
    // (8,10]
    ranges.next().addAll(new HIterator(/*empty*/
    new int[0]));
    // (10,12]
    ranges.next().addAll(new HIterator(12));
    // (12,-1]: depth 2
    ranges.next().addAll(new HIterator(14, -1));
    mt2.split(tok(8));
    mt2.split(tok(4));
    mt2.split(tok(12));
    mt2.split(tok(2));
    mt2.split(tok(10));
    mt2.split(tok(9));
    mt2.split(tok(11));
    ranges = mt2.invalids();
    // (-1,2]
    ranges.next().addAll(new HIterator(2));
    // (2,4]
    ranges.next().addAll(new HIterator(4));
    // (4,8]: depth 2
    ranges.next().addAll(new HIterator(6, 8));
    // (8,9]
    ranges.next().addAll(new HIterator(/*empty*/
    new int[0]));
    // (9,10]
    ranges.next().addAll(new HIterator(/*empty*/
    new int[0]));
    // (10,11]: depth 4
    ranges.next().addAll(new HIterator(/*empty*/
    new int[0]));
    // (11,12]: depth 4
    ranges.next().addAll(new HIterator(12));
    // (12,-1]: depth 2
    ranges.next().addAll(new HIterator(14, -1));
    byte[] mthash = mt.hash(full);
    byte[] mt2hash = mt2.hash(full);
    assertHashEquals("Tree hashes did not match: " + mt + " && " + mt2, mthash, mt2hash);
}
Also used : TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) BigInteger(java.math.BigInteger) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) TreeRange(org.apache.cassandra.utils.MerkleTree.TreeRange) Test(org.junit.Test)

Aggregations

TreeRange (org.apache.cassandra.utils.MerkleTree.TreeRange)13 Test (org.junit.Test)13 Range (org.apache.cassandra.dht.Range)9 BigInteger (java.math.BigInteger)6 BigIntegerToken (org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken)4 RowHash (org.apache.cassandra.utils.MerkleTree.RowHash)3 Token (org.apache.cassandra.dht.Token)2 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)2 DataOutputBuffer (org.apache.cassandra.io.util.DataOutputBuffer)2 TreeRangeIterator (org.apache.cassandra.utils.MerkleTree.TreeRangeIterator)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Random (java.util.Random)1 DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)1 TreeRangeIterator (org.apache.cassandra.utils.MerkleTrees.TreeRangeIterator)1