Search in sources :

Example 11 with LogNotAvailableException

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

the class FileCommitLog method dropOldLedgers.

@Override
public void dropOldLedgers(LogSequenceNumber lastCheckPointSequenceNumber) throws LogNotAvailableException {
    LOGGER.log(Level.SEVERE, "dropOldLedgers {2} lastCheckPointSequenceNumber: {0}, currentLedgerId: {1}", new Object[] { lastCheckPointSequenceNumber, currentLedgerId, tableSpaceName });
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(logDirectory)) {
        List<Path> names = new ArrayList<>();
        for (Path path : stream) {
            if (Files.isRegularFile(path) && (path.getFileName() + "").endsWith(LOGFILEEXTENSION)) {
                names.add(path);
            }
        }
        names.sort(Comparator.comparing(Path::toString));
        final Path last = names.isEmpty() ? null : names.get(names.size() - 1);
        int count = 0;
        long ledgerLimit = Math.min(lastCheckPointSequenceNumber.ledgerId, currentLedgerId);
        for (Path path : names) {
            boolean lastFile = path.equals(last);
            String name = (path.getFileName() + "").replace(LOGFILEEXTENSION, "");
            try {
                long ledgerId = Long.parseLong(name, 16);
                if (!lastFile && ledgerId < ledgerLimit) {
                    LOGGER.log(Level.SEVERE, "deleting logfile {0} for ledger {1}", new Object[] { path.toAbsolutePath(), ledgerId });
                    try {
                        Files.delete(path);
                    } catch (IOException errorDelete) {
                        LOGGER.log(Level.SEVERE, "fatal error while deleting file " + path, errorDelete);
                        throw new LogNotAvailableException(errorDelete);
                    }
                    ++count;
                }
            } catch (NumberFormatException notValid) {
            }
        }
        LOGGER.log(Level.SEVERE, "Deleted logfiles: {0}", count);
    } catch (IOException err) {
        failed = true;
        throw new LogNotAvailableException(err);
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LogNotAvailableException(herddb.log.LogNotAvailableException)

Example 12 with LogNotAvailableException

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

the class FileCommitLog method recovery.

@Override
public void recovery(LogSequenceNumber snapshotSequenceNumber, BiConsumer<LogSequenceNumber, LogEntry> consumer, boolean fencing) throws LogNotAvailableException {
    LOGGER.log(Level.INFO, "recovery {1}, snapshotSequenceNumber: {0}", new Object[] { snapshotSequenceNumber, tableSpaceName });
    // no lock is needed, we are at boot time
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(logDirectory)) {
        List<Path> names = new ArrayList<>();
        for (Path path : stream) {
            if (Files.isRegularFile(path) && (path.getFileName() + "").endsWith(LOGFILEEXTENSION)) {
                names.add(path);
            }
        }
        names.sort(Comparator.comparing(Path::toString));
        long offset = -1;
        for (Path p : names) {
            LOGGER.log(Level.INFO, "tablespace {1}, logfile is {0}", new Object[] { p.toAbsolutePath(), tableSpaceName });
            String name = (p.getFileName() + "").replace(LOGFILEEXTENSION, "");
            long ledgerId = Long.parseLong(name, 16);
            currentLedgerId = Math.max(currentLedgerId, ledgerId);
            offset = -1;
            try (CommitFileReader reader = new CommitFileReader(logDirectory, ledgerId)) {
                LogEntryWithSequenceNumber n = reader.nextEntry();
                while (n != null) {
                    offset = n.logSequenceNumber.offset;
                    if (n.logSequenceNumber.after(snapshotSequenceNumber)) {
                        LOGGER.log(Level.FINE, "RECOVER ENTRY {0}, {1}", new Object[] { n.logSequenceNumber, n.entry });
                        consumer.accept(n.logSequenceNumber, n.entry);
                    } else {
                        LOGGER.log(Level.FINE, "SKIP ENTRY {0}, {1}", new Object[] { n.logSequenceNumber, n.entry });
                    }
                    n = reader.nextEntry();
                }
            }
        }
        recoveredLogSequence = new LogSequenceNumber(currentLedgerId, offset);
        LOGGER.log(Level.INFO, "Tablespace {1}, max ledgerId is {0}", new Object[] { currentLedgerId, tableSpaceName });
    } catch (IOException | RuntimeException err) {
        failed = true;
        throw new LogNotAvailableException(err);
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) LogSequenceNumber(herddb.log.LogSequenceNumber) IOException(java.io.IOException) LogNotAvailableException(herddb.log.LogNotAvailableException)

Example 13 with LogNotAvailableException

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

the class FileCommitLog method writeEntry.

private int writeEntry(LogEntryHolderFuture entry) {
    try {
        CommitFileWriter writer = this.writer;
        if (writer == null) {
            throw new IOException("not yet writable");
        }
        long newSequenceNumber = ++writer.sequenceNumber;
        int written = writer.writeEntry(newSequenceNumber, entry.entry);
        if (writtenBytes > maxLogFileSize) {
            openNewLedger();
        }
        entry.done(new LogSequenceNumber(writer.ledgerId, newSequenceNumber));
        statsEntryLatency.registerSuccessfulEvent(System.currentTimeMillis() - entry.timestamp, TimeUnit.MILLISECONDS);
        return written;
    } catch (IOException | LogNotAvailableException err) {
        entry.error(err);
        statsEntryLatency.registerFailedEvent(System.currentTimeMillis() - entry.timestamp, TimeUnit.MILLISECONDS);
        return 0;
    }
}
Also used : LogSequenceNumber(herddb.log.LogSequenceNumber) IOException(java.io.IOException) LogNotAvailableException(herddb.log.LogNotAvailableException)

Example 14 with LogNotAvailableException

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

the class FileCommitLog method close.

@Override
public void close() throws LogNotAvailableException {
    closed = true;
    onClose.accept(this);
    try {
        if (writeQueue.isEmpty()) {
            writeQueue.add(new LogEntryHolderFuture(null, false));
        }
        spool.join();
    } catch (InterruptedException err) {
        Thread.currentThread().interrupt();
        throw new LogNotAvailableException(err);
    }
    if (writer != null) {
        writer.close();
    }
}
Also used : LogNotAvailableException(herddb.log.LogNotAvailableException)

Example 15 with LogNotAvailableException

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

the class FileCommitLog method log.

@Override
public CommitLogResult log(LogEntry edit, boolean sync) throws LogNotAvailableException {
    if (failed) {
        throw new LogNotAvailableException("file commit log is failed");
    }
    boolean hasListeners = isHasListeners();
    if (hasListeners) {
        sync = true;
    }
    if (LOGGER.isLoggable(Level.FINEST)) {
        LOGGER.log(Level.FINEST, "log {0}", edit);
    }
    LogEntryHolderFuture future = new LogEntryHolderFuture(edit, sync);
    try {
        queueSize.incrementAndGet();
        pendingEntries.incrementAndGet();
        writeQueue.put(future);
        if (!sync) {
            return new CommitLogResult(future.ack, false, /* deferred */
            false);
        } else {
            if (hasListeners) {
                future.ack.thenAccept((pos) -> {
                    notifyListeners(pos, edit);
                });
            }
            return new CommitLogResult(future.ack, false, /* deferred */
            true);
        }
    } catch (InterruptedException err) {
        Thread.currentThread().interrupt();
        throw new LogNotAvailableException(err);
    }
}
Also used : CommitLogResult(herddb.log.CommitLogResult) LogNotAvailableException(herddb.log.LogNotAvailableException)

Aggregations

LogNotAvailableException (herddb.log.LogNotAvailableException)35 CommitLogResult (herddb.log.CommitLogResult)20 LogEntry (herddb.log.LogEntry)19 StatementExecutionException (herddb.model.StatementExecutionException)18 DataStorageManagerException (herddb.storage.DataStorageManagerException)18 IOException (java.io.IOException)15 LogSequenceNumber (herddb.log.LogSequenceNumber)12 DDLStatementExecutionResult (herddb.model.DDLStatementExecutionResult)11 DumpedLogEntry (herddb.backup.DumpedLogEntry)10 DDLException (herddb.model.DDLException)9 ArrayList (java.util.ArrayList)9 MetadataStorageManagerException (herddb.metadata.MetadataStorageManagerException)7 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)7 IndexAlreadyExistsException (herddb.model.IndexAlreadyExistsException)7 Table (herddb.model.Table)7 ScanStatement (herddb.model.commands.ScanStatement)7 Bytes (herddb.utils.Bytes)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 CommitLog (herddb.log.CommitLog)6 FullRecoveryNeededException (herddb.log.FullRecoveryNeededException)6