google / leveldb

LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.
BSD 3-Clause "New" or "Revised" License
35.71k stars 7.72k forks source link

Potential Manifest and Log File ID Conflict During Recovery Process in LevelDB #1193

Open owenliang opened 2 weeks ago

owenliang commented 2 weeks ago

In the function Status VersionSet::Recover(bool* save_manifest), the statement manifest_filenumber = next_file; could potentially lead to a scenario where the identifier of the manifest file matches that of the log file for the memtable. The chronological sequence of events that might give rise to this situation is outlined as follows:

Memtable Writing: The memtable is actively being written to (with the log file at ID 4 and next_file_num set to 5), while the immutable memtable (imm) is empty.

Compaction Occurs: A compaction process consolidates SST files, and within the edit log, next_filenumber is recorded as 5.

Memtable Switch: The memtable becomes full, triggering a switch to a new log file (now at ID 5, with next_file_num advanced to 6), and the filled memtable is moved to the imm state.

Database Restart: The database undergoes a restart.

Recovery Process: During VersionSet::Recover(), the edit log with next_filenumber as 5 is read, prompting the creation and redoing of the manifest file using the ID 5.

Overlap Identified: Consequently, both the manifest file and the log file now share the same identifier, which is 5.

While this overlap in identifiers does not impede the functional operation of the database, I seek confirmation on whether this particular sequence of events is plausible. Thank you for your attention to this matter.