Search in sources :

Example 1 with Partition

use of com.twitter.common.zookeeper.Partitioner.Partition in project commons by twitter.

the class PartitionerTest method testJoin.

@Test
public void testJoin() throws Exception {
    // Test that the 1st member of the partition group owns the whole space.
    InstrumentedPartitioner firstPartitioner = new InstrumentedPartitioner(createZkClient());
    Partition firstPartition = join(firstPartitioner);
    assertTrue(firstPartition.isMember(0L));
    assertTrue(firstPartition.isMember(1L));
    assertTrue(firstPartition.isMember(2L));
    // Test that when additional members join partitions are added and existing partitions shrink.
    InstrumentedPartitioner secondPartitioner = new InstrumentedPartitioner(createZkClient());
    Partition secondPartition = join(secondPartitioner);
    firstPartitioner.observeGroupSize(2);
    assertTrue(firstPartition.isMember(0L));
    assertFalse(secondPartition.isMember(0L));
    assertFalse(firstPartition.isMember(1L));
    assertTrue(secondPartition.isMember(1L));
    assertTrue(firstPartition.isMember(2L));
    assertFalse(secondPartition.isMember(2L));
    InstrumentedPartitioner thirdPartitioner = new InstrumentedPartitioner(createZkClient());
    Partition thirdPartition = join(thirdPartitioner);
    firstPartitioner.observeGroupSize(3);
    secondPartitioner.observeGroupSize(3);
    assertTrue(firstPartition.isMember(0L));
    assertFalse(secondPartition.isMember(0L));
    assertFalse(thirdPartition.isMember(0L));
    assertFalse(firstPartition.isMember(1L));
    assertTrue(secondPartition.isMember(1L));
    assertFalse(thirdPartition.isMember(1L));
    assertFalse(firstPartition.isMember(2L));
    assertFalse(secondPartition.isMember(2L));
    assertTrue(thirdPartition.isMember(2L));
    assertTrue(firstPartition.isMember(3L));
    assertFalse(secondPartition.isMember(3L));
    assertFalse(thirdPartition.isMember(3L));
    // Test that members leaving the partition group results in the partitions being merged.
    firstPartition.cancel();
    secondPartitioner.observeGroupSize(2);
    thirdPartitioner.observeGroupSize(2);
    assertTrue(secondPartition.isMember(0L));
    assertFalse(thirdPartition.isMember(0L));
    assertFalse(secondPartition.isMember(1L));
    assertTrue(thirdPartition.isMember(1L));
    assertTrue(secondPartition.isMember(2L));
    assertFalse(thirdPartition.isMember(2L));
    thirdPartition.cancel();
    secondPartitioner.observeGroupSize(1);
    assertTrue(secondPartition.isMember(0L));
    assertTrue(secondPartition.isMember(1L));
    assertTrue(secondPartition.isMember(2L));
}
Also used : Partition(com.twitter.common.zookeeper.Partitioner.Partition) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Example 2 with Partition

use of com.twitter.common.zookeeper.Partitioner.Partition in project commons by twitter.

the class PartitionerTest method join.

private Partition join(Partitioner partitioner) throws JoinException, InterruptedException {
    final Partition partition = partitioner.join();
    addTearDown(new TearDown() {

        @Override
        public void tearDown() throws JoinException {
            partition.cancel();
        }
    });
    return partition;
}
Also used : Partition(com.twitter.common.zookeeper.Partitioner.Partition) TearDown(com.google.common.testing.TearDown) JoinException(com.twitter.common.zookeeper.Group.JoinException)

Aggregations

Partition (com.twitter.common.zookeeper.Partitioner.Partition)2 TearDown (com.google.common.testing.TearDown)1 JoinException (com.twitter.common.zookeeper.Group.JoinException)1 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)1 Test (org.junit.Test)1