Skip to content

Fix/replace rdb_erase_range() #2034

Closed
Closed

Description

@danielmewes

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

  1. added this to the milestone on Feb 27, 2014
  2. self-assigned this
    on Feb 27, 2014
  3. modified the milestones: subsequent, on Feb 27, 2014
  4. coffeemug commented on Feb 27, 2014

    @coffeemug
    Contributor

    @danielmewes -- most of the below is FYI. Hope it helps you.

    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.

    This was by design. The idea was that erase_range will 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:

    • It never worked properly
    • It worked properly, but then we started changing the code and didn't account for this

    Perhaps it's still salvageable?

    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.

    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.

  5. danielmewes commented on Feb 27, 2014

    @danielmewes
    MemberAuthor

    @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'...

  6. modified the milestones: subsequent, backlog on Mar 8, 2014
  7. 5 remaining items

  8. coffeemug commented on Apr 26, 2014

    @coffeemug
    Contributor

    Moving to subsequent. This seems important 😄

  9. danielmewes commented on Apr 26, 2014

    @danielmewes
    MemberAuthor

    I'm going to fix the secondary index case as part of #2251.

  10. danielmewes commented on Dec 12, 2014

    @danielmewes
    MemberAuthor

    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:

    Had a thought yesterday about the distribution query. Whenever a shard becomes a be_nothing, it runs an erase_range on the btree. erase_range is implemented in a way where it doesn't actually delete any btree nodes. The leaf nodes will be left underfull (many of them completely empty), and the inner nodes will still look like they're pointing to data.
    If the distribution query sees such an inner node, it will assume that all those inner node entries point to actual data. In the end, it will uniformly divide the total number of keys in the btree over all keys it has found in the inner nodes. As a consequence, the regions of the key that are actually still active shards and contain data, will receive a smaller document count estimate because the distribution query thinks that many of the keys still are in the now erased regions of the tree.
    For example: Say level 2 of the btree consists has inner nodes that point to 100 children overall.
    Say we currently have 10,000 documents into the tree, spread over the full key range.
    The distribution query will count the keys on level 2 (= 100) and look at the global key count of the btree (10,000). If we ask it to estimate the number of documents in the lower half of the btree, it will count the keys on level 2 that fall in that lower range (say ~50), and tell us that there are 10,000 / 100 * 50 = 5,000 documents in that range
    Now say we reshard such that the upper half of the key range is moved to a different server.
    The old server will do an erase_range, but will actually leave the inner nodes on level 2 intact.
    So if we now ask the distribution query for the number of documents in the lower half of the key range, it will still find 100 keys on level 2 (since they haven't been deleted by the erase_range). However the erase_range does update the global key count for the tree, which is now ~5,000. So the estimate it will generate for the lower half is now: 5,000 / 100 * 50 = 2,500
    6:11 But in reality all ~5,000 keys that are still stored in the btree are in the lower half, and the correct result would be 5,000

    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.

  11. modified the milestones: , subsequent on Dec 12, 2014
  12. timmaxw commented on Dec 12, 2014

    @timmaxw
    Member

    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.

  13. danielmewes commented on Dec 12, 2014

    @danielmewes
    MemberAuthor

    Fair enough, let's leave it in. I don't think it adds a lot of complexity to the parallel traversal implementation.

  14. timmaxw commented on Dec 12, 2014

    @timmaxw
    Member

    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.

  15. danielmewes commented on Dec 13, 2014

    @danielmewes
    MemberAuthor

    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.

  16. danielmewes commented on Dec 16, 2014

    @danielmewes
    MemberAuthor

    Finally completely fixed in next, 65e7a9e

  17. modified the milestones: , 1.16 on Jan 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions