Search in sources :

Example 11 with RoutingHop

use of org.bboxdb.network.routing.RoutingHop in project bboxdb by jnidzwetzki.

the class BBoxDBCluster method queryBoundingBoxAndTime.

@Override
public TupleListFuture queryBoundingBoxAndTime(final String table, final BoundingBox boundingBox, final long timestamp) throws BBoxDBException {
    if (membershipConnectionService.getNumberOfConnections() == 0) {
        throw new BBoxDBException("queryBoundingBoxAndTime called, but connection list is empty");
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Query by for bounding box {} in table {}", boundingBox, table);
    }
    final DistributionRegion distributionRegion = getRootNode(table);
    final Supplier<List<NetworkOperationFuture>> futureProvider = () -> {
        final List<RoutingHop> hops = RoutingHopHelper.getRoutingHopsForRead(distributionRegion, boundingBox);
        final List<NetworkOperationFuture> futures = new ArrayList<>();
        for (final RoutingHop hop : hops) {
            final BBoxDBInstance instance = hop.getDistributedInstance();
            final BBoxDBConnection connection = membershipConnectionService.getConnectionForInstance(instance);
            final RoutingHeader routingHeader = new RoutingHeader((short) 0, Arrays.asList(hop));
            final NetworkOperationFuture future = connection.getBboxDBClient().getBoundingBoxAndTimeFuture(table, boundingBox, timestamp, routingHeader);
            futures.add(future);
        }
        return futures;
    };
    return new TupleListFuture(futureProvider, new DoNothingDuplicateResolver(), table);
}
Also used : DistributionRegion(org.bboxdb.distribution.region.DistributionRegion) TupleListFuture(org.bboxdb.network.client.future.TupleListFuture) JoinedTupleListFuture(org.bboxdb.network.client.future.JoinedTupleListFuture) NetworkOperationFuture(org.bboxdb.network.client.future.NetworkOperationFuture) RoutingHeader(org.bboxdb.network.routing.RoutingHeader) DoNothingDuplicateResolver(org.bboxdb.storage.sstable.duplicateresolver.DoNothingDuplicateResolver) ArrayList(java.util.ArrayList) List(java.util.List) RoutingHop(org.bboxdb.network.routing.RoutingHop) BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance) BBoxDBException(org.bboxdb.misc.BBoxDBException)

Example 12 with RoutingHop

use of org.bboxdb.network.routing.RoutingHop in project bboxdb by jnidzwetzki.

the class RoutingHeaderHelper method getRoutingHeaderForLocalSystem.

/**
 * Get the routing header for the local system
 * @param table
 * @param serverAddress
 * @param tuple
 * @throws ZookeeperException
 * @throws BBoxDBException
 * @throws InterruptedException
 */
public static RoutingHeader getRoutingHeaderForLocalSystem(final String table, BoundingBox boundingBox, final boolean allowEmptyHop, final InetSocketAddress serverAddress, final boolean write) throws ZookeeperException, BBoxDBException, InterruptedException {
    final TupleStoreName ssTableName = new TupleStoreName(table);
    final String distributionGroup = ssTableName.getDistributionGroup();
    final SpacePartitioner spacepartitioner = SpacePartitionerCache.getInstance().getSpacePartitionerForGroupName(distributionGroup);
    final DistributionRegion distributionRegion = spacepartitioner.getRootNode();
    if (boundingBox == null) {
        boundingBox = BoundingBox.FULL_SPACE;
    }
    final List<RoutingHop> hops = getLocalHops(boundingBox, distributionRegion, write);
    if (hops == null || hops.isEmpty()) {
        if (!allowEmptyHop) {
            throw new BBoxDBException("Got empty result list when query for write: " + boundingBox + " / in table " + table);
        }
        return new RoutingHeader((short) 0, new ArrayList<>());
    }
    // Filter the local hop
    final List<RoutingHop> connectionHop = hops.stream().filter(r -> r.getDistributedInstance().getInetSocketAddress().equals(serverAddress)).collect(Collectors.toList());
    if (!allowEmptyHop && connectionHop.isEmpty()) {
        throw new BBoxDBException("Unable to find host " + serverAddress + " in global routing list: " + hops);
    }
    return new RoutingHeader((short) 0, connectionHop);
}
Also used : SpacePartitioner(org.bboxdb.distribution.partitioner.SpacePartitioner) Logger(org.slf4j.Logger) RoutingHeader(org.bboxdb.network.routing.RoutingHeader) LoggerFactory(org.slf4j.LoggerFactory) RoutingHop(org.bboxdb.network.routing.RoutingHop) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) BoundingBox(org.bboxdb.commons.math.BoundingBox) ArrayList(java.util.ArrayList) Const(org.bboxdb.misc.Const) TupleStoreName(org.bboxdb.storage.entity.TupleStoreName) ZookeeperException(org.bboxdb.distribution.zookeeper.ZookeeperException) List(java.util.List) DistributionRegion(org.bboxdb.distribution.region.DistributionRegion) RoutingHopHelper(org.bboxdb.network.routing.RoutingHopHelper) BBoxDBException(org.bboxdb.misc.BBoxDBException) SpacePartitionerCache(org.bboxdb.distribution.partitioner.SpacePartitionerCache) DistributionRegion(org.bboxdb.distribution.region.DistributionRegion) RoutingHeader(org.bboxdb.network.routing.RoutingHeader) TupleStoreName(org.bboxdb.storage.entity.TupleStoreName) RoutingHop(org.bboxdb.network.routing.RoutingHop) BBoxDBException(org.bboxdb.misc.BBoxDBException) SpacePartitioner(org.bboxdb.distribution.partitioner.SpacePartitioner)

Example 13 with RoutingHop

use of org.bboxdb.network.routing.RoutingHop in project bboxdb by jnidzwetzki.

the class TestRoutingHeader method testDispatchHeader.

/**
 * Test header dispatch
 */
@Test(timeout = 60000)
public void testDispatchHeader() {
    final RoutingHop hop1 = new RoutingHop(new BBoxDBInstance("host1:50500"), Arrays.asList(123l));
    final RoutingHop hop2 = new RoutingHop(new BBoxDBInstance("host2:50500"), Arrays.asList(456l));
    final List<RoutingHop> routingList = Arrays.asList(new RoutingHop[] { hop1, hop2 });
    final RoutingHeader routingHeader = new RoutingHeader((short) 0, routingList);
    Assert.assertEquals(0, routingHeader.getHop());
    Assert.assertFalse(routingHeader.reachedFinalInstance());
    Assert.assertEquals(hop1, routingHeader.getRoutingHop());
    final boolean res1 = routingHeader.dispatchToNextHop();
    Assert.assertTrue(res1);
    Assert.assertTrue(routingHeader.reachedFinalInstance());
    Assert.assertEquals(1, routingHeader.getHop());
    Assert.assertEquals(hop2, routingHeader.getRoutingHop());
    final boolean res2 = routingHeader.dispatchToNextHop();
    Assert.assertFalse(res2);
    Assert.assertTrue(routingHeader.reachedFinalInstance());
    Assert.assertEquals(1, routingHeader.getHop());
    final boolean res3 = routingHeader.dispatchToNextHop();
    Assert.assertFalse(res3);
    Assert.assertTrue(routingHeader.reachedFinalInstance());
}
Also used : RoutingHeader(org.bboxdb.network.routing.RoutingHeader) RoutingHop(org.bboxdb.network.routing.RoutingHop) BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance) Test(org.junit.Test)

Example 14 with RoutingHop

use of org.bboxdb.network.routing.RoutingHop in project bboxdb by jnidzwetzki.

the class TestRoutingHeader method testRoutingHeaderHopParser2.

/**
 * Test the hop parser
 */
@Test(timeout = 60000)
public void testRoutingHeaderHopParser2() {
    final RoutingHop hop1 = new RoutingHop(new BBoxDBInstance("host1:50500"), Arrays.asList(123l));
    final RoutingHop hop2 = new RoutingHop(new BBoxDBInstance("host2:50500"), Arrays.asList(456l));
    final RoutingHop hop3 = new RoutingHop(new BBoxDBInstance("host3:50500"), Arrays.asList(789l));
    final List<RoutingHop> routingList = new ArrayList<>();
    routingList.add(hop1);
    routingList.add(hop2);
    routingList.add(hop3);
    final RoutingHeader routingHeader = new RoutingHeader((short) 0, routingList);
    // Get routing list as string and parse list
    final String stringRoutingList = routingHeader.getRoutingListAsString();
    routingHeader.setRoutingList(stringRoutingList);
    final List<RoutingHop> parsedRoutingList = routingHeader.getRoutingList();
    Assert.assertEquals(3, parsedRoutingList.size());
    Assert.assertTrue(parsedRoutingList.contains(hop1));
    Assert.assertTrue(parsedRoutingList.contains(hop2));
    Assert.assertTrue(parsedRoutingList.contains(hop3));
}
Also used : ArrayList(java.util.ArrayList) RoutingHeader(org.bboxdb.network.routing.RoutingHeader) RoutingHop(org.bboxdb.network.routing.RoutingHop) BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance) Test(org.junit.Test)

Example 15 with RoutingHop

use of org.bboxdb.network.routing.RoutingHop in project bboxdb by jnidzwetzki.

the class InsertTupleHandler method handleRequest.

@Override
public /**
 * Handle the insert tuple request
 */
boolean handleRequest(final ByteBuffer encodedPackage, final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {
    if (logger.isDebugEnabled()) {
        logger.debug("Got insert tuple request");
    }
    try {
        final InsertTupleRequest insertTupleRequest = InsertTupleRequest.decodeTuple(encodedPackage);
        final RoutingHeader routingHeader = insertTupleRequest.getRoutingHeader();
        if (!routingHeader.isRoutedPackage()) {
            final String errorMessage = "Error while inserting tuple - package is not routed";
            logger.error(errorMessage);
            final ErrorResponse responsePackage = new ErrorResponse(packageSequence, errorMessage);
            clientConnectionHandler.writeResultPackage(responsePackage);
            return true;
        }
        // Needs to be rerouted?
        if (routingHeader.getHop() == -1) {
            routingHeader.dispatchToNextHop();
            final RoutingHop localHop = routingHeader.getRoutingHop();
            if (PackageRouter.checkLocalSystemNameMatches(localHop)) {
                processPackageLocally(packageSequence, clientConnectionHandler, insertTupleRequest);
            } else {
                logger.debug("Rerouting package {}", packageSequence);
                forwardRoutedPackage(packageSequence, clientConnectionHandler, insertTupleRequest);
            }
        } else {
            processPackageLocally(packageSequence, clientConnectionHandler, insertTupleRequest);
        }
    } catch (RejectedException e) {
        final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_LOCAL_OPERATION_REJECTED_RETRY + " " + e.getMessage());
        clientConnectionHandler.writeResultPackage(responsePackage);
    } catch (Throwable e) {
        logger.error("Error while inserting tuple", e);
        final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION);
        clientConnectionHandler.writeResultPackage(responsePackage);
    }
    return true;
}
Also used : RejectedException(org.bboxdb.commons.RejectedException) RoutingHeader(org.bboxdb.network.routing.RoutingHeader) RoutingHop(org.bboxdb.network.routing.RoutingHop) InsertTupleRequest(org.bboxdb.network.packages.request.InsertTupleRequest) ErrorResponse(org.bboxdb.network.packages.response.ErrorResponse)

Aggregations

RoutingHop (org.bboxdb.network.routing.RoutingHop)16 RoutingHeader (org.bboxdb.network.routing.RoutingHeader)15 BBoxDBInstance (org.bboxdb.distribution.membership.BBoxDBInstance)12 ArrayList (java.util.ArrayList)11 DistributionRegion (org.bboxdb.distribution.region.DistributionRegion)10 List (java.util.List)9 NetworkOperationFuture (org.bboxdb.network.client.future.NetworkOperationFuture)8 BBoxDBException (org.bboxdb.misc.BBoxDBException)6 JoinedTupleListFuture (org.bboxdb.network.client.future.JoinedTupleListFuture)6 TupleListFuture (org.bboxdb.network.client.future.TupleListFuture)5 DoNothingDuplicateResolver (org.bboxdb.storage.sstable.duplicateresolver.DoNothingDuplicateResolver)4 Test (org.junit.Test)3 InetSocketAddress (java.net.InetSocketAddress)2 EmptyResultFuture (org.bboxdb.network.client.future.EmptyResultFuture)2 DeletedTuple (org.bboxdb.storage.entity.DeletedTuple)2 Tuple (org.bboxdb.storage.entity.Tuple)2 TupleStoreName (org.bboxdb.storage.entity.TupleStoreName)2 HashMap (java.util.HashMap)1 Collectors (java.util.stream.Collectors)1 RejectedException (org.bboxdb.commons.RejectedException)1