forked from CodisLabs/codis
/
slots.go
130 lines (115 loc) · 2.62 KB
/
slots.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
// Copyright 2014 Wandoujia Inc. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package router
import (
"fmt"
"sync"
"github.com/wandoulabs/codis/pkg/models"
"github.com/wandoulabs/codis/pkg/proxy/redis"
"github.com/wandoulabs/codis/pkg/utils/errors"
"github.com/wandoulabs/codis/pkg/utils/log"
)
type Slot struct {
Id int
Info *models.Slot
Group *models.ServerGroup
backend struct {
addr string
host []byte
port []byte
bc *SharedBackendConn
}
migrate struct {
from string
bc *SharedBackendConn
}
wait sync.WaitGroup
lock struct {
hold bool
sync.RWMutex
}
}
func (s *Slot) blockAndWait() {
if !s.lock.hold {
s.lock.hold = true
s.lock.Lock()
}
s.wait.Wait()
}
func (s *Slot) unblock() {
if !s.lock.hold {
return
}
s.lock.hold = false
s.lock.Unlock()
}
func (s *Slot) reset() {
s.Info, s.Group = nil, nil
s.backend.addr = ""
s.backend.host = nil
s.backend.port = nil
s.backend.bc = nil
s.migrate.from = ""
s.migrate.bc = nil
}
func (s *Slot) forward(r *Request, key []byte) error {
s.lock.RLock()
bc, err := s.prepare(r, key)
s.lock.RUnlock()
if err != nil {
return err
} else {
bc.PushBack(r)
return nil
}
}
var ErrSlotIsNotReady = errors.New("slot is not ready, may be offline")
func (s *Slot) prepare(r *Request, key []byte) (*SharedBackendConn, error) {
if s.backend.bc == nil {
log.Infof("slot-%04d is not ready: key = %s", s.Id, key)
return nil, ErrSlotIsNotReady
}
if err := s.slotsmgrt(r, key); err != nil {
log.Warnf("slot-%04d migrate from = %s to %s failed: key = %s, error = %s",
s.Id, s.migrate.from, s.backend.addr, key, err)
return nil, err
} else {
r.slot = &s.wait
r.slot.Add(1)
return s.backend.bc, nil
}
}
func (s *Slot) slotsmgrt(r *Request, key []byte) error {
if len(key) == 0 || s.migrate.bc == nil {
return nil
}
m := &Request{
Resp: redis.NewArray([]*redis.Resp{
redis.NewBulkBytes([]byte("SLOTSMGRTTAGONE")),
redis.NewBulkBytes(s.backend.host),
redis.NewBulkBytes(s.backend.port),
redis.NewBulkBytes([]byte("3000")),
redis.NewBulkBytes(key),
}),
Wait: &sync.WaitGroup{},
}
s.migrate.bc.PushBack(m)
m.Wait.Wait()
resp, err := m.Response.Resp, m.Response.Err
if err != nil {
return err
}
if resp == nil {
return ErrRespIsRequired
}
if resp.IsError() {
return errors.New(fmt.Sprintf("error resp: %s", resp.Value))
}
if resp.IsInt() {
log.Debugf("slot-%04d migrate from %s to %s: key = %s, resp = %s",
s.Id, s.migrate.from, s.backend.addr, key, resp.Value)
return nil
} else {
return errors.New(fmt.Sprintf("error resp: should be integer, but got %s", resp.Type))
}
}