use of org.bboxdb.network.packages.response.ErrorResponse in project bboxdb by jnidzwetzki.
the class DeleteTableHandler method handleRequest.
@Override
public /**
* Handle the delete table call
*/
boolean handleRequest(final ByteBuffer encodedPackage, final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {
try {
final DeleteTableRequest deletePackage = DeleteTableRequest.decodeTuple(encodedPackage);
final TupleStoreName requestTable = deletePackage.getTable();
logger.info("Got delete call for table: {}", requestTable);
// Delete zookeeper configuration
final TupleStoreAdapter tupleStoreAdapter = ZookeeperClientFactory.getZookeeperClient().getTupleStoreAdapter();
tupleStoreAdapter.deleteTable(requestTable);
// Clear cached data
TupleStoreConfigurationCache.getInstance().clear();
clientConnectionHandler.writeResultPackage(new SuccessResponse(packageSequence));
} catch (Exception e) {
logger.warn("Error while delete tuple", e);
final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION);
clientConnectionHandler.writeResultPackage(responsePackage);
}
return true;
}
use of org.bboxdb.network.packages.response.ErrorResponse in project bboxdb by jnidzwetzki.
the class KeepAliveHandler method handleRequest.
/**
* Handle the keep alive package. Simply send a success response package back
*/
@Override
public boolean handleRequest(final ByteBuffer encodedPackage, final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {
this.clientConnectionHandler = clientConnectionHandler;
final KeepAliveRequest keepAliveRequst = KeepAliveRequest.decodeTuple(encodedPackage);
boolean gossipResult = true;
if (!keepAliveRequst.getTuples().isEmpty()) {
gossipResult = handleGossip(keepAliveRequst, clientConnectionHandler);
}
if (gossipResult) {
final SuccessResponse responsePackage = new SuccessResponse(packageSequence);
clientConnectionHandler.writeResultPackage(responsePackage);
} else {
final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_OUTDATED_TUPLES);
clientConnectionHandler.writeResultPackage(responsePackage);
}
return true;
}
use of org.bboxdb.network.packages.response.ErrorResponse in project bboxdb by jnidzwetzki.
the class ClientConnectionHandler method sendNextResultsForQuery.
/**
* Send next results for the given query
* @param packageSequence
* @param
* @throws PackageEncodeException
* @throws IOException
*/
public void sendNextResultsForQuery(final short packageSequence, final short querySequence) throws IOException, PackageEncodeException {
if (!getActiveQueries().containsKey(querySequence)) {
logger.error("Unable to resume query {} - package {} - not found", querySequence, packageSequence);
writeResultPackage(new ErrorResponse(packageSequence, ErrorMessages.ERROR_QUERY_NOT_FOUND));
return;
}
final Runnable queryRunable = new ExceptionSafeRunnable() {
@Override
protected void runThread() throws IOException, PackageEncodeException {
final ClientQuery clientQuery = getActiveQueries().get(querySequence);
clientQuery.fetchAndSendNextTuples(packageSequence);
if (clientQuery.isQueryDone()) {
logger.info("Query {} is done with {} tuples, removing iterator ", querySequence, clientQuery.getTotalSendTuples());
clientQuery.close();
getActiveQueries().remove(querySequence);
}
}
@Override
protected void afterExceptionHook() {
try {
writeResultPackage(new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION));
} catch (IOException | PackageEncodeException e) {
logger.error("Unable to send result package", e);
}
}
};
// Submit the runnable to our pool
if (threadPool.isShutdown()) {
logger.warn("Thread pool is shutting down, don't execute query: {}", querySequence);
writeResultPackage(new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION));
} else {
getThreadPool().submit(queryRunable);
}
}
use of org.bboxdb.network.packages.response.ErrorResponse in project bboxdb by jnidzwetzki.
the class QueryHelper method handleNonExstingTable.
/**
* Handle a non existing table
* @param requestTable
* @param clientConnectionHandler
* @return
* @throws PackageEncodeException
* @throws IOException
*/
public static boolean handleNonExstingTable(final TupleStoreName requestTable, final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {
// Table is locally known, prevent expensive zookeeper call
final TupleStoreManagerRegistry storageRegistry = clientConnectionHandler.getStorageRegistry();
if (storageRegistry.isStorageManagerKnown(requestTable)) {
return true;
}
final TupleStoreAdapter tupleStoreAdapter = ZookeeperClientFactory.getZookeeperClient().getTupleStoreAdapter();
try {
final boolean tableKnown = tupleStoreAdapter.isTableKnown(requestTable);
if (!tableKnown) {
clientConnectionHandler.writeResultPackage(new ErrorResponse(packageSequence, ErrorMessages.ERROR_TABLE_NOT_EXIST));
return false;
}
} catch (ZookeeperException e) {
logger.error("Got an exception while query for table", e);
return false;
}
return true;
}
use of org.bboxdb.network.packages.response.ErrorResponse in project bboxdb by jnidzwetzki.
the class HandleBoundingBoxQuery method handleQuery.
@Override
public /**
* Handle a bounding box query
*/
void handleQuery(final ByteBuffer encodedPackage, final short packageSequence, final ClientConnectionHandler clientConnectionHandler) throws IOException, PackageEncodeException {
try {
if (clientConnectionHandler.getActiveQueries().containsKey(packageSequence)) {
logger.error("Query sequence {} is already known, please close old query first", packageSequence);
return;
}
final QueryBoundingBoxRequest queryRequest = QueryBoundingBoxRequest.decodeTuple(encodedPackage);
final TupleStoreName requestTable = queryRequest.getTable();
if (!QueryHelper.handleNonExstingTable(requestTable, packageSequence, clientConnectionHandler)) {
return;
}
final OperatorTreeBuilder operatorTreeBuilder = new OperatorTreeBuilder() {
@Override
public Operator buildOperatorTree(final List<TupleStoreManager> storageManager) {
if (storageManager.size() != 1) {
throw new IllegalArgumentException("This operator tree needs 1 storage manager");
}
final BoundingBox boundingBox = queryRequest.getBoundingBox();
final SpatialIndexReadOperator operator = new SpatialIndexReadOperator(storageManager.get(0), boundingBox);
return operator;
}
};
final StreamClientQuery clientQuery = new StreamClientQuery(operatorTreeBuilder, queryRequest.isPagingEnabled(), queryRequest.getTuplesPerPage(), clientConnectionHandler, packageSequence, Arrays.asList(requestTable));
clientConnectionHandler.getActiveQueries().put(packageSequence, clientQuery);
clientConnectionHandler.sendNextResultsForQuery(packageSequence, packageSequence);
} catch (PackageEncodeException e) {
logger.warn("Got exception while decoding package", e);
clientConnectionHandler.writeResultPackage(new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION));
}
}
Aggregations