use of org.bboxdb.distribution.membership.BBoxDBInstance in project bboxdb by jnidzwetzki.
the class DistributionRegionHelper method getRegionsForPredicateAndBox.
/**
* Add the leaf nodes systems that are covered by the bounding box
* @param rootRegion
* @param boundingBox
* @param systems
* @return
*/
public static List<RoutingHop> getRegionsForPredicateAndBox(final DistributionRegion rootRegion, final BoundingBox boundingBox, final Predicate<DistributionRegionState> statePredicate) {
final Map<InetSocketAddress, RoutingHop> hops = new HashMap<>();
final List<DistributionRegion> regions = rootRegion.getThisAndChildRegions();
for (final DistributionRegion region : regions) {
if (!boundingBox.overlaps(region.getConveringBox())) {
continue;
}
if (!statePredicate.test(region.getState())) {
continue;
}
for (final BBoxDBInstance system : region.getSystems()) {
if (!hops.containsKey(system.getInetSocketAddress())) {
final RoutingHop routingHop = new RoutingHop(system, new ArrayList<Long>());
hops.put(system.getInetSocketAddress(), routingHop);
}
final RoutingHop routingHop = hops.get(system.getInetSocketAddress());
routingHop.addRegion(region.getRegionId());
}
}
return new ArrayList<>(hops.values());
}
use of org.bboxdb.distribution.membership.BBoxDBInstance in project bboxdb by jnidzwetzki.
the class DistributionRegionHelper method getOutdatedRegions.
/**
* Find the outdated regions for the distributed instance
* @param region
* @param distributedInstance
* @return
* @throws BBoxDBException
*/
public static List<OutdatedDistributionRegion> getOutdatedRegions(final DistributionRegion region, final BBoxDBInstance distributedInstance) throws BBoxDBException {
final List<OutdatedDistributionRegion> result = new ArrayList<>();
if (region == null) {
return result;
}
final List<DistributionRegion> regions = region.getThisAndChildRegions().stream().filter(r -> r.getSystems().contains(distributedInstance)).collect(Collectors.toList());
for (final DistributionRegion regionToInspect : regions) {
try {
final OutdatedDistributionRegion regionResult = processRegion(distributedInstance, ZookeeperClientFactory.getZookeeperClient(), regionToInspect);
if (regionResult != null) {
result.add(regionResult);
}
} catch (ZookeeperException e) {
throw new BBoxDBException(e);
}
}
return result;
}
use of org.bboxdb.distribution.membership.BBoxDBInstance in project bboxdb by jnidzwetzki.
the class DistributionRegionSyncer method updateLocalMappings.
/**
* Update the local mappings with the systems for region
* @param region
* @param systems
*/
private void updateLocalMappings() {
if (rootNode == null || distributionGroupName == null) {
logger.debug("Root node is {}, distributionGroupNameIs {}", rootNode, distributionGroupName);
return;
}
final BBoxDBInstance localInstance = ZookeeperClientFactory.getLocalInstanceName();
if (localInstance == null) {
logger.debug("Local instance name is not set, so no local mapping is possible");
return;
}
final List<DistributionRegion> allChildren = rootNode.getThisAndChildRegions();
final Set<Long> allExistingMappings = new HashSet<>(distributionRegionMapper.getAllRegionIds());
for (final DistributionRegion region : allChildren) {
final long regionId = region.getRegionId();
logger.debug("Processing region {}", regionId);
if (!region.getSystems().contains(localInstance)) {
continue;
}
if (DistributionRegionHelper.STATES_WRITE.contains(region.getState())) {
// Add the mapping to the nameprefix mapper
if (!allExistingMappings.contains(regionId)) {
distributionRegionMapper.addMapping(regionId, region.getConveringBox());
}
allExistingMappings.remove(regionId);
}
}
// Remove all active but not seen mappings
allExistingMappings.forEach(m -> distributionRegionMapper.removeMapping(m));
}
use of org.bboxdb.distribution.membership.BBoxDBInstance 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.membership.BBoxDBInstance in project bboxdb by jnidzwetzki.
the class DistributionRegionAdapter method processStatistics.
/**
* @param result
* @param statisticsPath
* @param childs
* @throws ZookeeperException
*/
private void processStatistics(final Map<BBoxDBInstance, Map<String, Long>> result, final String statisticsPath, final List<String> childs) throws ZookeeperException {
for (final String system : childs) {
final String path = statisticsPath + "/" + system;
final Map<String, Long> systemMap = new HashMap<>();
try {
final String sizePath = path + "/" + ZookeeperNodeNames.NAME_STATISTICS_TOTAL_SIZE;
if (zookeeperClient.exists(sizePath)) {
final String sizeString = zookeeperClient.readPathAndReturnString(sizePath);
final long size = MathUtil.tryParseLong(sizeString, () -> "Unable to parse " + sizeString);
systemMap.put(ZookeeperNodeNames.NAME_STATISTICS_TOTAL_SIZE, size);
}
final String tuplePath = path + "/" + ZookeeperNodeNames.NAME_STATISTICS_TOTAL_TUPLES;
if (zookeeperClient.exists(tuplePath)) {
final String tuplesString = zookeeperClient.readPathAndReturnString(tuplePath);
final long tuples = MathUtil.tryParseLong(tuplesString, () -> "Unable to parse " + tuplesString);
systemMap.put(ZookeeperNodeNames.NAME_STATISTICS_TOTAL_TUPLES, tuples);
}
result.put(new BBoxDBInstance(system), systemMap);
} catch (InputParseException | ZookeeperNotFoundException e) {
logger.error("Unable to read statistics", e);
}
}
}
Aggregations