Repository navigation
Description
What it is
rdb_erase_range() is a function that erases a certain range of keys from a btree. erasing is different from a regular delete() here in the sense that it doesn't generate delete entries which backfilling would otherwise pick up.
It's used in the following two cases (as far as I know):
- backfilling, to erase key ranges for which we are not sure if they might contain deleted keys (so we erase everything in that range on the backfillee and resend it completely)
- a node used to be a replica for a given shard, but becomes a "be_nothing". This causes the shard's data to be erased on that node.
Why it's broken
rdb_erase_range() is based on btree_erase_range_generic(). btree_erase_range_generic() uses a parallel traversal to walk over the specified range. When it encounters a leaf node, it erases all keys in that range from the leaf node.
However it doesn't modify the inner nodes of the btree at all! It doesn't maintain the btree invariants, and the tree becomes broken. We end up with a lot of underfull or even empty leaf nodes, and a bunch of internal nodes full of keys above them.
No levelling or merging is done.
Future "proper" btree operations can stumble over this and probably corrupt the tree even more. For example they might decide to perform levelling somewhere based on the information they have. But then they fail to pick a key to level around, because the leaf nodes are empty. All kinds of horrible things can happen.
It could be that we handle most of these cases correctly. But I highly doubt that (and #2015 could be an indication that we don't).
How to fix it?
I'm not sure yet.
We could only collect a set of keys to be deleted in the parallel traversal, and then erase one of them at a time through the usual process that takes care of re-balancing the tree. It would be slower, but then again this happens only in backfilling and when a node looses its role as a replica.
Activity
coffeemug commentedon Feb 27, 2014
@danielmewes -- most of the below is FYI. Hope it helps you.
This was by design. The idea was that
erase_rangewill break some of the invariants, but in such a way that doesn't break the invariants expected by other code. The thought process was that during later point insert/update/delete operations the code will walk over the broken invariants and lazily patch them.Of course I believe you that it's not what happens. There are two plausible possibilities:
Perhaps it's still salvageable?
Wouldn't it also happen during resharding? If I add a node and shard a table into two, wouldn't the first node range erase half of its data? That's a pretty common operation. I think the problem with converting to point erases is that it might be unacceptably slow. If we shard a 100M document table in half, how long would it take to point-delete 50M documents? (It might be ok, I'm not sure; we should probably test and see)
Another thing to keep in mind is #364. If we replace range sharding with hash sharding, range erase might become a lot less valuable (depending on how the consistent hashing algorithm works during a reshard). So perhaps lots of point deletes is a better solution than biting the bullet and implementing a proper range erase operation (or may be not -- I don't know how much work that is to support such a thing).
Also see #1762. Perhaps these are worth considering together.
danielmewes commentedon Feb 27, 2014
@coffeemug: Thanks for chiming in. I had also talked to @srh about this yesterday and I'm halfway into believing that our code does handle the underfull btree correctly. It still seems like bugs might sneak in very easily.
Maybe I should just write a few additional unit tests to verify that our code works reliably with such semi-btrees...
There is also new evidence now that the issue in #2015 has nothing to do with this, but is indeed a problem with either the cache or how we interact with it.
I'm removing the 'tp:bug' tag, and keep this as 'tp:hygiene'...
5 remaining items
coffeemug commentedon Apr 26, 2014
Moving to subsequent. This seems important 😄
danielmewes commentedon Apr 26, 2014
I'm going to fix the secondary index case as part of #2251.
danielmewes commentedon Dec 12, 2014
We should fix it for the primary tree as well, since it turns out that this can massively screw up the distribution query and approximate document count after resharding.
Here's why for the interested reader:
Instead of using erase_range (which is based on a parallel btree traversal), we should just delete every document from the primary tree individually (which will allow us to do proper rebalancing of the tree). We're already doing that in this scenario for secondary index trees, so it wouldn't make the process much slower than it already is.
I haven't checked, but this could well be the last piece of code that still uses the ability of parallel traversal to support write queries. We can consider getting rid of the support for that as well.
timmaxw commentedon Dec 12, 2014
In the long run, it seems like there are good use cases for parallel traversal supporting write queries. If we ever wanted to make
r.table("foo").update({"bar":r.row("bar").add(1)})not round-trip every document, we'd want parallel traveral to support write queries.danielmewes commentedon Dec 12, 2014
Fair enough, let's leave it in. I don't think it adds a lot of complexity to the parallel traversal implementation.
timmaxw commentedon Dec 12, 2014
On the other hand, it seems like it can produce totally broken results--if you write a key and replace it with a much larger value, you could create an overfull leaf node. So it seems like we'd at least have to make a major overhaul of it before using it for anything.
danielmewes commentedon Dec 13, 2014
Implemented in branch daniel_2034, CR 2393 by @timmaxw .
In a test, this indeed fixed the problem with starkly wrong distribution query results and approximate document counts.
danielmewes commentedon Dec 16, 2014
Finally completely fixed in
next, 65e7a9e