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