Search in sources :

Example 1 with LogEntry

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

the class FileCommitLogTest method testLog.

@Test
public void testLog() throws Exception {
    FileCommitLogManager manager = new FileCommitLogManager(folder.newFolder().toPath(), 64 * 1024 * 1024);
    int writeCount = 0;
    final long _startWrite = System.currentTimeMillis();
    try (CommitLog log = manager.createCommitLog("tt")) {
        log.startWriting();
        for (int i = 0; i < 10_000; i++) {
            log.log(LogEntryFactory.beginTransaction(0), false);
            writeCount++;
        }
    }
    final long _endWrite = System.currentTimeMillis();
    AtomicInteger readCount = new AtomicInteger();
    try (CommitLog log = manager.createCommitLog("tt")) {
        log.recovery(LogSequenceNumber.START_OF_TIME, new BiConsumer<LogSequenceNumber, LogEntry>() {

            @Override
            public void accept(LogSequenceNumber t, LogEntry u) {
                readCount.incrementAndGet();
            }
        }, true);
    }
    final long _endRead = System.currentTimeMillis();
    assertEquals(writeCount, readCount.get());
    System.out.println("Write time: " + (_endWrite - _startWrite) + " ms");
    System.out.println("Read time: " + (_endRead - _endWrite) + " ms");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogSequenceNumber(herddb.log.LogSequenceNumber) CommitLog(herddb.log.CommitLog) LogEntry(herddb.log.LogEntry) Test(org.junit.Test)

Example 2 with LogEntry

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

the class FileCommitLogTest method testLogsynch.

@Test
public void testLogsynch() throws Exception {
    FileCommitLogManager manager = new FileCommitLogManager(folder.newFolder().toPath(), 64 * 1024 * 1024);
    int writeCount = 0;
    final long _startWrite = System.currentTimeMillis();
    try (CommitLog log = manager.createCommitLog("tt")) {
        log.startWriting();
        for (int i = 0; i < 100; i++) {
            log.log(LogEntryFactory.beginTransaction(0), true);
            writeCount++;
        }
    }
    final long _endWrite = System.currentTimeMillis();
    AtomicInteger readCount = new AtomicInteger();
    try (CommitLog log = manager.createCommitLog("tt")) {
        log.recovery(LogSequenceNumber.START_OF_TIME, new BiConsumer<LogSequenceNumber, LogEntry>() {

            @Override
            public void accept(LogSequenceNumber t, LogEntry u) {
                readCount.incrementAndGet();
            }
        }, true);
    }
    final long _endRead = System.currentTimeMillis();
    assertEquals(writeCount, readCount.get());
    System.out.println("Write time: " + (_endWrite - _startWrite) + " ms");
    System.out.println("Read time: " + (_endRead - _endWrite) + " ms");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogSequenceNumber(herddb.log.LogSequenceNumber) CommitLog(herddb.log.CommitLog) LogEntry(herddb.log.LogEntry) Test(org.junit.Test)

Example 3 with LogEntry

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

the class TableSpaceManager method alterTable.

private StatementExecutionResult alterTable(AlterTableStatement alterTableStatement, TransactionContext transactionContext) throws TableDoesNotExistException, StatementExecutionException {
    generalLock.writeLock().lock();
    try {
        if (transactionContext.transactionId > 0) {
            throw new StatementExecutionException("ALTER TABLE cannot be executed inside a transaction (txid=" + transactionContext.transactionId + ")");
        }
        AbstractTableManager tableManager = tables.get(alterTableStatement.getTable());
        if (tableManager == null) {
            throw new TableDoesNotExistException("no table " + alterTableStatement.getTable() + " in tablespace " + tableSpaceName);
        }
        Table newTable;
        try {
            newTable = tableManager.getTable().applyAlterTable(alterTableStatement);
        } catch (IllegalArgumentException error) {
            throw new StatementExecutionException(error);
        }
        LogEntry entry = LogEntryFactory.alterTable(newTable, null);
        try {
            CommitLogResult pos = log.log(entry, entry.transactionId <= 0);
            apply(pos, entry, false);
        } catch (Exception err) {
            throw new StatementExecutionException(err);
        }
        return new DDLStatementExecutionResult(transactionContext.transactionId);
    } finally {
        generalLock.writeLock().unlock();
    }
}
Also used : TableDoesNotExistException(herddb.model.TableDoesNotExistException) Table(herddb.model.Table) CommitLogResult(herddb.log.CommitLogResult) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) StatementExecutionException(herddb.model.StatementExecutionException) LogEntry(herddb.log.LogEntry) DumpedLogEntry(herddb.backup.DumpedLogEntry) HDBException(herddb.client.HDBException) IndexAlreadyExistsException(herddb.model.IndexAlreadyExistsException) LogNotAvailableException(herddb.log.LogNotAvailableException) ClientSideMetadataProviderException(herddb.client.ClientSideMetadataProviderException) IndexDoesNotExistException(herddb.model.IndexDoesNotExistException) FullRecoveryNeededException(herddb.log.FullRecoveryNeededException) TableAlreadyExistsException(herddb.model.TableAlreadyExistsException) TimeoutException(java.util.concurrent.TimeoutException) DataStorageManagerException(herddb.storage.DataStorageManagerException) TableDoesNotExistException(herddb.model.TableDoesNotExistException) DDLException(herddb.model.DDLException) StatementExecutionException(herddb.model.StatementExecutionException) EOFException(java.io.EOFException) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException)

Example 4 with LogEntry

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

the class TableSpaceManager method dropIndex.

private StatementExecutionResult dropIndex(DropIndexStatement statement, Transaction transaction) throws StatementExecutionException {
    generalLock.writeLock().lock();
    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 {
        generalLock.writeLock().unlock();
    }
}
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 5 with LogEntry

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

the class TableSpaceManager method dumpTableSpace.

void dumpTableSpace(String dumpId, Channel _channel, int fetchSize, boolean includeLog) throws DataStorageManagerException, LogNotAvailableException {
    LOGGER.log(Level.SEVERE, "dumpTableSpace dumpId:" + dumpId + " channel " + _channel + " fetchSize:" + fetchSize + ", includeLog:" + 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());
        }
    };
    generalLock.writeLock().lock();
    try {
        if (includeLog) {
            log.attachCommitLogListener(logDumpReceiver);
        }
        checkpoint = checkpoint(true, true);
        /* Downgrade lock */
        generalLock.readLock().lock();
    } finally {
        generalLock.writeLock().unlock();
    }
    try {
        final int timeout = 60000;
        Map<String, Object> startData = new HashMap<>();
        startData.put("command", "start");
        LogSequenceNumber logSequenceNumber = log.getLastSequenceNumber();
        startData.put("ledgerid", logSequenceNumber.ledgerId);
        startData.put("offset", logSequenceNumber.offset);
        Message response_to_start = _channel.sendMessageWithReply(Message.TABLESPACE_DUMP_DATA(null, tableSpaceName, dumpId, startData), timeout);
        if (response_to_start.type != Message.TYPE_ACK) {
            LOGGER.log(Level.SEVERE, "error response at start command: " + response_to_start.parameters);
            return;
        }
        if (includeLog) {
            List<Transaction> transactionsSnapshot = new ArrayList<>();
            dataStorageManager.loadTransactions(logSequenceNumber, tableSpaceUUID, transactionsSnapshot::add);
            List<Transaction> batch = new ArrayList<>();
            for (Transaction t : transactionsSnapshot) {
                batch.add(t);
                if (batch.size() == 10) {
                    sendTransactionsDump(batch, _channel, dumpId, timeout, response_to_start);
                }
            }
            sendTransactionsDump(batch, _channel, dumpId, timeout, response_to_start);
        }
        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 {
                FullTableScanConsumer sink = new SingleTableDumper(tableSpaceName, tableManager, _channel, dumpId, timeout, fetchSize);
                tableManager.dump(sequenceNumber, sink);
            } catch (DataStorageManagerException err) {
                Map<String, Object> errorOnData = new HashMap<>();
                errorOnData.put("command", "error");
                _channel.sendMessageWithReply(Message.TABLESPACE_DUMP_DATA(null, tableSpaceName, dumpId, errorOnData), timeout);
                LOGGER.log(Level.SEVERE, "error sending dump id " + dumpId, err);
                return;
            }
        }
        if (!txlogentries.isEmpty()) {
            txlogentries.sort(Comparator.naturalOrder());
            sendDumpedCommitLog(txlogentries, _channel, dumpId, timeout);
        }
        Map<String, Object> finishData = new HashMap<>();
        LogSequenceNumber finishLogSequenceNumber = log.getLastSequenceNumber();
        finishData.put("ledgerid", finishLogSequenceNumber.ledgerId);
        finishData.put("offset", finishLogSequenceNumber.offset);
        finishData.put("command", "finish");
        _channel.sendOneWayMessage(Message.TABLESPACE_DUMP_DATA(null, tableSpaceName, dumpId, finishData), new SendResultCallback() {

            @Override
            public void messageSent(Message originalMessage, Throwable error) {
            }
        });
    } catch (InterruptedException | TimeoutException error) {
        LOGGER.log(Level.SEVERE, "error sending dump id " + dumpId);
    } finally {
        generalLock.readLock().unlock();
        if (includeLog) {
            log.removeCommitLogListener(logDumpReceiver);
        }
        for (Entry<String, LogSequenceNumber> entry : checkpoint.tablesCheckpoints.entrySet()) {
            dataStorageManager.unPinTableCheckpoint(tableSpaceUUID, entry.getKey(), entry.getValue());
        }
    }
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) DumpedLogEntry(herddb.backup.DumpedLogEntry) Message(herddb.network.Message) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) FullTableScanConsumer(herddb.storage.FullTableScanConsumer) SendResultCallback(herddb.network.SendResultCallback) LogEntry(herddb.log.LogEntry) DumpedLogEntry(herddb.backup.DumpedLogEntry) TimeoutException(java.util.concurrent.TimeoutException) CommitLogListener(herddb.log.CommitLogListener) LogSequenceNumber(herddb.log.LogSequenceNumber) TableCheckpoint(herddb.core.AbstractTableManager.TableCheckpoint) Transaction(herddb.model.Transaction) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

LogEntry (herddb.log.LogEntry)22 CommitLogResult (herddb.log.CommitLogResult)14 LogNotAvailableException (herddb.log.LogNotAvailableException)14 StatementExecutionException (herddb.model.StatementExecutionException)12 DataStorageManagerException (herddb.storage.DataStorageManagerException)11 DumpedLogEntry (herddb.backup.DumpedLogEntry)10 LogSequenceNumber (herddb.log.LogSequenceNumber)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 CommitLog (herddb.log.CommitLog)6 IndexAlreadyExistsException (herddb.model.IndexAlreadyExistsException)6 EOFException (java.io.EOFException)6 FullRecoveryNeededException (herddb.log.FullRecoveryNeededException)5 DDLStatementExecutionResult (herddb.model.DDLStatementExecutionResult)5 IndexDoesNotExistException (herddb.model.IndexDoesNotExistException)5 TableAlreadyExistsException (herddb.model.TableAlreadyExistsException)5 TableDoesNotExistException (herddb.model.TableDoesNotExistException)5 TimeoutException (java.util.concurrent.TimeoutException)5 Test (org.junit.Test)5 ClientSideMetadataProviderException (herddb.client.ClientSideMetadataProviderException)4 HDBException (herddb.client.HDBException)4