Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug report: Writing to wal succeeds, but memtable fails, which will lead to data inconsistency? #1244

Open
selfboot opened this issue Jan 24, 2025 · 0 comments

Comments

@selfboot
Copy link

In a write operation, if WAL is written successfully but memtable is written unsuccessfully, will this lead to data inconsistency?

If a memtable write fails, LevelDB returns an error status to the caller, indicating that the write operation was unsuccessful.

However, the WAL still retains records of these operations. When the database is restored, these records will be reapplied to the MemTable, resulting in inconsistent data.

Related code

    // Add to log and apply to memtable.  We can release the lock
    // during this phase since &w is currently responsible for logging
    // and protects against concurrent loggers and concurrent writes
    // into mem_.
    {
      mutex_.Unlock();
      status = log_->AddRecord(WriteBatchInternal::Contents(write_batch));
      bool sync_error = false;
      if (status.ok() && options.sync) {
        status = logfile_->Sync();
        if (!status.ok()) {
          sync_error = true;
        }
      }
      if (status.ok()) {
        status = WriteBatchInternal::InsertInto(write_batch, mem_);
      }
      mutex_.Lock();
      if (sync_error) {
        // The state of the log file is indeterminate: the log record we
        // just added may or may not show up when the DB is re-opened.
        // So we force the DB into a mode where all future writes fail.
        RecordBackgroundError(status);
      }
    }

Or did I misunderstand? Can anyone help explain this, thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant