• I
    Make Compaction class easier to use · 47b87439
    Igor Canadi 提交于
    Summary:
    The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
    
    Here are couple of things demonstrating that Compaction class is hard to use:
    1. we have two constructors of Compaction class
    2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
    3. it's easy to introduce a subtle and dangerous bug like this: D36225
    4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
    
    The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
    
    My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
    
    This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
    * have one Compaction constructor instead of two.
    * inputs_ is constant after construction
    * MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
    * SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
    * CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
    
    Test Plan:
    make check
    make asan_check
    make valgrind_check
    
    Reviewers: rven, anthony, sdong, yhchiang
    
    Reviewed By: yhchiang
    
    Subscribers: sdong, yhchiang, dhruba, leveldb
    
    Differential Revision: https://reviews.facebook.net/D36687
    47b87439
compaction.h 9.4 KB