Search in sources :

Example 11 with CommitLogResult

use of herddb.log.CommitLogResult in project herddb by diennea.

the class TableSpaceManager method dropIndex.

private StatementExecutionResult dropIndex(DropIndexStatement statement, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException {
    boolean lockAcquired = false;
    if (context.getTableSpaceLock() == 0) {
        long lockStamp = acquireWriteLock(statement);
        context.setTableSpaceLock(lockStamp);
        lockAcquired = true;
    }
    try {
        if (!indexes.containsKey(statement.getIndexName())) {
            if (statement.isIfExists()) {
                return new DDLStatementExecutionResult(transaction != null ? transaction.transactionId : 0);
            }
            throw new IndexDoesNotExistException("index " + statement.getIndexName() + " does not exist " + statement.getIndexName() + " on tableSpace " + statement.getTableSpace());
        }
        if (transaction != null && transaction.isIndexDropped(statement.getIndexName())) {
            if (statement.isIfExists()) {
                return new DDLStatementExecutionResult(transaction.transactionId);
            }
            throw new IndexDoesNotExistException("index does not exist " + statement.getIndexName() + " on tableSpace " + statement.getTableSpace());
        }
        LogEntry entry = LogEntryFactory.dropIndex(statement.getIndexName(), transaction);
        CommitLogResult pos;
        try {
            pos = log.log(entry, entry.transactionId <= 0);
        } catch (LogNotAvailableException ex) {
            throw new StatementExecutionException(ex);
        }
        apply(pos, entry, false);
        return new DDLStatementExecutionResult(entry.transactionId);
    } catch (DataStorageManagerException err) {
        throw new StatementExecutionException(err);
    } finally {
        if (lockAcquired) {
            releaseWriteLock(context.getTableSpaceLock(), statement);
            context.setTableSpaceLock(0);
        }
    }
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) IndexDoesNotExistException(herddb.model.IndexDoesNotExistException) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) CommitLogResult(herddb.log.CommitLogResult) LogEntry(herddb.log.LogEntry) DumpedLogEntry(herddb.backup.DumpedLogEntry) StatementExecutionException(herddb.model.StatementExecutionException) LogNotAvailableException(herddb.log.LogNotAvailableException)

Example 12 with CommitLogResult

use of herddb.log.CommitLogResult in project herddb by diennea.

the class TableSpaceManager method rollbackTransaction.

private CompletableFuture<StatementExecutionResult> rollbackTransaction(RollbackTransactionStatement statement, StatementEvaluationContext context) throws StatementExecutionException {
    long txId = statement.getTransactionId();
    validateTransactionBeforeTxCommand(txId);
    LogEntry entry = LogEntryFactory.rollbackTransaction(txId);
    long lockStamp = context.getTableSpaceLock();
    boolean lockAcquired = false;
    if (lockStamp == 0) {
        lockStamp = acquireReadLock(statement);
        context.setTableSpaceLock(lockStamp);
        lockAcquired = true;
    }
    CommitLogResult pos = log.log(entry, true);
    CompletableFuture<StatementExecutionResult> res = pos.logSequenceNumber.thenApplyAsync((lsn) -> {
        apply(pos, entry, false);
        return new TransactionResult(txId, TransactionResult.OutcomeType.ROLLBACK);
    }, callbacksExecutor);
    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) TransactionResult(herddb.model.TransactionResult) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) StatementExecutionResult(herddb.model.StatementExecutionResult) CommitLogResult(herddb.log.CommitLogResult) LogEntry(herddb.log.LogEntry) DumpedLogEntry(herddb.backup.DumpedLogEntry)

Example 13 with CommitLogResult

use of herddb.log.CommitLogResult in project herddb by diennea.

the class TableSpaceManager method createTable.

private StatementExecutionResult createTable(CreateTableStatement statement, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException {
    boolean lockAcquired = false;
    if (context.getTableSpaceLock() == 0) {
        long lockStamp = acquireWriteLock(statement);
        context.setTableSpaceLock(lockStamp);
        lockAcquired = true;
    }
    try {
        if (tables.containsKey(statement.getTableDefinition().name)) {
            if (statement.isIfExistsClause()) {
                return new DDLStatementExecutionResult(transaction != null ? transaction.transactionId : 0);
            }
            throw new TableAlreadyExistsException(statement.getTableDefinition().name);
        }
        for (Index additionalIndex : statement.getAdditionalIndexes()) {
            AbstractIndexManager exists = indexes.get(additionalIndex.name);
            if (exists != null) {
                LOGGER.log(Level.INFO, "Error while creating index " + additionalIndex.name + ", there is already an index " + exists.getIndex().name + " on table " + exists.getIndex().table);
                throw new IndexAlreadyExistsException(additionalIndex.name);
            }
        }
        Table table = statement.getTableDefinition();
        // validate foreign keys
        if (table.foreignKeys != null) {
            for (ForeignKeyDef def : table.foreignKeys) {
                AbstractTableManager parentTableManager = null;
                for (AbstractTableManager ab : tables.values()) {
                    if (ab.getTable().uuid.equals(def.parentTableId)) {
                        parentTableManager = ab;
                        break;
                    }
                }
                if (parentTableManager == null) {
                    throw new StatementExecutionException("Table " + def.parentTableId + " does not exist in tablespace " + tableSpaceName);
                }
                Table parentTable = parentTableManager.getTable();
                int i = 0;
                for (String col : def.columns) {
                    Column column = table.getColumn(col);
                    Column parentColumn = parentTable.getColumn(def.parentTableColumns[i]);
                    if (column == null) {
                        throw new StatementExecutionException("Cannot find column " + col);
                    }
                    if (parentColumn == null) {
                        throw new StatementExecutionException("Cannot find column " + def.parentTableColumns[i]);
                    }
                    if (!ColumnTypes.sameRawDataType(column.type, parentColumn.type)) {
                        throw new StatementExecutionException("Column " + table.name + "." + column.name + " is not the same tyepe of column " + parentTable.name + "." + parentColumn.name);
                    }
                    i++;
                }
            }
        }
        LogEntry entry = LogEntryFactory.createTable(statement.getTableDefinition(), transaction);
        CommitLogResult pos = log.log(entry, entry.transactionId <= 0);
        apply(pos, entry, false);
        for (Index additionalIndex : statement.getAdditionalIndexes()) {
            LogEntry index_entry = LogEntryFactory.createIndex(additionalIndex, transaction);
            CommitLogResult index_pos = log.log(index_entry, index_entry.transactionId <= 0);
            apply(index_pos, index_entry, false);
        }
        return new DDLStatementExecutionResult(entry.transactionId);
    } catch (DataStorageManagerException | LogNotAvailableException err) {
        throw new StatementExecutionException(err);
    } finally {
        if (lockAcquired) {
            releaseWriteLock(context.getTableSpaceLock(), statement);
            context.setTableSpaceLock(0);
        }
    }
}
Also used : TableAlreadyExistsException(herddb.model.TableAlreadyExistsException) DataStorageManagerException(herddb.storage.DataStorageManagerException) Table(herddb.model.Table) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) Index(herddb.model.Index) CommitLogResult(herddb.log.CommitLogResult) StatementExecutionException(herddb.model.StatementExecutionException) TableCheckpoint(herddb.core.AbstractTableManager.TableCheckpoint) IndexAlreadyExistsException(herddb.model.IndexAlreadyExistsException) Column(herddb.model.Column) ForeignKeyDef(herddb.model.ForeignKeyDef) LogEntry(herddb.log.LogEntry) DumpedLogEntry(herddb.backup.DumpedLogEntry) LogNotAvailableException(herddb.log.LogNotAvailableException)

Example 14 with CommitLogResult

use of herddb.log.CommitLogResult in project herddb by diennea.

the class TableSpaceManager method forceTransactionRollback.

private void forceTransactionRollback(long tx) throws LogNotAvailableException, DataStorageManagerException, DDLException {
    LOGGER.log(Level.FINER, "rolling back transaction {0}", tx);
    LogEntry rollback = LogEntryFactory.rollbackTransaction(tx);
    // let followers see the rollback on the log
    CommitLogResult pos = log.log(rollback, true);
    apply(pos, rollback, false);
}
Also used : CommitLogResult(herddb.log.CommitLogResult) LogEntry(herddb.log.LogEntry) DumpedLogEntry(herddb.backup.DumpedLogEntry)

Example 15 with CommitLogResult

use of herddb.log.CommitLogResult in project herddb by diennea.

the class TableSpaceManager method createIndex.

private StatementExecutionResult createIndex(CreateIndexStatement statement, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException {
    boolean lockAcquired = false;
    if (context.getTableSpaceLock() == 0) {
        long lockStamp = acquireWriteLock(statement);
        context.setTableSpaceLock(lockStamp);
        lockAcquired = true;
    }
    try {
        AbstractIndexManager exists = indexes.get(statement.getIndexDefinition().name);
        if (exists != null) {
            LOGGER.log(Level.INFO, "Error while creating index " + statement.getIndexDefinition().name + ", there is already an index " + exists.getIndex().name + " on table " + exists.getIndex().table);
            throw new IndexAlreadyExistsException(statement.getIndexDefinition().name);
        }
        LogEntry entry = LogEntryFactory.createIndex(statement.getIndexDefinition(), transaction);
        CommitLogResult pos;
        try {
            pos = log.log(entry, entry.transactionId <= 0);
        } catch (LogNotAvailableException ex) {
            throw new StatementExecutionException(ex);
        }
        apply(pos, entry, false);
        return new DDLStatementExecutionResult(entry.transactionId);
    } catch (DataStorageManagerException err) {
        throw new StatementExecutionException(err);
    } finally {
        if (lockAcquired) {
            releaseWriteLock(context.getTableSpaceLock(), statement);
            context.setTableSpaceLock(0);
        }
    }
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) IndexAlreadyExistsException(herddb.model.IndexAlreadyExistsException) CommitLogResult(herddb.log.CommitLogResult) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) LogEntry(herddb.log.LogEntry) DumpedLogEntry(herddb.backup.DumpedLogEntry) StatementExecutionException(herddb.model.StatementExecutionException) LogNotAvailableException(herddb.log.LogNotAvailableException)

Aggregations

CommitLogResult (herddb.log.CommitLogResult)34 LogEntry (herddb.log.LogEntry)30 LogNotAvailableException (herddb.log.LogNotAvailableException)26 StatementExecutionException (herddb.model.StatementExecutionException)23 DataStorageManagerException (herddb.storage.DataStorageManagerException)22 DumpedLogEntry (herddb.backup.DumpedLogEntry)19 DDLStatementExecutionResult (herddb.model.DDLStatementExecutionResult)13 IndexAlreadyExistsException (herddb.model.IndexAlreadyExistsException)12 LogSequenceNumber (herddb.log.LogSequenceNumber)11 DDLException (herddb.model.DDLException)11 TableDoesNotExistException (herddb.model.TableDoesNotExistException)11 IndexDoesNotExistException (herddb.model.IndexDoesNotExistException)10 TableAlreadyExistsException (herddb.model.TableAlreadyExistsException)10 Bytes (herddb.utils.Bytes)10 FullRecoveryNeededException (herddb.log.FullRecoveryNeededException)9 Table (herddb.model.Table)9 Transaction (herddb.model.Transaction)9 ArrayList (java.util.ArrayList)9 ClientSideMetadataProviderException (herddb.client.ClientSideMetadataProviderException)8 HDBException (herddb.client.HDBException)8