Versioned before code: cmd/gc/city_runtime.go at b78058917 . These selected excerpts are abridged for reading; every omission is marked in the source.
How to read this file
Follow the execution boundary
Click any line section to open its explanation. The complete file stays visible, and the selected explanation opens directly beneath the code it describes.
// Abridged from cmd/gc/city_runtime.go for reading; not the exact source.
// Tracing, logging, and guards are removed, and every omission is marked.
// Identifiers match the original so the linked source stays navigable.
// Source revision: b78058917bc65846db89e1c3b25dc17269822483.
package main
func (cr *CityRuntime) beadReconcileTick(
ctx context.Context,
result DesiredStateResult,
sessionBeads *sessionBeadSnapshot,
trace *sessionReconcilerTraceCycle,
bootReconcile bool,
) {
desiredState := result.State
store := cr.cityBeadStore()
if store == nil {
return
}
sessStore := cr.sessionsBeadStore()Lines 1–20
Enter through one process-owned tick
- What this section does
- Identifies the exact upstream revision and enters the controller method that owned this pass through the current snapshots.
- Why it matters for Temporal
- If this process stopped, the call stack and its current position disappeared. The next process could only run another tick against persisted facts.
- Process-local procedure
- Versioned source
- Snapshot input
// Omitted: the recordPhase trace helper, called after each phase below.
if sessionBeads == nil {
var sessionQueryPartial bool
sessionBeads, sessionQueryPartial = cr.loadSessionBeadSnapshotWithPartial()
result.SessionQueryPartial =
result.SessionQueryPartial || sessionQueryPartial
}
rigStores := cr.rigBeadStores()
assignedWorkBeads := result.AssignedWorkBeads
assignedWorkStoreRefs := result.AssignedWorkStoreRefs
released := releaseOrphanedPoolAssignmentsWhenSnapshotsComplete(
store,
cr.cfg,
cr.cityPath,
sessionBeads.OpenInfos(),
result,
rigStores,
)
if len(released) > 0 {
emitDeadAssigneeReopenedEvents(
cr.rec,
assignedWorkBeads,
released,
time.Now(),
)
assignedWorkBeads, assignedWorkStoreRefs =
filterReleasedAssignedWorkSnapshot(
assignedWorkBeads,
assignedWorkStoreRefs,
released,
)
}Lines 21–56
Repair what the previous owner left behind
- What this section does
- Reloads session state, searches for assignments whose session no longer owns them, reopens those records, and filters the current snapshot.
- Why it matters for Temporal
- The repair is inferential. It observes the resulting records after a failure rather than replaying the command that was in progress.
- Snapshot repair
- Orphan scan
- Reopen event
// Omitted: the store-identity "squatter" hold and the undesired-pool-session
// sweep, which can reload sessionBeads before the reconcile below.
openInfos := sessionBeads.OpenInfos()
cityName := cr.cityName
cfgNames := configuredSessionNamesWithSnapshot(cr.cfg, cityName, sessionBeads)
// poolDesired is how many sessions should be awake. Its full computation
// (pool-demand filter, partial retain, named-demand merge) is omitted.
poolDesired := result.PoolDesiredCounts
readyWaitSet, err := prepareWaitWakeStateWithSnapshot(
sessionpkg.NewStore(sessStore),
newWaitDependencyStoreSet(store, rigStores),
cr.nudgesBeadStore(),
time.Now(),
sessionBeads,
)
if err != nil {
readyWaitSet = nil // the original logs the error here
}
// Controller wake demand comes from assigned-work scans and scale_check, so
// the per-template work_query set stays empty on this path.
workSet := make(map[string]bool)
awakeAssignedWorkBeads, awakeAssignedStoreRefs :=
filterAssignedWorkBeadsForSessionWake(
cr.cfg,
cr.cityPath,
openInfos,
assignedWorkBeads,
assignedWorkStoreRefs,
)
reconcileStartOptions := []startExecutionOption{
withAsyncStartExecution(),
withAsyncStartFollowUp(cr.requestAsyncStartFollowUpTick),
withAsyncStartLimiter(cr.ensureAsyncStartLimiter()),
withAsyncStartTracker(&cr.asyncStarts),
withAsyncDrainAckStopTracker(&cr.asyncStops),
withMaxSessionAgeTracker(cr.mat),
withReadyAssignedFlags(readyAssignedFlagsForBeads(
result.ReadyAssigned,
awakeAssignedWorkBeads,
awakeAssignedStoreRefs,
)),
}
if bootReconcile {
reconcileStartOptions = append(
reconcileStartOptions,
withDeferSessionClosesOnBoot(),
)
}
reconcileSessionBeadsTracedWithNamedDemand(
ctx,
cr.cityPath,
sessionBeads.OpenForReconcile(),
sessionBeads,
desiredState,
cfgNames,
cr.cfg,
cr.sp,
sessStore,
cr.dops,
awakeAssignedWorkBeads,
rigStores,
readyWaitSet,
cr.sessionDrains,
cr.providerHealthGate,
poolDesired,
result.NamedSessionDemand,
result.snapshotQueryPartial(),
workSet,
cityName,
cr.it,
clock.Real{},
cr.rec,
cr.cfg.Session.StartupTimeoutDuration(),
cr.cfg.Daemon.DriftDrainTimeoutDuration(),
cr.stdout,
cr.stderr,
trace,
reconcileStartOptions...,
)Lines 57–142
Reconcile desired sessions with current sessions
- What this section does
- Selects assigned work that should wake a session, then passes a large state bundle into the session reconciler.
- Why it matters for Temporal
- The procedure crosses work-store, runtime, timing, and provider boundaries inside one controller pass. None of those calls were a durable step.
- Session reconciliation
- Desired state
- Runtime boundary
// Omitted: deferred drain follow-up and post-tick trace recording.
dispatchSessionBeads, err := loadSessionBeadSnapshot(sessStore.Store)
if err == nil {
_ = dispatchReadyWaitNudgesWithSnapshot(
cr.cityPath,
cr.cfg,
sessionpkg.NewStore(sessStore),
cr.nudgesBeadStore(),
time.Now(),
dispatchSessionBeads,
)
}
// Patrol fallback for a wake-socket enqueue lost during a process race.
cr.nudgeDispatchTick(ctx)Lines 143–159
Deliver the wake-up after reconciliation
- What this section does
- Reloads session state, sends ready-wait nudges, and runs a patrol fallback in case the wake socket lost an enqueue.
- Why it matters for Temporal
- The fallback addresses one delivery gap, but it still reconstructs intent from queue and session state after the fact.
- Nudge delivery
- Patrol fallback
- Second snapshot
// A separate backstop wakes live sessions that never claimed their bead.
if stalledPoolBeads, err := loadSessionBeads(sessStore.Store); err == nil {
nudgeStalledPoolClaims(
cr.sp,
cr.cfg,
sessStore,
stalledPoolBeads,
assignedWorkBeads,
time.Now(),
cr.stdout,
)
}
}Lines 160–173
Add another recovery lane for idle sessions
- What this section does
- Runs a separate backstop for a live session that received work but never claimed its trigger record.
- Why it matters for Temporal
- This is the accumulated cost of missing durable procedure state: each ambiguous boundary needs its own detector and retry rule.
- Idle-claim detector
- Re-nudge
- Repair loop