Help us shape the future of Go! Take the 2026 Go Developer Survey

hashing

package
v1.16.21 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package placement is an implementation of Consistent Hashing and Consistent Hashing With Bounded Loads.

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

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

https://github.com/lafikl/consistent/blob/master/consistent.go

Index

Constants

This section is empty.

Variables

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

ErrNoHosts is an error for no hosts.

Functions

This section is empty.

Types

type Consistent

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

Consistent represents a data structure for consistent hashing.

func NewConsistentHash

func NewConsistentHash(replicationFactory int64) *Consistent

NewConsistentHash returns a new consistent hash.

func NewFromExisting

func NewFromExisting(loadMap map[string]*Host, replicationFactor int64, virtualNodesCache *VirtualNodesCache) *Consistent

NewFromExisting creates a new consistent hash from existing values.

func (*Consistent) Add

func (c *Consistent) Add(host, id string, port int64) bool

Add adds a host with port to the table. Used on the Placement side to add hosts to the ring. It doesn't calculate vnodes.

func (*Consistent) Done

func (c *Consistent) Done(host string)

Done decrements the load of host by 1

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

func (*Consistent) Get

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

Get returns the host that owns `key`. It assumes that the struct was created on the Daprd side with NewFromExisting, which is where the hashes are calculated

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

It returns ErrNoHosts if the ring has no hosts in it or the hashes haven't been calculated (which how this struct is used on the Placement side)

func (*Consistent) GetHost

func (c *Consistent) GetHost(key string) (*Host, error)

GetHost gets a host.

func (*Consistent) GetLeast

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

GetLeast uses Consistent 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 (*Consistent) GetLoads

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

GetLoads returns the loads of all the hosts.

func (*Consistent) Hosts

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

Hosts return the list of hosts in the ring.

func (*Consistent) Inc

func (c *Consistent) Inc(host string)

Inc increments the load of host by 1

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

func (*Consistent) MaxLoad

func (c *Consistent) MaxLoad() int64

MaxLoad 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 (*Consistent) ReadInternals added in v1.7.0

func (c *Consistent) ReadInternals(reader func(map[uint64]string, []uint64, map[string]*Host, int64))

ReadInternals returns the internal data structure of the consistent hash.

func (*Consistent) Remove

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

Remove deletes host from the ring.

func (*Consistent) SortedSet added in v1.13.0

func (c *Consistent) SortedSet() (sortedSet []uint64)

func (*Consistent) UpdateLoad

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

UpdateLoad sets the load of `host` to the given `load`.

func (*Consistent) VirtualNodes added in v1.13.0

func (c *Consistent) VirtualNodes() map[uint64]string

type ConsistentHashTables

type ConsistentHashTables struct {
	Version string
	Entries map[string]*Consistent
}

ConsistentHashTables is a table holding a map of consistent hashes with a given version.

type Host

type Host struct {
	Name  string
	Port  int64
	Load  int64
	AppID string
}

Host represents a host of stateful entities with a given name, id, port and load.

func NewHost

func NewHost(name, id string, load int64, port int64) *Host

NewHost returns a new host.

type VirtualNodesCache added in v1.13.0

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

VirtualNodesCache data example:

100 -> (the replication factor)
	"192.168.1.89:62362" -> ["10056481384176189962", "10100244799470048543"...] (100 elements)
	"192.168.1.89:62362" -> ["10056481384176189962", "10100244799470048543"...]
200 ->
	"192.168.1.89:62362" -> ["10056481384176189962", "10100244799470048543"...] (200 elements)
	"192.168.1.89:62362" -> ["10056481384176189962", "10100244799470048543"...]

func NewVirtualNodesCache added in v1.13.0

func NewVirtualNodesCache() *VirtualNodesCache

func (*VirtualNodesCache) GetHashes added in v1.13.0

func (hc *VirtualNodesCache) GetHashes(replicationFactor int64, host string) []uint64

GetHashes retrieves the hashes for the given replication factor and IP address.

Jump to

Keyboard shortcuts

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