foundationdb
foundationdb copied to clipboard
Coalesce adjacent DataShard key ranges in ShardedRocksDB
Coalesce adjacent DataShard key ranges in ShardedRocksDB to reduce bookkeeping overhead and range fragmentation.
Problem
In the sharded RocksDB storage engine implementation (KeyValueStoreShardedRocksDB), when multiple key ranges are merged or assigned to the same physical shard, we ended up with multiple separate DataShard segments even when they were completely contiguous. This caused unnecessary bookkeeping overhead, fragmented the mapping of data shards, and bloated metadata. There was an existing TODO in the code to coalesce these continuous key ranges.
Solution
- Removed the old TODO and the diagnostic check that warned about multiple ranges under a single physical shard.
- Implemented a coalesceDataShards() method inside ShardManager.
- Whenever a new range is added, we sort the active data shards by their start keys and identify contiguous groups (where shardA.range.end == shardB.range.begin).
- Merge contiguous shards into a single DataShard with the consolidated range, clearing the old entries and updating the dataShardMap.
- Added a trace event ShardedRocksDBCoalescedRange to track coalescing events.
Testing
- Verification and testing was completed with the help of my co-author, @Renish-Patel ([email protected]). Huge thanks to Renish for helping implement the coalescing logic and driving the testing/validation!