edrf

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2022 License: MIT Imports: 5 Imported by: 0

README

godoc for dyxu/edrf goreportcard for dyxu/edrf

eDRF

eDRF is an extended Dominant Resource Fairness(eDRF) implement in golang, allowing you to assignment with binpack, weight and limit options.

Install

go get github.com/dyxu/edrf

Examples

Interface
// EDRF defines the extended Dominant Resource Fairness interface
type EDRF interface {
	// Assign assigns an piece of resource to task
	Assign() (Task, error)
	// AddTask adds a task
	AddTask(t Task) error
	// RemoveTask removes a task
	RemoveTask(t Task) error
	// Describe describes the eDRF detail
	Describe() string
}

// Cluster defines a cluster
type Cluster interface {
	Capacity() Resources
}

// Task defines a task
type Task interface {
	Name() string
	Piece() Resources
}

// BinpackOption defines whether enbale binpack policy or not
type BinpackOption interface {
	Binpack() bool
}

// LimitOption returns the limit resouce of task
type LimitOption interface {
	Limit(ResourceType) (ResourceAmount, bool)
}

// WeightOption returns the weight of task
type WeightOption interface {
	Weight(ResourceType) (float64, bool)
}
Standard DRF
import (
    "github.com/dyxu/edrf"
)

func main() {
	var tasks = []edrf.Task{
		edrf.NewTask(
			"A", // task name
			edrf.Resources{
				edrf.ResourceCPU:    1,
				edrf.ResourceMemory: 4,
			}, // piece
			nil, // limit
			nil, // weight
		),
		edrf.NewTask(
			"B",
			edrf.Resources{
				edrf.ResourceCPU:    3,
				edrf.ResourceMemory: 1,
			},
			nil,
			nil,
		),
	}
	cluster := edrf.NewCluster(
		edrf.Resources{
			edrf.ResourceCPU:    9,
			edrf.ResourceMemory: 18,
		}, // capacity
		false, // binpack policy
	)

	e := edrf.New(cluster, tasks...)
    allocated := map[string]edrf.Resources{}
    loop := 1
	for {
		task, err := e.Assign()
		if err != nil {
			break
		}
        fmt.Printf("Loop %.2d: assign to %s", loop, task.Name())
		alloc := allocated[task.Name()]
		alloc.Add(task.Piece())
		allocated[task.Name()] = alloc
        loop++
	}

    fmt.Print(e.Describe())
    fmt.Print(allocated)
}
Extended DRF
import (
    "github.com/dyxu/edrf"
)

func main() {
	var tasks = []edrf.Task{
		edrf.NewTask(
			"A", // task name
			edrf.Resources{
				edrf.ResourceCPU:    1,
				edrf.ResourceMemory: 4,
			}, // piece
			edrf.Resources{
				edrf.ResourceCPU: 3,
			}, // limit
			map[edrf.ResourceType]float64{
				edrf.ResourceCPU: 3.0,
			}, // weight
		),
		edrf.NewTask(
			"B",
			edrf.Resources{
				edrf.ResourceCPU:    3,
				edrf.ResourceMemory: 1,
			},
            nil,
			map[edrf.ResourceType]float64{
				edrf.ResourceCPU: 1.0,
			},
		),
	}
	cluster := edrf.NewCluster(
		edrf.Resources{
			edrf.ResourceCPU:    18,
			edrf.ResourceMemory: 36,
		}, // capacity
		true, // binpack policy
	)

	e := edrf.New(cluster, tasks...)
    loop := 1
    allocated := map[string]edrf.Resources{}
	for {
		task, err := e.Assign()
		if err != nil {
			break
		}
        fmt.Printf("Loop %.2d: assign to %s", loop, task.Name())
		alloc := allocated[task.Name()]
		alloc.Add(task.Piece())
		allocated[task.Name()] = alloc
        loop++
	}

    fmt.Print(e.Describe())
    fmt.Print(allocated)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoAssignableTask = errors.New("no assignable task")
	ErrExistTask        = errors.New("task exists")
	ErrTaskNotFound     = errors.New("task not found")
)

ErrorType

Functions

This section is empty.

Types

type BinpackOption

type BinpackOption interface {
	Binpack() bool
}

BinpackOption defines whether enbale binpack policy or not

type Cluster

type Cluster interface {
	Capacity() Resources
}

Cluster defines a cluster

func NewCluster

func NewCluster(capacity Resources, binpack bool) Cluster

type EDRF

type EDRF interface {
	// Assign assigns an piece of resource to task
	Assign() (Task, error)
	// AddTask adds a task
	AddTask(t Task) error
	// RemoveTask removes a task
	RemoveTask(t Task) error
	// Describe describes the eDRF detail
	Describe() string
}

EDRF defines the extended Dominant Resource Fairness interface

func New

func New(cluster Cluster, tasks ...Task) EDRF

New creates an eDRF implement

type LimitOption

type LimitOption interface {
	Limit(ResourceType) (ResourceAmount, bool)
}

LimitOption returns the limit resouce of task

type ResourceAmount

type ResourceAmount int64

type ResourceType

type ResourceType string

ResourceType defines resource type, e.g. cpu, memory, traffic

const (
	ResourceCPU     ResourceType = "cpu"
	ResourceMemory  ResourceType = "memory"
	ResourceTraffic ResourceType = "traffic"
)

type Resources

type Resources map[ResourceType]ResourceAmount

func (*Resources) Add

func (r *Resources) Add(t Resources)

func (Resources) DeepCopy

func (r Resources) DeepCopy() Resources

type Task

type Task interface {
	Name() string
	Piece() Resources
}

Task defines a task

func NewTask

func NewTask(name string, piece, limit Resources, weight map[ResourceType]float64) Task

type WeightOption

type WeightOption interface {
	Weight(ResourceType) (float64, bool)
}

WeightOption returns the weight of task

Jump to

Keyboard shortcuts

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