Documentation

Overview

An implementation of Consistent Hashing and Consistent Hashing With Bounded Loads.

https://en.wikipedia.org/wiki/ConsistentHash_hashing

https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html

refs: https://github.com/lafikl/consistent https://github.com/stathat/consistent

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoHosts = jerrors.New("no hosts added")
)

Functions

This section is empty.

Types

type ConsistentHash

type ConsistentHash struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewConsistentHash

func NewConsistentHash(opts ...Option) *ConsistentHash

func (*ConsistentHash) Add

func (c *ConsistentHash) Add(host string)

func (*ConsistentHash) Done

func (c *ConsistentHash) Done(host string)

Decrements the load of host by 1

should only be used with if you obtained a host with GetLeast

func (*ConsistentHash) Get

func (c *ConsistentHash) Get(key string) (string, error)

Returns the host that owns `key`.

As described in https://en.wikipedia.org/wiki/ConsistentHash_hashing

It returns ErrNoHosts if the ring has no hosts in it.

func (*ConsistentHash) GetHash

func (c *ConsistentHash) GetHash(hashKey uint32) (string, error)

Returns the host that owns `hashKey`.

It returns ErrNoHosts if the ring has no hosts in it.

func (*ConsistentHash) GetLeast

func (c *ConsistentHash) GetLeast(key string) (string, error)

It uses ConsistentHash Hashing With Bounded loads

https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html

to pick the least loaded host that can serve the key

It returns ErrNoHosts if the ring has no hosts in it.

func (*ConsistentHash) GetLoads

func (c *ConsistentHash) GetLoads() map[string]int64

Returns the loads of all the hosts

func (*ConsistentHash) GetN

func (c *ConsistentHash) GetN(name string, n int) ([]string, error)

GetN returns the N closest distinct elements to the name input in the circle.

func (*ConsistentHash) GetTwo

func (c *ConsistentHash) GetTwo(name string) (string, string, error)

GetTwo returns the two closest distinct elements to the name input in the circle.

func (*ConsistentHash) Hosts

func (c *ConsistentHash) Hosts() (hosts []string)

Return the list of hosts in the ring

func (*ConsistentHash) Inc

func (c *ConsistentHash) Inc(host string)

Increments the load of host by 1

should only be used with if you obtained a host with GetLeast

func (*ConsistentHash) MaxLoad

func (c *ConsistentHash) MaxLoad() int64

Returns the maximum load of the single host which is: (total_load/number_of_hosts)*1.25 total_load = is the total number of active requests served by hosts for more info: https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html

func (*ConsistentHash) Members

func (c *ConsistentHash) Members() []string

func (*ConsistentHash) Remove

func (c *ConsistentHash) Remove(host string) bool

Deletes host from the ring

func (*ConsistentHash) Set

func (c *ConsistentHash) Set(elts []string)

Set sets all the elements in the hash. If there are existing elements not present in elts, they will be removed.

func (*ConsistentHash) SetHashFunc

func (c *ConsistentHash) SetHashFunc(f HashFunc)

func (*ConsistentHash) UpdateLoad

func (c *ConsistentHash) UpdateLoad(host string, load int64)

Sets the load of `host` to the given `load`

type HashFunc

type HashFunc func([]byte) uint64

type Host

type Host struct {
	Name string
	Load int64
}

type Option

type Option func(*Options)

func WithHashFunc

func WithHashFunc(hash HashFunc) Option

func WithMaxVnodeNum

func WithMaxVnodeNum(maxVnodeNum int) Option

func WithReplicaNum

func WithReplicaNum(replicaNum int) Option

type Options

type Options struct {
	HashFunc    HashFunc
	ReplicaNum  int
	MaxVnodeNum int
}

Source Files