forked from buraksezer/consistent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relocation_percentage.go
65 lines (54 loc) · 1.43 KB
/
relocation_percentage.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
package main
import (
"fmt"
"math/rand"
"time"
"github.com/buraksezer/consistent"
"github.com/cespare/xxhash"
)
type Member string
func (m Member) String() string {
return string(m)
}
type hasher struct{}
func (h hasher) Sum64(data []byte) uint64 {
return xxhash.Sum64(data)
}
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func main() {
// Create a new consistent instance.
members := []consistent.Member{}
for i := 0; i < 8; i++ {
member := Member(fmt.Sprintf("node%d.olricmq", i))
members = append(members, member)
}
// Modify PartitionCount, ReplicationFactor and Load to increase or decrease
// relocation ratio.
cfg := consistent.Config{
PartitionCount: 271,
ReplicationFactor: 20,
Load: 1.25,
Hasher: hasher{},
}
c := consistent.New(members, cfg)
// Store current layout of partitions
owners := make(map[int]string)
for partID := 0; partID < cfg.PartitionCount; partID++ {
owners[partID] = c.GetPartitionOwner(partID).String()
}
// Add a new member
m := Member(fmt.Sprintf("node%d.olricmq", 9))
c.Add(m)
// Get the new layout and compare with the previous
var changed int
for partID, member := range owners {
owner := c.GetPartitionOwner(partID)
if member != owner.String() {
changed++
fmt.Printf("partID: %3d moved to %s from %s\n", partID, owner.String(), member)
}
}
fmt.Printf("\n%d%% of the partitions are relocated\n", (100*changed)/cfg.PartitionCount)
}