MrCroxx / RunKV

[WIP] An experimental cloud-native distributed KV engine for OLTP workload.
MIT License
68 stars 7 forks source link

bug: unexpected behavior when MergeIterator changing its direction #26

Closed MrCroxx closed 2 years ago

MrCroxx commented 2 years ago

Currently we simply rebuild the heap when changing the direction of MergeIterator, which is not correct.

Consider this case:

After seeking for 6, the inner iterators of MergeIterator will be like:

Direction::Forward

 1 -  5 -  9
           ^
 2 -  6 - 10
      ^
      ^
 3 -  7 - 11
      ^

If we want to get the prev key, MergeIterator will set its direction as Direction::Backward, and rebuild its heap (move all valid iterators to max heap). This will not modify the states of inner iterators, which is wrong.

Direction::Backward

 1 -  5 -  9
           ^
           ^
 2 -  6 - 10
      ^
 3 -  7 - 11
      ^

Then move to the previous key of the current state:

Direction::Backward

 1 -  5 -  9
      ^
 2 -  6 - 10
      ^
 3 -  7 - 11
      ^
      ^

The output would be 7.

To fix this, simply performing seek is not enough. The core problem is the syntax of current Seek::Random is only for forward moving, more details in #25 . With a bi-way random seek, we can easily handle the bug with seek.

Ref: #25