|
| 1 | +// Package phasetracking accumulates cumulative time-in-phase for instance |
| 2 | +// lifecycle phases. The tracker is embedded in instance metadata and updated |
| 3 | +// at every externally-observable state transition so consumers can use the |
| 4 | +// resulting durations for billing, observability, and analytics. |
| 5 | +// |
| 6 | +// Phases mirror the externally-observable values of instances.State (lowercased |
| 7 | +// so they remain stable in the API surface even if the internal enum is |
| 8 | +// renamed). The Initializing→Running transition is detected lazily when guest |
| 9 | +// boot markers are persisted, so the tracker reflects the same view of guest |
| 10 | +// readiness that the public State machine reports — not the bare moment the |
| 11 | +// VMM process came up. |
| 12 | +// |
| 13 | +// Transient internal substates that no external observer can see — for example |
| 14 | +// the Paused/Shutdown steps inside a single Standby or Stop orchestration — are |
| 15 | +// intentionally not recorded; they would be sub-millisecond blips inside a |
| 16 | +// non-yielding function call that adds noise without truth. |
| 17 | +// |
| 18 | +// Only the transition orchestration sites in lib/instances should call Record. |
| 19 | +// The tracker intentionally does not subscribe to the lifecycle event bus — |
| 20 | +// that bus is best-effort and lossy, which is unsuitable for billing. |
| 21 | +package phasetracking |
| 22 | + |
| 23 | +import "time" |
| 24 | + |
| 25 | +// Phase is the canonical lifecycle phase name. Values mirror instances.State |
| 26 | +// lowercased so they remain stable in the API surface even if the internal |
| 27 | +// State enum is renamed. |
| 28 | +type Phase string |
| 29 | + |
| 30 | +const ( |
| 31 | + PhaseStopped Phase = "stopped" |
| 32 | + PhaseCreated Phase = "created" |
| 33 | + PhaseInitializing Phase = "initializing" |
| 34 | + PhaseRunning Phase = "running" |
| 35 | + PhaseStandby Phase = "standby" |
| 36 | +) |
| 37 | + |
| 38 | +// Tracker accumulates cumulative wall-clock time spent in each phase. |
| 39 | +// |
| 40 | +// Invariants: |
| 41 | +// - Cumulative[phase] is the total ms spent in `phase` across all prior |
| 42 | +// completed visits to that phase. |
| 43 | +// - Time spent in the *current* phase (since `Since`) is NOT yet rolled into |
| 44 | +// Cumulative — callers that want "live" totals should use Snapshot. |
| 45 | +// - Current and Since must be updated atomically with Cumulative; that's |
| 46 | +// the contract of Record. Direct mutation is not supported. |
| 47 | +// |
| 48 | +// The zero value is valid: it represents an instance that has not entered |
| 49 | +// any phase yet. The first Record call sets Current and Since without |
| 50 | +// accruing time (there is no prior phase to accrue from). |
| 51 | +type Tracker struct { |
| 52 | + Current Phase `json:"current,omitempty"` |
| 53 | + Since time.Time `json:"since,omitempty"` |
| 54 | + Cumulative map[Phase]int64 `json:"cumulative,omitempty"` |
| 55 | +} |
| 56 | + |
| 57 | +// Record transitions into newPhase as of `now`, first accruing time-in-current |
| 58 | +// into Cumulative. Safe to call on a zero-value Tracker (first transition has |
| 59 | +// no prior phase, so no accrual happens). |
| 60 | +// |
| 61 | +// `now` is a parameter rather than time.Now() so tests can pin time and so |
| 62 | +// callers can use the same `now` value they're persisting elsewhere on the |
| 63 | +// metadata (e.g. StartedAt) without drift. |
| 64 | +func (t *Tracker) Record(newPhase Phase, now time.Time) { |
| 65 | + if t.Cumulative == nil { |
| 66 | + t.Cumulative = make(map[Phase]int64) |
| 67 | + } |
| 68 | + if t.Current != "" && !t.Since.IsZero() { |
| 69 | + elapsed := now.Sub(t.Since).Milliseconds() |
| 70 | + if elapsed > 0 { |
| 71 | + t.Cumulative[t.Current] += elapsed |
| 72 | + } |
| 73 | + } |
| 74 | + t.Current = newPhase |
| 75 | + t.Since = now |
| 76 | +} |
| 77 | + |
| 78 | +// Snapshot returns a copy of Cumulative with the in-flight time-in-current |
| 79 | +// rolled in, without mutating the tracker. Use this when reporting "running |
| 80 | +// time so far" — typically in the API response path. |
| 81 | +func (t Tracker) Snapshot(now time.Time) map[Phase]int64 { |
| 82 | + out := make(map[Phase]int64, len(t.Cumulative)+1) |
| 83 | + for k, v := range t.Cumulative { |
| 84 | + out[k] = v |
| 85 | + } |
| 86 | + if t.Current != "" && !t.Since.IsZero() { |
| 87 | + elapsed := now.Sub(t.Since).Milliseconds() |
| 88 | + if elapsed > 0 { |
| 89 | + out[t.Current] += elapsed |
| 90 | + } |
| 91 | + } |
| 92 | + return out |
| 93 | +} |
| 94 | + |
| 95 | +// Reset clears all accumulated state. Used when forking — the fork is a new |
| 96 | +// instance and must not inherit the source's phase history. |
| 97 | +func (t *Tracker) Reset() { |
| 98 | + t.Current = "" |
| 99 | + t.Since = time.Time{} |
| 100 | + t.Cumulative = nil |
| 101 | +} |
| 102 | + |
| 103 | +// Clone returns a deep copy of the tracker. The returned tracker shares no |
| 104 | +// state with the receiver, so independent Record/Reset calls do not interfere. |
| 105 | +func (t Tracker) Clone() Tracker { |
| 106 | + dst := t |
| 107 | + if t.Cumulative != nil { |
| 108 | + dst.Cumulative = make(map[Phase]int64, len(t.Cumulative)) |
| 109 | + for k, v := range t.Cumulative { |
| 110 | + dst.Cumulative[k] = v |
| 111 | + } |
| 112 | + } |
| 113 | + return dst |
| 114 | +} |
0 commit comments