forked from evalphobia/wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard_cluster.go
124 lines (108 loc) · 2.64 KB
/
shard_cluster.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package wizard
import (
"github.com/evalphobia/wizard/errors"
)
// ShardCluster is struct for sharded database cluster
type ShardCluster struct {
List []*ShardSet // sharded database clusters
slotsize int64
}
// Master is dummy method for interface
func (c ShardCluster) Master() *Node {
return nil
}
// Masters returns all db masters from the sharded clusters
func (c ShardCluster) Masters() []*Node {
var result []*Node
for _, s := range c.List {
if s.set == nil {
continue
}
result = append(result, s.set.Master())
}
return result
}
// Slave is dummy method for interface
func (c ShardCluster) Slave() *Node {
return nil
}
// Slaves randomly returns all db slaves from the sharded clusters
func (c ShardCluster) Slaves() []*Node {
var result []*Node
for _, s := range c.List {
if s.set == nil {
continue
}
result = append(result, s.set.Slave())
}
return result
}
// SelectByKey returns sharded cluster by shard key
func (c ShardCluster) SelectByKey(key interface{}) *StandardCluster {
i := getInt64(key)
mod := i % c.slotsize
for _, shard := range c.List {
if shard.InRange(mod) {
return shard.set
}
}
return nil
}
// RegisterShard adds cluster with hash slot range(min and max)
func (c *ShardCluster) RegisterShard(min, max int64, s *StandardCluster) error {
err := c.checkOverlapped(min, max)
if err != nil {
return err
}
ss := &ShardSet{
min: min,
max: max,
set: s,
}
err = ss.checkSlotSize(c.slotsize)
if err != nil {
return err
}
c.List = append(c.List, ss)
return nil
}
// checkOverlapped checks the hash slot range is not overlapped among the shards
func (c *ShardCluster) checkOverlapped(min, max int64) error {
for _, ss := range c.List {
switch {
case ss.InRange(min):
return errors.NewErrSlotMinOverlapped(min)
case ss.InRange(max):
return errors.NewErrSlotMaxOverlapped(max)
}
}
return nil
}
// ShardSet is struct of sharded cluster
type ShardSet struct {
min int64
max int64
set *StandardCluster
}
// InRange checks given number is in range of this shard
func (ss ShardSet) InRange(v int64) bool {
return ss.min <= v && v <= ss.max
}
// checkSlotSize checks given number is not minus and within slotsize
func (ss ShardSet) checkSlotSize(slot int64) error {
switch {
case !ss.isMinAboveZero():
return errors.NewErrSlotSizeMin(ss.min)
case !ss.isMaxInSlotSize(slot):
return errors.NewErrSlotSizeMax(ss.max, slot)
}
return nil
}
// isMinAboveZero checks given number is not minus
func (ss ShardSet) isMinAboveZero() bool {
return ss.min >= 0
}
// isMaxInSlotSize checks given number is within slotsize
func (ss ShardSet) isMaxInSlotSize(slot int64) bool {
return ss.max < slot
}