Skip to content

cephsqlite: support concurrent readers - #69199

Merged
batrick merged 11 commits into
ceph:mainfrom
batrick:i49958
Nov 6, 2026
Merged

batrick merged 11 commits into
ceph:mainfrom
batrick:i49958

Conversation

@batrick

@batrick batrick commented May 31, 2026 •

Copy link
Copy Markdown
Member

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

@github-actions github-actions Bot added libcephsqlite ceph SQLite3 VFS tests labels May 31, 2026
@batrick
batrick requested review from a team as code owners May 31, 2026 23:36
@batrick batrick changed the title SimpleRADOSStriper: fix AIO lifecycle and thread shutdown race in Sim… cephsqlite: support concurrent readers May 31, 2026
@batrick
batrick force-pushed the i49958 branch 4 times, most recently from a81176d to c154540 Compare June 5, 2026 14:01
@batrick

This comment was marked as outdated.

@batrick

This comment was marked as outdated.

@batrick
batrick requested a review from a team as a code owner June 6, 2026 01:49
@batrick
batrick force-pushed the i49958 branch 7 times, most recently from 4c73405 to 126f7ba Compare June 9, 2026 12:18
@batrick

batrick commented Jun 9, 2026

Copy link
Copy Markdown
Member Author

This PR has been added to main integration testing by pdonnell started 2026-06-09-12:34.

@batrick
batrick force-pushed the i49958 branch 3 times, most recently from d37d833 to 7d0218b Compare June 15, 2026 20:28
@batrick

batrick commented Jul 12, 2026

Copy link
Copy Markdown
Member Author

This PR has been added to main integration testing by pdonnell started 2026-07-12-17:08.

@batrick
batrick force-pushed the i49958 branch 2 times, most recently from f5a6e08 to 7b95725 Compare November 1, 2026 14:37
@batrick
batrick marked this pull request as draft November 2, 2026 00:35
@batrick

batrick commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

Making some more adjustments but I think this is getting closer.

@batrick
batrick marked this pull request as ready for review November 3, 2026 01:36

@NitzanMordhai NitzanMordhai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@batrick, please see my comments

Comment thread src/SimpleRADOSStriper.cc Outdated
d(10) << "escalating to Exclusive" << dendl;
/* allow Exclusive lock if Reserved <= lock state <= Pending */
rados::cls::lock::lock(&op, primary, ClsLockType::EXCLUSIVE, cookie.to_string(), "", lockdesc, duration, flags);
op.cmpxattr(XATTR_STATE, LIBRADOS_CMPXATTR_OP_LTE, state2bl(LockLevel::Reserved));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going from Shared to Exclusive now always fails. The state on disk is Shared, but this check requires at least Reserved, so we get EBUSY every time

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're looking at an out-of-date diff somehow.

Comment thread src/SimpleRADOSStriper.cc
Comment on lines +369 to +377
op.getxattr(XATTR_ALLOCATED, &bl_alloc, &prval_alloc);
op.getxattr(XATTR_EXCL, &bl_excl, &prval_excl);
op.getxattr(XATTR_LAYOUT_OBJECT_SIZE, &bl_layout_os, &prval_layout_os);
op.getxattr(XATTR_LAYOUT_STRIPE_COUNT, &bl_layout_sc, &prval_layout_sc);
op.getxattr(XATTR_LAYOUT_STRIPE_UNIT, &bl_layout_su, &prval_layout_su);
op.getxattr(XATTR_SIZE, &bl_size, &prval_size);
op.getxattr(XATTR_ALLOCATED, &bl_alloc, &prval_alloc);
op.getxattr(XATTR_STATE, &bl_state, &prval_state);
op.getxattr(XATTR_VERSION, &bl_version, &prval_version);
op.getxattr(XATTR_WRITE_EPOCH, &bl_epoch, &prval_epoch);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think a failed getxattr is contained to its prval. In PrimaryLogPG::do_osd_ops() a getxattr on a missing xattr sets result = -ENODATA, and unless the op has FAILOK the loop breaks and the whole read returns -ENODATA, so operate() fails before any of the prval checks run. create() sets OP_FAILOK on the same reads for this reason.

It matters for upgrades: databases created before this PR don't have striper.state or striper.write_epoch, and since open() is just get_metadata(), I think opening any existing mgr database would fail with SQLITE_IOERR.

Comment thread src/SimpleRADOSStriper.cc Outdated
d(10) << "escalating to Pending" << dendl;
/* allow Pending lock if lock state == Reserved */
rados::cls::lock::lock(&op, primary, ClsLockType::SHARED, cookie.to_string(), "", lockdesc, duration, flags);
op.cmpxattr(XATTR_STATE, LIBRADOS_CMPXATTR_OP_LTE, state2bl(LockLevel::Reserved));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changed from OP_EQ to OP_LTE, but with " OP " that now means
ondisk >= Reserved, while the comment above still says "== Reserved".
That breaks the new Pending fallback for Shared -> Exclusive, which SQLite uses for hot-journal rollback (your HotJournalRollbackSharedToExclusive test).
When the Exclusive attempt gets EBUSY we switch to Pending, but the on-disk state is still Shared, so this cmpxattr fails with ECANCELED. That goes through recover_lock(), which finds striper.excl empty and returns EBUSY, and we never reach Pending. New readers are never held off, and each retry costs extra round trips.

Should this be OP_GTE (Reserved >= ondisk)?
The striper.excl check already makes sure nobody else holds Reserved. The test only checks the Reserved -> Exclusive path, so a two-client Shared -> Exclusive case would catch it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm cleaning this up more but it's not valid to stop in Reserved/Pending from Shared. So this doesn't matter in practice.

Comment thread src/pybind/mgr/mgr_module.py Outdated
db.execute('PRAGMA PAGE_SIZE = 65536')
db.execute('PRAGMA CACHE_SIZE = 64')
db.execute('PRAGMA TEMP_STORE = memory')
db.execute('PRAGMA busy_timeout = 60000') # 60s, longer than lock_keeper_timeout

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two small things. This runs after the earlier PRAGMAs and after the ceph_status() query in open_db(), so those still get Python's default 5s. Passing timeout= to sqlite3.connect() in open_db() would cover the whole connection. Also, the 60s is tied to the current default of cephsqlite_lock_renewal_timeout.
Could it be derived from that option instead, so raising the lease doesn't bring the failure back?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it to come from the option.

@batrick
batrick force-pushed the i49958 branch 2 times, most recently from 7d8563c to 18f8754 Compare November 4, 2026 17:24
@batrick

batrick commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

Latest run passes: https://tracker.ceph.com/issues/82165

Will add a full orch/rados/upgrade too.

@NitzanMordhai NitzanMordhai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@batrick, just 1 change is needed, but everything other than that looks good.

self.log.debug(f"using uri {uri}")
db = sqlite3.connect(uri, check_same_thread=False, uri=True, isolation_level=None) # type: ignore[call-arg]
busy_timeout = 60.0
lock_renewal_timeout = self.get_ceph_option('cephsqlite_lock_renewal_timeout')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cephsqlite_lock_renewal_timeout is in milliseconds, and sqlite3.connect timeout is in seconds. maybe / 1000 ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, thanks and fixed!

This patch addresses two issues in the teardown phase:

1. `wait_for_aios(true)` is explicitly invoked to prevent in-flight AIO
   completions from being destroyed when `aios` goes out of scope,
   preventing memory corruption/segmentation faults.
2. The `shutdown` flag is now protected by the `lock_keeper_mutex` when
   toggled, preventing a race condition where the thread misses the
   notification and blocks indefinitely on thread join.

Assisted-by: Gemini
Signed-off-by: Patrick Donnelly <[email protected]>
Improves debug observability by logging the retrieved file size during
`stat` operations.

Assisted-by: Gemini
Signed-off-by: Patrick Donnelly <[email protected]>
Appends the connection cookie to the `dout_prefix` output. This helps
operators and developers easily differentiate between different database
handles in the logs.

Assisted-by: Gemini
Signed-off-by: Patrick Donnelly <[email protected]>
Instead of using an exclusive lock for all database access, allow shared
reader locks to improve read-heavy workload throughput. This patch maps
SQLite VFS locking states to Ceph RADOS advisory locks using a single
primary lock object (`striper.lock`) paired with extended attributes
(`striper.state` and `striper.excl`) to safely facilitate atomic lock
escalation without deadlocks:

- SQLITE_LOCK_SHARED (1): Acquires a SHARED lock on the primary object and
  verifies `striper.state` > None.
- SQLITE_LOCK_RESERVED (2): Acquires a SHARED lock and sets `striper.state`
  to 2. Populates `striper.excl` with the client's address as a gatekeeper
  so only one client can intend to write at a time, bypassing the classic
  SQLite Upgrade Deadlock.
- SQLITE_LOCK_PENDING (3): Upgrades `striper.state` to 3, blocking new readers.
- SQLITE_LOCK_EXCLUSIVE (4): Upgrades the RADOS lock to EXCLUSIVE and sets
  `striper.state` to 4. Returns -EBUSY cleanly if other SHARED readers
  still exist, ensuring no transactional state is corrupted.

Additionally implements `xCheckReservedLock` by checking the reservation
state via `striper.state` to quickly report if any client currently holds
intent to write, satisfying SQLite's concurrency requirements.

Assisted-by: Gemini
Fixes: https://tracker.ceph.com/issues/49958
Signed-off-by: Patrick Donnelly <[email protected]>
This introduces comprehensive test coverage for the newly implemented
SQLite VFS to RADOS advisory lock state machine, verifying the atomic
escalation paths and contention handling.

Specific scenarios newly covered:
- ConcurrentReaders: Verifies multiple clients can successfully hold
  SHARED locks simultaneously without blocking.
- ReservedLockConcurrency: Ensures a RESERVED lock allows existing
  readers to continue while correctly preventing other immediate transactions.
- ExclusiveLockConcurrency: Validates EXCLUSIVE locks correctly return
  -EBUSY to block incoming readers.
- LockDowngrade: Validates locks correctly step down from EXCLUSIVE
  to SHARED after a transaction commits while a read cursor is still open.
- DeadClientRecovery: Simulates a crashed writer by injecting a stale
  XATTR_EXCL and verifies the safe recovery and lock-stealing logic.
- ContentionWaitLoop: Verifies the -EBUSY backoff/retry loop correctly
  waits for locks to free up before reaching the acquire_timeout.
- LockRenewal: Confirms the background lock_keeper thread successfully
  renews both SHARED and EXCLUSIVE leases to prevent premature expiration.

Assisted-by: Gemini
Signed-off-by: Patrick Donnelly <[email protected]>
These are not part of any public ABI/API.

Signed-off-by: Patrick Donnelly <[email protected]>
Which is apparently required by the sqlite3 busy_timeout. Previously,
this was implemented (incorrectly) within the VFS SimpleRADOSStriper
itself.

Signed-off-by: Patrick Donnelly <[email protected]>
The wait is no longer done by the VFS layer (in general without a lock
timeout pragma).

Signed-off-by: Patrick Donnelly <[email protected]>
Now that libcephsqlite implements xSleep and supports the SQLite busy
handler, setting a 60-second (default) busy timeout allows mgr modules
to wait out transient lock contention or lock recoveries rather than
immediately aborting with SQLITE_BUSY. This duration safely exceeds
SimpleRADOSStriper's 30-second lock_keeper_timeout.

Signed-off-by: Patrick Donnelly <[email protected]>
@batrick
batrick merged commit 1cbffd3 into ceph:main Nov 6, 2026
15 checks passed
@batrick
batrick deleted the i49958 branch November 6, 2026 00:27
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

This is an automated message by src/script/redmine-upkeep.py.

I found one or more Fixes: tags in the commit messages in

git log 1cbffd3fb160ccc2f6f7d8959d47bf2849757fc2^..1cbffd3fb160ccc2f6f7d8959d47bf2849757fc2

The referenced tickets are:

Those tickets do not reference this merged Pull Request. If this Pull Request merge resolves any of those tickets, please update the "Pull Request ID" field on each ticket. A future run of this script will appropriately update them.

Update Log: https://github.com/ceph/ceph/actions/runs/42136970262

Matan-B added a commit to ceph/ceph-ci that referenced this pull request Nov 11, 2026
ceph/ceph#69199 escalates the cephsqlite striper
lock with a write op ending in CMPXATTR, which sets rval = 1 on a true
comparison. Classic zeroes that in PrimaryLogPG::execute_ctx() for any
non-returnvec write, but crimson had the same clause in an else-if chain
behind a may_read() branch, and CMPXATTR is __CEPH_OSD_OP(RD, ...), so
the branch was never reached and the client got 1.

Move the zeroing out of the chain.

Signed-off-by: Matan Breizman <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Matan-B added a commit that referenced this pull request Nov 11, 2026
#69199 escalates the cephsqlite striper
lock with a write op ending in CMPXATTR, which sets rval = 1 on a true
comparison. Classic zeroes that in PrimaryLogPG::execute_ctx() for any
non-returnvec write, but crimson had the same clause in an else-if chain
behind a may_read() branch, and CMPXATTR is __CEPH_OSD_OP(RD, ...), so
the branch was never reached and the client got 1.

Move the zeroing out of the chain.

Signed-off-by: Matan Breizman <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants