-
Notifications
You must be signed in to change notification settings - Fork 26
/
action.go
290 lines (242 loc) · 6.76 KB
/
action.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
// Copyright 2014 Wandoujia Inc. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package models
import (
"encoding/json"
"fmt"
"path"
"sort"
"strconv"
"strings"
"time"
"github.com/ledisdb/xcodis/utils"
"github.com/ngaut/zkhelper"
"github.com/juju/errors"
"github.com/ngaut/go-zookeeper/zk"
log "github.com/ngaut/logging"
)
type ActionType string
const (
ACTION_TYPE_SERVER_GROUP_CHANGED ActionType = "group_changed"
ACTION_TYPE_SERVER_GROUP_REMOVE ActionType = "group_remove"
ACTION_TYPE_SLOT_CHANGED ActionType = "slot_changed"
ACTION_TYPE_MULTI_SLOT_CHANGED ActionType = "multi_slot_changed"
ACTION_TYPE_SLOT_MIGRATE ActionType = "slot_migrate"
ACTION_TYPE_SLOT_PREMIGRATE ActionType = "slot_premigrate"
)
const (
GC_TYPE_N = iota + 1
GC_TYPE_SEC
)
type Action struct {
Type ActionType `json:"type"`
Desc string `json:"desc"`
Target interface{} `json:"target"`
Ts string `json:"ts"` // timestamp
Receivers []string `json:"receivers"`
}
func GetWatchActionPath(productName string) string {
return fmt.Sprintf("/zk/codis/db_%s/actions", productName)
}
func GetActionWithSeq(zkConn zkhelper.Conn, productName string, seq int64) (*Action, error) {
var act Action
data, _, err := zkConn.Get(path.Join(GetWatchActionPath(productName), "action_"+fmt.Sprintf("%0.10d", seq)))
if err != nil {
return nil, errors.Trace(err)
}
if err := json.Unmarshal(data, &act); err != nil {
return nil, errors.Trace(err)
}
return &act, nil
}
func GetActionObject(zkConn zkhelper.Conn, productName string, seq int64, act interface{}) error {
data, _, err := zkConn.Get(path.Join(GetWatchActionPath(productName), "action_"+fmt.Sprintf("%0.10d", seq)))
if err != nil {
return errors.Trace(err)
}
if err := json.Unmarshal(data, act); err != nil {
return errors.Trace(err)
}
return nil
}
var ErrReceiverTimeout = errors.New("receiver timeout")
func WaitForReceiver(zkConn zkhelper.Conn, productName string, actionZkPath string, proxies []ProxyInfo) error {
if len(proxies) == 0 {
return nil
}
times := 0
var proxyIds []string
var offlineProxyIds []string
for _, p := range proxies {
proxyIds = append(proxyIds, p.Id)
}
sort.Strings(proxyIds)
// check every 500ms
for times < 60 {
if times >= 6 && (times*500)%1000 == 0 {
log.Warning("abnormal waiting time for receivers", actionZkPath)
}
nodes, _, err := zkConn.Children(actionZkPath)
if err != nil {
return errors.Trace(err)
}
var confirmIds []string
for _, node := range nodes {
id := path.Base(node)
confirmIds = append(confirmIds, id)
}
if len(confirmIds) != 0 {
sort.Strings(confirmIds)
if utils.Strings(proxyIds).Eq(confirmIds) {
return nil
}
offlineProxyIds = proxyIds[len(confirmIds)-1:]
}
times += 1
time.Sleep(500 * time.Millisecond)
}
if len(offlineProxyIds) > 0 {
log.Error("proxies didn't responed: ", offlineProxyIds)
}
// set offline proxies
for _, id := range offlineProxyIds {
log.Errorf("mark proxy %s to PROXY_STATE_MARK_OFFLINE", id)
if err := SetProxyStatus(zkConn, productName, id, PROXY_STATE_MARK_OFFLINE); err != nil {
return err
}
}
return ErrReceiverTimeout
}
func GetActionSeqList(zkConn zkhelper.Conn, productName string) ([]int, error) {
nodes, _, err := zkConn.Children(GetWatchActionPath(productName))
if err != nil {
return nil, errors.Trace(err)
}
return ExtraSeqList(nodes)
}
func ExtraSeqList(nodes []string) ([]int, error) {
var seqs []int
for _, nodeName := range nodes {
seq, err := strconv.Atoi(strings.Split(nodeName, "_")[1])
if err != nil {
return nil, errors.Trace(err)
}
seqs = append(seqs, seq)
}
sort.Ints(seqs)
return seqs, nil
}
func ActionGC(zkConn zkhelper.Conn, productName string, gcType int, keep int) error {
prefix := GetWatchActionPath(productName)
exists, err := zkhelper.NodeExists(zkConn, prefix)
if err != nil {
return errors.Trace(err)
}
if !exists {
// if action path not exists just return nil
return nil
}
actions, _, err := zkConn.Children(prefix)
if err != nil {
return errors.Trace(err)
}
var act Action
currentTs := time.Now().Unix()
if gcType == GC_TYPE_N {
sort.Strings(actions)
if len(actions) <= keep {
return nil
}
for _, action := range actions[:len(actions)-keep] {
if err := zkhelper.DeleteRecursive(zkConn, path.Join(prefix, action), -1); err != nil {
return errors.Trace(err)
}
}
} else if gcType == GC_TYPE_SEC {
secs := keep
for _, action := range actions {
b, _, err := zkConn.Get(path.Join(prefix, action))
if err != nil {
return errors.Trace(err)
}
if err := json.Unmarshal(b, &act); err != nil {
return errors.Trace(err)
}
log.Info(action, act.Ts)
ts, _ := strconv.ParseInt(act.Ts, 10, 64)
if currentTs-ts > int64(secs) {
if err := zkConn.Delete(path.Join(prefix, action), -1); err != nil {
return errors.Trace(err)
}
}
}
}
return nil
}
func CreateActionRootPath(zkConn zkhelper.Conn, path string) error {
// if action dir not exists, create it first
exists, err := zkhelper.NodeExists(zkConn, path)
if err != nil {
return errors.Trace(err)
}
if !exists {
_, err := zkhelper.CreateOrUpdate(zkConn, path, "", 0, zkhelper.DefaultDirACLs(), true)
if err != nil {
return errors.Trace(err)
}
}
return nil
}
func NewAction(zkConn zkhelper.Conn, productName string, actionType ActionType, target interface{}, desc string, needConfirm bool) error {
ts := strconv.FormatInt(time.Now().Unix(), 10)
action := &Action{
Type: actionType,
Desc: desc,
Target: target,
Ts: ts,
}
// set action receivers
proxies, err := ProxyList(zkConn, productName, func(p *ProxyInfo) bool {
return p.State == PROXY_STATE_ONLINE
})
if err != nil {
return errors.Trace(err)
}
for _, p := range proxies {
action.Receivers = append(action.Receivers, p.Id)
}
b, _ := json.Marshal(action)
prefix := GetWatchActionPath(productName)
err = CreateActionRootPath(zkConn, prefix)
if err != nil {
return errors.Trace(err)
}
// create action node
actionCreated, err := zkConn.Create(prefix+"/action_", b, int32(zk.FlagSequence), zkhelper.DefaultDirACLs())
if err != nil {
log.Error(err, prefix)
return errors.Trace(err)
}
if needConfirm {
if err := WaitForReceiver(zkConn, productName, actionCreated, proxies); err != nil {
return errors.Trace(err)
}
}
return nil
}
func ForceRemoveLock(zkConn zkhelper.Conn, productName string) error {
lockPath := fmt.Sprintf("/zk/codis/db_%s/LOCK", productName)
children, _, err := zkConn.Children(lockPath)
if err != nil {
return errors.Trace(err)
}
for _, c := range children {
fullPath := path.Join(lockPath, c)
log.Info("deleting..", fullPath)
err := zkConn.Delete(fullPath, 0)
if err != nil {
return errors.Trace(err)
}
}
return nil
}