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));
}
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;
}
Aggregations