consistent

package module
v0.0.0-...-9f3bf9f Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2020 License: MIT Imports: 6 Imported by: 0

README

consistent

Go Report Card GoDoc 中文版

Overview

This package implements consistent hash algorithm using hash ring, it is thread-safe, and can be used concurrently. You can add node with weight, update node's weight and delete node in hash ring. This package doesn't require third-party dependency.

Install

go get -u -v github.com/liangyaopei/consistent

Example

func TestNew(t *testing.T) {
	nodes := []string{
		"185.199.110.153",
		"185.199.110.154",
		"185.199.110.155",
	}
	ring := consistent.New(nodes, consistent.DefaultHashFn)

	keys := []string{"January", "February", "March", "April", "May", "June",
		"July", "August", "September", "October", "November", "December"}

	nodeAdd := "185.199.110.156"
	nodeDel := "185.199.110.153"

	oriDis := make(map[string]string)
	for _, key := range keys {
		node := ring.LocateKeyStr(key)
		oriDis[key] = node
	}

	ring.AddNodeWeight(nodeAdd, 1)
	addDes := make(map[string]string)
	for _, key := range keys {
		node := ring.LocateKeyStr(key)
		addDes[key] = node
	}

	ring.DelNode(nodeDel)
	delDes := make(map[string]string)
	for _, key := range keys {
		node := ring.LocateKeyStr(key)
		delDes[key] = node
	}

	t.Logf("adding node:%s,del node:%s", nodeAdd, nodeDel)

	for _, key := range keys {
		t.Logf("key:%15s,ori:%15s,add:%15s,del:%15s", key, oriDis[key], addDes[key], delDes[key])
	}

	for node, weight := range ring.GetNodeWeight() {
		t.Logf("node:%s,weight:%d", node, weight)
	}

	for node, keys := range ring.GetHashRing() {
		t.Logf("node:%s,keys:%v", node, keys)
	}
}

Hash Function

To make hashed key distribute more uniformly, DefaultHashFn hash the key twice.

func DefaultHashFn(data []byte) uint64 {
	fn := fnv.New64a()
	sum := fn.Sum(data)
	fn.Reset()
	fn.Write(sum)
	return fn.Sum64()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultHashFn

func DefaultHashFn(data []byte) uint64

DefaultHashFn return the fnv hash function hashed twice to make the distribution more uniformly

Types

type HashFn

type HashFn func([]byte) uint64

HashFn is the definition of hashing byte array to uin64

type HashRing

type HashRing struct {
	// contains filtered or unexported fields
}

HashRing represents hash ring

func New

func New(nodes []string, hashFn HashFn) *HashRing

New returns a hash ring with customized hash function

func NewWithWeight

func NewWithWeight(nodeWeight map[string]int, hashFn HashFn) *HashRing

NewWithWeight returns a hash ring with customized weight

func (*HashRing) AddNode

func (r *HashRing) AddNode(node string) *HashRing

AddNode add new node with weight 1

func (*HashRing) AddNodeWeight

func (r *HashRing) AddNodeWeight(node string, weight int) *HashRing

AddNodeWeight add new node with weight

func (*HashRing) DelNode

func (r *HashRing) DelNode(node string) *HashRing

DelNode deletes node in hash ring

func (*HashRing) GetHashRing

func (r *HashRing) GetHashRing() map[string][]uint64

GetHashRing return hash ring string is node, []uint64 is corresponding key

func (*HashRing) GetNodeWeight

func (r *HashRing) GetNodeWeight() map[string]int

GetNodeWeight returns all node and corresponding weight in hash ring

func (*HashRing) GetNodes

func (r *HashRing) GetNodes() []string

GetNodes returns all nodes in hash ring

func (*HashRing) LocateKey

func (r *HashRing) LocateKey(data []byte) string

LocateKey locates the position of byte array data in hash ring

func (*HashRing) LocateKeyStr

func (r *HashRing) LocateKeyStr(data string) string

LocateKeyStr locates the position of string data in hash ring

func (*HashRing) UpdateNodeWeight

func (r *HashRing) UpdateNodeWeight(node string, weight int) *HashRing

UpdateNodeWeight update node' weight it can increase or decrease the node's replication

Jump to

Keyboard shortcuts

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