-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
store.go
263 lines (219 loc) · 5.62 KB
/
store.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
// Copyright 2016 CodisLabs. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package models
import (
"fmt"
"path/filepath"
"regexp"
"github.com/CodisLabs/codis/pkg/utils/errors"
"github.com/CodisLabs/codis/pkg/utils/log"
)
func init() {
if filepath.Separator != '/' {
log.Panicf("bad Separator = '%c', must be '/'", filepath.Separator)
}
}
const JodisDir = "/jodis"
func JodisPath(product string, token string) string {
return filepath.Join(JodisDir, product, fmt.Sprintf("proxy-%s", token))
}
const CodisDir = "/codis3"
func ProductDir(product string) string {
return filepath.Join(CodisDir, product)
}
func LockPath(product string) string {
return filepath.Join(CodisDir, product, "topom")
}
func SlotPath(product string, sid int) string {
return filepath.Join(CodisDir, product, "slots", fmt.Sprintf("slot-%04d", sid))
}
func GroupDir(product string) string {
return filepath.Join(CodisDir, product, "group")
}
func ProxyDir(product string) string {
return filepath.Join(CodisDir, product, "proxy")
}
func GroupPath(product string, gid int) string {
return filepath.Join(CodisDir, product, "group", fmt.Sprintf("group-%04d", gid))
}
func ProxyPath(product string, token string) string {
return filepath.Join(CodisDir, product, "proxy", fmt.Sprintf("proxy-%s", token))
}
func SentinelPath(product string) string {
return filepath.Join(CodisDir, product, "sentinel")
}
func LoadTopom(client Client, product string, must bool) (*Topom, error) {
b, err := client.Read(LockPath(product), must)
if err != nil || b == nil {
return nil, err
}
t := &Topom{}
if err := jsonDecode(t, b); err != nil {
return nil, err
}
return t, nil
}
type Store struct {
client Client
product string
}
func NewStore(client Client, product string) *Store {
return &Store{client, product}
}
func (s *Store) Close() error {
return s.client.Close()
}
func (s *Store) Client() Client {
return s.client
}
func (s *Store) LockPath() string {
return LockPath(s.product)
}
func (s *Store) SlotPath(sid int) string {
return SlotPath(s.product, sid)
}
func (s *Store) GroupDir() string {
return GroupDir(s.product)
}
func (s *Store) ProxyDir() string {
return ProxyDir(s.product)
}
func (s *Store) GroupPath(gid int) string {
return GroupPath(s.product, gid)
}
func (s *Store) ProxyPath(token string) string {
return ProxyPath(s.product, token)
}
func (s *Store) SentinelPath() string {
return SentinelPath(s.product)
}
func (s *Store) Acquire(topom *Topom) error {
return s.client.Create(s.LockPath(), topom.Encode())
}
func (s *Store) Release() error {
return s.client.Delete(s.LockPath())
}
func (s *Store) LoadTopom(must bool) (*Topom, error) {
return LoadTopom(s.client, s.product, must)
}
func (s *Store) SlotMappings() ([]*SlotMapping, error) {
slots := make([]*SlotMapping, MaxSlotNum)
for i := range slots {
m, err := s.LoadSlotMapping(i, false)
if err != nil {
return nil, err
}
if m != nil {
slots[i] = m
} else {
slots[i] = &SlotMapping{Id: i}
}
}
return slots, nil
}
func (s *Store) LoadSlotMapping(sid int, must bool) (*SlotMapping, error) {
b, err := s.client.Read(s.SlotPath(sid), must)
if err != nil || b == nil {
return nil, err
}
m := &SlotMapping{}
if err := jsonDecode(m, b); err != nil {
return nil, err
}
return m, nil
}
func (s *Store) UpdateSlotMapping(m *SlotMapping) error {
return s.client.Update(s.SlotPath(m.Id), m.Encode())
}
func (s *Store) ListGroup() (map[int]*Group, error) {
paths, err := s.client.List(s.GroupDir(), false)
if err != nil {
return nil, err
}
group := make(map[int]*Group)
for _, path := range paths {
b, err := s.client.Read(path, true)
if err != nil {
return nil, err
}
g := &Group{}
if err := jsonDecode(g, b); err != nil {
return nil, err
}
group[g.Id] = g
}
return group, nil
}
func (s *Store) LoadGroup(gid int, must bool) (*Group, error) {
b, err := s.client.Read(s.GroupPath(gid), must)
if err != nil || b == nil {
return nil, err
}
g := &Group{}
if err := jsonDecode(g, b); err != nil {
return nil, err
}
return g, nil
}
func (s *Store) UpdateGroup(g *Group) error {
return s.client.Update(s.GroupPath(g.Id), g.Encode())
}
func (s *Store) DeleteGroup(gid int) error {
return s.client.Delete(s.GroupPath(gid))
}
func (s *Store) ListProxy() (map[string]*Proxy, error) {
paths, err := s.client.List(s.ProxyDir(), false)
if err != nil {
return nil, err
}
proxy := make(map[string]*Proxy)
for _, path := range paths {
b, err := s.client.Read(path, true)
if err != nil {
return nil, err
}
p := &Proxy{}
if err := jsonDecode(p, b); err != nil {
return nil, err
}
proxy[p.Token] = p
}
return proxy, nil
}
func (s *Store) LoadProxy(token string, must bool) (*Proxy, error) {
b, err := s.client.Read(s.ProxyPath(token), must)
if err != nil || b == nil {
return nil, err
}
p := &Proxy{}
if err := jsonDecode(p, b); err != nil {
return nil, err
}
return p, nil
}
func (s *Store) UpdateProxy(p *Proxy) error {
return s.client.Update(s.ProxyPath(p.Token), p.Encode())
}
func (s *Store) DeleteProxy(token string) error {
return s.client.Delete(s.ProxyPath(token))
}
func (s *Store) LoadSentinel(must bool) (*Sentinel, error) {
b, err := s.client.Read(s.SentinelPath(), must)
if err != nil || b == nil {
return nil, err
}
p := &Sentinel{}
if err := jsonDecode(p, b); err != nil {
return nil, err
}
return p, nil
}
func (s *Store) UpdateSentinel(p *Sentinel) error {
return s.client.Update(s.SentinelPath(), p.Encode())
}
func ValidateProduct(name string) error {
if regexp.MustCompile(`^\w[\w\.\-]*$`).MatchString(name) {
return nil
}
return errors.Errorf("bad product name = %s", name)
}