liblb: github.com/lafikl/liblb/p2c Index | Examples | Files

package p2c

import "github.com/lafikl/liblb/p2c"

P2C will distribute the traffic by choosing two hosts either via hashing or randomly and then pick the least loaded of the two. It gaurantees that the max load of a server is ln(ln(n)), where n is the number of servers.

All operations in P2C are concurrency-safe.

For more info: https://brooker.co.za/blog/2012/01/17/two-random.html

http://www.eecs.harvard.edu/~michaelm/postscripts/handbook2001.pdf

Code:

hosts := []string{"127.0.0.1:8009", "127.0.0.1:8008", "127.0.0.1:8007"}

// Power of Two choices example
lb := p2c.New(hosts...)
for i := 0; i < 10; i++ {
    // uses random power of two choices, because the key length == 0
    host, err := lb.Balance("")
    if err != nil {
        log.Fatal(err)
    }
    // load should be around 33% per host
    fmt.Printf("Send request #%d to host %s\n", i, host)
    // when the work assign to the host is done
    lb.Done(host)
}

// Partial Key Grouping example
pp2c := p2c.New(hosts...)
for i := 0; i < 10; i++ {
    // uses PKG because the key length is > 0
    host, err := pp2c.Balance("hello world")
    if err != nil {
        log.Fatal(err)
    }

    // traffic should be split between two nodes only
    fmt.Printf("Send request #%d to host %s\n", i, host)
    // when the work assign to the host is done
    pp2c.Done(host)

}

Index

Examples

Package Files

p2c.go

type P2C Uses

type P2C struct {
    sync.Mutex
    // contains filtered or unexported fields
}

func New Uses

func New(hosts ...string) *P2C

New returns a new instance of RandomTwoBalancer

func (*P2C) Add Uses

func (p *P2C) Add(hostName string)

func (*P2C) Balance Uses

func (p *P2C) Balance(key string) (string, error)

Balance picks two servers either randomly (if no key supplied), or via hashing (PKG) if given a key, then it returns the least loaded one between the two.

Partial Key Grouping (PKG) is great for skewed data workloads, which also needs to be determinstic in the way of choosing which servers to send requests too. https://arxiv.org/pdf/1510.07623.pdf the maximum load of a server in PKG at anytime is: `max_load-avg_load`

func (*P2C) Done Uses

func (p *P2C) Done(host string)

Decrments the load of the host (if found) by 1

func (*P2C) GetLoad Uses

func (p *P2C) GetLoad(host string) (load uint64, err error)

Returns the current load of the server, or it returns liblb.ErrNoHost if the host doesn't exist.

func (*P2C) Remove Uses

func (p *P2C) Remove(host string)

func (*P2C) UpdateLoad Uses

func (p *P2C) UpdateLoad(host string, load uint64) error

UpdateLoad updates the load of a host

Package p2c imports 6 packages (graph). Updated 2017-07-03. Refresh now. Tools for package owners.