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);
}
}
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);
}
}
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;
}
}
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();
}
}
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);
}
}
Aggregations