Search in sources :

Example 26 with BBoxDBInstance

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());
}
Also used : HashMap(java.util.HashMap) OutdatedDistributionRegion(org.bboxdb.distribution.OutdatedDistributionRegion) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) RoutingHop(org.bboxdb.network.routing.RoutingHop) BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance)

Example 27 with BBoxDBInstance

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;
}
Also used : Arrays(java.util.Arrays) OutdatedDistributionRegion(org.bboxdb.distribution.OutdatedDistributionRegion) Multiset(com.google.common.collect.Multiset) RoutingHop(org.bboxdb.network.routing.RoutingHop) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) DistributionRegionState(org.bboxdb.distribution.partitioner.DistributionRegionState) BoundingBox(org.bboxdb.commons.math.BoundingBox) ZookeeperClient(org.bboxdb.distribution.zookeeper.ZookeeperClient) ArrayList(java.util.ArrayList) Const(org.bboxdb.misc.Const) ZookeeperException(org.bboxdb.distribution.zookeeper.ZookeeperException) HashMultiset(com.google.common.collect.HashMultiset) Map(java.util.Map) BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance) ZookeeperClientFactory(org.bboxdb.distribution.zookeeper.ZookeeperClientFactory) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Retryer(org.bboxdb.commons.Retryer) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) List(java.util.List) Entry(java.util.Map.Entry) Optional(java.util.Optional) BBoxDBException(org.bboxdb.misc.BBoxDBException) ZookeeperException(org.bboxdb.distribution.zookeeper.ZookeeperException) OutdatedDistributionRegion(org.bboxdb.distribution.OutdatedDistributionRegion) OutdatedDistributionRegion(org.bboxdb.distribution.OutdatedDistributionRegion) ArrayList(java.util.ArrayList) BBoxDBException(org.bboxdb.misc.BBoxDBException)

Example 28 with BBoxDBInstance

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));
}
Also used : BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance) HashSet(java.util.HashSet)

Example 29 with BBoxDBInstance

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());
    }
}
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 30 with BBoxDBInstance

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);
        }
    }
}
Also used : InputParseException(org.bboxdb.commons.InputParseException) HashMap(java.util.HashMap) BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance)

Aggregations

BBoxDBInstance (org.bboxdb.distribution.membership.BBoxDBInstance)78 Test (org.junit.Test)36 ArrayList (java.util.ArrayList)28 DistributionRegion (org.bboxdb.distribution.region.DistributionRegion)25 BBoxDBException (org.bboxdb.misc.BBoxDBException)16 ZookeeperClient (org.bboxdb.distribution.zookeeper.ZookeeperClient)14 List (java.util.List)13 RoutingHop (org.bboxdb.network.routing.RoutingHop)13 BBoxDBInstanceManager (org.bboxdb.distribution.membership.BBoxDBInstanceManager)11 ZookeeperException (org.bboxdb.distribution.zookeeper.ZookeeperException)11 RoutingHeader (org.bboxdb.network.routing.RoutingHeader)11 BoundingBox (org.bboxdb.commons.math.BoundingBox)9 NetworkOperationFuture (org.bboxdb.network.client.future.NetworkOperationFuture)8 Map (java.util.Map)7 HashMap (java.util.HashMap)6 ZookeeperBBoxDBInstanceAdapter (org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter)6 ZookeeperNotFoundException (org.bboxdb.distribution.zookeeper.ZookeeperNotFoundException)6 JoinedTupleListFuture (org.bboxdb.network.client.future.JoinedTupleListFuture)6 TupleListFuture (org.bboxdb.network.client.future.TupleListFuture)6 OptionalDouble (java.util.OptionalDouble)4