chooser

package
v0.0.0-...-48fa796 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2021 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultIdealDynamicSize is the default target ideal dynamic cluster
	// size used for the initial cluster.
	DefaultIdealDynamicSize = 5

	// IdealDynamicSizePath is the path key used for the chooser. It usually
	// gets used with a namespace prefix.
	IdealDynamicSizePath = "/dynamicsize/idealclustersize"
)

Variables

This section is empty.

Functions

func DynamicSizeGet

func DynamicSizeGet(ctx context.Context, client interfaces.Client) (uint16, error)

DynamicSizeGet gets the currently set dynamic size set in the cluster.

func DynamicSizeSet

func DynamicSizeSet(ctx context.Context, client interfaces.Client, size uint16) (bool, error)

DynamicSizeSet sets the dynamic size in the cluster. It returns true if it changed or set the value.

Types

type Chooser

type Chooser interface {
	// Validate validates the chooser implementation to ensure the params
	// represent a valid instantiation.
	Validate() error

	// Init initializes the chooser and passes in some useful data and
	// handles.
	Init(*Data) error

	// Connect will be called with a client interfaces.Client that you can
	// use if necessary to store some shared state between instances of this
	// and watch for external changes. Sharing state between members should
	// be avoided if possible, and there is no guarantee that your data
	// won't be deleted in a disaster. There are no backups for this,
	// regenerate anything you might need. Additionally, this may only be
	// used inside the Chooser method, since Connect is only called after
	// Init. This is however very useful for implementing special choosers.
	// Since some operations can run on connect, it gets a context. If you
	// cancel this context, then you might expect that Watch could die too.
	// Both of these should get cancelled if you call Disconnect.
	Connect(context.Context, interfaces.Client) error // we get given a namespaced client

	// Disconnect tells us to cancel our use of the client interface that we
	// got from the Connect method. We must not return until we're done.
	Disconnect() error

	// Watch is called by the engine to allow us to Watch for changes that
	// might cause us to want to re-evaluate our nomination decision. It
	// should error if it cannot startup. Once it is running, it should send
	// a nil error on every event, and an error if things go wrong. When
	// Disconnect is shutdown, then that should cause this to exit. When
	// this sends events, Choose will usually eventually get called in
	// response.
	Watch() (chan error, error)

	// Choose takes the current peer membership state, and the available
	// volunteers, and produces a list of who we should add and who should
	// quit. In general, it's best to only remove one member at a time, in
	// particular because this will get called iteratively on future events,
	// and it can remove subsequent members on the next iteration. One
	// important note: when building a new cluster, we do assume that out of
	// one available volunteer, and no members, that this first volunteer is
	// selected. Make sure that any implementations of this function do this
	// as well, since otherwise the hardcoded initial assumption would be
	// proven wrong here!
	// TODO: we could pass in two lists of hostnames instead of the full
	// URLsMap here, but let's keep it more complicated now in case, and
	// reduce it down later if needed...
	// TODO: should we add a step arg here ?
	Choose(membership, volunteers etcdtypes.URLsMap) (nominees, quitters []string, err error)

	// Close runs some cleanup routines in case there is anything that you'd
	// like to free after we're done.
	Close() error
}

Chooser represents the interface you must implement if you want to be able to control which cluster members are added and removed. Remember that this can get run from any peer (server) machine in the cluster, and that this may change as different leaders are elected! Do not assume any state will remain between invocations. If you want to maintain hysteresis or state, make sure to synchronize it in etcd.

type Data

type Data struct {
	// Hostname is the hostname running this chooser instance. It can be
	// used as a unique key in the cluster.
	Hostname string // ourself

	Debug bool
	Logf  func(format string, v ...interface{})
}

Data represents the input data that is passed to the chooser.

type DynamicSize

type DynamicSize struct {
	// IdealClusterSize is the ideal target size for this cluster. If it is
	// set to zero, then it will use DefaultIdealDynamicSize as the value.
	IdealClusterSize uint16
	// contains filtered or unexported fields
}

DynamicSize is a simple implementation of the Chooser interface. This helps select which machines to add and remove as we elastically grow and shrink our cluster. TODO: think of a better name

func (*DynamicSize) Choose

func (obj *DynamicSize) Choose(membership, volunteers etcdtypes.URLsMap) ([]string, []string, error)

Choose accepts a list of current membership, and a list of volunteers. From that we can decide who we should add and remove. We return a list of those nominated, and unnominated users respectively.

func (*DynamicSize) Close

func (obj *DynamicSize) Close() error

Close runs some cleanup routines.

func (*DynamicSize) Connect

func (obj *DynamicSize) Connect(ctx context.Context, client interfaces.Client) error

Connect is called to accept an etcd.KV namespace that we can use.

func (*DynamicSize) Disconnect

func (obj *DynamicSize) Disconnect() error

Disconnect is called to cancel our use of the etcd.KV connection.

func (*DynamicSize) Init

func (obj *DynamicSize) Init(data *Data) error

Init accepts some useful data and handles.

func (*DynamicSize) Validate

func (obj *DynamicSize) Validate() error

Validate validates the struct.

func (*DynamicSize) Watch

func (obj *DynamicSize) Watch() (chan error, error)

Watch is called to send events anytime we might want to change membership. It is also used to watch for changes so that when we get an event, we know to honour the change in Choose.

Jump to

Keyboard shortcuts

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