Documentation
¶
Overview ¶
Package hashring provides a consistent hash ring with virtual nodes whose placement is bit-for-bit compatible with the dragonfly-hashring crate, so the dragonfly scheduler, the dragonfly client and the dragonfly-sdk select the same node for the same key.
Index ¶
Constants ¶
const DefaultVNodesPerNode = 512
DefaultVNodesPerNode is the default number of virtual nodes of each node on the ring, shared by the seed peer selectors of dragonfly and dragonfly-sdk.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶ added in v0.2.0
type Option func(r *VNodeHashRing)
Option is a functional option for configuring the hash ring.
func WithVNodesPerNode ¶ added in v0.2.0
WithVNodesPerNode set the number of virtual nodes of each node on the ring.
type VNode ¶
type VNode struct {
// contains filtered or unexported fields
}
VNode is a virtual node on the consistent hash ring. Each physical node is represented by multiple virtual nodes to better balance key distribution.
type VNodeHashRing ¶
type VNodeHashRing struct {
// contains filtered or unexported fields
}
VNodeHashRing is a consistent hash ring that uses virtual nodes to improve key distribution. When a physical node is added, vnodesPerNode virtual nodes are inserted into the ring. It is not safe for concurrent use.
func New ¶
func New(options ...Option) *VNodeHashRing
New returns a new hash ring with DefaultVNodesPerNode virtual nodes per node.
func (*VNodeHashRing) Add ¶
func (r *VNodeHashRing) Add(name string)
Add adds vnodesPerNode virtual nodes of the named node to the ring. The new virtual nodes are sorted and merged into the ring from the back, which costs linear time in the ring size instead of sorting the whole ring, and keeps the existing virtual nodes before the new ones with the same key.
func (*VNodeHashRing) Get ¶
func (r *VNodeHashRing) Get(key string) (VNode, bool)
Get returns the virtual node responsible for the key, and false when the ring is empty.
func (*VNodeHashRing) GetWithReplicas ¶
func (r *VNodeHashRing) GetWithReplicas(key string, replicas int) []VNode
GetWithReplicas returns the virtual node responsible for the key along with the next replicas virtual nodes clockwise on the ring, and nil when the ring is empty. When replicas is larger than the length of the ring, the result shrinks to the entire ring.
func (*VNodeHashRing) IsEmpty ¶
func (r *VNodeHashRing) IsEmpty() bool
IsEmpty reports whether the ring has no virtual nodes.
func (*VNodeHashRing) Len ¶
func (r *VNodeHashRing) Len() int
Len returns the number of virtual nodes on the ring.