Search in sources :

Example 1 with ZookeeperBBoxDBInstanceAdapter

use of org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter in project bboxdb by jnidzwetzki.

the class TestDistributedInstanceManager method testChangedEventOnChange.

/**
 * Test changed event generation
 * @throws InterruptedException
 * @throws ZookeeperException
 */
@Test(timeout = 30000)
public void testChangedEventOnChange() throws InterruptedException, ZookeeperException {
    final BBoxDBInstanceManager distributedInstanceManager = BBoxDBInstanceManager.getInstance();
    final BBoxDBInstance instance1 = new BBoxDBInstance("node6:5050");
    final BBoxDBInstance instance2 = new BBoxDBInstance("node7:5050");
    final ZookeeperClient zookeeperClient1 = getNewZookeeperClient(instance1);
    final ZookeeperBBoxDBInstanceAdapter zookeeperBBoxDBInstanceAdapter1 = new ZookeeperBBoxDBInstanceAdapter(zookeeperClient1);
    BBoxDBInstanceManager.getInstance().startMembershipObserver(zookeeperClient1);
    final ZookeeperClient zookeeperClient2 = getNewZookeeperClient(instance2);
    final ZookeeperBBoxDBInstanceAdapter zookeeperBBoxDBInstanceAdapter2 = new ZookeeperBBoxDBInstanceAdapter(zookeeperClient2);
    zookeeperBBoxDBInstanceAdapter1.updateStateData(instance1, BBoxDBInstanceState.READY);
    zookeeperBBoxDBInstanceAdapter2.updateStateData(instance2, BBoxDBInstanceState.READY);
    Thread.sleep(5000);
    final CountDownLatch changedLatch = new CountDownLatch(1);
    distributedInstanceManager.registerListener(new BiConsumer<DistributedInstanceEvent, BBoxDBInstance>() {

        @Override
        public void accept(final DistributedInstanceEvent event, final BBoxDBInstance instance) {
            if (event == DistributedInstanceEvent.CHANGED) {
                if (instance.socketAddressEquals(instance2)) {
                    Assert.assertTrue(instance.getState() == BBoxDBInstanceState.OUTDATED);
                    changedLatch.countDown();
                }
            } else {
                // Unexpected event
                Assert.assertTrue(false);
            }
        }
    });
    zookeeperBBoxDBInstanceAdapter2.updateStateData(instance2, BBoxDBInstanceState.OUTDATED);
    changedLatch.await();
    distributedInstanceManager.removeAllListener();
    zookeeperClient1.shutdown();
    zookeeperClient2.shutdown();
}
Also used : ZookeeperClient(org.bboxdb.distribution.zookeeper.ZookeeperClient) BBoxDBInstanceManager(org.bboxdb.distribution.membership.BBoxDBInstanceManager) BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance) ZookeeperBBoxDBInstanceAdapter(org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter) DistributedInstanceEvent(org.bboxdb.distribution.membership.DistributedInstanceEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with ZookeeperBBoxDBInstanceAdapter

use of org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter in project bboxdb by jnidzwetzki.

the class TestDistributedInstanceManager method testRegisterInstance4.

/**
 * Two instances are known and changing
 * @throws ZookeeperException
 */
@Test(timeout = 60000)
public void testRegisterInstance4() throws InterruptedException, ZookeeperException {
    final BBoxDBInstanceManager distributedInstanceManager = BBoxDBInstanceManager.getInstance();
    final BBoxDBInstance instance1 = new BBoxDBInstance("node1:5050");
    final BBoxDBInstance instance2 = new BBoxDBInstance("node2:5050");
    final ZookeeperClient zookeeperClient1 = getNewZookeeperClient(instance1);
    final ZookeeperBBoxDBInstanceAdapter zookeeperBBoxDBInstanceAdapter1 = new ZookeeperBBoxDBInstanceAdapter(zookeeperClient1);
    final ZookeeperClient zookeeperClient2 = getNewZookeeperClient(instance2);
    final ZookeeperBBoxDBInstanceAdapter zookeeperBBoxDBInstanceAdapter2 = new ZookeeperBBoxDBInstanceAdapter(zookeeperClient2);
    BBoxDBInstanceManager.getInstance().startMembershipObserver(zookeeperClient1);
    Thread.sleep(1000);
    Assert.assertEquals(2, distributedInstanceManager.getInstances().size());
    Assert.assertTrue(BBoxDBInstanceState.FAILED == distributedInstanceManager.getInstances().get(0).getState());
    Assert.assertTrue(BBoxDBInstanceState.FAILED == distributedInstanceManager.getInstances().get(1).getState());
    // Change instance 1
    zookeeperBBoxDBInstanceAdapter1.updateStateData(instance1, BBoxDBInstanceState.OUTDATED);
    Thread.sleep(1000);
    for (final BBoxDBInstance instance : distributedInstanceManager.getInstances()) {
        if (instance.socketAddressEquals(instance1)) {
            Assert.assertTrue(instance.getState() == BBoxDBInstanceState.OUTDATED);
        } else {
            Assert.assertTrue(instance.getState() == BBoxDBInstanceState.FAILED);
        }
    }
    // Change instance 2
    zookeeperBBoxDBInstanceAdapter2.updateStateData(instance2, BBoxDBInstanceState.READY);
    Thread.sleep(2000);
    for (final BBoxDBInstance instance : distributedInstanceManager.getInstances()) {
        if (instance.socketAddressEquals(instance1)) {
            Assert.assertTrue(instance.getState() == BBoxDBInstanceState.OUTDATED);
        } else {
            Assert.assertTrue(instance.getState() == BBoxDBInstanceState.READY);
        }
    }
    zookeeperClient1.shutdown();
    zookeeperClient2.shutdown();
}
Also used : ZookeeperClient(org.bboxdb.distribution.zookeeper.ZookeeperClient) BBoxDBInstanceManager(org.bboxdb.distribution.membership.BBoxDBInstanceManager) BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance) ZookeeperBBoxDBInstanceAdapter(org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter) Test(org.junit.Test)

Example 3 with ZookeeperBBoxDBInstanceAdapter

use of org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter in project bboxdb by jnidzwetzki.

the class DistributedRecoveryService method init.

@Override
public void init() {
    try {
        final ZookeeperClient zookeeperClient = ZookeeperClientFactory.getZookeeperClient();
        final ZookeeperBBoxDBInstanceAdapter zookeeperBBoxDBInstanceAdapter = new ZookeeperBBoxDBInstanceAdapter(zookeeperClient);
        final BBoxDBInstance distributedInstance = ZookeeperClientFactory.getLocalInstanceName();
        zookeeperBBoxDBInstanceAdapter.updateStateData(distributedInstance, BBoxDBInstanceState.OUTDATED);
        logger.info("Running recovery for local stored data");
        runRecovery();
        logger.info("Running recovery for local stored data DONE");
        zookeeperBBoxDBInstanceAdapter.updateStateData(distributedInstance, BBoxDBInstanceState.READY);
    } catch (ZookeeperException | ZookeeperNotFoundException e) {
        logger.error("Got an exception during recovery: ", e);
    }
}
Also used : ZookeeperClient(org.bboxdb.distribution.zookeeper.ZookeeperClient) ZookeeperException(org.bboxdb.distribution.zookeeper.ZookeeperException) ZookeeperNotFoundException(org.bboxdb.distribution.zookeeper.ZookeeperNotFoundException) ZookeeperBBoxDBInstanceAdapter(org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter) BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance)

Example 4 with ZookeeperBBoxDBInstanceAdapter

use of org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter in project bboxdb by jnidzwetzki.

the class ZookeeperInstanceRegisterer method shutdown.

@Override
public void shutdown() {
    try {
        final ZookeeperBBoxDBInstanceAdapter zookeeperBBoxDBInstanceAdapter = new ZookeeperBBoxDBInstanceAdapter(zookeeperClient);
        instance.setState(BBoxDBInstanceState.FAILED);
        zookeeperBBoxDBInstanceAdapter.updateStateData(instance);
    } catch (Exception e) {
        logger.error("Exception while updating instance", e);
    }
}
Also used : ZookeeperBBoxDBInstanceAdapter(org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter)

Example 5 with ZookeeperBBoxDBInstanceAdapter

use of org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter in project bboxdb by jnidzwetzki.

the class ZookeeperInstanceRegisterer method init.

@Override
public void init() {
    if (instance == null) {
        logger.error("Unable to determine local instance name");
        return;
    }
    try {
        final ZookeeperBBoxDBInstanceAdapter zookeeperBBoxDBInstanceAdapter = new ZookeeperBBoxDBInstanceAdapter(zookeeperClient);
        zookeeperBBoxDBInstanceAdapter.updateNodeInfo(instance);
        zookeeperBBoxDBInstanceAdapter.updateStateData(instance);
    } catch (ZookeeperException e) {
        logger.error("Exception while registering instance", e);
    }
}
Also used : ZookeeperBBoxDBInstanceAdapter(org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter)

Aggregations

ZookeeperBBoxDBInstanceAdapter (org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter)8 BBoxDBInstance (org.bboxdb.distribution.membership.BBoxDBInstance)6 ZookeeperClient (org.bboxdb.distribution.zookeeper.ZookeeperClient)6 BBoxDBInstanceManager (org.bboxdb.distribution.membership.BBoxDBInstanceManager)4 Test (org.junit.Test)4 CountDownLatch (java.util.concurrent.CountDownLatch)2 DistributedInstanceEvent (org.bboxdb.distribution.membership.DistributedInstanceEvent)2 ZookeeperException (org.bboxdb.distribution.zookeeper.ZookeeperException)2 ZookeeperNotFoundException (org.bboxdb.distribution.zookeeper.ZookeeperNotFoundException)1