Rewrite corrupt block number calculations to be more clear.

This commit is contained in:
Steve Atherton 2023-03-16 13:02:15 -07:00
parent df5b15e56c
commit 5c795c3abe

View File

@ -153,18 +153,28 @@ struct PageChecksumCodec {
if (!silent) {
auto severity = SevError;
if (g_network->isSimulated()) {
auto firstBlock = pageNumber == 1 ? 0 : ((pageNumber - 1) * pageLen) / 4096;
auto lastBlock = (pageNumber * pageLen + 4095) / 4096;
// Calculate file offsets for the read/write operation space
// Operation starts at a 1-based pageNumber and is of size pageLen
int64_t fileOffsetStart = (pageNumber - 1) * pageLen;
// End refers to the offset after the operation, not the last byte.
int64_t fileOffsetEnd = fileOffsetStart + pageLen;
auto iter = g_simulator->corruptedBlocks.lower_bound(std::make_pair(filename, firstBlock));
if (iter != g_simulator->corruptedBlocks.end() && iter->first == filename && iter->second < lastBlock) {
// Convert the file offsets to potentially corrupt block numbers
// Corrupt block numbers are 0-based and 4096 bytes in length.
int64_t corruptBlockStart = fileOffsetStart / 4096;
// corrupt block end is the block number AFTER the operation
int64_t corruptBlockEnd = (fileOffsetEnd + 4095) / 4096;
auto iter = g_simulator->corruptedBlocks.lower_bound(std::make_pair(filename, corruptBlockStart));
if (iter != g_simulator->corruptedBlocks.end() && iter->first == filename &&
iter->second < corruptBlockEnd) {
severity = SevWarnAlways;
}
TraceEvent("CheckCorruption")
.detail("Filename", filename)
.detail("NextFile", iter->first)
.detail("FirstBlock", firstBlock)
.detail("LastBlock", lastBlock)
.detail("BlockStart", corruptBlockStart)
.detail("BlockEnd", corruptBlockEnd)
.detail("NextBlock", iter->second);
}
TraceEvent trEvent(severity, "SQLitePageChecksumFailure");