Search in sources :

Example 11 with DataStorageManagerException

use of herddb.storage.DataStorageManagerException 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 DataStorageManagerException

use of herddb.storage.DataStorageManagerException 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 13 with DataStorageManagerException

use of herddb.storage.DataStorageManagerException in project herddb by diennea.

the class TableSpaceManager method dumpTableSpace.

@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE")
void dumpTableSpace(String dumpId, Channel channel, int fetchSize, boolean includeLog) throws DataStorageManagerException, LogNotAvailableException {
    LOGGER.log(Level.INFO, "dumpTableSpace dumpId:{0} channel {1} fetchSize:{2}, includeLog:{3}", new Object[] { dumpId, channel, fetchSize, includeLog });
    TableSpaceCheckpoint checkpoint;
    List<DumpedLogEntry> txlogentries = new CopyOnWriteArrayList<>();
    CommitLogListener logDumpReceiver = new CommitLogListener() {

        @Override
        public void logEntry(LogSequenceNumber logPos, LogEntry data) {
            // we are going to capture all the changes to the tablespace during the dump, in order to replay
            // eventually 'missed' changes during the dump
            txlogentries.add(new DumpedLogEntry(logPos, data.serialize()));
        // LOGGER.log(Level.SEVERE, "dumping entry " + logPos + ", " + data + " nentries: " + txlogentries.size());
        }
    };
    long lockStamp = acquireWriteLock(null);
    if (includeLog) {
        log.attachCommitLogListener(logDumpReceiver);
    }
    checkpoint = checkpoint(true, /* compact records*/
    true, true);
    LOGGER.log(Level.INFO, "Created checkpoint at {}", checkpoint);
    if (checkpoint == null) {
        throw new DataStorageManagerException("failed to create a checkpoint, check logs for the reason");
    }
    /* Downgrade lock */
    // System.err.println("DOWNGRADING LOCK " + lockStamp + " TO READ");
    lockStamp = generalLock.tryConvertToReadLock(lockStamp);
    if (lockStamp == 0) {
        throw new DataStorageManagerException("unable to downgrade lock");
    }
    try {
        final int timeout = 60000;
        LogSequenceNumber checkpointSequenceNumber = checkpoint.sequenceNumber;
        long id = channel.generateRequestId();
        LOGGER.log(Level.INFO, "start sending dump, dumpId: {0} to client {1}", new Object[] { dumpId, channel });
        try (Pdu response_to_start = channel.sendMessageWithPduReply(id, PduCodec.TablespaceDumpData.write(id, tableSpaceName, dumpId, "start", null, stats.getTablesize(), checkpointSequenceNumber.ledgerId, checkpointSequenceNumber.offset, null, null), timeout)) {
            if (response_to_start.type != Pdu.TYPE_ACK) {
                LOGGER.log(Level.SEVERE, "error response at start command");
                return;
            }
        }
        if (includeLog) {
            List<Transaction> transactionsSnapshot = new ArrayList<>();
            dataStorageManager.loadTransactions(checkpointSequenceNumber, tableSpaceUUID, transactionsSnapshot::add);
            List<Transaction> batch = new ArrayList<>();
            for (Transaction t : transactionsSnapshot) {
                batch.add(t);
                if (batch.size() == 10) {
                    sendTransactionsDump(batch, channel, dumpId, timeout);
                }
            }
            sendTransactionsDump(batch, channel, dumpId, timeout);
        }
        for (Entry<String, LogSequenceNumber> entry : checkpoint.tablesCheckpoints.entrySet()) {
            final AbstractTableManager tableManager = tables.get(entry.getKey());
            final LogSequenceNumber sequenceNumber = entry.getValue();
            if (tableManager.isSystemTable()) {
                continue;
            }
            try {
                LOGGER.log(Level.INFO, "Sending table checkpoint for {} took at sequence number {}", new Object[] { tableManager.getTable().name, sequenceNumber });
                FullTableScanConsumer sink = new SingleTableDumper(tableSpaceName, tableManager, channel, dumpId, timeout, fetchSize);
                tableManager.dump(sequenceNumber, sink);
            } catch (DataStorageManagerException err) {
                LOGGER.log(Level.SEVERE, "error sending dump id " + dumpId, err);
                long errorid = channel.generateRequestId();
                try (Pdu response = channel.sendMessageWithPduReply(errorid, PduCodec.TablespaceDumpData.write(id, tableSpaceName, dumpId, "error", null, 0, 0, 0, null, null), timeout)) {
                }
                return;
            }
        }
        if (!txlogentries.isEmpty()) {
            txlogentries.sort(Comparator.naturalOrder());
            sendDumpedCommitLog(txlogentries, channel, dumpId, timeout);
        }
        LogSequenceNumber finishLogSequenceNumber = log.getLastSequenceNumber();
        channel.sendOneWayMessage(PduCodec.TablespaceDumpData.write(id, tableSpaceName, dumpId, "finish", null, 0, finishLogSequenceNumber.ledgerId, finishLogSequenceNumber.offset, null, null), (Throwable error) -> {
            if (error != null) {
                LOGGER.log(Level.SEVERE, "Cannot send last dump msg for " + dumpId, error);
            } else {
                LOGGER.log(Level.INFO, "Sent last dump msg for " + dumpId);
            }
        });
    } catch (InterruptedException | TimeoutException error) {
        LOGGER.log(Level.SEVERE, "error sending dump id " + dumpId, error);
    } finally {
        releaseReadLock(lockStamp, "senddump");
        if (includeLog) {
            log.removeCommitLogListener(logDumpReceiver);
        }
        for (Entry<String, LogSequenceNumber> entry : checkpoint.tablesCheckpoints.entrySet()) {
            String tableName = entry.getKey();
            AbstractTableManager tableManager = tables.get(tableName);
            String tableUUID = tableManager.getTable().uuid;
            LogSequenceNumber seqNumber = entry.getValue();
            LOGGER.log(Level.INFO, "unPinTableCheckpoint {0}.{1} ({2}) {3}", new Object[] { tableSpaceUUID, tableName, tableUUID, seqNumber });
            dataStorageManager.unPinTableCheckpoint(tableSpaceUUID, tableUUID, seqNumber);
        }
    }
}
Also used : Pdu(herddb.proto.Pdu) DataStorageManagerException(herddb.storage.DataStorageManagerException) DumpedLogEntry(herddb.backup.DumpedLogEntry) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) CommitLogListener(herddb.log.CommitLogListener) LogSequenceNumber(herddb.log.LogSequenceNumber) TableCheckpoint(herddb.core.AbstractTableManager.TableCheckpoint) FullTableScanConsumer(herddb.storage.FullTableScanConsumer) Transaction(herddb.model.Transaction) LogEntry(herddb.log.LogEntry) DumpedLogEntry(herddb.backup.DumpedLogEntry) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) TimeoutException(java.util.concurrent.TimeoutException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 14 with DataStorageManagerException

use of herddb.storage.DataStorageManagerException 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)

Example 15 with DataStorageManagerException

use of herddb.storage.DataStorageManagerException in project herddb by diennea.

the class TableSpaceManager method bootIndex.

AbstractIndexManager bootIndex(Index index, AbstractTableManager tableManager, boolean created, long transaction, boolean rebuild, boolean restore) throws DataStorageManagerException {
    long _start = System.currentTimeMillis();
    if (!created) {
        LOGGER.log(Level.INFO, "bootIndex {0} {1}.{2}.{3} uuid {4} - {5}", new Object[] { nodeId, tableSpaceName, index.table, index.name, index.uuid, index.type });
    }
    AbstractIndexManager prevIndexManager = indexes.remove(index.name);
    if (prevIndexManager != null) {
        if (restore) {
            // restoring an index already booted in a previous life
            LOGGER.log(Level.INFO, "bootIndex {0} {1}.{2}.{3} uuid {4} - {5} already exists on this tablespace. It will be truncated", new Object[] { nodeId, tableSpaceName, index.table, index.name, index.uuid, index.type });
            prevIndexManager.dropIndexData();
        } else {
            LOGGER.log(Level.INFO, "bootIndex {0} {1}.{2}.{3} uuid {4} - {5}", new Object[] { nodeId, tableSpaceName, index.table, index.name, index.uuid, index.type });
            if (indexes.containsKey(index.name)) {
                throw new DataStorageManagerException("Index" + index.name + " already present in tableSpace " + tableSpaceName);
            }
        }
    }
    final int writeLockTimeout = dbmanager.getServerConfiguration().getInt(ServerConfiguration.PROPERTY_WRITELOCK_TIMEOUT, ServerConfiguration.PROPERTY_WRITELOCK_TIMEOUT_DEFAULT);
    final int readLockTimeout = dbmanager.getServerConfiguration().getInt(ServerConfiguration.PROPERTY_READLOCK_TIMEOUT, ServerConfiguration.PROPERTY_READLOCK_TIMEOUT_DEFAULT);
    AbstractIndexManager indexManager;
    switch(index.type) {
        case Index.TYPE_HASH:
            indexManager = new MemoryHashIndexManager(index, tableManager, log, dataStorageManager, this, tableSpaceUUID, transaction, writeLockTimeout, readLockTimeout);
            break;
        case Index.TYPE_BRIN:
            indexManager = new BRINIndexManager(index, dbmanager.getMemoryManager(), tableManager, log, dataStorageManager, this, tableSpaceUUID, transaction, writeLockTimeout, readLockTimeout);
            break;
        default:
            throw new DataStorageManagerException("invalid NON-UNIQUE index type " + index.type);
    }
    indexes.put(index.name, indexManager);
    // this must be mutable (see DROP INDEX)
    Map<String, AbstractIndexManager> newMap = new HashMap<>();
    newMap.put(index.name, indexManager);
    indexesByTable.merge(index.table, newMap, (a, b) -> {
        Map<String, AbstractIndexManager> map = new HashMap<>(a);
        map.putAll(b);
        return map;
    });
    indexManager.start(tableManager.getBootSequenceNumber());
    if (!created) {
        long _stop = System.currentTimeMillis();
        LOGGER.log(Level.INFO, "bootIndex {0} {1}.{2} time {3} ms", new Object[] { nodeId, tableSpaceName, index.name, (_stop - _start) + "" });
    }
    if (rebuild) {
        indexManager.rebuild();
    }
    dbmanager.getPlanner().clearCache();
    return indexManager;
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MemoryHashIndexManager(herddb.index.MemoryHashIndexManager) BRINIndexManager(herddb.index.brin.BRINIndexManager) TableCheckpoint(herddb.core.AbstractTableManager.TableCheckpoint)

Aggregations

DataStorageManagerException (herddb.storage.DataStorageManagerException)140 IOException (java.io.IOException)92 ArrayList (java.util.ArrayList)46 LogSequenceNumber (herddb.log.LogSequenceNumber)42 Bytes (herddb.utils.Bytes)30 Path (java.nio.file.Path)30 ExtendedDataInputStream (herddb.utils.ExtendedDataInputStream)25 SimpleByteArrayInputStream (herddb.utils.SimpleByteArrayInputStream)24 InputStream (java.io.InputStream)24 StatementExecutionException (herddb.model.StatementExecutionException)23 Table (herddb.model.Table)22 HashMap (java.util.HashMap)22 LogEntry (herddb.log.LogEntry)21 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)21 LogNotAvailableException (herddb.log.LogNotAvailableException)20 Record (herddb.model.Record)20 Index (herddb.model.Index)19 CommitLogResult (herddb.log.CommitLogResult)18 Transaction (herddb.model.Transaction)18 PostCheckpointAction (herddb.core.PostCheckpointAction)16