Documentation
¶
Overview ¶
Package timers is the worker of the timer rows (C-09.FR-12, ADR-0006, ADR-0007): on every replica it claims the due timers of each Organization with FOR UPDATE SKIP LOCKED and a lease, fires each in a transaction of its own through the handler of its kind, and waits for the earliest deadline, a notification of a new timer or a move of the development clock. Deadlines are business times and leases real times; a timer overdue after downtime fires once, and a timer whose lease ran out is claimed again by any replica.
Index ¶
- Constants
- func AckNoticeDue(start time.Time, first time.Duration, n int64) time.Time
- func AckNoticeGap(first time.Duration, n int64) time.Duration
- func DueAckNotices(start time.Time, first time.Duration, sent int64, now time.Time) int64
- func DueReminders(deadline time.Time, next int64, first, limit time.Duration, now time.Time) (int64, time.Time)
- func ReminderInterval(k int64, first, limit time.Duration) time.Duration
- type Handler
- type Store
- type Timer
- type Worker
Constants ¶
const ( Lease = time.Minute Batch = 100 MaxWait = 30 * time.Second MinWait = 100 * time.Millisecond FailureBackoff = time.Second )
The defaults of the worker: the lease of a claimed timer, the rows one claim takes, the longest wait between rounds when no deadline is nearer, which bounds what a missed notification costs, the shortest one, so that a due timer another replica is claiming right now is not polled in a tight loop, and the first wait after a failed round.
const AckTimeoutNotices = 3
AckTimeoutNotices is the most notices one ack timeout sends; the last makes the Alert Group Unclaimed (C-17.FR-2, FR-3).
const AutoUnacknowledgeAfter = 2
AutoUnacknowledgeAfter is timers.auto_unacknowledge_after: the unanswered Reminders in a row after which the next one auto-unacknowledges instead, where the Route has auto-unacknowledge on (C-17.FR-5).
Variables ¶
This section is empty.
Functions ¶
func AckNoticeDue ¶
AckNoticeDue is when notice n (1 to AckTimeoutNotices) of an ack timeout started at start is due, with the first interval first: start + (2ⁿ − 1)·first, that is first, 3·first and 7·first after the start.
func AckNoticeGap ¶
AckNoticeGap is the interval before notice n: 2ⁿ⁻¹·first, that is first, 2·first and 4·first.
func DueAckNotices ¶
DueAckNotices is how many notices of an ack timeout started at start, after the sent ones, are due at now: those whose time came, at most the notices left.
func DueReminders ¶
func DueReminders(deadline time.Time, next int64, first, limit time.Duration, now time.Time) (int64, time.Time)
DueReminders counts the Reminders due at now when Reminder next is due at deadline: deadline itself, then each one interval of the following step after the one before, as long as it is not after now. It returns the count, zero when deadline is after now, and when the last of them was due. Once the interval reaches the cap the rest are counted at once, so that days of downtime are not walked one step at a time.
func ReminderInterval ¶
ReminderInterval is the interval before Reminder k (1, 2, …) after the one before it, or after the acknowledgement for the first: min(first·2ᵏ⁻¹, limit), limit being the cap of route.reminders. It stops doubling once the cap is reached, so a large k never overflows; a k below 1 is the first.
Types ¶
type Handler ¶
type Handler func(ctx context.Context, tx dbgen.DBTX, orgID int64, t Timer) (func(context.Context), error)
Handler fires one timer of its kind in the transaction tx of the timer, for the Organization orgID, and returns what to run once that transaction committed, or nil. A handler checks the state of its subject itself: a timer that is no longer due for it changes nothing.
type Store ¶
type Store interface {
// Claim leases at most limit due timers of the kinds of the Organization orgID.
Claim(ctx context.Context, l db.Lease, orgID int64, kinds []string, limit int32) ([]Timer, error)
// Fire runs h for a claimed timer in a transaction that still holds its lease, and returns what h returns.
Fire(ctx context.Context, l db.Lease, orgID int64, t Timer, h Handler) (func(context.Context), error)
// Next is how long until a timer of the kinds of the Organization can be claimed, at most limit: until the
// earliest deadline of a free one on the business clock, or the end of the earliest lease on the real clock.
Next(ctx context.Context, l db.Lease, orgID int64, kinds []string, limit time.Duration) (time.Duration, error)
}
Store claims, fires and schedules the timers.
type Timer ¶
type Timer struct {
ID int64
AlertGroupID *int64
StormID *int64
Kind string
Deadline time.Time
Attempts int64
}
Timer is a claimed timer row.
type Worker ¶
type Worker struct {
Store Store
// Lease names this replica in lease_owner and carries the clocks: due times on the business clock, leases on
// the real clock.
Lease db.Lease
// Organizations lists the Organizations whose timers it fires: one in L1.
Organizations func(ctx context.Context) ([]int64, error)
// Handlers fire each kind; the worker claims only these kinds.
Handlers map[string]Handler
Log *logging.Logger
// MaxWait bounds the wait between rounds, and Batch the timers one claim takes; zero is the default.
MaxWait time.Duration
Batch int32
// Wait waits for d or until a wake arrives; nil waits on the real clock.
Wait func(ctx context.Context, d time.Duration, wake <-chan struct{})
// contains filtered or unexported fields
}
Worker fires the due timers of this replica.
func (*Worker) Round ¶
Round fires every due timer of the kinds it has handlers for in every Organization, claiming Batch at a time, and returns how long until the next one can be claimed, at most MaxWait. A timer that fails is logged and fires again once its lease runs out; the round goes on. Timers of other kinds wait for the release that fires them.