Documentation
¶
Overview ¶
Package clock is the source of the current time for muster's timers that measure elapsed time: the reconnect backoff of a remote MCPServer, the orchestrator's retry and health-probe ticks, the aggregator's capability poll, the age of its core catalogue and the reconciler's requeues and resync.
The clock controllers take ¶
Clock is k8s.io/utils/clock's WithTickerAndDelayedExecution: Now, Since, After, NewTimer, Sleep, Tick, NewTicker and AfterFunc. It plugs into client-go's delaying and rate-limiting work queues (their Clock option), and through them into controller-runtime's RequeueAfter, which such a queue schedules. The controllers of durable runs (giantswarm/muster#1934) take a Clock in their constructors and build their queue with it: production passes Process(), unit tests k8s.io/utils/clock/testing's FakeClock.
Process returns the process clock. In production it is the system time (k8s.io/utils/clock.RealClock) and nothing can move it. The package-level functions (Now, Since, Until, NewTicker) are the process clock's, for code that is not handed a Clock.
The controllable clock ¶
When MUSTER_TEST_CLOCK names a Unix socket -- the integration test harness sets it for every instance it runs -- the process clock is controllable and the process serves a control endpoint on that socket (StartControl). A plain scenario advance makes one jump. Each due timer fires once at the landing instant, even when the jump spans many ticks of the harness's retry, health and resync timers. To fire each interval's timers at its own instant, pass step to test_advance_clock. The runner waits for idle after each step so callbacks can re-arm. It refuses calls requiring more than 5000 landings before advancing any clock. The offset is process-wide and only ever grows, as wall time does.
Two more variables shape that clock, and act only together with MUSTER_TEST_CLOCK, so production can never move its clock:
- MUSTER_TEST_CLOCK_START (RFC 3339) freezes the clock: Now is that instant plus the offset and does not follow the system time, and timers, tickers and AfterFuncs fire only through an advance. Every time muster stores is then an exact instant a scenario can assert on.
- MUSTER_TEST_CLOCK_OFFSET (a Go duration) starts the process already advanced by it: a process the harness restarts resumes the scenario's time from its first instant instead of running on the system time until it is advanced again.
Quiescence ¶
Track marks work in flight -- a reconcile queued or running, a capability poll, an orchestrator tick -- and RegisterBusyProbe adds a probe for work no code runs for yet, such as a work queue's items not yet handed to a worker. The control endpoint's idle verb answers idle only when no probe answers busy, no tracked work is in flight, no AfterFunc callback is running and no timer or ticker is due and not yet fired or has fired a value its receiver has not taken yet. The harness's test_wait_idle waits for it, so a scenario can assert that nothing more happens. In production Track and RegisterBusyProbe cost an atomic load and a compare, and no probe is ever called.
What muster has not received yet is no work: a watch event on its way from the API server, a request in transit. A scenario waits for its effect before it waits for idle.
Wall time on purpose ¶
Leader election must stay on the system time, not on this clock: a large advance would expire every Lease at once and hand it to a follower. A scenario that needs a leader change soon shortens the lease durations through the environment instead. The timeouts of single requests and the filesystem detector's debounce stay on the system time too: they bound how long something takes rather than when it is due.
Index ¶
- Constants
- func Advance(d time.Duration) (time.Duration, error)
- func Now() time.Time
- func Offset() time.Duration
- func RegisterBusyProbe(name string, probe func() (busy bool, why string)) (unregister func())
- func RemoteAdvance(path string, d time.Duration) (time.Duration, error)
- func RemoteIdle(path string) (busy []string, err error)
- func RemoteOffset(path string) (time.Duration, error)
- func Since(t time.Time) time.Duration
- func StartControl() (stop func(), err error)
- func Track(kind string) (done func())
- func Until(t time.Time) time.Duration
- type Clock
- type Ticker
- type Timer
Constants ¶
const ( // EnvControlSocket is the environment variable that selects the clock's // control socket. Set by the integration test harness for every instance // it runs; unset in production, where nothing can move the clock. EnvControlSocket = "MUSTER_TEST_CLOCK" // EnvStart freezes the controllable clock at an RFC 3339 instant: Now is // that instant plus the offset, and timers fire only when the clock is // advanced. Ignored without EnvControlSocket. EnvStart = "MUSTER_TEST_CLOCK_START" // EnvOffset starts the controllable clock advanced by a Go duration, so // a restarted process resumes the scenario's time from its first // instant. Ignored without EnvControlSocket. EnvOffset = "MUSTER_TEST_CLOCK_OFFSET" )
Variables ¶
This section is empty.
Functions ¶
func Advance ¶
Advance moves the process clock forward by d and returns its total offset. Each due timer, ticker and AfterFunc fires once at the landing instant. A negative d is refused: the clock only moves forward. So is any d in production, where the process clock is the system time.
func Offset ¶
Offset returns how far the process clock has been advanced past the time it started from; always zero in production.
func RegisterBusyProbe ¶ added in v5.67.0
RegisterBusyProbe adds a probe the idle verb consults: while it answers busy, so is the process, named "<name>: <why>". It reports what Track cannot see because no code runs for it -- a work queue's items queued but not yet handed to a worker (queue.Len() > 0). The probe runs on the idle verb's goroutine and may take its owner's locks; it must not block for long. unregister removes it and may be called more than once. In production the probe is never called, and registering costs an atomic load and a compare.
func RemoteAdvance ¶
RemoteAdvance makes one jump of d on the process serving path and returns its total offset. Every due timer fires once at the landing instant.
func RemoteIdle ¶ added in v5.67.0
RemoteIdle asks the process serving its control endpoint on path whether it is idle. It returns nil when it is, else what keeps it busy.
func RemoteOffset ¶
RemoteOffset returns the total offset of the process serving its control endpoint on path.
func StartControl ¶
func StartControl() (stop func(), err error)
StartControl serves the clock's control endpoint on the Unix socket named by MUSTER_TEST_CLOCK and returns the function that stops it and removes the socket file. With the variable unset it serves nothing and returns a no-op. A socket file a previous life of the process left behind is replaced. An invalid MUSTER_TEST_CLOCK_START or MUSTER_TEST_CLOCK_OFFSET is an error.
func Track ¶ added in v5.67.0
func Track(kind string) (done func())
Track marks one piece of work of the given kind -- a reconcile, a poll, a tick's handling -- as in flight until done is called, and the idle verb answers busy meanwhile. Call Track before handing the work to another goroutine and done when it has finished, so no gap opens between a timer's value being received and its work being tracked. done may be called more than once. In production, where nothing serves the idle verb, Track costs an atomic load and a compare and returns a no-op.
Types ¶
type Clock ¶ added in v5.67.0
type Clock = k8sclock.WithTickerAndDelayedExecution
Clock is the clock muster's controllers take in their constructors: k8s.io/utils/clock's WithTickerAndDelayedExecution, so it is also the Clock of a client-go work queue. Production passes Process().
type Ticker ¶
type Ticker struct {
// C is the channel on which the ticks are delivered.
C <-chan time.Time
// contains filtered or unexported fields
}
Ticker delivers ticks at intervals on the process clock, like time.Ticker, and also when the clock is advanced past its next tick. A receiver that has not taken the previous tick is not sent a second one. It is the process clock's NewTicker with a channel field, for the callers that predate Clock.