git-hulk opened a new issue, #1281:
URL: https://github.com/apache/incubator-kvrocks/issues/1281

   ### Search before asking
   
   - [X] I had searched in the 
[issues](https://github.com/apache/incubator-kvrocks/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:
   
   ```C++
   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.
   
   To be noticed: it only fixes this issue partially since it cannot 
read-it-own-writes in this transaction. To make the context clear, I will 
submit another issue to describe this solution.
   
   ### Are you willing to submit a PR?
   
   - [X] I'm willing to submit a PR!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to