gxconsistent

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2020 License: Apache-2.0, MIT Imports: 10 Imported by: 0

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 added in v0.3.2

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

func NewConsistentHash added in v0.3.1

func NewConsistentHash(opts ...Option) *ConsistentHash

func (*ConsistentHash) Add added in v0.3.2

func (c *ConsistentHash) Add(host string)

func (*ConsistentHash) Done added in v0.3.2

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 added in v0.3.2

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 added in v0.3.2

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 added in v0.3.2

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 added in v0.3.2

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

Returns the loads of all the hosts

func (*ConsistentHash) GetN added in v0.3.2

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 added in v0.3.2

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 added in v0.3.2

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

Return the list of hosts in the ring

func (*ConsistentHash) Inc added in v0.3.2

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 added in v0.3.2

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 added in v0.3.2

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

func (*ConsistentHash) Remove added in v0.3.2

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

Deletes host from the ring

func (*ConsistentHash) Set added in v0.3.2

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 added in v0.3.2

func (c *ConsistentHash) SetHashFunc(f HashFunc)

func (*ConsistentHash) UpdateLoad added in v0.3.2

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

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

type HashFunc added in v0.3.1

type HashFunc func([]byte) uint64

type Host

type Host struct {
	Name string
	Load int64
}

type Option added in v0.3.2

type Option func(*Options)

func WithHashFunc added in v0.3.2

func WithHashFunc(hash HashFunc) Option

func WithMaxVnodeNum added in v0.3.2

func WithMaxVnodeNum(maxVnodeNum int) Option

func WithReplicaNum added in v0.3.2

func WithReplicaNum(replicaNum int) Option

type Options added in v0.3.2

type Options struct {
	HashFunc    HashFunc
	ReplicaNum  int
	MaxVnodeNum int
}

Jump to

Keyboard shortcuts

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