naive

package
v3.26.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package naive contains a naive implementation of the Balancer interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Balancer

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

Balancer is a naive implementation of the controller.Balancer interface. It helps manage the relationships between workers and jobs. The logic it uses to balance jobs across workers is very simple; it bases everything off the number of workers and number of jobs. It does not take anything else (such as job size, worker capabilities, etc) into consideration.

func New

func New(name string, fjs FreeJobService, wjs WorkerJobService, logger logger.Logger) *Balancer

New returns a new instance of Balancer.

func (*Balancer) AddJobs

func (b *Balancer) AddJobs(ctx context.Context, jobs ...fmt.Stringer) ([]dax.WorkerDiff, error)

AddJobs adds one or more jobs to an existing worker. If there are no existing workers, the jobs are placed into the free list and will be assigned to a worker once one becomes available.

func (*Balancer) AddWorker

func (b *Balancer) AddWorker(ctx context.Context, worker fmt.Stringer) ([]dax.WorkerDiff, error)

AddWorker adds a worker to the Balancer's worker pool. This may cause the Balancer to assign existing jobs that are currently in the free list to the worker. Also, the worker will immediately be available for assignments of new jobs.

func (*Balancer) Balance

func (b *Balancer) Balance(ctx context.Context) ([]dax.WorkerDiff, error)

Balance ensures that all jobs are being handled by a worker by assigning jobs in the free list to workers, and by moving job assignments around in order to balance the load on workers.

func (*Balancer) CurrentState

func (b *Balancer) CurrentState(ctx context.Context) ([]dax.WorkerInfo, error)

CurrentState returns the current state of worker and job assignments. Note that there could be unassigned jobs which are not captured in this output. Calling Balance() would force any unassigned jobs to be assigned (assuming there is at least one worker), and the output would then reflect that.

func (*Balancer) RemoveJob

func (b *Balancer) RemoveJob(ctx context.Context, job fmt.Stringer) ([]dax.WorkerDiff, error)

RemoveJob removes a job from the worker to which is was assigned. If the job is not currently assigned to a worker, but it is in the free list, then it will be removed from the free list.

func (*Balancer) RemoveWorker

func (b *Balancer) RemoveWorker(ctx context.Context, worker fmt.Stringer) ([]dax.WorkerDiff, error)

RemoveWorker removes a worker from the worker pool and moves any of its currently assigned jobs to the free list. If the intention is to remove a worker and reassign its jobs to other workers, then RemoveWorker() should be followed by Balance().

func (*Balancer) WorkerState

func (b *Balancer) WorkerState(ctx context.Context, worker dax.Worker) (dax.WorkerInfo, error)

WorkerState returns the current state of job assignments for a given worker.

func (*Balancer) WorkersForJobPrefix

func (b *Balancer) WorkersForJobPrefix(ctx context.Context, prefix string) ([]dax.WorkerInfo, error)

func (*Balancer) WorkersForJobs

func (b *Balancer) WorkersForJobs(ctx context.Context, jobs []dax.Job) ([]dax.WorkerInfo, error)

WorkersForJobs returns the list of workers for the given jobs. If a given job is not currently assigned to a worker, it will be ignored.

type FreeJobService

type FreeJobService interface {
	CreateFreeJobs(ctx context.Context, balancerName string, job ...dax.Job) error
	DeleteFreeJob(ctx context.Context, balancerName string, job dax.Job) error
	ListFreeJobs(ctx context.Context, balancerName string) (dax.Jobs, error)
	MergeFreeJobs(ctx context.Context, balancerName string, jobs dax.Jobs) error
}

type WorkerJobService

type WorkerJobService interface {
	WorkersJobs(ctx context.Context, balancerName string) ([]dax.WorkerInfo, error)

	WorkerCount(ctx context.Context, balancerName string) (int, error)
	ListWorkers(ctx context.Context, balancerName string) (dax.Workers, error)
	WorkerExists(ctx context.Context, balancerName string, worker dax.Worker) (bool, error)
	CreateWorker(ctx context.Context, balancerName string, worker dax.Worker) error
	DeleteWorker(ctx context.Context, balancerName string, worker dax.Worker) error

	CreateJobs(ctx context.Context, balancerName string, worker dax.Worker, job ...dax.Job) error
	DeleteJob(ctx context.Context, balancerName string, worker dax.Worker, job dax.Job) error
	JobCounts(ctx context.Context, balancerName string, worker ...dax.Worker) (map[dax.Worker]int, error)
	ListJobs(ctx context.Context, balancerName string, worker dax.Worker) (dax.Jobs, error)
}

Directories

Path Synopsis
Package boltdb contains the boltdb implementation of the Balancer interface.
Package boltdb contains the boltdb implementation of the Balancer interface.

Jump to

Keyboard shortcuts

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