Search in sources :

Example 1 with DistributionRegionState

use of org.bboxdb.distribution.partitioner.DistributionRegionState in project bboxdb by jnidzwetzki.

the class TestKDtreeSpacePartitioner method testDistributionRegionSplitAndMerge.

/**
 * Test the split of a distribution region
 * @throws Exception
 */
@Test(timeout = 60000)
public void testDistributionRegionSplitAndMerge() throws Exception {
    final DistributionRegionAdapter distributionRegionAdapter = ZookeeperClientFactory.getZookeeperClient().getDistributionRegionAdapter();
    // Split and update
    System.out.println("---> Get space partitioner");
    final KDtreeSpacePartitioner spacePartitioner = (KDtreeSpacePartitioner) getSpacePartitioner();
    System.out.println("---> Get space partitioner - DONE");
    final DistributionRegion rootNode = spacePartitioner.getRootNode();
    System.out.println("---> Get root node - DONE");
    Assert.assertEquals(0, rootNode.getRegionId());
    Assert.assertEquals(TEST_GROUP, rootNode.getDistributionGroupName());
    final DistributionRegionState stateForDistributionRegion1 = distributionRegionAdapter.getStateForDistributionRegion(rootNode);
    Assert.assertEquals(DistributionRegionState.ACTIVE, stateForDistributionRegion1);
    System.out.println("--> Set split for node");
    spacePartitioner.splitNode(rootNode, 10);
    spacePartitioner.waitForSplitCompleteZookeeperCallback(rootNode, 2);
    final DistributionRegion firstChild = rootNode.getDirectChildren().get(0);
    Assert.assertEquals(10.0, firstChild.getConveringBox().getCoordinateHigh(0), DELTA);
    final DistributionRegionState stateForDistributionRegion2 = distributionRegionAdapter.getStateForDistributionRegion(rootNode);
    Assert.assertEquals(DistributionRegionState.SPLITTING, stateForDistributionRegion2);
    // Reread group from zookeeper
    final DistributionRegion newDistributionGroup = spacePartitioner.getRootNode();
    final DistributionRegion firstChildNew = newDistributionGroup.getDirectChildren().get(0);
    Assert.assertEquals(10.0, firstChildNew.getConveringBox().getCoordinateHigh(0), DELTA);
    final DistributionRegionState stateForDistributionRegion3 = distributionRegionAdapter.getStateForDistributionRegion(newDistributionGroup);
    Assert.assertEquals(DistributionRegionState.SPLITTING, stateForDistributionRegion3);
    Assert.assertEquals(1, rootNode.getDirectChildren().get(0).getRegionId());
    Assert.assertEquals(2, rootNode.getDirectChildren().get(1).getRegionId());
    // Delete children
    System.out.println("---> Calling prepare merge");
    spacePartitioner.prepareMerge(spacePartitioner.getRootNode().getDirectChildren(), spacePartitioner.getRootNode());
    System.out.println("---> Calling merge complete");
    spacePartitioner.mergeComplete(spacePartitioner.getRootNode().getDirectChildren(), spacePartitioner.getRootNode());
    final DistributionRegion newDistributionGroup2 = spacePartitioner.getRootNode();
    final DistributionRegionState stateForDistributionRegion4 = distributionRegionAdapter.getStateForDistributionRegion(newDistributionGroup2);
    Assert.assertEquals(DistributionRegionState.ACTIVE, stateForDistributionRegion4);
}
Also used : DistributionRegion(org.bboxdb.distribution.region.DistributionRegion) DistributionRegionState(org.bboxdb.distribution.partitioner.DistributionRegionState) DistributionRegionAdapter(org.bboxdb.distribution.zookeeper.DistributionRegionAdapter) KDtreeSpacePartitioner(org.bboxdb.distribution.partitioner.KDtreeSpacePartitioner) Test(org.junit.Test)

Example 2 with DistributionRegionState

use of org.bboxdb.distribution.partitioner.DistributionRegionState in project bboxdb by jnidzwetzki.

the class DistributionRegionSyncer method updateNode.

/**
 * Update the given node
 * @param nodePath
 * @param region
 * @throws InterruptedException
 */
private void updateNode(final String nodePath, final DistributionRegion region) throws InterruptedException {
    logger.debug("updateNode called with node {}", nodePath);
    final Watcher callbackWatcher = this;
    final Retryer<Boolean> retryer = new Retryer<>(10, 100, () -> {
        try {
            final Collection<BBoxDBInstance> systemsForDistributionRegion = distributionRegionAdapter.getSystemsForDistributionRegion(region);
            region.setSystems(systemsForDistributionRegion);
            final int regionId = distributionGroupAdapter.getRegionIdForPath(nodePath);
            if (region.getRegionId() != regionId) {
                throw new RuntimeException("Replacing region id " + region.getRegionId() + " with " + regionId + " on " + nodePath);
            }
            final DistributionRegionState regionState = distributionRegionAdapter.getStateForDistributionRegion(nodePath, callbackWatcher);
            region.setState(regionState);
            updateChildrenForRegion(nodePath, region);
        } catch (ZookeeperNotFoundException e) {
            // Node is deleted, let the deletion callback remove the node
            logger.debug("Skippping node update for path {}, node is deleted", nodePath);
        }
        return true;
    });
    retryer.execute();
    if (!retryer.isSuccessfully()) {
        logger.error("Got error while rereading tree", retryer.getLastException());
    }
}
Also used : Retryer(org.bboxdb.commons.Retryer) ZookeeperNotFoundException(org.bboxdb.distribution.zookeeper.ZookeeperNotFoundException) Watcher(org.apache.zookeeper.Watcher) DistributionRegionState(org.bboxdb.distribution.partitioner.DistributionRegionState) BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance)

Example 3 with DistributionRegionState

use of org.bboxdb.distribution.partitioner.DistributionRegionState in project bboxdb by jnidzwetzki.

the class RegionSplitter method tryToSetToFullSplitting.

/**
 * Try to set the region split state
 *
 * @param region
 * @param distributionRegionAdapter
 * @return
 */
private boolean tryToSetToFullSplitting(final DistributionRegion region, final DistributionRegionAdapter distributionRegionAdapter) {
    try {
        // Try to set region state to full. If this fails, another node is already
        // splits the region
        final boolean setToFullResult = distributionRegionAdapter.setToFull(region);
        if (!setToFullResult) {
            final DistributionRegionState stateForDistributionRegion = distributionRegionAdapter.getStateForDistributionRegion(region);
            logger.info("Unable to set state to full for region: {}, stopping split. Old state was {}", region.getIdentifier(), stateForDistributionRegion);
            return false;
        }
    } catch (Throwable e) {
        logger.warn("Got uncought exception during split: " + region.getIdentifier(), e);
        return false;
    }
    return true;
}
Also used : DistributionRegionState(org.bboxdb.distribution.partitioner.DistributionRegionState)

Example 4 with DistributionRegionState

use of org.bboxdb.distribution.partitioner.DistributionRegionState in project bboxdb by jnidzwetzki.

the class DistributionRegionAdapter method setToSplitMerging.

/**
 * Set the given region to full (if possible)
 * @param region
 * @return
 * @throws ZookeeperException
 * @throws ZookeeperNotFoundException
 */
public boolean setToSplitMerging(final DistributionRegion region) throws ZookeeperException, ZookeeperNotFoundException {
    logger.debug("Set state for {} to merging", region.getIdentifier());
    final String zookeeperPath = getZookeeperPathForDistributionRegionState(region);
    final DistributionRegionState oldState = getStateForDistributionRegion(region);
    if (oldState != DistributionRegionState.SPLIT) {
        logger.debug("Old state is not active (old value {})", oldState);
        return false;
    }
    final boolean result = zookeeperClient.testAndReplaceValue(zookeeperPath, DistributionRegionState.SPLIT.getStringValue(), DistributionRegionState.SPLIT_MERGING.getStringValue());
    NodeMutationHelper.markNodeMutationAsComplete(zookeeperClient, zookeeperPath);
    return result;
}
Also used : DistributionRegionState(org.bboxdb.distribution.partitioner.DistributionRegionState)

Example 5 with DistributionRegionState

use of org.bboxdb.distribution.partitioner.DistributionRegionState in project bboxdb by jnidzwetzki.

the class DistributionRegionAdapter method setToFull.

/**
 * Set the given region to full (if possible)
 * @param region
 * @return
 * @throws ZookeeperException
 * @throws ZookeeperNotFoundException
 */
public boolean setToFull(final DistributionRegion region) throws ZookeeperException, ZookeeperNotFoundException {
    logger.debug("Set state for {} to full", region.getIdentifier());
    final String zookeeperPath = getZookeeperPathForDistributionRegionState(region);
    final DistributionRegionState oldState = getStateForDistributionRegion(region);
    if (oldState != DistributionRegionState.ACTIVE) {
        logger.debug("Old state is not active (old value {})", oldState);
        return false;
    }
    final boolean result = zookeeperClient.testAndReplaceValue(zookeeperPath, DistributionRegionState.ACTIVE.getStringValue(), DistributionRegionState.ACTIVE_FULL.getStringValue());
    NodeMutationHelper.markNodeMutationAsComplete(zookeeperClient, zookeeperPath);
    return result;
}
Also used : DistributionRegionState(org.bboxdb.distribution.partitioner.DistributionRegionState)

Aggregations

DistributionRegionState (org.bboxdb.distribution.partitioner.DistributionRegionState)5 Watcher (org.apache.zookeeper.Watcher)1 Retryer (org.bboxdb.commons.Retryer)1 BBoxDBInstance (org.bboxdb.distribution.membership.BBoxDBInstance)1 KDtreeSpacePartitioner (org.bboxdb.distribution.partitioner.KDtreeSpacePartitioner)1 DistributionRegion (org.bboxdb.distribution.region.DistributionRegion)1 DistributionRegionAdapter (org.bboxdb.distribution.zookeeper.DistributionRegionAdapter)1 ZookeeperNotFoundException (org.bboxdb.distribution.zookeeper.ZookeeperNotFoundException)1 Test (org.junit.Test)1