Skip to content

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

Open
@selfboot

Description

@selfboot

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions