|
@@ -9,6 +9,8 @@
|
|
|
#include <sstream>
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
#include <fcntl.h>
|
|
|
|
|
+#include <sys/mman.h>
|
|
|
|
|
+#include <sys/stat.h>
|
|
|
#include <unistd.h>
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
namespace smartbotic::database {
|
|
namespace smartbotic::database {
|
|
@@ -333,33 +335,24 @@ bool WriteAheadLog::open() {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Find existing WAL files and determine highest sequence
|
|
|
|
|
|
|
+ // Find existing WAL files and determine highest sequence.
|
|
|
|
|
+ // v2.21.6 - through scanFile: the old loop stopped at the first record it
|
|
|
|
|
+ // could not read, so damage in the current file made the sequence too low
|
|
|
|
|
+ // and new appends reused sequence numbers already on disk.
|
|
|
auto walFiles = getWalFiles();
|
|
auto walFiles = getWalFiles();
|
|
|
- if (!walFiles.empty()) {
|
|
|
|
|
- // Read the last file to get the highest sequence
|
|
|
|
|
- for (auto it = walFiles.rbegin(); it != walFiles.rend(); ++it) {
|
|
|
|
|
- std::ifstream file(*it, std::ios::binary);
|
|
|
|
|
- if (!file) continue;
|
|
|
|
|
-
|
|
|
|
|
- auto header = readHeader(file);
|
|
|
|
|
- if (!header) continue;
|
|
|
|
|
-
|
|
|
|
|
- // Read entries to find highest sequence
|
|
|
|
|
- while (file) {
|
|
|
|
|
- uint32_t length;
|
|
|
|
|
- file.read(reinterpret_cast<char*>(&length), 4);
|
|
|
|
|
- if (!file || length == 0) break;
|
|
|
|
|
-
|
|
|
|
|
- std::vector<uint8_t> entryData(length + 4);
|
|
|
|
|
- std::memcpy(entryData.data(), &length, 4);
|
|
|
|
|
- file.read(reinterpret_cast<char*>(entryData.data() + 4), length);
|
|
|
|
|
- if (!file) break;
|
|
|
|
|
-
|
|
|
|
|
- auto entry = WalEntry::deserialize(entryData);
|
|
|
|
|
- if (entry && entry->sequence > sequence_.load()) {
|
|
|
|
|
- sequence_.store(entry->sequence);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ lastOpenTruncatedBytes_ = 0;
|
|
|
|
|
+ for (const auto& path : walFiles) {
|
|
|
|
|
+ ReplayFileStats stats;
|
|
|
|
|
+ scanFile(path, [this](const WalEntry& entry) {
|
|
|
|
|
+ if (entry.sequence > sequence_.load()) sequence_.store(entry.sequence);
|
|
|
|
|
+ }, stats);
|
|
|
|
|
+ // A torn tail of the file appends go to (a crash mid-write): appending
|
|
|
|
|
+ // after it would leave a damaged record in the middle of the file.
|
|
|
|
|
+ // Cut it at the end of the last valid record.
|
|
|
|
|
+ if (path == currentFilePath() && stats.headerReadable && stats.bytesRead < stats.fileBytes) {
|
|
|
|
|
+ std::error_code rec;
|
|
|
|
|
+ std::filesystem::resize_file(path, stats.bytesRead, rec);
|
|
|
|
|
+ if (!rec) lastOpenTruncatedBytes_ = stats.fileBytes - stats.bytesRead;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -740,67 +733,141 @@ void WriteAheadLog::writeHeader() {
|
|
|
currentFile_.flush();
|
|
currentFile_.flush();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-uint64_t WriteAheadLog::replayFile(const std::filesystem::path& path, uint64_t fromSequence,
|
|
|
|
|
- const std::function<void(const WalEntry&)>& callback,
|
|
|
|
|
- ReplayFileStats& stats) {
|
|
|
|
|
|
|
+namespace {
|
|
|
|
|
+// seq, timestamp, op, collection len, id len, data flag, options flag, crc
|
|
|
|
|
+constexpr uint32_t kMinRecordBody = 8 + 8 + 1 + 2 + 2 + 1 + 1 + 4;
|
|
|
|
|
+constexpr uint32_t kMaxRecordBody = 1u << 30;
|
|
|
|
|
+// A resync candidate's sequence must lie within this distance above the last
|
|
|
|
|
+// good one: a cheap filter before the checksum, generous enough for any WAL.
|
|
|
|
|
+constexpr uint64_t kMaxSequenceGap = 100'000'000ULL;
|
|
|
|
|
+
|
|
|
|
|
+uint32_t le32(const uint8_t* p) {
|
|
|
|
|
+ return static_cast<uint32_t>(p[0]) | static_cast<uint32_t>(p[1]) << 8 |
|
|
|
|
|
+ static_cast<uint32_t>(p[2]) << 16 | static_cast<uint32_t>(p[3]) << 24;
|
|
|
|
|
+}
|
|
|
|
|
+uint64_t le64(const uint8_t* p) {
|
|
|
|
|
+ uint64_t v = 0;
|
|
|
|
|
+ for (int i = 7; i >= 0; --i) v = (v << 8) | p[i];
|
|
|
|
|
+ return v;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// The record at `off` if it frames, checksums and parses; nullopt otherwise.
|
|
|
|
|
+std::optional<WalEntry> recordAt(const uint8_t* base, uint64_t size, uint64_t off,
|
|
|
|
|
+ uint32_t& lenOut) {
|
|
|
|
|
+ if (off + 4 > size) return std::nullopt;
|
|
|
|
|
+ const uint32_t len = le32(base + off);
|
|
|
|
|
+ if (len < kMinRecordBody || len > kMaxRecordBody || off + 4 + len > size) return std::nullopt;
|
|
|
|
|
+ std::vector<uint8_t> rec(base + off, base + off + 4 + len);
|
|
|
|
|
+ auto e = WalEntry::deserialize(rec);
|
|
|
|
|
+ if (e) lenOut = len;
|
|
|
|
|
+ return e;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// Read-only mapping of a whole file, unmapped on scope exit.
|
|
|
|
|
+struct MappedFile {
|
|
|
|
|
+ const uint8_t* data = nullptr;
|
|
|
|
|
+ uint64_t size = 0;
|
|
|
|
|
+ explicit MappedFile(const std::filesystem::path& path) {
|
|
|
|
|
+ const int fd = ::open(path.c_str(), O_RDONLY);
|
|
|
|
|
+ if (fd < 0) return;
|
|
|
|
|
+ struct stat st {};
|
|
|
|
|
+ if (::fstat(fd, &st) == 0 && st.st_size > 0) {
|
|
|
|
|
+ void* p = ::mmap(nullptr, static_cast<size_t>(st.st_size), PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
|
+ if (p != MAP_FAILED) {
|
|
|
|
|
+ data = static_cast<const uint8_t*>(p);
|
|
|
|
|
+ size = static_cast<uint64_t>(st.st_size);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ ::close(fd);
|
|
|
|
|
+ }
|
|
|
|
|
+ ~MappedFile() {
|
|
|
|
|
+ if (data) ::munmap(const_cast<uint8_t*>(data), static_cast<size_t>(size));
|
|
|
|
|
+ }
|
|
|
|
|
+ MappedFile(const MappedFile&) = delete;
|
|
|
|
|
+ MappedFile& operator=(const MappedFile&) = delete;
|
|
|
|
|
+};
|
|
|
|
|
+} // namespace
|
|
|
|
|
+
|
|
|
|
|
+// v2.21.6 - the one reader of a WAL file, for replay() and open() alike.
|
|
|
|
|
+//
|
|
|
|
|
+// It used to stop at the first record it could not read: a production file
|
|
|
|
|
+// had ONE damaged record (the 2.21.5 flush race) and the 160,261 intact
|
|
|
|
|
+// records behind it were never applied. Now a record that does not frame,
|
|
|
|
|
+// checksum or parse starts a resync: the scan moves forward a byte at a time
|
|
|
|
|
+// to the next offset holding a record that does - a plausible length, an op
|
|
|
|
|
+// type in range and a sequence above the last good one (cheap filters), then
|
|
|
|
|
+// the checksum and a full parse, which a random offset passes with
|
|
|
|
|
+// negligible probability. What was skipped is counted. Bytes after the last
|
|
|
|
|
+// valid record that no resync can parse (a torn tail) end the file;
|
|
|
|
|
+// `stats.bytesRead` is then the end of the last valid record.
|
|
|
|
|
+void WriteAheadLog::scanFile(const std::filesystem::path& path,
|
|
|
|
|
+ const std::function<void(const WalEntry&)>& onEntry,
|
|
|
|
|
+ ReplayFileStats& stats) {
|
|
|
|
|
+ MappedFile f(path);
|
|
|
std::error_code size_ec;
|
|
std::error_code size_ec;
|
|
|
stats.fileBytes = std::filesystem::file_size(path, size_ec);
|
|
stats.fileBytes = std::filesystem::file_size(path, size_ec);
|
|
|
- std::ifstream file(path, std::ios::binary);
|
|
|
|
|
- if (!file) {
|
|
|
|
|
|
|
+ if (!f.data || f.size < sizeof(Header) || std::memcmp(f.data, "CALWAL01", 8) != 0) {
|
|
|
stats.headerReadable = false;
|
|
stats.headerReadable = false;
|
|
|
- return 0;
|
|
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
+ const uint64_t start = le64(f.data + 8);
|
|
|
|
|
+ uint64_t floor = start > 0 ? start - 1 : 0; // sequences here are above it
|
|
|
|
|
+ uint64_t off = sizeof(Header);
|
|
|
|
|
+ stats.bytesRead = off;
|
|
|
|
|
+
|
|
|
|
|
+ while (off < f.size) {
|
|
|
|
|
+ uint32_t len = 0;
|
|
|
|
|
+ if (auto e = recordAt(f.data, f.size, off, len)) {
|
|
|
|
|
+ if (stats.firstSequence == 0) stats.firstSequence = e->sequence;
|
|
|
|
|
+ else if (e->sequence <= stats.lastSequence) ++stats.outOfOrder;
|
|
|
|
|
+ stats.lastSequence = e->sequence;
|
|
|
|
|
+ if (e->sequence > floor) floor = e->sequence;
|
|
|
|
|
+ onEntry(*e);
|
|
|
|
|
+ off += 4 + static_cast<uint64_t>(len);
|
|
|
|
|
+ stats.bytesRead = off;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- auto header = readHeader(file);
|
|
|
|
|
- if (!header) {
|
|
|
|
|
- stats.headerReadable = false;
|
|
|
|
|
- return 0;
|
|
|
|
|
|
|
+ // Damaged at `off`.
|
|
|
|
|
+ ++stats.unreadable;
|
|
|
|
|
+ if (stats.firstUnreadableOffset == 0) stats.firstUnreadableOffset = off;
|
|
|
|
|
+ if (off + 4 <= f.size) {
|
|
|
|
|
+ const uint32_t l = le32(f.data + off);
|
|
|
|
|
+ if (l >= kMinRecordBody && l <= kMaxRecordBody && off + 4 + l <= f.size &&
|
|
|
|
|
+ le32(f.data + off + l) != crc32::calculate(f.data + off + 4, l - 4)) {
|
|
|
|
|
+ ++stats.checksumFailures;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ uint64_t c = off + 1;
|
|
|
|
|
+ bool found = false;
|
|
|
|
|
+ for (; c + 4 + kMinRecordBody <= f.size; ++c) {
|
|
|
|
|
+ const uint8_t op = f.data[c + 4 + 16];
|
|
|
|
|
+ if (op < static_cast<uint8_t>(WalOpType::INSERT) ||
|
|
|
|
|
+ op > static_cast<uint8_t>(WalOpType::VEC_DELETE)) continue;
|
|
|
|
|
+ const uint64_t seq = le64(f.data + c + 4);
|
|
|
|
|
+ if (seq <= floor || seq > floor + kMaxSequenceGap) continue;
|
|
|
|
|
+ uint32_t l2 = 0;
|
|
|
|
|
+ if (recordAt(f.data, f.size, c, l2)) { found = true; break; }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!found) break; // nothing valid follows: a torn tail
|
|
|
|
|
+ stats.skippedBytes += c - off;
|
|
|
|
|
+ ++stats.resynced;
|
|
|
|
|
+ off = c;
|
|
|
}
|
|
}
|
|
|
- stats.bytesRead = static_cast<uint64_t>(file.tellg());
|
|
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
+uint64_t WriteAheadLog::replayFile(const std::filesystem::path& path, uint64_t fromSequence,
|
|
|
|
|
+ const std::function<void(const WalEntry&)>& callback,
|
|
|
|
|
+ ReplayFileStats& stats) {
|
|
|
uint64_t count = 0;
|
|
uint64_t count = 0;
|
|
|
-
|
|
|
|
|
- while (file) {
|
|
|
|
|
- // Read entry length
|
|
|
|
|
- uint32_t length;
|
|
|
|
|
- file.read(reinterpret_cast<char*>(&length), 4);
|
|
|
|
|
- if (!file || length == 0) break;
|
|
|
|
|
-
|
|
|
|
|
- // Read entry data
|
|
|
|
|
- std::vector<uint8_t> entryData(length + 4);
|
|
|
|
|
- std::memcpy(entryData.data(), &length, 4);
|
|
|
|
|
- file.read(reinterpret_cast<char*>(entryData.data() + 4), length);
|
|
|
|
|
- if (!file) break;
|
|
|
|
|
- const uint64_t entryOffset = stats.bytesRead;
|
|
|
|
|
- stats.bytesRead += 4 + static_cast<uint64_t>(length);
|
|
|
|
|
-
|
|
|
|
|
- // Deserialize and invoke callback
|
|
|
|
|
- auto entry = WalEntry::deserialize(entryData);
|
|
|
|
|
- if (!entry) {
|
|
|
|
|
- ++stats.unreadable;
|
|
|
|
|
- if (stats.firstUnreadableOffset == 0) stats.firstUnreadableOffset = entryOffset;
|
|
|
|
|
- if (length >= 4) {
|
|
|
|
|
- uint32_t stored = 0;
|
|
|
|
|
- for (int i = 0; i < 4; ++i) {
|
|
|
|
|
- stored |= static_cast<uint32_t>(entryData[entryData.size() - 4 + i]) << (i * 8);
|
|
|
|
|
- }
|
|
|
|
|
- if (stored != crc32::calculate(entryData.data() + 4, length - 4)) {
|
|
|
|
|
- ++stats.checksumFailures;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- if (stats.firstSequence == 0) stats.firstSequence = entry->sequence;
|
|
|
|
|
- else if (entry->sequence <= stats.lastSequence) ++stats.outOfOrder;
|
|
|
|
|
- stats.lastSequence = entry->sequence;
|
|
|
|
|
- if (entry->sequence > fromSequence) {
|
|
|
|
|
- callback(*entry);
|
|
|
|
|
- count++;
|
|
|
|
|
|
|
+ scanFile(path, [&](const WalEntry& entry) {
|
|
|
|
|
+ if (entry.sequence > fromSequence) {
|
|
|
|
|
+ callback(entry);
|
|
|
|
|
+ ++count;
|
|
|
} else {
|
|
} else {
|
|
|
++stats.atOrBelow;
|
|
++stats.atOrBelow;
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
|
|
+ }, stats);
|
|
|
stats.applied = count;
|
|
stats.applied = count;
|
|
|
return count;
|
|
return count;
|
|
|
}
|
|
}
|