Search in sources :

Example 1 with MerkleTreesTest

use of org.apache.cassandra.utils.MerkleTreesTest in project cassandra by apache.

the class DifferenceHolderTest method testFromMismatchedMerkleTrees.

@Test
public void testFromMismatchedMerkleTrees() throws UnknownHostException {
    IPartitioner partitioner = Murmur3Partitioner.instance;
    Range<Token> fullRange = new Range<>(partitioner.getMinimumToken(), partitioner.getMinimumToken());
    int maxsize = 16;
    InetAddressAndPort a1 = InetAddressAndPort.getByName("127.0.0.1");
    InetAddressAndPort a2 = InetAddressAndPort.getByName("127.0.0.2");
    // merkle tree building stolen from MerkleTreesTest:
    MerkleTrees mts1 = new MerkleTrees(partitioner);
    MerkleTrees mts2 = new MerkleTrees(partitioner);
    mts1.addMerkleTree(32, fullRange);
    mts2.addMerkleTree(32, fullRange);
    mts1.init();
    mts2.init();
    // add dummy hashes to both trees
    for (MerkleTree.TreeRange range : mts1.rangeIterator()) range.addAll(new MerkleTreesTest.HIterator(range.right));
    for (MerkleTree.TreeRange range : mts2.rangeIterator()) range.addAll(new MerkleTreesTest.HIterator(range.right));
    MerkleTree.TreeRange leftmost = null;
    MerkleTree.TreeRange middle = null;
    // give some room for splitting
    mts1.maxsize(fullRange, maxsize + 2);
    // split the leftmost
    Iterator<MerkleTree.TreeRange> ranges = mts1.rangeIterator();
    leftmost = ranges.next();
    mts1.split(leftmost.right);
    // set the hashes for the leaf of the created split
    middle = mts1.get(leftmost.right);
    middle.hash(digest("arbitrary!"));
    mts1.get(partitioner.midpoint(leftmost.left, leftmost.right)).hash(digest("even more arbitrary!"));
    TreeResponse tr1 = new TreeResponse(a1, mts1);
    TreeResponse tr2 = new TreeResponse(a2, mts2);
    DifferenceHolder dh = new DifferenceHolder(Lists.newArrayList(tr1, tr2));
    assertTrue(dh.get(a1).get(a2).size() == 1);
    assertTrue(dh.hasDifferenceBetween(a1, a2, fullRange));
    // only a1 is added as a key - see comment in dh.keyHosts()
    assertEquals(Sets.newHashSet(a1), dh.keyHosts());
}
Also used : MerkleTrees(org.apache.cassandra.utils.MerkleTrees) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) MerkleTree(org.apache.cassandra.utils.MerkleTree) TreeResponse(org.apache.cassandra.repair.TreeResponse) IPartitioner(org.apache.cassandra.dht.IPartitioner) Test(org.junit.Test) MerkleTreesTest(org.apache.cassandra.utils.MerkleTreesTest)

Aggregations

IPartitioner (org.apache.cassandra.dht.IPartitioner)1 Range (org.apache.cassandra.dht.Range)1 Token (org.apache.cassandra.dht.Token)1 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)1 TreeResponse (org.apache.cassandra.repair.TreeResponse)1 MerkleTree (org.apache.cassandra.utils.MerkleTree)1 MerkleTrees (org.apache.cassandra.utils.MerkleTrees)1 MerkleTreesTest (org.apache.cassandra.utils.MerkleTreesTest)1 Test (org.junit.Test)1