forked from CodisLabs/codis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
169 lines (142 loc) · 3.02 KB
/
router.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
// Copyright 2014 Wandoujia Inc. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package router
import (
"strings"
"sync"
"github.com/wandoulabs/codis/pkg/models"
"github.com/wandoulabs/codis/pkg/utils/errors"
"github.com/wandoulabs/codis/pkg/utils/log"
)
const MaxSlotNum = models.DEFAULT_SLOT_NUM
type Router struct {
mu sync.Mutex
auth string
pool map[string]*SharedBackendConn
slots [MaxSlotNum]*Slot
closed bool
}
func New() *Router {
return NewWithAuth("")
}
func NewWithAuth(auth string) *Router {
s := &Router{
auth: auth,
pool: make(map[string]*SharedBackendConn),
}
for i := 0; i < len(s.slots); i++ {
s.slots[i] = &Slot{id: i}
}
return s
}
func (s *Router) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return nil
}
for i := 0; i < len(s.slots); i++ {
s.resetSlot(i)
}
s.closed = true
return nil
}
var errClosedRouter = errors.New("use of closed router")
func (s *Router) ResetSlot(i int) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return errClosedRouter
}
s.resetSlot(i)
return nil
}
func (s *Router) FillSlot(i int, addr, from string, lock bool) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return errClosedRouter
}
s.fillSlot(i, addr, from, lock)
return nil
}
func (s *Router) KeepAlive() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return errClosedRouter
}
for _, bc := range s.pool {
bc.KeepAlive()
}
return nil
}
func (s *Router) Dispatch(r *Request) error {
hkey := getHashKey(r.Resp, r.OpStr)
slot := s.slots[hashSlot(hkey)]
return slot.forward(r, hkey)
}
func (s *Router) getBackendConn(addr string) *SharedBackendConn {
bc := s.pool[addr]
if bc != nil {
bc.IncrRefcnt()
} else {
bc = NewSharedBackendConn(addr, s.auth)
s.pool[addr] = bc
}
return bc
}
func (s *Router) putBackendConn(bc *SharedBackendConn) {
if bc != nil && bc.Close() {
delete(s.pool, bc.Addr())
}
}
func (s *Router) isValidSlot(i int) bool {
return i >= 0 && i < len(s.slots)
}
func (s *Router) resetSlot(i int) {
if !s.isValidSlot(i) {
return
}
slot := s.slots[i]
slot.blockAndWait()
s.putBackendConn(slot.backend.bc)
s.putBackendConn(slot.migrate.bc)
slot.reset()
slot.unblock()
}
func (s *Router) fillSlot(i int, addr, from string, lock bool) {
if !s.isValidSlot(i) {
return
}
slot := s.slots[i]
slot.blockAndWait()
s.putBackendConn(slot.backend.bc)
s.putBackendConn(slot.migrate.bc)
slot.reset()
if len(addr) != 0 {
xx := strings.Split(addr, ":")
if len(xx) >= 1 {
slot.backend.host = []byte(xx[0])
}
if len(xx) >= 2 {
slot.backend.port = []byte(xx[1])
}
slot.backend.addr = addr
slot.backend.bc = s.getBackendConn(addr)
}
if len(from) != 0 {
slot.migrate.from = from
slot.migrate.bc = s.getBackendConn(from)
}
if !lock {
slot.unblock()
}
if slot.migrate.bc != nil {
log.Infof("fill slot %04d, backend.addr = %s, migrate.from = %s",
i, slot.backend.addr, slot.migrate.from)
} else {
log.Infof("fill slot %04d, backend.addr = %s",
i, slot.backend.addr)
}
}