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

Atomic commit write batches in the command EXEC #1281

Closed
2 tasks done
git-hulk opened this issue Feb 27, 2023 · 1 comment · Fixed by #1287
Closed
2 tasks done

Atomic commit write batches in the command EXEC #1281

git-hulk opened this issue Feb 27, 2023 · 1 comment · Fixed by #1287
Assignees
Labels
enhancement type enhancement

Comments

@git-hulk
Copy link
Member

git-hulk commented Feb 27, 2023

Search before asking

  • I had searched in the issues and found no similar issues.

Motivation

As mentioned in #487, the current transaction is NOT an atomic operation, some operations may be committed to DB but some are not if they crashed in the middle way of executing the command EXEC.

To fix this issue, we need to gather all write operations into one RocksDB's WriteBatch, then commit it at once.

Solution

We can add a new function to create a shared WriteBatch for the Multi-Exec command like the below:

Status Storage::Begin() {
    is_txn_mode = true;
    txn_write_batch = make_shared<*rocksdb::WriteBatch>();
}

rocksdb::WriteBatch *Storage::GetWriteBatch() {
   if (is_txn_mode) {
      return txn_write_batch.get();
   }
   return make_shared<*rocksdb::WriteBatch>();
}

Status Storage::Commit() {
    is_txn_mode = false;
    // write txn WriteBatch to RocksDB
    txn_write_batch.reset();
}

For the Command Exec, we need to explicitly call the Begin() and Commit() to enter and leave the transaction mode. So that Kvrocks will create a new WriteBatch for each writes operation if it's NOT in transaction mode, and use the shared WriteBatch to collect all write operations if it's in the transaction mode.

Are you willing to submit a PR?

  • I'm willing to submit a PR!
@aleksraiden
Copy link
Contributor

Very important to me, waiting for this in upstream. Lot of thanks!

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

Successfully merging a pull request may close this issue.

2 participants