rocksdb icon indicating copy to clipboard operation
rocksdb copied to clipboard

max_num_locks miscounts shared locks

Open dvasquezlopez opened this issue 1 month ago • 3 comments

This looks like a refcount bug. AcquireLocked() counts a shared key once, but UnLockKey() subtracts one per holder, so a key still held by others stops being counted. Transactions can then lock more keys than max_num_locks allows, and debug builds hit the assert in UnLockKey(). The tests only use max_num_locks with exclusive locks, so maybe nobody has hit this, or shared locks were never meant to count toward the limit.

Found by Specula at main commit a8a626ba8b4959af988ed3205afacc0583c037d5. The repro below shows it on Ubuntu 22.04 with GCC 11.4, against both v11.12 and main at 0adcea5ea49c113650f2a481e8a9f76616533600:

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' &&
// With max_num_locks=1, t3 cannot lock "b" while t1 and t2 share-lock "a",
// but it can once t1 releases "a", even though t2 still holds it.
#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.IsLockLimit() ? "LockLimit" : s.ToString();
}

static void Row(TransactionDB* db, const char* label, const Status& s) {
  std::cout << std::left << std::setw(37) << label << "t3 locks \"b\"="
            << std::setw(11) << Name(s)
            << "locked keys=" << db->GetLockStatusData().size() << 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;
  TransactionDBOptions txn_db_options;
  txn_db_options.max_num_locks = 1;
  TransactionDB* db = nullptr;
  Check(TransactionDB::Open(options, txn_db_options, argv[1], &db), "Open");
  Check(db->Put(WriteOptions(), "a", "v"), "Put a");
  Check(db->Put(WriteOptions(), "b", "v"), "Put b");
  std::cout << "RocksDB " << ROCKSDB_MAJOR << "." << ROCKSDB_MINOR << "."
            << ROCKSDB_PATCH << ", max_num_locks=1" << std::endl;

  Transaction* t1 = db->BeginTransaction(WriteOptions());
  Transaction* t2 = db->BeginTransaction(WriteOptions());
  Transaction* t3 = db->BeginTransaction(WriteOptions());
  std::string value;
  Check(t1->GetForUpdate(ReadOptions(), "a", &value, /*exclusive=*/false),
        "t1 GetForUpdate");
  Check(t2->GetForUpdate(ReadOptions(), "a", &value, /*exclusive=*/false),
        "t2 GetForUpdate");
  Row(db, "t1 and t2 share-lock \"a\"", t3->GetForUpdate(ReadOptions(), "b", &value));
  Check(t1->Rollback(), "t1 Rollback");
  Row(db, "t1 releases \"a\", t2 still holds it", t3->GetForUpdate(ReadOptions(), "b", &value));

  Check(t2->Rollback(), "t2 Rollback");
  Check(t3->Rollback(), "t3 Rollback");
  delete t3;
  delete t2;
  delete t1;
  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. If shared locks count toward the limit, the second line should read t3 locks "b"=LockLimit locked keys=1:

RocksDB 11.12.0, max_num_locks=1
t1 and t2 share-lock "a"             t3 locks "b"=LockLimit  locked keys=1
t1 releases "a", t2 still holds it   t3 locks "b"=OK         locked keys=2

dvasquezlopez avatar Oct 29 '26 02:10 dvasquezlopez

Thanks for the clear report. I reproduced the mismatch by reading the code, and I would like to work on a fix.

On the open question: TransactionDBOptions::max_num_locks is documented as "the maximum number of keys that can be locked at the same time", so counting a shared key once is the intended behaviour. PerKeyPointLockManager already follows it, since LockMapStripe::ReleaseLastLockHolder only decrements locked_key_cnt when the last holder releases the key. Only PointLockManager::UnLockKey decrements for every holder that leaves.

My plan is to move that decrement into the branch where the key entry is erased, and to add a test next to LockLimitTest where two transactions share a key under max_num_locks = 1. If anyone is already working on this, or you would prefer a different approach, please say so and I will stop or adjust.

therealemmanueloppong avatar Nov 01 '26 14:11 therealemmanueloppong

#15522 has had this same change open since October 30: the decrement moved into the branch that erases the key entry, with tests in point_lock_manager_test (including a max_num_locks = 2 case where three shared holders release) and in transaction_test, plus a release note. Happy to fold in anything from #15539 that is missing.

JonSnow1807 avatar Nov 01 '26 22:11 JonSnow1807

Thanks for the note, and sorry for the duplicate work. I looked at both side by side: you were first, and #15522 covers everything mine does (the decrement moved into the erase branch, the max_num_locks cases in point_lock_manager_test, the rollback scenario in transaction_test, and the release note). There is nothing in #15539 to fold in.

I'll close #15539 so reviewers have one place to look, and I'll leave #15513 to #15522. Good catch on the bug, by the way.

therealemmanueloppong avatar Nov 01 '26 22:11 therealemmanueloppong