fix(events): recover partial appends without restarting
This commit is contained in:
1 parent
7b6ea939a5
commit
a19909f152
2 files changed
+402
-14
No files matched your search
@@ -60,6 +60,7 @@ public final class LocalEventLog implements AutoCloseable {
|
||||
private final java.nio.channels.FileLock writerLock;
|
||||
private long sequence;
|
||||
private Path segment;
|
||||
private Path damagedSegment;
|
||||
private LocalDate segmentDate;
|
||||
private long segmentBytes;
|
||||
private int segmentNumber;
|
||||
@@ -203,24 +204,40 @@ public final class LocalEventLog implements AutoCloseable {
|
||||
}
|
||||
boolean recovered = false;
|
||||
for (var path : files) {
|
||||
if (Files.isSymbolicLink(path)) throw new IOException("Symbolic link in Fantasy Stick event segments");
|
||||
var bytes = Files.readAllBytes(path);
|
||||
int prefix=0,cursor=0;while(cursor<bytes.length){int end=cursor;while(end<bytes.length&&bytes[end]!='\n')end++;if(end==bytes.length)break;var line=java.util.Arrays.copyOfRange(bytes,cursor,end+1);if(!validJsonObjectLine(line))break;prefix=end+1;cursor=end+1;}if(prefix==bytes.length)continue;
|
||||
recovered = true;
|
||||
var quarantine = path.resolveSibling(path.getFileName() + ".corrupt-" + UUID.randomUUID());
|
||||
try (var channel = FileChannel.open(quarantine, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
|
||||
var tail = ByteBuffer.wrap(bytes, prefix, bytes.length - prefix);
|
||||
while (tail.hasRemaining()) channel.write(tail);
|
||||
channel.force(true);
|
||||
}
|
||||
try (var channel = FileChannel.open(path, StandardOpenOption.WRITE)) {
|
||||
channel.truncate(prefix);
|
||||
channel.force(true);
|
||||
}
|
||||
recovered |= recoverInterruptedTail(path);
|
||||
}
|
||||
return recovered;
|
||||
}
|
||||
|
||||
/** Quarantine must be forced before truncation; failure leaves pending work and repair state intact. */
|
||||
private boolean recoverInterruptedTail(Path path) throws IOException {
|
||||
if (Files.isSymbolicLink(path)) throw new IOException("Symbolic link in Fantasy Stick event segments");
|
||||
var bytes = Files.readAllBytes(path);
|
||||
int prefix = 0;
|
||||
int cursor = 0;
|
||||
while (cursor < bytes.length) {
|
||||
int end = cursor;
|
||||
while (end < bytes.length && bytes[end] != '\n') end++;
|
||||
if (end == bytes.length) break;
|
||||
var line = java.util.Arrays.copyOfRange(bytes, cursor, end + 1);
|
||||
if (!validJsonObjectLine(line)) break;
|
||||
prefix = end + 1;
|
||||
cursor = end + 1;
|
||||
}
|
||||
if (prefix == bytes.length) return false;
|
||||
var quarantine = path.resolveSibling(path.getFileName() + ".corrupt-" + UUID.randomUUID());
|
||||
try (var channel = FileChannel.open(quarantine, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
|
||||
var tail = ByteBuffer.wrap(bytes, prefix, bytes.length - prefix);
|
||||
while (tail.hasRemaining()) channel.write(tail);
|
||||
channel.force(true);
|
||||
}
|
||||
try (var channel = FileChannel.open(path, StandardOpenOption.WRITE)) {
|
||||
channel.truncate(prefix);
|
||||
channel.force(true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private int recoverPending() throws IOException {
|
||||
List<Path> files;
|
||||
try (var stream = Files.list(pending)) {
|
||||
@@ -247,6 +264,10 @@ public final class LocalEventLog implements AutoCloseable {
|
||||
}
|
||||
|
||||
private void append(byte[] line) throws IOException {
|
||||
if (damagedSegment != null) {
|
||||
recoverInterruptedTail(damagedSegment);
|
||||
damagedSegment = null;
|
||||
}
|
||||
var date = LocalDate.ofInstant(clock.instant(), ZoneOffset.UTC);
|
||||
if (segment == null || !date.equals(segmentDate)
|
||||
|| segmentBytes > 0 && segmentBytes + line.length > maximumSegmentBytes) {
|
||||
@@ -256,6 +277,14 @@ public final class LocalEventLog implements AutoCloseable {
|
||||
var buffer = ByteBuffer.wrap(line);
|
||||
while (buffer.hasRemaining()) channel.write(buffer);
|
||||
channel.force(true);
|
||||
} catch (IOException failure) {
|
||||
// Even open/force/close failures can leave uncertain bytes. Never reuse this
|
||||
// segment or acknowledge pending work until its tail has been repaired.
|
||||
damagedSegment = segment;
|
||||
segment = null;
|
||||
segmentDate = null;
|
||||
segmentBytes = 0;
|
||||
throw failure;
|
||||
}
|
||||
segmentBytes += line.length;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user