Implement setOldestVersion
This commit is contained in:
265
ConflictSet.cpp
265
ConflictSet.cpp
@@ -1380,6 +1380,114 @@ void addWriteRange(Node *&root, int64_t oldestVersion,
|
||||
}
|
||||
}
|
||||
|
||||
struct FirstGeqStepwise {
|
||||
Node *n;
|
||||
std::span<const uint8_t> remaining;
|
||||
int cmp;
|
||||
|
||||
enum Phase {
|
||||
Init,
|
||||
// Being in this phase implies that the key matches the search path exactly
|
||||
// up to this point
|
||||
Search,
|
||||
DownLeftSpine
|
||||
};
|
||||
Phase phase;
|
||||
|
||||
FirstGeqStepwise(Node *n, std::span<const uint8_t> remaining)
|
||||
: n(n), remaining(remaining), phase(Init) {}
|
||||
|
||||
// Not being done implies that n is not the firstGeq
|
||||
bool step() {
|
||||
switch (phase) {
|
||||
case Search:
|
||||
if (remaining.size() == 0) {
|
||||
int c = getChildGeq(n, 0);
|
||||
assert(c >= 0);
|
||||
n = getChildExists(n, c);
|
||||
return downLeftSpine();
|
||||
} else {
|
||||
int c = getChildGeq(n, remaining[0]);
|
||||
if (c == remaining[0]) {
|
||||
n = getChildExists(n, c);
|
||||
remaining = remaining.subspan(1, remaining.size() - 1);
|
||||
} else {
|
||||
if (c >= 0) {
|
||||
n = getChildExists(n, c);
|
||||
return downLeftSpine();
|
||||
} else {
|
||||
n = nextSibling(n);
|
||||
return downLeftSpine();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (n->partialKeyLen > 0) {
|
||||
int commonLen = std::min<int>(n->partialKeyLen, remaining.size());
|
||||
for (int i = 0; i < commonLen; ++i) {
|
||||
auto c = n->partialKey[i] <=> remaining[i];
|
||||
if (c == 0) {
|
||||
continue;
|
||||
}
|
||||
if (c > 0) {
|
||||
return downLeftSpine();
|
||||
} else {
|
||||
n = nextSibling(n);
|
||||
return downLeftSpine();
|
||||
}
|
||||
}
|
||||
if (commonLen == n->partialKeyLen) {
|
||||
// partial key matches
|
||||
remaining =
|
||||
remaining.subspan(commonLen, remaining.size() - commonLen);
|
||||
} else if (n->partialKeyLen > int(remaining.size())) {
|
||||
// n is the first physical node greater than remaining, and there's no
|
||||
// eq node
|
||||
return downLeftSpine();
|
||||
}
|
||||
}
|
||||
[[fallthrough]];
|
||||
case Init:
|
||||
phase = Search;
|
||||
if (remaining.size() == 0 && n->entryPresent) {
|
||||
cmp = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case DownLeftSpine:
|
||||
int c = getChildGeq(n, 0);
|
||||
assert(c >= 0);
|
||||
n = getChildExists(n, c);
|
||||
if (n->entryPresent) {
|
||||
cmp = 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
__builtin_unreachable(); // GCOVR_EXCL_LINE
|
||||
}
|
||||
|
||||
bool downLeftSpine() {
|
||||
phase = DownLeftSpine;
|
||||
if (n == nullptr || n->entryPresent) {
|
||||
cmp = 1;
|
||||
return true;
|
||||
}
|
||||
return step();
|
||||
}
|
||||
};
|
||||
|
||||
Iterator firstGeq(Node *n, const std::span<const uint8_t> key) {
|
||||
FirstGeqStepwise stepwise{n, key};
|
||||
while (!stepwise.step())
|
||||
;
|
||||
return {stepwise.n, stepwise.cmp};
|
||||
}
|
||||
|
||||
Iterator firstGeq(Node *n, std::string_view key) {
|
||||
return firstGeq(
|
||||
n, std::span<const uint8_t>((const uint8_t *)key.data(), key.size()));
|
||||
}
|
||||
|
||||
struct __attribute__((visibility("hidden"))) ConflictSet::Impl {
|
||||
|
||||
void check(const ReadRange *reads, Result *result, int count) const {
|
||||
@@ -1407,8 +1515,10 @@ struct __attribute__((visibility("hidden"))) ConflictSet::Impl {
|
||||
for (int i = 0; i < count; ++i) {
|
||||
const auto &w = writes[i];
|
||||
if (w.end.len > 0) {
|
||||
keyUpdates += 2;
|
||||
addWriteRange(root, oldestVersion, w);
|
||||
} else {
|
||||
keyUpdates += 1;
|
||||
auto *n =
|
||||
insert(&root, std::span<const uint8_t>(w.begin.p, w.begin.len),
|
||||
w.writeVersion, true);
|
||||
@@ -1427,9 +1537,39 @@ struct __attribute__((visibility("hidden"))) ConflictSet::Impl {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setOldestVersion(int64_t oldestVersion) {
|
||||
this->oldestVersion = oldestVersion;
|
||||
Node *prev = firstGeq(root, removalKey).n;
|
||||
assert(prev != nullptr);
|
||||
while (keyUpdates-- > 0) {
|
||||
Node *n = nextLogical(prev);
|
||||
if (n == nullptr) {
|
||||
removalKey = {};
|
||||
return;
|
||||
}
|
||||
|
||||
if (std::max(prev->entry.pointVersion, prev->entry.rangeVersion) <=
|
||||
oldestVersion) {
|
||||
// Any transaction prev would have prevented from committing is
|
||||
// going to fail with TooOld anyway.
|
||||
|
||||
// We still need to make sure that we don't introduce false positives by
|
||||
// just removing it though.
|
||||
if (n->entry.rangeVersion <= oldestVersion) {
|
||||
prev->entryPresent = false;
|
||||
if (prev->numChildren == 0 && prev->parent != nullptr) {
|
||||
eraseChild(prev->parent, prev->parentsIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
prev = n;
|
||||
}
|
||||
removalKeyArena = Arena();
|
||||
removalKey = getSearchPath(removalKeyArena, prev);
|
||||
}
|
||||
|
||||
explicit Impl(int64_t oldestVersion) : oldestVersion(oldestVersion) {
|
||||
// Insert ""
|
||||
root = newNode();
|
||||
@@ -1440,6 +1580,10 @@ struct __attribute__((visibility("hidden"))) ConflictSet::Impl {
|
||||
}
|
||||
~Impl() { destroyTree(root); }
|
||||
|
||||
Arena removalKeyArena;
|
||||
std::span<const uint8_t> removalKey;
|
||||
int64_t keyUpdates = 0;
|
||||
|
||||
Node *root;
|
||||
int64_t oldestVersion;
|
||||
};
|
||||
@@ -1629,123 +1773,16 @@ void checkParentPointers(Node *node, bool &success) {
|
||||
}
|
||||
}
|
||||
|
||||
struct FirstGeqStepwise {
|
||||
Node *n;
|
||||
std::span<const uint8_t> remaining;
|
||||
int cmp;
|
||||
|
||||
enum Phase {
|
||||
Init,
|
||||
// Being in this phase implies that the key matches the search path exactly
|
||||
// up to this point
|
||||
Search,
|
||||
DownLeftSpine
|
||||
};
|
||||
Phase phase;
|
||||
|
||||
FirstGeqStepwise(Node *n, std::span<const uint8_t> remaining)
|
||||
: n(n), remaining(remaining), phase(Init) {}
|
||||
|
||||
// Not being done implies that n is not the firstGeq
|
||||
bool step() {
|
||||
switch (phase) {
|
||||
case Search:
|
||||
if (remaining.size() == 0) {
|
||||
int c = getChildGeq(n, 0);
|
||||
assert(c >= 0);
|
||||
n = getChildExists(n, c);
|
||||
return downLeftSpine();
|
||||
} else {
|
||||
int c = getChildGeq(n, remaining[0]);
|
||||
if (c == remaining[0]) {
|
||||
n = getChildExists(n, c);
|
||||
remaining = remaining.subspan(1, remaining.size() - 1);
|
||||
} else {
|
||||
if (c >= 0) {
|
||||
n = getChildExists(n, c);
|
||||
return downLeftSpine();
|
||||
} else {
|
||||
n = nextSibling(n);
|
||||
return downLeftSpine();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (n->partialKeyLen > 0) {
|
||||
int commonLen = std::min<int>(n->partialKeyLen, remaining.size());
|
||||
for (int i = 0; i < commonLen; ++i) {
|
||||
auto c = n->partialKey[i] <=> remaining[i];
|
||||
if (c == 0) {
|
||||
continue;
|
||||
}
|
||||
if (c > 0) {
|
||||
return downLeftSpine();
|
||||
} else {
|
||||
n = nextSibling(n);
|
||||
return downLeftSpine();
|
||||
}
|
||||
}
|
||||
if (commonLen == n->partialKeyLen) {
|
||||
// partial key matches
|
||||
remaining =
|
||||
remaining.subspan(commonLen, remaining.size() - commonLen);
|
||||
} else if (n->partialKeyLen > int(remaining.size())) {
|
||||
// n is the first physical node greater than remaining, and there's no
|
||||
// eq node
|
||||
return downLeftSpine();
|
||||
}
|
||||
}
|
||||
[[fallthrough]];
|
||||
case Init:
|
||||
phase = Search;
|
||||
if (remaining.size() == 0 && n->entryPresent) {
|
||||
cmp = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case DownLeftSpine:
|
||||
int c = getChildGeq(n, 0);
|
||||
assert(c >= 0);
|
||||
n = getChildExists(n, c);
|
||||
if (n->entryPresent) {
|
||||
cmp = 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
__builtin_unreachable(); // GCOVR_EXCL_LINE
|
||||
}
|
||||
|
||||
bool downLeftSpine() {
|
||||
phase = DownLeftSpine;
|
||||
if (n == nullptr || n->entryPresent) {
|
||||
cmp = 1;
|
||||
return true;
|
||||
}
|
||||
return step();
|
||||
}
|
||||
};
|
||||
|
||||
Iterator firstGeq(Node *n, const std::span<const uint8_t> key) {
|
||||
FirstGeqStepwise stepwise{n, key};
|
||||
while (!stepwise.step())
|
||||
;
|
||||
return {stepwise.n, stepwise.cmp};
|
||||
}
|
||||
|
||||
Iterator firstGeq(Node *n, std::string_view key) {
|
||||
return firstGeq(
|
||||
n, std::span<const uint8_t>((const uint8_t *)key.data(), key.size()));
|
||||
}
|
||||
|
||||
[[maybe_unused]] int64_t checkMaxVersion(Node *root, Node *node,
|
||||
bool &success) {
|
||||
int64_t oldestVersion, bool &success) {
|
||||
int64_t expected = std::numeric_limits<int64_t>::lowest();
|
||||
if (node->entryPresent) {
|
||||
expected = std::max(expected, node->entry.pointVersion);
|
||||
}
|
||||
for (int i = getChildGeq(node, 0); i >= 0; i = getChildGeq(node, i + 1)) {
|
||||
auto *child = getChildExists(node, i);
|
||||
expected = std::max(expected, checkMaxVersion(root, child, success));
|
||||
expected = std::max(expected,
|
||||
checkMaxVersion(root, child, oldestVersion, success));
|
||||
if (child->entryPresent) {
|
||||
expected = std::max(expected, child->entry.rangeVersion);
|
||||
}
|
||||
@@ -1759,7 +1796,7 @@ Iterator firstGeq(Node *n, std::string_view key) {
|
||||
expected = std::max(expected, borrowed.n->entry.rangeVersion);
|
||||
}
|
||||
}
|
||||
if (node->maxVersion != expected) {
|
||||
if (node->maxVersion > oldestVersion && node->maxVersion != expected) {
|
||||
fprintf(stderr, "%s has max version %" PRId64 " . Expected %" PRId64 "\n",
|
||||
getSearchPathPrintable(node).c_str(), node->maxVersion, expected);
|
||||
success = false;
|
||||
@@ -1783,11 +1820,11 @@ Iterator firstGeq(Node *n, std::string_view key) {
|
||||
return total;
|
||||
}
|
||||
|
||||
bool checkCorrectness(Node *node) {
|
||||
bool checkCorrectness(Node *node, int64_t oldestVersion) {
|
||||
bool success = true;
|
||||
|
||||
checkParentPointers(node, success);
|
||||
checkMaxVersion(node, node, success);
|
||||
checkMaxVersion(node, node, oldestVersion, success);
|
||||
checkEntriesExist(node, success);
|
||||
|
||||
return success;
|
||||
@@ -1842,7 +1879,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||
#if DEBUG_VERBOSE && !defined(NDEBUG)
|
||||
fprintf(stderr, "Check correctness\n");
|
||||
#endif
|
||||
bool success = checkCorrectness(driver.cs.root);
|
||||
bool success = checkCorrectness(driver.cs.root, driver.cs.oldestVersion);
|
||||
if (!success) {
|
||||
debugPrintDot(stdout, driver.cs.root);
|
||||
fflush(stdout);
|
||||
|
Reference in New Issue
Block a user