Skip to content

lock, workspace: add shared RLock to Locker and guard EnsureSnapshotTag against DeleteWorkspace - #617

Merged
YoyinZyc merged 1 commit into
google:mainfrom
YoyinZyc:feat/ws5-locker-rlock
Nov 16, 2026
Merged

YoyinZyc merged 1 commit into
google:mainfrom
YoyinZyc:feat/ws5-locker-rlock

Conversation

@YoyinZyc

@YoyinZyc YoyinZyc commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Follow up of #592 (comment)
Part of #584

Summary

Adds shared read-lock (RLock) support to lock.Locker (MemoryLocker and PostgreSQL locks leases) and coordinates Workspace lifecycle operations using a strict two-level lock hierarchy ("workspace" $\to$ "workspace-tag", deadlock-free by construction):

Lock Kind Key Granularity Type Exclusive Lock Shared RLock Responsibility
"workspace" atespace / wsName RWLock CreateWorkspace, DeleteWorkspace External lazy EnsureSnapshotTag Guards Workspace lifecycle: mutations/deletions are exclusive, while multiple custom-Profile snapshot builds share read access and block deletion until finished.
"workspace-tag" atespace / tagName Mutex (Lock only) Cold-miss Tag builder (none — warm hits are lock-free) Coalesces concurrent cold misses for the exact same (wsUID, templateName) onto a single setup actor.

Concurrency Scenarios Covered

  1. Concurrent CreateWorkspace("ws1") calls: Serialized by exclusive Lock("workspace", ws1). The first call runs Prepare (skipping redundant RLock("workspace") to avoid self-deadlock) and marks Ready; the second observes Ready with an identical spec and returns idempotently.
  2. CreateTask / lazy EnsureSnapshotTag while CreateWorkspace("ws1") is still preparing: Rejected immediately by CreateTask (Phase == Preparing), and any direct EnsureSnapshotTag call blocks on RLock("workspace", ws1) behind CreateWorkspace's exclusive Lock.
  3. DeleteWorkspace("ws1") while CreateWorkspace("ws1") is still preparing: DeleteWorkspace blocks on Lock("workspace", ws1) until CreateWorkspace finishes and releases, then sweeps all Tags, setup actors, and the DB record.
  4. Concurrent warm-hit CreateTask calls (default or already-cached Profile): Step 2 (GetTag == COMPLETED) returns immediately without acquiring either lock (zero DB lock contention or writer starvation).
  5. Concurrent cold-miss CreateTask calls with the same custom profA: All acquire shared RLock("workspace", ws1) concurrently, then serialize on Lock("workspace-tag", tagA). Only the first holder spawns a setup actor; the remaining callers double-check GetTag(tagA) and reuse the completed Tag.
  6. Concurrent cold-miss CreateTask calls with different custom profA and profB: Both acquire shared RLock("workspace", ws1) simultaneously and lock distinct keys Lock("workspace-tag", tagA) / Lock("workspace-tag", tagB), building both snapshots in parallel.
  7. DeleteWorkspace("ws1") arrives mid-flight during lazy EnsureSnapshotTag builds: DeleteWorkspace's exclusive Lock("workspace", ws1) waits until all active RLock("workspace", ws1) holders finish creating their Tags and cleaning up setup actors, then sweeps every Tag under wsUID with zero orphans.
  8. Late EnsureSnapshotTag arrives after DeleteWorkspace("ws1") (or after same-name recreation with a new UID): Once EnsureSnapshotTag acquires RLock("workspace", ws1), verifyLiveWorkspace re-reads the store under the shared lock, detects ErrNotFound (or a mismatched metadata.uid / non-Ready phase), and aborts with FailedPrecondition before spawning any setup actor or Tag.

@mayawang mayawang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice, the lock ordering and lease handling look right to me. One thing before merge: the locks primary key moves from (key) to (key, token), but the old tryLock does INSERT ... ON CONFLICT (key), so during a rolling upgrade every old replica fails every Lock once the first new pod migrates the table. Could we either note "stop all replicas before upgrading" in the release notes/docs, or keep a compatible constraint for one release? Smaller: worth documenting on Locker that the Postgres RLock favours readers (a delete can wait behind long builds) while MemoryLocker favours writers, and that RLock isn't re-entrant.

@mayawang

Copy link
Copy Markdown
Collaborator

To be clear, the upgrade note is fine as a follow-up, go ahead and land

…ag against DeleteWorkspace

Part of google#563

- internal/lock: add RLock to Locker and upgrade MemoryLocker to a ref-counted sync.RWMutex.
- internal/store/postgres: upgrade locks table to composite (key, token) primary key with shared ('S') and exclusive ('X') lease modes serialized via transaction-scoped pg_advisory_xact_lock during tryAcquire.
- internal/controller, internal/server, cmd/ax-server: acquire shared RLock("workspace") in external EnsureSnapshotTag cold misses (while Prepare continues under CreateWorkspace's exclusive Lock) and re-verify stored Workspace existence, UID, and Ready phase under RLock so lazy profile builds run in parallel without racing DeleteWorkspace.
@YoyinZyc
YoyinZyc force-pushed the feat/ws5-locker-rlock branch from 1c9b4dd to 97b6237 Compare November 16, 2026 23:30
@YoyinZyc

Copy link
Copy Markdown
Collaborator Author

Quickly added/updated the notes and docs

@YoyinZyc
YoyinZyc merged commit 167ee0b into google:main Nov 16, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants