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