Documentation
¶
Overview ¶
Package evwatch runs the event sources of the daemon watchers on demand.
A watcher like drbdmon reads the events a system tool prints, as "drbdsetup events2", for the instances holding a resource of its kind to evaluate their status again. Such a source is a process, and a connection to a service: a node holding no resource of its kind has no event to wait for, and a "virsh event" kept running would keep a socket-activated libvirtd running for nothing.
The Counter counts the local resources of a kind from the instance statuses, the Runner runs the source while the count is not zero, and starts it again when it ends, the Debouncer publishes a change once the events about it settle, and StreamLines runs a tool printing an event per line, with a bounded wait once it is stopped.
Index ¶
- Variables
- func KeysOf(st instance.Status, infoKey string, types ...string) []string
- func ProbeHelp(ctx context.Context, env []string, flag, name string, args ...string) error
- func StreamLines(ctx context.Context, env []string, onLine func(string), name string, ...) (string, error)
- type Counter
- type Debouncer
- type Policy
- type Runner
- type Source
- type State
Constants ¶
This section is empty.
Variables ¶
var DefaultPolicy = Policy{ StopGrace: 60 * time.Second, RestartMin: 5 * time.Second, RestartMax: 5 * time.Minute, QuickFailure: 10 * time.Second, CheckTimeout: 10 * time.Second, }
DefaultPolicy is the policy of a source never disabled: a service it connects to can be down for long and come back.
var ErrUnsupported = errors.New("unsupported")
ErrUnsupported is the error of a source that can not run on the node.
Functions ¶
func KeysOf ¶
KeysOf returns the keys of the resources of the status whose driver type is one of types: the value of their status info infoKey, as the name of a libvirt domain. A resource of the type with no such value is counted all the same, with an empty key: it is one the watcher has events for, which can not be told apart.
func ProbeHelp ¶
ProbeHelp checks a tool supports an event stream, by the help it prints: name with args, the variables of env added, prints flag.
Only a tool that ran to its end and printed no flag is unsupported, the error wrapping ErrUnsupported: an older tool, whose help has no such option, or no such command and exits in error. A probe that timed out, or could not run, says nothing of the tool, and its error is retried, as an engine busy or a node loaded.
func StreamLines ¶
func StreamLines(ctx context.Context, env []string, onLine func(string), name string, args ...string) (string, error)
StreamLines runs name with args, the variables of env added to the environment, and calls onLine for each line it prints, until it ends or the context is done. The command is then sent a SIGTERM, and its output closed streamWaitDelay later, so a stop never waits on it longer.
It returns the last lines of the stderr of the command, to say why it ended, and its error.
Types ¶
type Counter ¶
type Counter struct {
// contains filtered or unexported fields
}
Counter counts the local resources a watcher has events for, by the key their events name them with, as the name of a libvirt domain or of a drbd resource, for each instance holding some.
func (*Counter) Keys ¶
Keys returns the names of the counted resources, each once, sorted, but for the resources counted with no name.
type Debouncer ¶
type Debouncer struct {
// contains filtered or unexported fields
}
Debouncer calls a function for a key once the notifications about it settle: a notification within the delay of the previous one delays the call, so the events of one change, as the two lines of a domain start, make one call.
func NewDebouncer ¶
NewDebouncer returns a Debouncer calling fn for a key delay after the last notification about it.
type Policy ¶
type Policy struct {
// StopGrace is how long the source runs on once no resource of
// its kind is left, so a provision retried or an object deleted
// and created again does not stop and start it.
StopGrace time.Duration
// RestartMin is the delay before the source runs again once it
// ended, doubled at each quick failure up to RestartMax.
RestartMin time.Duration
RestartMax time.Duration
// QuickFailure is the run a source ending sooner than fails
// quickly. A longer run resets the count of quick failures.
QuickFailure time.Duration
// MaxQuickFailures is how many quick failures in a row disable
// the source until no resource of its kind is left. Zero never
// disables it.
MaxQuickFailures int
// CheckTimeout bounds a Check.
CheckTimeout time.Duration
}
Policy says how a Runner runs its source.
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner runs a source while the resources of its kind are not zero.
A demand going from zero to more starts the source at once: the risk is an event missed, not a source started for nothing. A demand back to zero stops it once the stop grace of its policy ends, unless the demand comes back meanwhile. A source ending runs again after a delay doubled at each quick failure, and one that can not run on the node is disabled until the demand is back to zero.
One goroutine owns the state: SetDemand never blocks, the latest demand wins, and Stop returns once the source stopped.
func NewRunner ¶
func NewRunner(src Source, policy Policy, log *plog.Logger, onReady func(), onEvent func(key string)) *Runner
NewRunner returns a runner of src, calling onReady each time the source is ready, for a resync of the resources it misses events of, and onEvent for each event.
type Source ¶
type Source interface {
// Name names the source in the logs, as "drbdsetup events2".
Name() string
// Check says whether the source can run on the node. An error
// wrapping ErrUnsupported says it can not, as a tool not
// installed or too old: the source is not tried again before the
// next resource of its kind. Another error is retried.
Check(ctx context.Context) error
// Run reads the events until the context is done or the source
// ends. It calls ready once the events are read, which a resync
// follows, and emit for each event, with the key of the resource
// it is about. It returns once nothing runs any more.
Run(ctx context.Context, ready func(), emit func(key string)) error
}
Source is the event source of a watcher, as a tool printing the events it reads.
type State ¶
type State string
State is the state of a Runner.
const ( // StateIdle is a runner with no resource of its kind: its source // does not run. StateIdle State = "idle" // StateRunning is a source running. StateRunning State = "running" // StateDraining is a source running on with no resource of its kind // left, until its grace delay ends. StateDraining State = "draining" // StateBackoff is a source waiting to run again, once it ended or // its check failed. StateBackoff State = "backoff" // StateDisabled is a source that can not run on the node, until no // resource of its kind is left. StateDisabled State = "disabled" )