forked from coreos/torus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ketama.go
109 lines (94 loc) · 2.34 KB
/
ketama.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package ring
import (
"errors"
"fmt"
"reflect"
"github.com/coreos/torus"
"github.com/coreos/torus/models"
"github.com/serialx/hashring"
)
type ketama struct {
version int
rep int
peers torus.PeerInfoList
ring *hashring.HashRing
}
func init() {
registerRing(Ketama, "ketama", makeKetama)
}
func makeKetama(r *models.Ring) (torus.Ring, error) {
rep := int(r.ReplicationFactor)
if rep == 0 {
rep = 1
}
pi := torus.PeerInfoList(r.Peers)
return &ketama{
version: int(r.Version),
peers: pi,
rep: rep,
ring: hashring.NewWithWeights(pi.GetWeights()),
}, nil
}
func (k *ketama) GetPeers(key torus.BlockRef) (torus.PeerPermutation, error) {
s, ok := k.ring.GetNodes(string(key.ToBytes()), len(k.peers))
if !ok {
return torus.PeerPermutation{}, errors.New("couldn't get sufficient nodes")
}
return torus.PeerPermutation{
Peers: s,
Replication: k.rep,
}, nil
}
func (k *ketama) Members() torus.PeerList { return k.peers.PeerList() }
func (k *ketama) Describe() string {
s := fmt.Sprintf("Ring: Ketama\nReplication:%d\nPeers:", k.rep)
for _, x := range k.peers {
s += fmt.Sprintf("\n\t%s", x)
}
return s
}
func (k *ketama) Type() torus.RingType { return Ketama }
func (k *ketama) Version() int { return k.version }
func (k *ketama) Marshal() ([]byte, error) {
var out models.Ring
out.Version = uint32(k.version)
out.ReplicationFactor = uint32(k.rep)
out.Type = uint32(k.Type())
out.Peers = k.peers
return out.Marshal()
}
func (k *ketama) AddPeers(peers torus.PeerInfoList) (torus.Ring, error) {
newPeers := k.peers.Union(peers)
if reflect.DeepEqual(newPeers.PeerList(), k.peers.PeerList()) {
return nil, torus.ErrExists
}
newk := &ketama{
version: k.version + 1,
rep: k.rep,
peers: newPeers,
ring: hashring.NewWithWeights(newPeers.GetWeights()),
}
return newk, nil
}
func (k *ketama) RemovePeers(pl torus.PeerList) (torus.Ring, error) {
newPeers := k.peers.AndNot(pl)
if len(newPeers) == len(k.Members()) {
return nil, torus.ErrNotExist
}
newk := &ketama{
version: k.version + 1,
rep: k.rep,
peers: newPeers,
ring: hashring.NewWithWeights(newPeers.GetWeights()),
}
return newk, nil
}
func (k *ketama) ChangeReplication(r int) (torus.Ring, error) {
newk := &ketama{
version: k.version + 1,
rep: r,
peers: k.peers,
ring: k.ring,
}
return newk, nil
}