rocksdb icon indicating copy to clipboard operation
rocksdb copied to clipboard

Pessimistic transactions can commit lost updates when upgrading an unvalidated shared lock

Open dvasquezlopez opened this issue 1 month ago • 1 comments

A pessimistic transaction can silently overwrite another transaction's committed update, the lost-update anomaly that snapshot validation exists to prevent. SetSnapshot() guarantees that "any keys successfully written ... have not been modified outside of this transaction since the time the snapshot was set", and Put() reports such a write-write conflict as Busy. If the transaction first share-locks the key with GetForUpdate(..., /*exclusive=*/false, /*do_validate=*/false), Put() returns OK instead and the commit replaces the newer value; with an exclusive GetForUpdate, the same sequence returns Busy.

The conflict is detected and then discarded. Put() upgrades the shared lock to exclusive, and because the earlier lock skipped validation, TryLock() validates against the snapshot now, which returns Busy. The failure branch downgrades the lock back to shared but assigns the downgrade's OK to s, so TryLock() tracks the key as exclusive and returns OK. The transaction then writes a key it holds only in shared mode, which other transactions can still share-lock. The assignment became reachable in #7013, which moved the downgrade out of an if (!previously_locked) block that an upgrade never enters.

Specula found this at RocksDB main commit a8a626ba8b4959af988ed3205afacc0583c037d5. The repro below was verified on Ubuntu 22.04 with GCC 11.4 against v11.12 and against main at 302eaa8b1f5b04ee56c9d79236c514fcd9f413fc:

REPRO_DIR=$(mktemp -d /tmp/rocksdb-txn.XXXXXX)
git clone --depth 1 --branch v11.12 https://github.com/meta/rocksdb.git "$REPRO_DIR/rocksdb" &&
make -C "$REPRO_DIR/rocksdb" -j"$(nproc)" DISABLE_WARNING_AS_ERROR=1 static_lib &&
cat > "$REPRO_DIR/repro.cc" <<'CC' &&
// A validated Put after a shared GetForUpdate(do_validate=false) commits over a
// write made after the transaction's snapshot. Every other row returns Busy or
// has no conflicting write.
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>

#include "rocksdb/utilities/transaction_db.h"
#include "rocksdb/version.h"

using namespace rocksdb;

static void Check(const Status& s, const char* what) {
  if (!s.ok()) {
    std::cerr << what << ": " << s.ToString() << std::endl;
    std::exit(1);
  }
}

static std::string Name(const Status& s) {
  return s.ok() ? "OK" : s.IsBusy() ? "Busy" : s.ToString();
}

static std::string LockMode(TransactionDB* db, const std::string& key) {
  for (const auto& [cf_id, info] : db->GetLockStatusData()) {
    if (info.key == key) {
      return info.exclusive ? "exclusive" : "shared";
    }
  }
  return "none";
}

enum class Read { kNone, kExclusive, kShared };

static void Run(TransactionDB* db, const std::string& key, Read read,
                bool write_after_snapshot, const char* label) {
  Check(db->Put(WriteOptions(), key, "v0"), "Put v0");
  Transaction* txn = db->BeginTransaction(WriteOptions());
  txn->SetSnapshot();
  if (write_after_snapshot) {
    Check(db->Put(WriteOptions(), key, "v_other"), "Put v_other");
  }
  if (read != Read::kNone) {
    std::string value;
    Check(txn->GetForUpdate(ReadOptions(), key, &value,
                            /*exclusive=*/read == Read::kExclusive,
                            /*do_validate=*/false),
          "GetForUpdate");
  }
  Status put = txn->Put(key, "v_txn");
  std::string lock = LockMode(db, key);
  Check(put.ok() ? txn->Commit() : txn->Rollback(), "Commit or Rollback");
  delete txn;
  std::string value;
  Check(db->Get(ReadOptions(), key, &value), "Get");
  std::cout << std::left << std::setw(46) << label << "Put=" << std::setw(6)
            << Name(put) << "lock=" << std::setw(11) << lock
            << "value=" << value << std::endl;
}

int main(int argc, char** argv) {
  if (argc != 2) {
    std::cerr << "usage: " << argv[0] << " <new db dir>" << std::endl;
    return 2;
  }
  Options options;
  options.create_if_missing = true;
  TransactionDB* db = nullptr;
  Check(TransactionDB::Open(options, TransactionDBOptions(), argv[1], &db),
        "Open");
  std::cout << "RocksDB " << ROCKSDB_MAJOR << "." << ROCKSDB_MINOR << "."
            << ROCKSDB_PATCH << std::endl;
  Run(db, "a", Read::kNone, true, "no GetForUpdate,        write after snapshot");
  Run(db, "b", Read::kExclusive, true,
      "exclusive GetForUpdate, write after snapshot");
  Run(db, "c", Read::kShared, false, "shared GetForUpdate,    no other write");
  Run(db, "d", Read::kShared, true, "shared GetForUpdate,    write after snapshot");
  delete db;
  return 0;
}
CC
g++ -std=c++20 -O2 "$REPRO_DIR/repro.cc" -I"$REPRO_DIR/rocksdb/include" \
  "$REPRO_DIR/rocksdb/librocksdb.a" \
  $(sed -n 's/^PLATFORM_LDFLAGS=//p' "$REPRO_DIR/rocksdb/make_config.mk") \
  -o "$REPRO_DIR/repro" &&
"$REPRO_DIR/repro" "$REPRO_DIR/db"

Observed output. The last line should read Put=Busy lock=shared value=v_other:

RocksDB 11.12.0
no GetForUpdate,        write after snapshot  Put=Busy  lock=none       value=v_other
exclusive GetForUpdate, write after snapshot  Put=Busy  lock=exclusive  value=v_other
shared GetForUpdate,    no other write        Put=OK    lock=exclusive  value=v_txn
shared GetForUpdate,    write after snapshot  Put=OK    lock=shared     value=v_txn

dvasquezlopez avatar Oct 29 '26 00:10 dvasquezlopez

Thanks for the detailed report. I reproduced it with a new TransactionTest case (a snapshot is set, the key is modified after it, then GetForUpdate(exclusive=false, do_validate=false) followed by Put() returns OK instead of Busy), and the cause is as described: in PessimisticTransaction::TryLock() the downgrade's OK overwrites the validation failure in s.

I have a fix ready that keeps the downgrade status in its own variable so the Busy is returned, and I am running the transaction test suites before opening a PR. If someone is already working on this, please let me know.

therealemmanueloppong avatar Nov 01 '26 15:11 therealemmanueloppong