Repository navigation
Expand file tree
/
Copy pathproxy.go
More file actions
476 lines (418 loc) · 11.1 KB
/
Copy pathproxy.go
File metadata and controls
476 lines (418 loc) · 11.1 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Copyright 2016 CodisLabs. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package proxy
import (
"bytes"
"encoding/json"
"errors"
"net"
"net/http"
"os"
"path"
"strconv"
"strings"
"sync"
"time"
"github.com/CodisLabs/codis/pkg/models"
"github.com/CodisLabs/codis/pkg/proxy/router"
"github.com/CodisLabs/codis/pkg/utils/log"
"github.com/wandoulabs/go-zookeeper/zk"
topo "github.com/wandoulabs/go-zookeeper/zk"
)
type Server struct {
conf *Config
topo *Topology
info models.ProxyInfo
groups map[int]int
lastActionSeq int
evtbus chan interface{}
router *router.Router
listener net.Listener
kill chan interface{}
wait sync.WaitGroup
stop sync.Once
}
func New(addr string, debugVarAddr string, conf *Config) *Server {
log.Infof("create proxy with config: %+v", conf)
proxyHost := strings.Split(addr, ":")[0]
debugHost := strings.Split(debugVarAddr, ":")[0]
hostname, err := os.Hostname()
if err != nil {
log.PanicErrorf(err, "get host name failed")
}
if proxyHost == "0.0.0.0" || strings.HasPrefix(proxyHost, "127.0.0.") || proxyHost == "" {
proxyHost = hostname
}
if debugHost == "0.0.0.0" || strings.HasPrefix(debugHost, "127.0.0.") || debugHost == "" {
debugHost = hostname
}
s := &Server{conf: conf, lastActionSeq: -1, groups: make(map[int]int)}
s.topo = NewTopo(conf.productName, conf.zkAddr, conf.fact, conf.provider, conf.zkSessionTimeout)
s.info.Id = conf.proxyId
s.info.State = models.PROXY_STATE_OFFLINE
s.info.Addr = proxyHost + ":" + strings.Split(addr, ":")[1]
s.info.DebugVarAddr = debugHost + ":" + strings.Split(debugVarAddr, ":")[1]
s.info.Pid = os.Getpid()
s.info.StartAt = time.Now().String()
s.kill = make(chan interface{})
log.Infof("proxy info = %+v", s.info)
if l, err := net.Listen(conf.proto, addr); err != nil {
log.PanicErrorf(err, "open listener failed")
} else {
s.listener = l
}
s.router = router.NewWithAuth(conf.passwd)
s.evtbus = make(chan interface{}, 1024)
s.register()
s.wait.Add(1)
go func() {
defer s.wait.Done()
s.serve()
}()
return s
}
func (s *Server) SetMyselfOnline() error {
log.Info("mark myself online")
info := models.ProxyInfo{
Id: s.conf.proxyId,
State: models.PROXY_STATE_ONLINE,
}
b, _ := json.Marshal(info)
url := "http://" + s.conf.dashboardAddr + "/api/proxy"
res, err := http.Post(url, "application/json", bytes.NewReader(b))
if err != nil {
return err
}
if res.StatusCode != 200 {
return errors.New("response code is not 200")
}
return nil
}
func (s *Server) serve() {
defer s.close()
if !s.waitOnline() {
return
}
s.rewatchNodes()
for i := 0; i < router.MaxSlotNum; i++ {
s.fillSlot(i)
}
log.Info("proxy is serving")
go func() {
defer s.close()
s.handleConns()
}()
s.loopEvents()
}
func (s *Server) handleConns() {
ch := make(chan net.Conn, 4096)
defer close(ch)
go func() {
for c := range ch {
x := router.NewSessionSize(c, s.conf.passwd, s.conf.maxBufSize, s.conf.maxTimeout)
go x.Serve(s.router, s.conf.maxPipeline)
}
}()
for {
c, err := s.listener.Accept()
if err != nil {
if ne, ok := err.(net.Error); ok && ne.Temporary() {
log.WarnErrorf(err, "[%p] proxy accept new connection failed, get temporary error", s)
time.Sleep(time.Millisecond*10)
continue
}
log.WarnErrorf(err, "[%p] proxy accept new connection failed, get non-temporary error, must shutdown", s)
return
} else {
ch <- c
}
}
}
func (s *Server) Info() models.ProxyInfo {
return s.info
}
func (s *Server) Join() {
s.wait.Wait()
}
func (s *Server) Close() error {
s.close()
s.wait.Wait()
return nil
}
func (s *Server) close() {
s.stop.Do(func() {
s.listener.Close()
if s.router != nil {
s.router.Close()
}
close(s.kill)
})
}
func (s *Server) rewatchProxy() {
_, err := s.topo.WatchNode(path.Join(models.GetProxyPath(s.topo.ProductName), s.info.Id), s.evtbus)
if err != nil {
log.PanicErrorf(err, "watch node failed")
}
}
func (s *Server) rewatchNodes() []string {
nodes, err := s.topo.WatchChildren(models.GetWatchActionPath(s.topo.ProductName), s.evtbus)
if err != nil {
log.PanicErrorf(err, "watch children failed")
}
return nodes
}
func (s *Server) register() {
if _, err := s.topo.CreateProxyInfo(&s.info); err != nil {
log.PanicErrorf(err, "create proxy node failed")
}
if _, err := s.topo.CreateProxyFenceNode(&s.info); err != nil && err != zk.ErrNodeExists {
log.PanicErrorf(err, "create fence node failed")
}
log.Warn("********** Attention **********")
log.Warn("You should use `kill {pid}` rather than `kill -9 {pid}` to stop me,")
log.Warn("or the node resisted on zk will not be cleaned when I'm quiting and you must remove it manually")
log.Warn("*******************************")
}
func (s *Server) markOffline() {
s.topo.Close(s.info.Id)
s.info.State = models.PROXY_STATE_MARK_OFFLINE
}
func (s *Server) waitOnline() bool {
for {
info, err := s.topo.GetProxyInfo(s.info.Id)
if err != nil {
log.PanicErrorf(err, "get proxy info failed: %s", s.info.Id)
}
switch info.State {
case models.PROXY_STATE_MARK_OFFLINE:
log.Infof("mark offline, proxy got offline event: %s", s.info.Id)
s.markOffline()
return false
case models.PROXY_STATE_ONLINE:
s.info.State = info.State
log.Infof("we are online: %s", s.info.Id)
s.rewatchProxy()
return true
}
select {
case <-s.kill:
log.Infof("mark offline, proxy is killed: %s", s.info.Id)
s.markOffline()
return false
default:
}
log.Infof("wait to be online: %s", s.info.Id)
time.Sleep(3 * time.Second)
}
}
func getEventPath(evt interface{}) string {
return evt.(topo.Event).Path
}
func needResponse(receivers []string, self models.ProxyInfo) bool {
var info models.ProxyInfo
for _, v := range receivers {
err := json.Unmarshal([]byte(v), &info)
if err != nil {
if v == self.Id {
return true
}
return false
}
if info.Id == self.Id && info.Pid == self.Pid && info.StartAt == self.StartAt {
return true
}
}
return false
}
func groupMaster(groupInfo models.ServerGroup) string {
var master string
for _, server := range groupInfo.Servers {
if server.Type == models.SERVER_TYPE_MASTER {
if master != "" {
log.Panicf("two master not allowed: %+v", groupInfo)
}
master = server.Addr
}
}
if master == "" {
log.Panicf("master not found: %+v", groupInfo)
}
return master
}
func (s *Server) resetSlot(i int) {
s.router.ResetSlot(i)
}
func (s *Server) fillSlot(i int) {
slotInfo, slotGroup, err := s.topo.GetSlotByIndex(i)
if err != nil {
log.PanicErrorf(err, "get slot by index failed", i)
}
var from string
var addr = groupMaster(*slotGroup)
if slotInfo.State.Status == models.SLOT_STATUS_MIGRATE {
fromGroup, err := s.topo.GetGroup(slotInfo.State.MigrateStatus.From)
if err != nil {
log.PanicErrorf(err, "get migrate from failed")
}
from = groupMaster(*fromGroup)
if from == addr {
log.Panicf("set slot %04d migrate from %s to %s", i, from, addr)
}
}
s.groups[i] = slotInfo.GroupId
s.router.FillSlot(i, addr, from,
slotInfo.State.Status == models.SLOT_STATUS_PRE_MIGRATE)
}
func (s *Server) onSlotRangeChange(param *models.SlotMultiSetParam) {
log.Infof("slotRangeChange %+v", param)
for i := param.From; i <= param.To; i++ {
switch param.Status {
case models.SLOT_STATUS_OFFLINE:
s.resetSlot(i)
case models.SLOT_STATUS_ONLINE:
s.fillSlot(i)
default:
log.Panicf("can not handle status %v", param.Status)
}
}
}
func (s *Server) onGroupChange(groupId int) {
log.Infof("group changed %d", groupId)
for i, g := range s.groups {
if g == groupId {
s.fillSlot(i)
}
}
}
func (s *Server) responseAction(seq int64) {
log.Infof("send response seq = %d", seq)
err := s.topo.DoResponse(int(seq), &s.info)
if err != nil {
log.InfoErrorf(err, "send response seq = %d failed", seq)
}
}
func (s *Server) getActionObject(seq int, target interface{}) {
act := &models.Action{Target: target}
err := s.topo.GetActionWithSeqObject(int64(seq), act)
if err != nil {
log.PanicErrorf(err, "get action object failed, seq = %d", seq)
}
log.Infof("action %+v", act)
}
func (s *Server) checkAndDoTopoChange(seq int) bool {
act, err := s.topo.GetActionWithSeq(int64(seq))
if err != nil { //todo: error is not "not exist"
log.PanicErrorf(err, "action failed, seq = %d", seq)
}
if !needResponse(act.Receivers, s.info) { //no need to response
return false
}
log.Warnf("action %v receivers %v", seq, act.Receivers)
switch act.Type {
case models.ACTION_TYPE_SLOT_MIGRATE, models.ACTION_TYPE_SLOT_CHANGED,
models.ACTION_TYPE_SLOT_PREMIGRATE:
slot := &models.Slot{}
s.getActionObject(seq, slot)
s.fillSlot(slot.Id)
case models.ACTION_TYPE_SERVER_GROUP_CHANGED:
serverGroup := &models.ServerGroup{}
s.getActionObject(seq, serverGroup)
s.onGroupChange(serverGroup.Id)
case models.ACTION_TYPE_SERVER_GROUP_REMOVE:
//do not care
case models.ACTION_TYPE_MULTI_SLOT_CHANGED:
param := &models.SlotMultiSetParam{}
s.getActionObject(seq, param)
s.onSlotRangeChange(param)
default:
log.Panicf("unknown action %+v", act)
}
return true
}
func (s *Server) processAction(e interface{}) {
if strings.Index(getEventPath(e), models.GetProxyPath(s.topo.ProductName)) == 0 {
info, err := s.topo.GetProxyInfo(s.info.Id)
if err != nil {
log.PanicErrorf(err, "get proxy info failed: %s", s.info.Id)
}
switch info.State {
case models.PROXY_STATE_MARK_OFFLINE:
log.Infof("mark offline, proxy got offline event: %s", s.info.Id)
s.markOffline()
case models.PROXY_STATE_ONLINE:
s.rewatchProxy()
default:
log.Panicf("unknown proxy state %v", info)
}
return
}
//re-watch
nodes := s.rewatchNodes()
seqs, err := models.ExtraSeqList(nodes)
if err != nil {
log.PanicErrorf(err, "get seq list failed")
}
if len(seqs) == 0 || !s.topo.IsChildrenChangedEvent(e) {
return
}
//get last pos
index := -1
for i, seq := range seqs {
if s.lastActionSeq < seq {
index = i
//break
//only handle latest action
}
}
if index < 0 {
return
}
actions := seqs[index:]
for _, seq := range actions {
exist, err := s.topo.Exist(path.Join(s.topo.GetActionResponsePath(seq), s.info.Id))
if err != nil {
log.PanicErrorf(err, "get action failed")
}
if exist {
continue
}
if s.checkAndDoTopoChange(seq) {
s.responseAction(int64(seq))
}
}
s.lastActionSeq = seqs[len(seqs)-1]
}
func (s *Server) loopEvents() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
var tick int = 0
for s.info.State == models.PROXY_STATE_ONLINE {
select {
case <-s.kill:
log.Infof("mark offline, proxy is killed: %s", s.info.Id)
s.markOffline()
case e := <-s.evtbus:
evtPath := getEventPath(e)
log.Infof("got event %s, %v, lastActionSeq %d", s.info.Id, e, s.lastActionSeq)
if strings.Index(evtPath, models.GetActionResponsePath(s.conf.productName)) == 0 {
seq, err := strconv.Atoi(path.Base(evtPath))
if err != nil {
log.ErrorErrorf(err, "parse action seq failed")
} else {
if seq < s.lastActionSeq {
log.Infof("ignore seq = %d", seq)
continue
}
}
}
s.processAction(e)
case <-ticker.C:
if maxTick := s.conf.pingPeriod; maxTick != 0 {
if tick++; tick >= maxTick {
s.router.KeepAlive()
tick = 0
}
}
}
}
}