hashring

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

README

Dragonfly Hashring

GitHub release LICENSE

Consistent hash ring with virtual nodes shared by the Dragonfly scheduler, the Dragonfly client and the Dragonfly SDKs. Positions are SipHash-2-4 values with a zero key, so the Go module and the Rust crate place keys bit-for-bit identically and every component selects the same seed peer or scheduler for the same task.

Packages

Package Description
d7y.io/hashring Go module for scheduler and SDK
dragonfly-hashring Rust crate for client and SDK

Usage

Go:

import "d7y.io/hashring"

ring := hashring.New()
ring.Add("default-seed-client-0")
ring.Add("default-seed-client-1")

vnode, _ := ring.Get(taskID)
name := vnode.Name()

Rust:

use dragonfly_hashring::VNodeHashRing;

let mut ring = VNodeHashRing::new();
ring.add("default-seed-client-0".to_string());
ring.add("default-seed-client-1".to_string());

let name = ring.get(&task_id).unwrap().name();

Both constructors use DefaultVNodesPerNode virtual nodes per node, pass hashring.WithVNodesPerNode(n) or call VNodeHashRing::with_vnodes_per_node(n) to override it.

Documentation

You can find the full documentation on the d7y.io.

Community

Join the conversation and help the community grow. Here are the ways to get involved:

Contributing

You should check out our CONTRIBUTING and develop the project together.

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

View Source
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

func WithVNodesPerNode(vnodesPerNode int) Option

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.

func (VNode) Name

func (v VNode) Name() string

Name returns the physical node name associated with this virtual node.

func (VNode) String

func (v VNode) String() string

String formats the virtual node as "name|id".

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.

Jump to

Keyboard shortcuts

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