Fixed a nasty bug in rbyd where shrinking the last id can leave bad alts

This was particularly nasty to track down, the bad alts left in this way
are zero-weight, zero-tag alts that point out of the bounds of the rbyd.
This creates an immovable-object/unstoppable-force situation since the
alt that will never be followed should always be followed. This ended up
creating a confusing issue later since grows can follow this alt and
cause the alt state to fall apart.

The solution is to check for shrink leaves that drop to weight zero and
prune them. This has a side-effect of nicely handling over-sized
shrinks, though these shouldn't happen anyways and are being asserted
on.

Because I really, really don't want a regression, I've added a specific
test for this, though the minimal reproducible case is a bit complex.
The state of the rbyd is rather sensitive and it's not fully clear to me
what ultimately triggers the breakdown of the rbyd tree.

Also added a slightly better check for grow/shrink tags on altle leaves.
I don't know if this is strictly required but I know it keeps me sane.
This commit is contained in:
Christopher Haster
2023-03-11 02:06:02 -06:00
parent 89ab174f33
commit eb6b5332a0
2 changed files with 97 additions and 10 deletions
+12 -8
View File
@@ -2515,13 +2515,13 @@ static int lfsr_rbyd_append(lfs_t *lfs, lfsr_rbyd_t *rbyd,
// if we diverged, merge the bounds
LFS_ASSERT(diverged < 4);
if (diverged == 2) {
// finished on upper path
// finished on lower path
tag_ = other_tag_;
id_ = other_id_;
branch = other_branch;
upper_id = other_upper_id;
} else if (diverged == 3) {
// finished on lower path
// finished on upper path
lower_id = other_lower_id;
}
@@ -2535,8 +2535,10 @@ static int lfsr_rbyd_append(lfs_t *lfs, lfsr_rbyd_t *rbyd,
// found an old removed tag, no split needed, just prune the
// removed tag
} else if ((id_ < id
|| (id_ == id && lfsr_tag_key(tag_) < lfsr_tag_key(tag)))) {
} else if (id_ < id
|| (id_ == id && lfsr_tag_key(tag_) < lfsr_tag_key(tag)
&& tag != LFSR_TAG_GROW
&& tag != LFSR_TAG_SHRINK)) {
// split less than
//
// note this is consistent for all appends and only happens when
@@ -2551,8 +2553,10 @@ static int lfsr_rbyd_append(lfs_t *lfs, lfsr_rbyd_t *rbyd,
} else if (tag == LFSR_TAG_SHRINK) {
// decrease weight when shrinking
alt = LFSR_TAG_ALT(B, GT, 0);
weight = upper_id - lower_id - 1 - lfsr_data_len(data);
if (upper_id - lower_id - 1 > (lfs_ssize_t)lfsr_data_len(data)) {
alt = LFSR_TAG_ALT(B, GT, 0);
weight = upper_id - lower_id - 1 - lfsr_data_len(data);
}
} else if (id_ > id
|| (id_ == id && lfsr_tag_key(tag_) > lfsr_tag_key(tag))) {
@@ -3360,11 +3364,11 @@ static int lfsr_btree_commit(lfs_t *lfs,
// note grow/shrink with 0 is treated as a noop in rbyd
if (rbyd->weight >= pweight) {
scratch_attrs[1] = *LFSR_ATTR(
GROW, pid, NULL, rbyd->weight-pweight,
GROW, pid-(pweight-1), NULL, rbyd->weight-pweight,
NULL);
} else {
scratch_attrs[1] = *LFSR_ATTR(
SHRINK, pid, NULL, pweight-rbyd->weight,
SHRINK, pid-(pweight-1), NULL, pweight-rbyd->weight,
NULL);
}