use of herddb.model.StatementExecutionResult in project herddb by diennea.
the class TableSpaceManager method executePlannedOperationStatement.
private CompletableFuture<StatementExecutionResult> executePlannedOperationStatement(Statement statement, TransactionContext transactionContext, StatementEvaluationContext context) {
long lockStamp = context.getTableSpaceLock();
boolean lockAcquired = false;
if (lockStamp == 0) {
lockStamp = acquireReadLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
SQLPlannedOperationStatement planned = (SQLPlannedOperationStatement) statement;
CompletableFuture<StatementExecutionResult> res;
try {
res = planned.getRootOp().executeAsync(this, transactionContext, context, false, false);
} catch (HerdDBInternalException err) {
// ensure we are able to release locks correctly
LOGGER.log(Level.SEVERE, "Internal error", err);
res = Futures.exception(err);
}
// });
if (lockAcquired) {
res = releaseReadLock(res, lockStamp, statement).thenApply(s -> {
context.setTableSpaceLock(0);
return s;
});
}
return res;
}
use of herddb.model.StatementExecutionResult in project herddb by diennea.
the class TableSpaceManager method executeStatementAsync.
public CompletableFuture<StatementExecutionResult> executeStatementAsync(Statement statement, StatementEvaluationContext context, TransactionContext transactionContext) throws StatementExecutionException {
if (transactionContext.transactionId == TransactionContext.AUTOTRANSACTION_ID && // Do not autostart transaction on alter table statements
statement.supportsTransactionAutoCreate()) {
AtomicLong capturedTx = new AtomicLong();
boolean wasHoldingTableSpaceLock = context.getTableSpaceLock() != 0;
CompletableFuture<StatementExecutionResult> newTransaction = beginTransactionAsync(context, false);
CompletableFuture<StatementExecutionResult> finalResult = newTransaction.thenCompose((StatementExecutionResult begineTransactionResult) -> {
TransactionContext newtransactionContext = new TransactionContext(begineTransactionResult.transactionId);
capturedTx.set(newtransactionContext.transactionId);
return executeStatementAsyncInternal(statement, context, newtransactionContext, true);
});
finalResult.whenComplete((xx, error) -> {
if (!wasHoldingTableSpaceLock) {
releaseReadLock(context.getTableSpaceLock(), "begin implicit transaction");
}
long txId = capturedTx.get();
if (error != null && txId > 0) {
LOGGER.log(Level.FINE, tableSpaceName + " force rollback of implicit transaction " + txId, error);
try {
rollbackTransaction(new RollbackTransactionStatement(tableSpaceName, txId), context).get();
} catch (InterruptedException ex) {
LOGGER.log(Level.SEVERE, tableSpaceName + " Cannot rollback implicit tx " + txId, ex);
Thread.currentThread().interrupt();
error.addSuppressed(ex);
} catch (ExecutionException ex) {
LOGGER.log(Level.SEVERE, tableSpaceName + " Cannot rollback implicit tx " + txId, ex.getCause());
error.addSuppressed(ex.getCause());
} catch (Throwable t) {
LOGGER.log(Level.SEVERE, tableSpaceName + " Cannot rollback implicittx " + txId, t);
error.addSuppressed(t);
}
}
});
return finalResult;
} else {
return executeStatementAsyncInternal(statement, context, transactionContext, false);
}
}
use of herddb.model.StatementExecutionResult in project herddb by diennea.
the class DBManager method executeStatementAsync.
public CompletableFuture<StatementExecutionResult> executeStatementAsync(Statement statement, StatementEvaluationContext context, TransactionContext transactionContext) {
context.setDefaultTablespace(statement.getTableSpace());
context.setManager(this);
context.setTransactionContext(transactionContext);
// LOGGER.log(Level.SEVERE, "executeStatement {0}", new Object[]{statement});
String tableSpace = statement.getTableSpace();
if (tableSpace == null) {
return Futures.exception(new StatementExecutionException("invalid null tableSpace"));
}
if (statement instanceof CreateTableSpaceStatement) {
if (transactionContext.transactionId > 0) {
return Futures.exception(new StatementExecutionException("CREATE TABLESPACE cannot be issued inside a transaction"));
}
return CompletableFuture.completedFuture(createTableSpace((CreateTableSpaceStatement) statement));
}
if (statement instanceof AlterTableSpaceStatement) {
if (transactionContext.transactionId > 0) {
return Futures.exception(new StatementExecutionException("ALTER TABLESPACE cannot be issued inside a transaction"));
}
return CompletableFuture.completedFuture(alterTableSpace((AlterTableSpaceStatement) statement));
}
if (statement instanceof DropTableSpaceStatement) {
if (transactionContext.transactionId > 0) {
return Futures.exception(new StatementExecutionException("DROP TABLESPACE cannot be issued inside a transaction"));
}
return CompletableFuture.completedFuture(dropTableSpace((DropTableSpaceStatement) statement));
}
if (statement instanceof TableSpaceConsistencyCheckStatement) {
if (transactionContext.transactionId > 0) {
return Futures.exception(new StatementExecutionException("TABLESPACECONSISTENCYCHECK cannot be issue inside a transaction"));
}
return CompletableFuture.completedFuture(createTableSpaceCheckSum((TableSpaceConsistencyCheckStatement) statement));
}
TableSpaceManager manager = tablesSpaces.get(tableSpace);
if (manager == null) {
return Futures.exception(new NotLeaderException("No such tableSpace " + tableSpace + " here (at " + nodeId + "). " + "Maybe the server is starting "));
}
if (errorIfNotLeader && !manager.isLeader()) {
return Futures.exception(new NotLeaderException("node " + nodeId + " is not leader for tableSpace " + tableSpace));
}
CompletableFuture<StatementExecutionResult> res = manager.executeStatementAsync(statement, context, transactionContext);
if (statement instanceof DDLStatement) {
res.whenComplete((s, err) -> {
planner.clearCache();
});
planner.clearCache();
}
// });
return res;
}
Aggregations