use of org.bboxdb.network.client.future.EmptyResultFuture in project bboxdb by jnidzwetzki.
the class BBoxDBCluster method lockTuple.
@Override
public EmptyResultFuture lockTuple(final String table, final Tuple tuple) throws BBoxDBException {
final DistributionRegion distributionRegion = getRootNode(table);
final Supplier<List<NetworkOperationFuture>> supplier = () -> {
final List<RoutingHop> hops = RoutingHopHelper.getRoutingHopsForWrite(distributionRegion, tuple.getBoundingBox());
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().createLockTupleFuture(table, tuple, routingHeader);
futures.add(future);
}
return futures;
};
// When version locking fails, try again with another version
return new EmptyResultFuture(supplier, FutureRetryPolicy.RETRY_POLICY_NONE);
}
use of org.bboxdb.network.client.future.EmptyResultFuture in project bboxdb by jnidzwetzki.
the class BBoxDBClientExample method main.
/**
* Connect to the BBoxDB Server at localhost and insert some tuples
*
* @param args
* @throws ExecutionException
* @throws InterruptedException
* @throws BBoxDBException
*/
public static void main(String[] args) throws InterruptedException, ExecutionException, BBoxDBException {
// A 2 dimensional table (member of distribution group 'mygroup3') with the name 'testdata'
final int dimensions = 2;
final String distributionGroup = "mygroup3";
final String mytable = distributionGroup + "_testdata";
// The name of the cluster
final String clustername = "mycluster";
// The zookeeper connect points
final List<String> connectPoints = Arrays.asList("localhost:2181");
// Connect to the server
final BBoxDB bboxdbClient = new BBoxDBCluster(connectPoints, clustername);
bboxdbClient.connect();
// Check the connection state
if (!bboxdbClient.isConnected()) {
System.out.println("Error while connecting to the BBoxDB cluster");
System.exit(-1);
}
// Clean the old content of the distribution group
final EmptyResultFuture deleteGroupResult = bboxdbClient.deleteDistributionGroup(distributionGroup);
deleteGroupResult.waitForAll();
if (deleteGroupResult.isFailed()) {
System.err.println("Unable to delete distribution group: " + distributionGroup);
System.err.println(deleteGroupResult.getAllMessages());
System.exit(-1);
}
// Create a new distribution group
final DistributionGroupConfiguration configuration = DistributionGroupConfigurationBuilder.create(dimensions).withReplicationFactor((short) 3).build();
final EmptyResultFuture createGroupResult = bboxdbClient.createDistributionGroup(distributionGroup, configuration);
createGroupResult.waitForAll();
if (createGroupResult.isFailed()) {
System.err.println("Unable to create distribution group: " + distributionGroup);
System.err.println(createGroupResult.getAllMessages());
System.exit(-1);
}
// Create the table
final TupleStoreConfiguration tableConfig = TupleStoreConfigurationBuilder.create().allowDuplicates(false).build();
final EmptyResultFuture createTableResult = bboxdbClient.createTable(mytable, tableConfig);
createTableResult.waitForAll();
if (createTableResult.isFailed()) {
System.err.println("Unable to create table group: " + mytable);
System.err.println(createTableResult.getAllMessages());
System.exit(-1);
}
// Insert two new tuples
final Tuple tuple1 = new Tuple("key1", new BoundingBox(0d, 5d, 0d, 1d), "mydata1".getBytes());
final EmptyResultFuture insertResult1 = bboxdbClient.insertTuple(mytable, tuple1);
final Tuple tuple2 = new Tuple("key2", new BoundingBox(-1d, 2d, -1d, 2d), "mydata2".getBytes());
final EmptyResultFuture insertResult2 = bboxdbClient.insertTuple(mytable, tuple2);
// Wait for the insert operations to complete
insertResult1.waitForAll();
insertResult2.waitForAll();
if (insertResult1.isFailed()) {
System.err.println("Unable to insert tuple: " + insertResult1.getAllMessages());
System.exit(-1);
}
if (insertResult2.isFailed()) {
System.err.println("Unable to insert tuple: " + insertResult2.getAllMessages());
System.exit(-1);
}
// Query by key
final TupleListFuture resultFuture1 = bboxdbClient.queryKey(mytable, "key");
// We got a future object, the search is performed asynchronous
// Wait for the result
resultFuture1.waitForAll();
if (resultFuture1.isFailed()) {
System.err.println("NetworkOperationFuture is failed: " + resultFuture1.getAllMessages());
System.exit(-1);
}
// Output all tuples
for (final Tuple tuple : resultFuture1) {
System.out.println(tuple);
}
// Query by bounding box
final TupleListFuture resultFuture2 = bboxdbClient.queryBoundingBox(mytable, new BoundingBox(-0.5d, 1d, -0.5d, 1d));
// Again, we got a future object, the search is performed asynchronous
resultFuture2.waitForAll();
if (resultFuture2.isFailed()) {
System.err.println("NetworkOperationFuture is failed: " + resultFuture2.getAllMessages());
System.exit(-1);
}
// Output all tuples
for (final Tuple tuple : resultFuture2) {
System.out.println("Tuple: " + tuple);
}
bboxdbClient.disconnect();
}
use of org.bboxdb.network.client.future.EmptyResultFuture in project bboxdb by jnidzwetzki.
the class BBoxDBClient method lockTuple.
@Override
public EmptyResultFuture lockTuple(final String table, final Tuple tuple) throws BBoxDBException {
final RoutingHeader routingHeader = RoutingHeaderHelper.getRoutingHeaderForLocalSystemWriteNE(table, BoundingBox.FULL_SPACE, true, connection.getServerAddress());
final NetworkOperationFuture future = createLockTupleFuture(table, tuple, routingHeader);
// When version locking fails, try again with another version
return new EmptyResultFuture(future, FutureRetryPolicy.RETRY_POLICY_NONE);
}
use of org.bboxdb.network.client.future.EmptyResultFuture in project bboxdb by jnidzwetzki.
the class BBoxDBCluster method deleteTuple.
@Override
public EmptyResultFuture deleteTuple(final String table, final String key, final long timestamp) throws BBoxDBException {
final DeletedTuple tuple = new DeletedTuple(key);
final DistributionRegion distributionRegion = getRootNode(table);
final Supplier<List<NetworkOperationFuture>> supplier = () -> {
final List<RoutingHop> hops = RoutingHopHelper.getRoutingHopsForWrite(distributionRegion, tuple.getBoundingBox());
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().getInsertTupleFuture(table, tuple, routingHeader);
futures.add(future);
}
return futures;
};
return new EmptyResultFuture(supplier);
}
use of org.bboxdb.network.client.future.EmptyResultFuture in project bboxdb by jnidzwetzki.
the class PackageRouter method sendInsertPackage.
/**
* @param insertTupleRequest
* @return
* @throws InterruptedException
* @throws PackageEncodeException
* @throws TimeoutException
* @throws ExecutionException
*/
protected boolean sendInsertPackage(final InsertTupleRequest insertTupleRequest) throws InterruptedException, PackageEncodeException {
final RoutingHeader routingHeader = insertTupleRequest.getRoutingHeader();
final RoutingHop routingHop = routingHeader.getRoutingHop();
final BBoxDBInstance receiverInstance = routingHop.getDistributedInstance();
final BBoxDBConnection connection = MembershipConnectionService.getInstance().getConnectionForInstance(receiverInstance);
if (connection == null) {
logger.error("Unable to get a connection to system: {}", receiverInstance);
return false;
}
final BBoxDBClient bboxDBClient = connection.getBboxDBClient();
final EmptyResultFuture insertFuture = bboxDBClient.insertTuple(insertTupleRequest.getTable().getFullname(), insertTupleRequest.getTuple(), routingHeader);
try {
insertFuture.waitForAll(ROUTING_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
} catch (TimeoutException e) {
logger.warn("Routing timeout, retry routing: {}", connection);
return false;
}
final boolean operationSuccess = (!insertFuture.isFailed());
return operationSuccess;
}
Aggregations