watcher

package
v0.0.0-...-327cdba Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WatchNodes

func WatchNodes(ctx context.Context, cur ipb.CuratorClient, f Follower) error

WatchNodes runs a WatchRequest for NodesInCluster with the given Curator channel. Any updates to the state of the nodes is processed through the given Follower.

This is the main interface to follow a state of nodes in the cluster and act upon any changes. SimpleFollower is given as a type to implement the simplest kind of callback-driven interface to various events, but users are free to implement Follower on their own.

This function will exit with a context error whenever the given context is canceled, or return with whatever error is returned by the Follower implementation.

Types

type Follower

type Follower interface {
	// Filter should return true if a node is of interest to the follower - when it
	// has all required fields present and at a requested state.
	//
	// For example, a Follower which wishes to watch for nodes' external IP
	// addresses would filter out all nodes which don't have an address assigned.
	Filter(a *ipb.Node) bool

	// Equals should return true if a given node's state is identical, from the point
	// of view of the Follower, to some other state. Correctly implementing this
	// function allows the Follower to only receive calls to New/Updated/Deleted when
	// the node actually changed in a meaningful and actionable way.
	//
	// For example, a Follower which wishes to watch for nodes' external IP addresses
	// would return true only if the two nodes' external IP addresses actually
	// differed.
	Equals(a *ipb.Node, b *ipb.Node) bool

	// New will be called when a node has appeared from the point of view of the
	// Follower (i.e. started existing on the cluster and then also passed the Filter
	// function).
	//
	// Any returned error is considered fatal and will stop future use of the
	// Follower, e.g. WatchNodes will return.
	New(new *ipb.Node) error

	// Updated will be called when a node has been updated from the point of view of
	// the Follower (i.e. has not been filtered out, and Equals returned false).
	//
	// Any returned error is considered fatal and will stop future use of the
	// Follower, e.g. WatchNodes will return.
	Updated(prev *ipb.Node, new *ipb.Node) error

	// Deleted will be called when a node has been removed from the point of view of
	// the Follower (i.e. has been filtered out, or has been removed from the cluster
	// altogether).
	//
	// Any returned error is considered fatal and will stop future use of the
	// Follower, e.g. WatchNodes will return.
	Deleted(prev *ipb.Node) error

	// BatchDone will be called at the end of any batch of node updates (either New,
	// Updated or Deleted calls). This can be used by Followers to reduce the number
	// of mutations of an expensive resource, for example if the Nodes watch
	// mechanism is used to feed some other stateful system which also supports
	// batch-based updates.
	//
	// Just exactly how large batches are is an implementation detail of the
	// underlying Curator watch protocol and the way update events get created by the
	// Curator and sent over the wire.
	//
	// Note: BatchDone() will not be called if any of the New/Updated/Deleted
	// implementations returned an error - the follower will be terminated
	// immediately!
	BatchDone() error
}

A Follower is some subsystem which wishes to be notified about changes to a cluster's node state.

It provides function to filter out state and state transitions that are interesting to itself, and functions which will be called when the filtered state changes.

The Filter and Equals functions make up a 'view' of the cluster state from the point of view of the Follower. That is, a Follower which only cares about some subset of nodes and expresses said subset with Filter will only see these nodes in its nodeSet and in its callbacks' calls. Similarly, updates to the nodes will also be filtered out accordingly to Equals.

A simple callback-based implementation is available in SimpleFollower.

type SimpleFollower

type SimpleFollower struct {
	// FilterFn corresponds to Follower.Filter - see its documentation for more
	// details.
	FilterFn func(a *ipb.Node) bool
	// EqualsFn corresponds to Follower.Equals - see its documentation for more
	// details.
	EqualsFn func(a *ipb.Node, b *ipb.Node) bool

	// OnNewUpdated will be called whenever a node is updated or appears for the
	// first time from the point of view of the Follower.
	OnNewUpdated func(new *ipb.Node) error
	// OnDeleted will be called whenever a node disappears from the point of view of
	// the Follower.
	OnDeleted func(prev *ipb.Node) error

	// OnBatchDone will be called at the end of a batch of NewUpdated/Deleted calls
	// from the underlying Curator watch mechanism.
	OnBatchDone func() error
}

SimpleFollower is a callback struct based implementation of a Follower, with the additional collapse of New and Updated into a NewUpdated function.

This is the simplest way to use the Follower / WatchNodes system from a function.

func (SimpleFollower) BatchDone

func (f SimpleFollower) BatchDone() error

func (SimpleFollower) Deleted

func (f SimpleFollower) Deleted(prev *ipb.Node) error

func (SimpleFollower) Equals

func (f SimpleFollower) Equals(a *ipb.Node, b *ipb.Node) bool

func (SimpleFollower) Filter

func (f SimpleFollower) Filter(a *ipb.Node) bool

func (SimpleFollower) New

func (f SimpleFollower) New(new *ipb.Node) error

func (SimpleFollower) Updated

func (f SimpleFollower) Updated(_prev *ipb.Node, new *ipb.Node) error

type Watcher

type Watcher struct {
	// contains filtered or unexported fields
}

Watcher returned by WatchNode. Must be closed by calling Close().

func WatchNode

func WatchNode(ctx context.Context, cur ipb.CuratorClient, nid string) *Watcher

WatchNode runs a WatchRequest for NodeInCluster with the given Curator channel. The returned Watcher can then be queries in a for loop to detect changes to the targeted node.

func (*Watcher) Close

func (w *Watcher) Close()

Close RPC call associted with this Watcher. Must be called at least once after the Watcher is not used anymore.

func (*Watcher) Error

func (w *Watcher) Error() error

Error returns underlying error for this Watcher, nil if no error is present. After an error is returned, the Watcher cannot be used anymore.

func (*Watcher) Next

func (w *Watcher) Next() bool

Next returns true if the next call to Node() is valid, false otherwise. Each call to Next blocks until an update to the node data is available.

If false is returned, Error() should be called to get to the underlying error which caused this call to fail.

func (*Watcher) Node

func (w *Watcher) Node() *ipb.Node

Node returns the cached node state for this Watcher. The same node data is returned until Next() is called. The caller can hold on to the returned Node pointer, as the node data will not be modified in place with updates - instead, a new Node object will be returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL