pax_global_header00006660000000000000000000000064144435152250014517gustar00rootroot0000000000000052 comment=a3f0d18e5ea668de31ad6f80c6395fd0cffcb51f golang-github-serialx-hashring-0.0~git20190422.8b29126/000077500000000000000000000000001444351522500220155ustar00rootroot00000000000000golang-github-serialx-hashring-0.0~git20190422.8b29126/LICENSE000066400000000000000000000020701444351522500230210ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 Sung-jin Hong Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-serialx-hashring-0.0~git20190422.8b29126/README.md000066400000000000000000000035651444351522500233050ustar00rootroot00000000000000hashring ============================ 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 in Go with the extra methods to add and remove nodes. Using ============================ Importing :: ```go import "github.com/serialx/hashring" ``` Basic example usage :: ```go 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. ```go 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 :: ```go 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 :: ```go 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") ``` golang-github-serialx-hashring-0.0~git20190422.8b29126/hashring.go000066400000000000000000000132411444351522500241500ustar00rootroot00000000000000package hashring import ( "crypto/md5" "math" "sort" "strconv" ) type HashKey uint32 type HashKeyOrder []HashKey func (h HashKeyOrder) Len() int { return len(h) } func (h HashKeyOrder) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h HashKeyOrder) Less(i, j int) bool { return h[i] < h[j] } type HashRing struct { ring map[HashKey]string sortedKeys []HashKey nodes []string weights map[string]int } func New(nodes []string) *HashRing { hashRing := &HashRing{ ring: make(map[HashKey]string), sortedKeys: make([]HashKey, 0), nodes: nodes, weights: make(map[string]int), } hashRing.generateCircle() return hashRing } func NewWithWeights(weights map[string]int) *HashRing { nodes := make([]string, 0, len(weights)) for node, _ := range weights { nodes = append(nodes, node) } hashRing := &HashRing{ ring: make(map[HashKey]string), sortedKeys: make([]HashKey, 0), nodes: nodes, weights: weights, } hashRing.generateCircle() return hashRing } func (h *HashRing) Size() int { return len(h.nodes) } func (h *HashRing) UpdateWithWeights(weights map[string]int) { nodesChgFlg := false if len(weights) != len(h.weights) { nodesChgFlg = true } else { for node, newWeight := range weights { oldWeight, ok := h.weights[node] if !ok || oldWeight != newWeight { nodesChgFlg = true break } } } if nodesChgFlg { newhring := NewWithWeights(weights) h.weights = newhring.weights h.nodes = newhring.nodes h.ring = newhring.ring h.sortedKeys = newhring.sortedKeys } } func (h *HashRing) generateCircle() { totalWeight := 0 for _, node := range h.nodes { if weight, ok := h.weights[node]; ok { totalWeight += weight } else { totalWeight += 1 h.weights[node] = 1 } } for _, node := range h.nodes { weight := h.weights[node] factor := math.Floor(float64(40*len(h.nodes)*weight) / float64(totalWeight)) for j := 0; j < int(factor); j++ { nodeKey := node + "-" + strconv.FormatInt(int64(j), 10) bKey := hashDigest(nodeKey) for i := 0; i < 3; i++ { key := hashVal(bKey[i*4 : i*4+4]) h.ring[key] = node h.sortedKeys = append(h.sortedKeys, key) } } } sort.Sort(HashKeyOrder(h.sortedKeys)) } func (h *HashRing) GetNode(stringKey string) (node string, ok bool) { pos, ok := h.GetNodePos(stringKey) if !ok { return "", false } return h.ring[h.sortedKeys[pos]], true } func (h *HashRing) GetNodePos(stringKey string) (pos int, ok bool) { if len(h.ring) == 0 { return 0, false } key := h.GenKey(stringKey) nodes := h.sortedKeys pos = sort.Search(len(nodes), func(i int) bool { return nodes[i] > key }) if pos == len(nodes) { // Wrap the search, should return first node return 0, true } else { return pos, true } } func (h *HashRing) GenKey(key string) HashKey { bKey := hashDigest(key) return hashVal(bKey[0:4]) } func (h *HashRing) GetNodes(stringKey string, size int) (nodes []string, ok bool) { pos, ok := h.GetNodePos(stringKey) if !ok { return nil, false } if size > len(h.nodes) { return nil, false } returnedValues := make(map[string]bool, size) //mergedSortedKeys := append(h.sortedKeys[pos:], h.sortedKeys[:pos]...) resultSlice := make([]string, 0, size) for i := pos; i < pos+len(h.sortedKeys); i++ { key := h.sortedKeys[i%len(h.sortedKeys)] val := h.ring[key] if !returnedValues[val] { returnedValues[val] = true resultSlice = append(resultSlice, val) } if len(returnedValues) == size { break } } return resultSlice, len(resultSlice) == size } func (h *HashRing) AddNode(node string) *HashRing { return h.AddWeightedNode(node, 1) } func (h *HashRing) AddWeightedNode(node string, weight int) *HashRing { if weight <= 0 { return h } if _, ok := h.weights[node]; ok { return h } nodes := make([]string, len(h.nodes), len(h.nodes)+1) copy(nodes, h.nodes) nodes = append(nodes, node) weights := make(map[string]int) for eNode, eWeight := range h.weights { weights[eNode] = eWeight } weights[node] = weight hashRing := &HashRing{ ring: make(map[HashKey]string), sortedKeys: make([]HashKey, 0), nodes: nodes, weights: weights, } hashRing.generateCircle() return hashRing } func (h *HashRing) UpdateWeightedNode(node string, weight int) *HashRing { if weight <= 0 { return h } /* node is not need to update for node is not existed or weight is not changed */ if oldWeight, ok := h.weights[node]; (!ok) || (ok && oldWeight == weight) { return h } nodes := make([]string, len(h.nodes), len(h.nodes)) copy(nodes, h.nodes) weights := make(map[string]int) for eNode, eWeight := range h.weights { weights[eNode] = eWeight } weights[node] = weight hashRing := &HashRing{ ring: make(map[HashKey]string), sortedKeys: make([]HashKey, 0), nodes: nodes, weights: weights, } hashRing.generateCircle() return hashRing } func (h *HashRing) RemoveNode(node string) *HashRing { /* if node isn't exist in hashring, don't refresh hashring */ if _, ok := h.weights[node]; !ok { return h } nodes := make([]string, 0) for _, eNode := range h.nodes { if eNode != node { nodes = append(nodes, eNode) } } weights := make(map[string]int) for eNode, eWeight := range h.weights { if eNode != node { weights[eNode] = eWeight } } hashRing := &HashRing{ ring: make(map[HashKey]string), sortedKeys: make([]HashKey, 0), nodes: nodes, weights: weights, } hashRing.generateCircle() return hashRing } func hashVal(bKey []byte) HashKey { return ((HashKey(bKey[3]) << 24) | (HashKey(bKey[2]) << 16) | (HashKey(bKey[1]) << 8) | (HashKey(bKey[0]))) } func hashDigest(key string) [md5.Size]byte { return md5.Sum([]byte(key)) } golang-github-serialx-hashring-0.0~git20190422.8b29126/hashring_test.go000066400000000000000000000411751444351522500252160ustar00rootroot00000000000000package hashring import ( "reflect" "testing" ) func expectNode(t *testing.T, hashRing *HashRing, key string, expectedNode string) { node, ok := hashRing.GetNode(key) if !ok || node != expectedNode { t.Error("GetNode(", key, ") expected", expectedNode, "but got", node) } } func expectNodes(t *testing.T, hashRing *HashRing, key string, expectedNodes []string) { nodes, ok := hashRing.GetNodes(key, 2) sliceEquality := reflect.DeepEqual(nodes, expectedNodes) if !ok || !sliceEquality { t.Error("GetNodes(", key, ") expected", expectedNodes, "but got", nodes) } } func expectWeights(t *testing.T, hashRing *HashRing, expectedWeights map[string]int) { weightsEquality := reflect.DeepEqual(hashRing.weights, expectedWeights) if !weightsEquality { t.Error("Weights expected", expectedWeights, "but got", hashRing.weights) } } func expectNodesABC(t *testing.T, hashRing *HashRing) { // Python hash_ring module test case expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test1", "b") expectNode(t, hashRing, "test2", "b") expectNode(t, hashRing, "test3", "c") expectNode(t, hashRing, "test4", "c") expectNode(t, hashRing, "test5", "a") expectNode(t, hashRing, "aaaa", "b") expectNode(t, hashRing, "bbbb", "a") } func expectNodeRangesABC(t *testing.T, hashRing *HashRing) { expectNodes(t, hashRing, "test", []string{"a", "b"}) expectNodes(t, hashRing, "test", []string{"a", "b"}) expectNodes(t, hashRing, "test1", []string{"b", "c"}) expectNodes(t, hashRing, "test2", []string{"b", "a"}) expectNodes(t, hashRing, "test3", []string{"c", "a"}) expectNodes(t, hashRing, "test4", []string{"c", "b"}) expectNodes(t, hashRing, "test5", []string{"a", "c"}) expectNodes(t, hashRing, "aaaa", []string{"b", "a"}) expectNodes(t, hashRing, "bbbb", []string{"a", "b"}) } func expectNodesABCD(t *testing.T, hashRing *HashRing) { // Somehow adding d does not load balance these keys... expectNodesABC(t, hashRing) } func TestNew(t *testing.T) { nodes := []string{"a", "b", "c"} hashRing := New(nodes) expectNodesABC(t, hashRing) expectNodeRangesABC(t, hashRing) } func TestNewEmpty(t *testing.T) { nodes := []string{} hashRing := New(nodes) node, ok := hashRing.GetNode("test") if ok || node != "" { t.Error("GetNode(test) expected (\"\", false) but got (", node, ",", ok, ")") } nodes, rok := hashRing.GetNodes("test", 2) if rok || !(len(nodes) == 0) { t.Error("GetNode(test) expected ( [], false ) but got (", nodes, ",", rok, ")") } } func TestForMoreNodes(t *testing.T) { nodes := []string{"a", "b", "c"} hashRing := New(nodes) nodes, ok := hashRing.GetNodes("test", 5) if ok || !(len(nodes) == 0) { t.Error("GetNode(test) expected ( [], false ) but got (", nodes, ",", ok, ")") } } func TestForEqualNodes(t *testing.T) { nodes := []string{"a", "b", "c"} hashRing := New(nodes) nodes, ok := hashRing.GetNodes("test", 3) if !ok && (len(nodes) == 3) { t.Error("GetNode(test) expected ( [a b c], true ) but got (", nodes, ",", ok, ")") } } func TestNewSingle(t *testing.T) { nodes := []string{"a"} hashRing := New(nodes) expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test1", "a") expectNode(t, hashRing, "test2", "a") expectNode(t, hashRing, "test3", "a") // This triggers the edge case where sortedKey search resulting in not found expectNode(t, hashRing, "test14", "a") expectNode(t, hashRing, "test15", "a") expectNode(t, hashRing, "test16", "a") expectNode(t, hashRing, "test17", "a") expectNode(t, hashRing, "test18", "a") expectNode(t, hashRing, "test19", "a") expectNode(t, hashRing, "test20", "a") } func TestNewWeighted(t *testing.T) { weights := make(map[string]int) weights["a"] = 1 weights["b"] = 2 weights["c"] = 1 hashRing := NewWithWeights(weights) expectNode(t, hashRing, "test", "b") expectNode(t, hashRing, "test", "b") expectNode(t, hashRing, "test1", "b") expectNode(t, hashRing, "test2", "b") expectNode(t, hashRing, "test3", "c") expectNode(t, hashRing, "test4", "b") expectNode(t, hashRing, "test5", "b") expectNode(t, hashRing, "aaaa", "b") expectNode(t, hashRing, "bbbb", "a") expectNodes(t, hashRing, "test", []string{"b", "a"}) } func TestRemoveNode(t *testing.T) { nodes := []string{"a", "b", "c"} hashRing := New(nodes) hashRing = hashRing.RemoveNode("b") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test1", "c") // Migrated to c from b expectNode(t, hashRing, "test2", "a") // Migrated to a from b expectNode(t, hashRing, "test3", "c") expectNode(t, hashRing, "test4", "c") expectNode(t, hashRing, "test5", "a") expectNode(t, hashRing, "aaaa", "a") // Migrated to a from b expectNode(t, hashRing, "bbbb", "a") expectNodes(t, hashRing, "test", []string{"a", "c"}) } func TestAddNode(t *testing.T) { nodes := []string{"a", "c"} hashRing := New(nodes) hashRing = hashRing.AddNode("b") expectNodesABC(t, hashRing) defaultWeights := map[string]int{ "a": 1, "b": 1, "c": 1, } expectWeights(t, hashRing, defaultWeights) } func TestAddNode2(t *testing.T) { nodes := []string{"a", "c"} hashRing := New(nodes) hashRing = hashRing.AddNode("b") hashRing = hashRing.AddNode("b") expectNodesABC(t, hashRing) expectNodeRangesABC(t, hashRing) } func TestAddNode3(t *testing.T) { nodes := []string{"a", "b", "c"} hashRing := New(nodes) hashRing = hashRing.AddNode("d") // Somehow adding d does not load balance these keys... expectNodesABCD(t, hashRing) hashRing = hashRing.AddNode("e") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test1", "b") expectNode(t, hashRing, "test2", "b") expectNode(t, hashRing, "test3", "c") expectNode(t, hashRing, "test4", "c") expectNode(t, hashRing, "test5", "a") expectNode(t, hashRing, "aaaa", "b") expectNode(t, hashRing, "bbbb", "e") // Migrated to e from a expectNodes(t, hashRing, "test", []string{"a", "b"}) hashRing = hashRing.AddNode("f") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test1", "b") expectNode(t, hashRing, "test2", "f") // Migrated to f from b expectNode(t, hashRing, "test3", "f") // Migrated to f from c expectNode(t, hashRing, "test4", "c") expectNode(t, hashRing, "test5", "f") // Migrated to f from a expectNode(t, hashRing, "aaaa", "b") expectNode(t, hashRing, "bbbb", "e") expectNodes(t, hashRing, "test", []string{"a", "b"}) } func TestDuplicateNodes(t *testing.T) { nodes := []string{"a", "a", "a", "a", "b"} hashRing := New(nodes) expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test1", "b") expectNode(t, hashRing, "test2", "b") expectNode(t, hashRing, "test3", "a") expectNode(t, hashRing, "test4", "b") expectNode(t, hashRing, "test5", "a") expectNode(t, hashRing, "aaaa", "b") expectNode(t, hashRing, "bbbb", "a") } func TestAddWeightedNode(t *testing.T) { nodes := []string{"a", "c"} hashRing := New(nodes) hashRing = hashRing.AddWeightedNode("b", 0) hashRing = hashRing.AddWeightedNode("b", 2) hashRing = hashRing.AddWeightedNode("b", 2) expectNode(t, hashRing, "test", "b") expectNode(t, hashRing, "test", "b") expectNode(t, hashRing, "test1", "b") expectNode(t, hashRing, "test2", "b") expectNode(t, hashRing, "test3", "c") expectNode(t, hashRing, "test4", "b") expectNode(t, hashRing, "test5", "b") expectNode(t, hashRing, "aaaa", "b") expectNode(t, hashRing, "bbbb", "a") expectNodes(t, hashRing, "test", []string{"b", "a"}) } func TestUpdateWeightedNode(t *testing.T) { nodes := []string{"a", "c"} hashRing := New(nodes) hashRing = hashRing.AddWeightedNode("b", 1) hashRing = hashRing.UpdateWeightedNode("b", 2) hashRing = hashRing.UpdateWeightedNode("b", 2) hashRing = hashRing.UpdateWeightedNode("b", 0) hashRing = hashRing.UpdateWeightedNode("d", 2) expectNode(t, hashRing, "test", "b") expectNode(t, hashRing, "test", "b") expectNode(t, hashRing, "test1", "b") expectNode(t, hashRing, "test2", "b") expectNode(t, hashRing, "test3", "c") expectNode(t, hashRing, "test4", "b") expectNode(t, hashRing, "test5", "b") expectNode(t, hashRing, "aaaa", "b") expectNode(t, hashRing, "bbbb", "a") expectNodes(t, hashRing, "test", []string{"b", "a"}) } func TestRemoveAddNode(t *testing.T) { nodes := []string{"a", "b", "c"} hashRing := New(nodes) expectNodesABC(t, hashRing) expectNodeRangesABC(t, hashRing) hashRing = hashRing.RemoveNode("b") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test1", "c") // Migrated to c from b expectNode(t, hashRing, "test2", "a") // Migrated to a from b expectNode(t, hashRing, "test3", "c") expectNode(t, hashRing, "test4", "c") expectNode(t, hashRing, "test5", "a") expectNode(t, hashRing, "aaaa", "a") // Migrated to a from b expectNode(t, hashRing, "bbbb", "a") expectNodes(t, hashRing, "test", []string{"a", "c"}) expectNodes(t, hashRing, "test", []string{"a", "c"}) expectNodes(t, hashRing, "test1", []string{"c", "a"}) expectNodes(t, hashRing, "test2", []string{"a", "c"}) expectNodes(t, hashRing, "test3", []string{"c", "a"}) expectNodes(t, hashRing, "test4", []string{"c", "a"}) expectNodes(t, hashRing, "test5", []string{"a", "c"}) expectNodes(t, hashRing, "aaaa", []string{"a", "c"}) expectNodes(t, hashRing, "bbbb", []string{"a", "c"}) hashRing = hashRing.AddNode("b") expectNodesABC(t, hashRing) expectNodeRangesABC(t, hashRing) } func TestRemoveAddWeightedNode(t *testing.T) { weights := make(map[string]int) weights["a"] = 1 weights["b"] = 2 weights["c"] = 1 hashRing := NewWithWeights(weights) expectWeights(t, hashRing, weights) expectNode(t, hashRing, "test", "b") expectNode(t, hashRing, "test", "b") expectNode(t, hashRing, "test1", "b") expectNode(t, hashRing, "test2", "b") expectNode(t, hashRing, "test3", "c") expectNode(t, hashRing, "test4", "b") expectNode(t, hashRing, "test5", "b") expectNode(t, hashRing, "aaaa", "b") expectNode(t, hashRing, "bbbb", "a") expectNodes(t, hashRing, "test", []string{"b", "a"}) expectNodes(t, hashRing, "test", []string{"b", "a"}) expectNodes(t, hashRing, "test1", []string{"b", "c"}) expectNodes(t, hashRing, "test2", []string{"b", "a"}) expectNodes(t, hashRing, "test3", []string{"c", "b"}) expectNodes(t, hashRing, "test4", []string{"b", "a"}) expectNodes(t, hashRing, "test5", []string{"b", "a"}) expectNodes(t, hashRing, "aaaa", []string{"b", "a"}) expectNodes(t, hashRing, "bbbb", []string{"a", "b"}) hashRing = hashRing.RemoveNode("c") delete(weights, "c") expectWeights(t, hashRing, weights) expectNode(t, hashRing, "test", "b") expectNode(t, hashRing, "test", "b") expectNode(t, hashRing, "test1", "b") expectNode(t, hashRing, "test2", "b") expectNode(t, hashRing, "test3", "b") // Migrated to b from c expectNode(t, hashRing, "test4", "b") expectNode(t, hashRing, "test5", "b") expectNode(t, hashRing, "aaaa", "b") expectNode(t, hashRing, "bbbb", "a") expectNodes(t, hashRing, "test", []string{"b", "a"}) expectNodes(t, hashRing, "test", []string{"b", "a"}) expectNodes(t, hashRing, "test1", []string{"b", "a"}) expectNodes(t, hashRing, "test2", []string{"b", "a"}) expectNodes(t, hashRing, "test3", []string{"b", "a"}) expectNodes(t, hashRing, "test4", []string{"b", "a"}) expectNodes(t, hashRing, "test5", []string{"b", "a"}) expectNodes(t, hashRing, "aaaa", []string{"b", "a"}) expectNodes(t, hashRing, "bbbb", []string{"a", "b"}) } func TestAddRemoveNode(t *testing.T) { nodes := []string{"a", "b", "c"} hashRing := New(nodes) hashRing = hashRing.AddNode("d") // Somehow adding d does not load balance these keys... expectNodesABCD(t, hashRing) expectNodes(t, hashRing, "test", []string{"a", "b"}) expectNodes(t, hashRing, "test", []string{"a", "b"}) expectNodes(t, hashRing, "test1", []string{"b", "d"}) expectNodes(t, hashRing, "test2", []string{"b", "d"}) expectNodes(t, hashRing, "test3", []string{"c", "d"}) expectNodes(t, hashRing, "test4", []string{"c", "b"}) expectNodes(t, hashRing, "test5", []string{"a", "d"}) expectNodes(t, hashRing, "aaaa", []string{"b", "a"}) expectNodes(t, hashRing, "bbbb", []string{"a", "b"}) hashRing = hashRing.AddNode("e") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test1", "b") expectNode(t, hashRing, "test2", "b") expectNode(t, hashRing, "test3", "c") expectNode(t, hashRing, "test4", "c") expectNode(t, hashRing, "test5", "a") expectNode(t, hashRing, "aaaa", "b") expectNode(t, hashRing, "bbbb", "e") // Migrated to e from a expectNodes(t, hashRing, "test", []string{"a", "b"}) expectNodes(t, hashRing, "test", []string{"a", "b"}) expectNodes(t, hashRing, "test1", []string{"b", "d"}) expectNodes(t, hashRing, "test2", []string{"b", "d"}) expectNodes(t, hashRing, "test3", []string{"c", "e"}) expectNodes(t, hashRing, "test4", []string{"c", "b"}) expectNodes(t, hashRing, "test5", []string{"a", "e"}) expectNodes(t, hashRing, "aaaa", []string{"b", "e"}) expectNodes(t, hashRing, "bbbb", []string{"e", "a"}) hashRing = hashRing.AddNode("f") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test1", "b") expectNode(t, hashRing, "test2", "f") // Migrated to f from b expectNode(t, hashRing, "test3", "f") // Migrated to f from c expectNode(t, hashRing, "test4", "c") expectNode(t, hashRing, "test5", "f") // Migrated to f from a expectNode(t, hashRing, "aaaa", "b") expectNode(t, hashRing, "bbbb", "e") expectNodes(t, hashRing, "test", []string{"a", "b"}) expectNodes(t, hashRing, "test", []string{"a", "b"}) expectNodes(t, hashRing, "test1", []string{"b", "d"}) expectNodes(t, hashRing, "test2", []string{"f", "b"}) expectNodes(t, hashRing, "test3", []string{"f", "c"}) expectNodes(t, hashRing, "test4", []string{"c", "b"}) expectNodes(t, hashRing, "test5", []string{"f", "a"}) expectNodes(t, hashRing, "aaaa", []string{"b", "e"}) expectNodes(t, hashRing, "bbbb", []string{"e", "f"}) hashRing = hashRing.RemoveNode("e") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test", "a") expectNode(t, hashRing, "test1", "b") expectNode(t, hashRing, "test2", "f") expectNode(t, hashRing, "test3", "f") expectNode(t, hashRing, "test4", "c") expectNode(t, hashRing, "test5", "f") expectNode(t, hashRing, "aaaa", "b") expectNode(t, hashRing, "bbbb", "f") // Migrated to f from e expectNodes(t, hashRing, "test", []string{"a", "b"}) expectNodes(t, hashRing, "test", []string{"a", "b"}) expectNodes(t, hashRing, "test1", []string{"b", "d"}) expectNodes(t, hashRing, "test2", []string{"f", "b"}) expectNodes(t, hashRing, "test3", []string{"f", "c"}) expectNodes(t, hashRing, "test4", []string{"c", "b"}) expectNodes(t, hashRing, "test5", []string{"f", "a"}) expectNodes(t, hashRing, "aaaa", []string{"b", "a"}) expectNodes(t, hashRing, "bbbb", []string{"f", "a"}) hashRing = hashRing.RemoveNode("f") expectNodesABCD(t, hashRing) expectNodes(t, hashRing, "test", []string{"a", "b"}) expectNodes(t, hashRing, "test", []string{"a", "b"}) expectNodes(t, hashRing, "test1", []string{"b", "d"}) expectNodes(t, hashRing, "test2", []string{"b", "d"}) expectNodes(t, hashRing, "test3", []string{"c", "d"}) expectNodes(t, hashRing, "test4", []string{"c", "b"}) expectNodes(t, hashRing, "test5", []string{"a", "d"}) expectNodes(t, hashRing, "aaaa", []string{"b", "a"}) expectNodes(t, hashRing, "bbbb", []string{"a", "b"}) hashRing = hashRing.RemoveNode("d") expectNodesABC(t, hashRing) expectNodeRangesABC(t, hashRing) } func BenchmarkHashes(b *testing.B) { nodes := []string{"a", "b", "c", "d", "e", "f", "g"} hashRing := New(nodes) tt := []struct { key string nodes []string }{ {"test", []string{"a", "b"}}, {"test", []string{"a", "b"}}, {"test1", []string{"b", "d"}}, {"test2", []string{"f", "b"}}, {"test3", []string{"f", "c"}}, {"test4", []string{"c", "b"}}, {"test5", []string{"f", "a"}}, {"aaaa", []string{"b", "a"}}, {"bbbb", []string{"f", "a"}}, } b.ResetTimer() for i := 0; i < b.N; i++ { o := tt[i%len(tt)] hashRing.GetNodes(o.key, 2) } } func BenchmarkHashesSingle(b *testing.B) { nodes := []string{"a", "b", "c", "d", "e", "f", "g"} hashRing := New(nodes) tt := []struct { key string nodes []string }{ {"test", []string{"a", "b"}}, {"test", []string{"a", "b"}}, {"test1", []string{"b", "d"}}, {"test2", []string{"f", "b"}}, {"test3", []string{"f", "c"}}, {"test4", []string{"c", "b"}}, {"test5", []string{"f", "a"}}, {"aaaa", []string{"b", "a"}}, {"bbbb", []string{"f", "a"}}, } b.ResetTimer() for i := 0; i < b.N; i++ { o := tt[i%len(tt)] hashRing.GetNode(o.key) } } func BenchmarkNew(b *testing.B) { nodes := []string{"a", "b", "c", "d", "e", "f", "g"} b.ResetTimer() for i := 0; i < b.N; i++ { _ = New(nodes) } }