hashring

package
v0.0.0-...-3c22e23 Latest Latest
Warning

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

Go to latest
Published: May 10, 2017 License: Apache-2.0, MIT Imports: 5 Imported by: 1

README

hashring

Implements consistent hashing that can be used when the number of server nodes can increase or decrease (like in memcached). The hashing ring is built using the same algorithm as libketama.

This is a port of Python hash_ring library https://pypi.python.org/pypi/hash_ring/ in Go with the extra methods to add and remove nodes.

Using

Importing ::

import "github.com/crackcell/kihaadhoo/collections/hashring"

Basic example usage ::

memcacheServers := []string{"192.168.0.246:11212",
                            "192.168.0.247:11212",
                            "192.168.0.249:11212"}

ring := hashring.New(memcacheServers)
server, _ := ring.GetNode("my_key")

To fulfill replication requirements, you can also get a list of servers that should store your key.

serversInRing := []string{"192.168.0.246:11212",
                          "192.168.0.247:11212",
                          "192.168.0.248:11212",
                          "192.168.0.249:11212",
                          "192.168.0.250:11212",
                          "192.168.0.251:11212",
                          "192.168.0.252:11212"}

replicaCount := 3
ring := hashring.New(serversInRing)
server, _ := ring.GetNodes("my_key", replicaCount)

Using weights example ::

weights := make(map[string]int)
weights["192.168.0.246:11212"] = 1
weights["192.168.0.247:11212"] = 2
weights["192.168.0.249:11212"] = 1

ring := hashring.NewWithWeights(weights)
server, _ := ring.GetNode("my_key")

Adding and removing nodes example ::

memcacheServers := []string{"192.168.0.246:11212",
                            "192.168.0.247:11212",
                            "192.168.0.249:11212"}

ring := hashring.New(memcacheServers)
ring = ring.RemoveNode("192.168.0.246:11212")
ring = ring.AddNode("192.168.0.250:11212")
server := ring.GetNode("my_key")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotEnoughNodes = errors.New("not enough nodes")
)

Functions

This section is empty.

Types

type HashKey

type HashKey uint32

type HashKeyOrder

type HashKeyOrder []HashKey

func (HashKeyOrder) Len

func (h HashKeyOrder) Len() int

func (HashKeyOrder) Less

func (h HashKeyOrder) Less(i, j int) bool

func (HashKeyOrder) Swap

func (h HashKeyOrder) Swap(i, j int)

type HashRing

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

func New

func New(nodes []string) *HashRing

func NewWithWeights

func NewWithWeights(weights map[string]int) *HashRing

func (*HashRing) AddNode

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

func (*HashRing) AddWeightedNode

func (h *HashRing) AddWeightedNode(node string, weight int) *HashRing

func (*HashRing) GenKey

func (h *HashRing) GenKey(key string) HashKey

func (*HashRing) GetNode

func (h *HashRing) GetNode(stringKey string) (node string, err error)

func (*HashRing) GetNodePos

func (h *HashRing) GetNodePos(stringKey string) (pos int, err error)

func (*HashRing) GetNodes

func (h *HashRing) GetNodes(stringKey string, size int) (nodes []string, err error)

func (*HashRing) RemoveNode

func (h *HashRing) RemoveNode(node string) *HashRing

func (*HashRing) Size

func (h *HashRing) Size() int

func (*HashRing) UpdateWeightedNode

func (h *HashRing) UpdateWeightedNode(node string, weight int) *HashRing

func (*HashRing) UpdateWithWeights

func (h *HashRing) UpdateWithWeights(weights map[string]int)

Jump to

Keyboard shortcuts

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