speedb-io / speedb

A RocksDB compliant high performance scalable embedded key-value store
https://www.speedb.io/
Apache License 2.0
898 stars 67 forks source link

Proactive Flushes: WBM::ShouldInitiateAnotherFlushMemOnly may return incorrect result due to a race #758

Closed udi-speedb closed 10 months ago

udi-speedb commented 10 months ago

This method calculates the difference between 2 atomic variables:

  bool ShouldInitiateAnotherFlushMemOnly(size_t curr_memory_used) const {
    return (curr_memory_used - memory_being_freed_ >=
                additional_flush_step_size_ / 2 &&
            curr_memory_used >= additional_flush_initiation_size_);
  }

However, the code decreases curr_memory_used and then memory_beingfreed. Therefore it may happen that the difference will overflow (rather than being negative) and the method will return true rather than false.