/
context.go
304 lines (275 loc) · 7.07 KB
/
context.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright 2016 CodisLabs. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package topom
import (
"net"
"sync"
"time"
"github.com/CodisLabs/codis/pkg/models"
"github.com/CodisLabs/codis/pkg/utils"
"github.com/CodisLabs/codis/pkg/utils/errors"
"github.com/CodisLabs/codis/pkg/utils/log"
"github.com/CodisLabs/codis/pkg/utils/math2"
)
const MaxSlotNum = models.MaxSlotNum
type context struct {
slots []*models.SlotMapping
group map[int]*models.Group
proxy map[string]*models.Proxy
sentinel *models.Sentinel
hosts struct {
sync.Mutex
m map[string]net.IP
}
method int
}
func (ctx *context) getSlotMapping(sid int) (*models.SlotMapping, error) {
if len(ctx.slots) != MaxSlotNum {
return nil, errors.Errorf("invalid number of slots = %d/%d", len(ctx.slots), MaxSlotNum)
}
if sid >= 0 && sid < MaxSlotNum {
return ctx.slots[sid], nil
}
return nil, errors.Errorf("slot-[%d] doesn't exist", sid)
}
func (ctx *context) getSlotMappingsByGroupId(gid int) []*models.SlotMapping {
var slots = []*models.SlotMapping{}
for _, m := range ctx.slots {
if m.GroupId == gid || m.Action.TargetId == gid {
slots = append(slots, m)
}
}
return slots
}
func (ctx *context) maxSlotActionIndex() (maxIndex int) {
for _, m := range ctx.slots {
if m.Action.State != models.ActionNothing {
maxIndex = math2.MaxInt(maxIndex, m.Action.Index)
}
}
return maxIndex
}
func (ctx *context) isSlotLocked(m *models.SlotMapping) bool {
switch m.Action.State {
case models.ActionNothing, models.ActionPending:
return ctx.isGroupLocked(m.GroupId)
case models.ActionPreparing:
return ctx.isGroupLocked(m.GroupId)
case models.ActionPrepared:
return true
case models.ActionMigrating:
return ctx.isGroupLocked(m.GroupId) || ctx.isGroupLocked(m.Action.TargetId)
case models.ActionFinished:
return ctx.isGroupLocked(m.Action.TargetId)
default:
log.Panicf("slot-[%d] action state is invalid:\n%s", m.Id, m.Encode())
}
return false
}
func (ctx *context) toSlot(m *models.SlotMapping, p *models.Proxy) *models.Slot {
slot := &models.Slot{
Id: m.Id,
Locked: ctx.isSlotLocked(m),
ForwardMethod: ctx.method,
}
switch m.Action.State {
case models.ActionNothing, models.ActionPending:
slot.BackendAddr = ctx.getGroupMaster(m.GroupId)
slot.BackendAddrGroupId = m.GroupId
slot.ReplicaGroups = ctx.toReplicaGroups(m.GroupId, p)
case models.ActionPreparing:
slot.BackendAddr = ctx.getGroupMaster(m.GroupId)
slot.BackendAddrGroupId = m.GroupId
case models.ActionPrepared:
fallthrough
case models.ActionMigrating:
slot.BackendAddr = ctx.getGroupMaster(m.Action.TargetId)
slot.BackendAddrGroupId = m.Action.TargetId
slot.MigrateFrom = ctx.getGroupMaster(m.GroupId)
slot.MigrateFromGroupId = m.GroupId
case models.ActionFinished:
slot.BackendAddr = ctx.getGroupMaster(m.Action.TargetId)
slot.BackendAddrGroupId = m.Action.TargetId
default:
log.Panicf("slot-[%d] action state is invalid:\n%s", m.Id, m.Encode())
}
return slot
}
func (ctx *context) lookupIPAddr(addr string) net.IP {
ctx.hosts.Lock()
defer ctx.hosts.Unlock()
ip, ok := ctx.hosts.m[addr]
if !ok {
if tcpAddr := utils.ResolveTCPAddrTimeout(addr, 50*time.Millisecond); tcpAddr != nil {
ctx.hosts.m[addr] = tcpAddr.IP
return tcpAddr.IP
} else {
ctx.hosts.m[addr] = nil
return nil
}
}
return ip
}
func (ctx *context) toReplicaGroups(gid int, p *models.Proxy) [][]string {
g := ctx.group[gid]
switch {
case g == nil:
return nil
case g.Promoting.State != models.ActionNothing:
return nil
case len(g.Servers) <= 1:
return nil
}
var dc string
var ip net.IP
if p != nil {
dc = p.DataCenter
ip = ctx.lookupIPAddr(p.AdminAddr)
}
getPriority := func(s *models.GroupServer) int {
if ip == nil || dc != s.DataCenter {
return 2
}
if ip.Equal(ctx.lookupIPAddr(s.Addr)) {
return 0
} else {
return 1
}
}
var groups [3][]string
for _, s := range g.Servers {
if s.ReplicaGroup {
p := getPriority(s)
groups[p] = append(groups[p], s.Addr)
}
}
var replicas [][]string
for _, l := range groups {
if len(l) != 0 {
replicas = append(replicas, l)
}
}
return replicas
}
func (ctx *context) toSlotSlice(slots []*models.SlotMapping, p *models.Proxy) []*models.Slot {
var slice = make([]*models.Slot, len(slots))
for i, m := range slots {
slice[i] = ctx.toSlot(m, p)
}
return slice
}
func (ctx *context) getGroup(gid int) (*models.Group, error) {
if g := ctx.group[gid]; g != nil {
return g, nil
}
return nil, errors.Errorf("group-[%d] doesn't exist", gid)
}
func (ctx *context) getGroupIndex(g *models.Group, addr string) (int, error) {
for i, x := range g.Servers {
if x.Addr == addr {
return i, nil
}
}
return -1, errors.Errorf("group-[%d] doesn't have server-[%s]", g.Id, addr)
}
func (ctx *context) getGroupByServer(addr string) (*models.Group, int, error) {
for _, g := range ctx.group {
for i, x := range g.Servers {
if x.Addr == addr {
return g, i, nil
}
}
}
return nil, -1, errors.Errorf("server-[%s] doesn't exist", addr)
}
func (ctx *context) maxSyncActionIndex() (maxIndex int) {
for _, g := range ctx.group {
for _, x := range g.Servers {
if x.Action.State == models.ActionPending {
maxIndex = math2.MaxInt(maxIndex, x.Action.Index)
}
}
}
return maxIndex
}
func (ctx *context) minSyncActionIndex() string {
var d *models.GroupServer
for _, g := range ctx.group {
for _, x := range g.Servers {
if x.Action.State == models.ActionPending {
if d == nil || x.Action.Index < d.Action.Index {
d = x
}
}
}
}
if d == nil {
return ""
}
return d.Addr
}
func (ctx *context) getGroupMaster(gid int) string {
if g := ctx.group[gid]; g != nil && len(g.Servers) != 0 {
return g.Servers[0].Addr
}
return ""
}
func (ctx *context) getGroupMasters() map[int]string {
var masters = make(map[int]string)
for _, g := range ctx.group {
if len(g.Servers) != 0 {
masters[g.Id] = g.Servers[0].Addr
}
}
return masters
}
func (ctx *context) getGroupIds() map[int]bool {
var groups = make(map[int]bool)
for _, g := range ctx.group {
groups[g.Id] = true
}
return groups
}
func (ctx *context) isGroupInUse(gid int) bool {
for _, m := range ctx.slots {
if m.GroupId == gid || m.Action.TargetId == gid {
return true
}
}
return false
}
func (ctx *context) isGroupLocked(gid int) bool {
if g := ctx.group[gid]; g != nil {
switch g.Promoting.State {
case models.ActionNothing:
return false
case models.ActionPreparing:
return false
case models.ActionPrepared:
return true
case models.ActionFinished:
return false
default:
log.Panicf("invalid state of group-[%d] = %s", g.Id, g.Encode())
}
}
return false
}
func (ctx *context) isGroupPromoting(gid int) bool {
if g := ctx.group[gid]; g != nil {
return g.Promoting.State != models.ActionNothing
}
return false
}
func (ctx *context) getProxy(token string) (*models.Proxy, error) {
if p := ctx.proxy[token]; p != nil {
return p, nil
}
return nil, errors.Errorf("proxy-[%s] doesn't exist", token)
}
func (ctx *context) maxProxyId() (maxId int) {
for _, p := range ctx.proxy {
maxId = math2.MaxInt(maxId, p.Id)
}
return maxId
}