Search in sources :

Example 41 with StatementExecutionResult

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;
}
Also used : HDBException(herddb.client.HDBException) SystablesTableManager(herddb.core.system.SystablesTableManager) CommitTransactionStatement(herddb.model.commands.CommitTransactionStatement) TableCheckpoint(herddb.core.AbstractTableManager.TableCheckpoint) Table(herddb.model.Table) ClientConfiguration(herddb.client.ClientConfiguration) SyslogstatusManager(herddb.core.system.SyslogstatusManager) OpStatsLogger(org.apache.bookkeeper.stats.OpStatsLogger) IndexAlreadyExistsException(herddb.model.IndexAlreadyExistsException) Map(java.util.Map) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) LogNotAvailableException(herddb.log.LogNotAvailableException) PduCodec(herddb.proto.PduCodec) CommitLogResult(herddb.log.CommitLogResult) ClientSideMetadataProviderException(herddb.client.ClientSideMetadataProviderException) LogSequenceNumber(herddb.log.LogSequenceNumber) Set(java.util.Set) ScanStatement(herddb.model.commands.ScanStatement) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) HDBClient(herddb.client.HDBClient) TranslatedQuery(herddb.sql.TranslatedQuery) StatsLogger(org.apache.bookkeeper.stats.StatsLogger) TableDataChecksum(herddb.data.consistency.TableDataChecksum) DropTableStatement(herddb.model.commands.DropTableStatement) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Bytes(herddb.utils.Bytes) SysdualTableManager(herddb.core.system.SysdualTableManager) LogEntry(herddb.log.LogEntry) SysnodesTableManager(herddb.core.system.SysnodesTableManager) ArrayList(java.util.ArrayList) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) TableSpaceDoesNotExistException(herddb.model.TableSpaceDoesNotExistException) TransactionContext(herddb.model.TransactionContext) Transaction(herddb.model.Transaction) BRINIndexManager(herddb.index.brin.BRINIndexManager) SystablestatsTableManager(herddb.core.system.SystablestatsTableManager) BiConsumer(java.util.function.BiConsumer) CommitLogListener(herddb.log.CommitLogListener) ForeignKeyDef(herddb.model.ForeignKeyDef) IndexDoesNotExistException(herddb.model.IndexDoesNotExistException) TableSpaceManagerStats(herddb.core.stats.TableSpaceManagerStats) LogEntryType(herddb.log.LogEntryType) LogEntryFactory(herddb.log.LogEntryFactory) IOException(java.io.IOException) DataStorageManager(herddb.storage.DataStorageManager) DropIndexStatement(herddb.model.commands.DropIndexStatement) ColumnTypes(herddb.model.ColumnTypes) ExecutionException(java.util.concurrent.ExecutionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) Column(herddb.model.Column) KeyValue(herddb.utils.KeyValue) FullRecoveryNeededException(herddb.log.FullRecoveryNeededException) StampedLock(java.util.concurrent.locks.StampedLock) ServerHostData(herddb.network.ServerHostData) TableAlreadyExistsException(herddb.model.TableAlreadyExistsException) RollbackTransactionStatement(herddb.model.commands.RollbackTransactionStatement) CreateTableStatement(herddb.model.commands.CreateTableStatement) TimeoutException(java.util.concurrent.TimeoutException) JMXUtils(herddb.jmx.JMXUtils) TransactionResult(herddb.model.TransactionResult) MetadataStorageManager(herddb.metadata.MetadataStorageManager) Channel(herddb.network.Channel) ServerConfiguration(herddb.server.ServerConfiguration) Futures(herddb.utils.Futures) DataStorageManagerException(herddb.storage.DataStorageManagerException) Index(herddb.model.Index) TableDoesNotExistException(herddb.model.TableDoesNotExistException) AlterTableStatement(herddb.model.commands.AlterTableStatement) SysindexcolumnsTableManager(herddb.core.system.SysindexcolumnsTableManager) DataScanner(herddb.model.DataScanner) DDLException(herddb.model.DDLException) StatementExecutionException(herddb.model.StatementExecutionException) Collection(java.util.Collection) SysstatementsTableManager(herddb.core.system.SysstatementsTableManager) BeginTransactionStatement(herddb.model.commands.BeginTransactionStatement) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletionException(java.util.concurrent.CompletionException) TableAwareStatement(herddb.model.TableAwareStatement) Logger(java.util.logging.Logger) EOFException(java.io.EOFException) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) HDBConnection(herddb.client.HDBConnection) List(java.util.List) FullTableScanConsumer(herddb.storage.FullTableScanConsumer) SystransactionsTableManager(herddb.core.system.SystransactionsTableManager) NodeMetadata(herddb.model.NodeMetadata) Entry(java.util.Map.Entry) Optional(java.util.Optional) TableSpace(herddb.model.TableSpace) SysconfigTableManager(herddb.core.system.SysconfigTableManager) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) SysindexesTableManager(herddb.core.system.SysindexesTableManager) Statement(herddb.model.Statement) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) DataScannerException(herddb.model.DataScannerException) SystablespacesTableManager(herddb.core.system.SystablespacesTableManager) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Pdu(herddb.proto.Pdu) Level(java.util.logging.Level) HashSet(java.util.HashSet) SysclientsTableManager(herddb.core.system.SysclientsTableManager) TableChecksum(herddb.data.consistency.TableChecksum) ExecutorService(java.util.concurrent.ExecutorService) DumpedLogEntry(herddb.backup.DumpedLogEntry) SysforeignkeysTableManager(herddb.core.system.SysforeignkeysTableManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StatementExecutionResult(herddb.model.StatementExecutionResult) TimeUnit(java.util.concurrent.TimeUnit) CommitLog(herddb.log.CommitLog) ClientSideMetadataProvider(herddb.client.ClientSideMetadataProvider) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) SystablespacereplicastateTableManager(herddb.core.system.SystablespacereplicastateTableManager) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) StatementEvaluationContext(herddb.model.StatementEvaluationContext) SyscolumnsTableManager(herddb.core.system.SyscolumnsTableManager) Comparator(java.util.Comparator) Collections(java.util.Collections) TableManagerStats(herddb.core.stats.TableManagerStats) MemoryHashIndexManager(herddb.index.MemoryHashIndexManager) SystemProperties(herddb.utils.SystemProperties) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) StatementExecutionResult(herddb.model.StatementExecutionResult) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement)

Example 42 with StatementExecutionResult

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);
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) TransactionContext(herddb.model.TransactionContext) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) StatementExecutionResult(herddb.model.StatementExecutionResult) RollbackTransactionStatement(herddb.model.commands.RollbackTransactionStatement) ExecutionException(java.util.concurrent.ExecutionException) StatementExecutionException(herddb.model.StatementExecutionException)

Example 43 with StatementExecutionResult

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;
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DropTableSpaceStatement(herddb.model.commands.DropTableSpaceStatement) AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) TableSpaceConsistencyCheckStatement(herddb.model.commands.TableSpaceConsistencyCheckStatement) NotLeaderException(herddb.model.NotLeaderException) DDLStatement(herddb.model.DDLStatement) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) StatementExecutionResult(herddb.model.StatementExecutionResult) StatementExecutionException(herddb.model.StatementExecutionException)

Aggregations

StatementExecutionResult (herddb.model.StatementExecutionResult)43 StatementExecutionException (herddb.model.StatementExecutionException)35 TransactionContext (herddb.model.TransactionContext)32 DataScanner (herddb.model.DataScanner)27 Table (herddb.model.Table)26 DataScannerException (herddb.model.DataScannerException)25 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)23 DDLStatementExecutionResult (herddb.model.DDLStatementExecutionResult)20 ArrayList (java.util.ArrayList)20 Statement (herddb.model.Statement)18 ScanStatement (herddb.model.commands.ScanStatement)18 HashMap (java.util.HashMap)18 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)18 RollbackTransactionStatement (herddb.model.commands.RollbackTransactionStatement)17 Bytes (herddb.utils.Bytes)17 AtomicLong (java.util.concurrent.atomic.AtomicLong)17 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)16 Map (java.util.Map)16 List (java.util.List)14 Column (herddb.model.Column)12