Repository navigation
lock, workspace: add shared RLock to Locker and guard EnsureSnapshotTag against DeleteWorkspace - #617
Conversation
mayawang
left a comment
There was a problem hiding this comment.
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.
|
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.
1c9b4dd to
97b6237
Compare
|
Quickly added/updated the notes and docs |
Follow up of #592 (comment)
Part of #584
Summary
Adds shared read-lock ($\to$
RLock) support tolock.Locker(MemoryLockerand PostgreSQLlocksleases) and coordinates Workspace lifecycle operations using a strict two-level lock hierarchy ("workspace""workspace-tag", deadlock-free by construction):LockRLock"workspace"atespace / wsNameCreateWorkspace,DeleteWorkspaceEnsureSnapshotTag"workspace-tag"atespace / tagNameLockonly)(wsUID, templateName)onto a single setup actor.Concurrency Scenarios Covered
CreateWorkspace("ws1")calls: Serialized by exclusiveLock("workspace", ws1). The first call runsPrepare(skipping redundantRLock("workspace")to avoid self-deadlock) and marksReady; the second observesReadywith an identical spec and returns idempotently.CreateTask/ lazyEnsureSnapshotTagwhileCreateWorkspace("ws1")is still preparing: Rejected immediately byCreateTask(Phase == Preparing), and any directEnsureSnapshotTagcall blocks onRLock("workspace", ws1)behindCreateWorkspace's exclusiveLock.DeleteWorkspace("ws1")whileCreateWorkspace("ws1")is still preparing:DeleteWorkspaceblocks onLock("workspace", ws1)untilCreateWorkspacefinishes and releases, then sweeps all Tags, setup actors, and the DB record.CreateTaskcalls (default or already-cached Profile): Step 2 (GetTag==COMPLETED) returns immediately without acquiring either lock (zero DB lock contention or writer starvation).CreateTaskcalls with the same customprofA: All acquire sharedRLock("workspace", ws1)concurrently, then serialize onLock("workspace-tag", tagA). Only the first holder spawns a setup actor; the remaining callers double-checkGetTag(tagA)and reuse the completed Tag.CreateTaskcalls with different customprofAandprofB: Both acquire sharedRLock("workspace", ws1)simultaneously and lock distinct keysLock("workspace-tag", tagA)/Lock("workspace-tag", tagB), building both snapshots in parallel.DeleteWorkspace("ws1")arrives mid-flight during lazyEnsureSnapshotTagbuilds:DeleteWorkspace's exclusiveLock("workspace", ws1)waits until all activeRLock("workspace", ws1)holders finish creating their Tags and cleaning up setup actors, then sweeps every Tag underwsUIDwith zero orphans.EnsureSnapshotTagarrives afterDeleteWorkspace("ws1")(or after same-name recreation with a newUID): OnceEnsureSnapshotTagacquiresRLock("workspace", ws1),verifyLiveWorkspacere-reads the store under the shared lock, detectsErrNotFound(or a mismatchedmetadata.uid/ non-Readyphase), and aborts withFailedPreconditionbefore spawning any setup actor or Tag.