You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by "mapleFU (via GitHub)" <gi...@apache.org> on 2023/02/28 16:30:33 UTC

[GitHub] [incubator-kvrocks] mapleFU commented on a diff in pull request #1287: Use the RocksDB WriteBatchWithIndex to implement the read-your-own-writes in transaction

mapleFU commented on code in PR #1287:
URL: https://github.com/apache/incubator-kvrocks/pull/1287#discussion_r1120328375


##########
src/storage/storage.cc:
##########
@@ -639,12 +677,33 @@ void Storage::SetIORateLimit(int64_t max_io_mb) {
 
 rocksdb::DB *Storage::GetDB() { return db_; }
 
-Status Storage::WriteToPropagateCF(const std::string &key, const std::string &value) {
-  rocksdb::WriteBatch batch;
+void Storage::BeginTxn() {
+  is_txn_mode_ = true;
+  txn_write_batch_ = std::make_unique<rocksdb::WriteBatchWithIndex>();
+}
+
+Status Storage::CommitTxn() {
+  is_txn_mode_ = false;
+  auto s = Write(write_opts_, txn_write_batch_->GetWriteBatch());
+  txn_write_batch_.reset();
+  if (s.ok()) {
+    return {Status::cOK};
+  }
+  return {Status::NotOK, s.ToString()};
+}
+
+std::shared_ptr<rocksdb::WriteBatchBase> Storage::GetWriteBatch() {

Review Comment:
   Should we name it `GetWriteBatchBase` here? And why we use shared_ptr rather than raw pointer here?



##########
src/commands/cmd_txn.cc:
##########
@@ -68,13 +69,16 @@ class CommandExec : public Commander {
       return Status::OK();
     }
 
+    auto storage = svr->storage_;
     // Reply multi length first
     conn->Reply(Redis::MultiLen(conn->GetMultiExecCommands()->size()));
     // Execute multi-exec commands
     conn->SetInExec();
+    storage->BeginTxn();
     conn->ExecuteCommands(conn->GetMultiExecCommands());
+    auto s = storage->CommitTxn();

Review Comment:
   Just a naive question. If a exception is thrown in `conn->ExecuteCommands` (I didn't review carefully, so didn't know would it throw). The transaction will never recover until another txn begin and do the thing



##########
src/storage/storage.cc:
##########
@@ -639,12 +677,33 @@ void Storage::SetIORateLimit(int64_t max_io_mb) {
 
 rocksdb::DB *Storage::GetDB() { return db_; }
 
-Status Storage::WriteToPropagateCF(const std::string &key, const std::string &value) {
-  rocksdb::WriteBatch batch;
+void Storage::BeginTxn() {
+  is_txn_mode_ = true;
+  txn_write_batch_ = std::make_unique<rocksdb::WriteBatchWithIndex>();

Review Comment:
   Does this txn has consistent read snapshot?



-- 
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: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org