-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
549 lines (438 loc) · 11.3 KB
/
server.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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"regexp"
"time"
"github.com/garyburd/redigo/redis"
)
var (
// DefaultIdleTimeout sets the duration after which idle Redis connections
// in the pool are closed.
DefaultIdleTimeout = 5 * time.Minute
// DefaultMaxIdle is the number of idle Redis connections allowed in the pool.
DefaultMaxIdle = 3
// ErrNoDef is returned when a user attempts to get a definition that does
// not exist.
ErrNoDef = errors.New("no def")
// ErrDefExists is returned when the user attempts to create a definition
// that already exists.
ErrDefExists = errors.New("def exists")
// ErrBadDefName is returned when a user attempts to create a definition
// with a bad name.
ErrBadDefName = errors.New("name may only contain [A-Za-z0-9-_.] chars")
// MaxAttempts is the max number of alias generation attempts to make
// before giving up on finding a new, unused, alias.
MaxAttempts = 100
// ErrMaxAttemptsReached is returned if MaxAttempts generation attempts
// are made and all of the generated aliases already exist.
ErrMaxAttemptsReached = errors.New("max attempts reached")
// Def name validation regex.
nameRegex *regexp.Regexp
// Unused regex?
splitRegex *regexp.Regexp
// Prefix for internal use.
internalPrefix = "_:%s"
// Prefix for index definitions.
defPrefix = "d:%s"
valuePrefix = "v:%d"
seqPrefix = "s:%d"
// Prefix for keys, aliases, and sequences.
// These are scoped by the definition id.
keyPrefix = "k:%d:%s"
aliasPrefix = "a:%d:%s"
)
func mk(f string, v ...interface{}) string {
return fmt.Sprintf(f, v...)
}
func init() {
nameRegex = regexp.MustCompile(`^[A-Za-z0-9-_\.]+$`)
splitRegex = regexp.MustCompile(`[\s,\t]+`)
}
// Status constants to communicate the state of the underlying key.
const (
StatusExists = Status(iota + 1)
StatusCreated
StatusMissing
)
// Status represents the state of some key underlying the service.
type Status int
// String returns a string representation of a Status.
func (s Status) String() string {
switch s {
case StatusExists:
return "exists"
case StatusCreated:
return "created"
case StatusMissing:
return "missing"
}
return ""
}
// MarshalJSON returns a JSON representation of a Status.
func (s Status) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
// IdentAlias represents an identity and an alias for that identity along
// with the state of the underlying key representing it.
type IdentAlias struct {
Ident string `json:"ident"`
Alias string `json:"alias,omitempty"`
Status Status `json:"status,omitempty"`
}
// Server serves the alias service.
type Server struct {
RedisAddr string
RedisDB int
RedisPass string
RedisTLS bool
Log *log.Logger
Pool *redis.Pool
}
func (s *Server) handleClose(c io.Closer) {
err := c.Close()
if err != nil {
s.Log.Printf("close error: %s\n", err)
}
}
// Close shuts down the server.
func (s *Server) Close() {
if s.Pool != nil {
s.handleClose(s.Pool)
}
}
// Init initializes a new server.
func (s *Server) Init() {
s.Log = log.New(os.Stderr, "aliases: ", 0)
// Create a pool of Redis connections.
s.Pool = &redis.Pool{
Dial: func() (redis.Conn, error) {
return redis.Dial(
"tcp",
s.RedisAddr,
redis.DialDatabase(s.RedisDB),
redis.DialPassword(s.RedisPass),
redis.DialUseTLS(s.RedisTLS),
)
},
IdleTimeout: DefaultIdleTimeout,
MaxIdle: DefaultMaxIdle,
}
}
// GetDefs retrieves multiple existing alias generation definitions.
func (s *Server) GetDefs() ([]json.RawMessage, error) {
conn := s.Pool.Get()
defer s.handleClose(conn)
// Keys of the definitions.
keys, err := redis.Strings(conn.Do("KEYS", "v:*"))
if err != nil {
return nil, err
}
if len(keys) == 0 {
return []json.RawMessage{}, nil
}
args := make([]interface{}, len(keys))
for i, k := range keys {
args[i] = k
}
vals, err := redis.Strings(conn.Do("MGET", args...))
if err != nil {
return nil, err
}
defs := make([]json.RawMessage, len(vals))
for i, val := range vals {
defs[i] = json.RawMessage(val)
}
return defs, nil
}
// DelDef marks a index for deletion.
func (s *Server) DelDef(name string) error {
def, err := s.GetDef(name)
if err != nil {
return err
}
// Internally mark as deleted to be cleaned up.
def.Deleted = true
b, err := json.Marshal(def)
if err != nil {
return err
}
conn := s.Pool.Get()
defer s.handleClose(conn)
// Delete name entry to make inaccessable and update definition.
conn.Send("MULTI")
conn.Send("DEL", mk(defPrefix, name))
conn.Send("SET", mk(valuePrefix, def.ID), string(b))
if _, err := conn.Do("EXEC"); err != nil {
return err
}
s.Log.Printf("deleted '%s'", def.Name)
return nil
}
// GetDef retrieves an existing alias generation definition.
func (s *Server) GetDef(name string) (*Def, error) {
conn := s.Pool.Get()
defer s.handleClose(conn)
id, err := redis.Int64(conn.Do("GET", mk(defPrefix, name)))
if err == redis.ErrNil {
return nil, ErrNoDef
} else if err != nil {
return nil, err
}
blob, err := redis.Bytes(conn.Do("GET", mk(valuePrefix, id)))
if err != nil {
return nil, err
}
if blob == nil {
panic(fmt.Sprintf("missing def value for %s", name))
}
var g Def
if err := json.Unmarshal(blob, &g); err != nil {
return nil, err
}
return &g, nil
}
func (s *Server) validateDef(def *Def) error {
if def.Name == "" {
return errors.New("name required")
}
if !nameRegex.MatchString(def.Name) {
return ErrBadDefName
}
if def.Type == "" {
return errors.New("type required")
}
switch def.Type {
case "seq":
case "rand":
if def.Minlen < MinRandMinlen {
return errors.New("rand min length too small")
}
if len(def.Chars) < MinRandChars {
return errors.New("too few chars for rand")
}
case "uuid":
default:
return errors.New("unknown type")
}
return nil
}
// CreateDef creates a new index for generating aliases.
// d:foo -> 0
// v:0 -> { ... }
func (s *Server) CreateDef(def *Def) error {
if err := s.validateDef(def); err != nil {
return err
}
// Check if there is an existing definition.
conn := s.Pool.Get()
defer s.handleClose(conn)
// Lookup up def by name.
defKey := mk(defPrefix, def.Name)
exists, err := redis.Bool(conn.Do("EXISTS", defKey))
if err != nil {
return err
}
// Cannot create a def by the same name.
if exists {
return ErrDefExists
}
// Get a new key.
defIDKey := mk(internalPrefix, "def:id")
id, err := redis.Int64(conn.Do("INCR", defIDKey))
if err != nil {
return err
}
def.ID = int(id)
b, err := json.Marshal(def)
if err != nil {
return err
}
valueKey := mk(valuePrefix, def.ID)
args := []interface{}{
defKey, def.ID,
valueKey, string(b),
}
// Initialize the sequence.
if def.Type == "seq" {
seqKey := mk(seqPrefix, def.ID)
args = append(args, seqKey, def.Offset)
}
_, err = conn.Do("MSET", args...)
if err != nil {
return err
}
s.Log.Printf("created def '%s' (id=%d)", def.Name, def.ID)
return nil
}
// UpdateDef updates an existing alias generation definition.
func (s *Server) UpdateDef(name string, def *Def) error {
if err := s.validateDef(def); err != nil {
return err
}
// Check if there is an existing definition.
conn := s.Pool.Get()
defer s.handleClose(conn)
b, err := json.Marshal(def)
if err != nil {
return err
}
// Delete previous definition.
if name != def.Name {
_, err = conn.Do("DEL", mk(defPrefix, name))
if err != nil {
return err
}
}
// Set name and value key.
defKey := mk(defPrefix, def.Name)
valueKey := mk(valuePrefix, def.ID)
_, err = conn.Do("MSET", defKey, def.ID, valueKey, string(b))
if err != nil {
return err
}
s.Log.Printf("updated def '%s'", def.Name)
return nil
}
// Gen generates a new alias for a slice of identities, given an existing definition.
// It will keep trying to find a new, unused, alias for MaxAttempts before
// returning ErrMaxAttemptsReached.
func (s *Server) Gen(def *Def, idents []*IdentAlias) ([]*IdentAlias, error) {
conn := s.Pool.Get()
defer s.handleClose(conn)
// Generator for this line.
gen := MakeGen(conn, def)
for _, ia := range idents {
if ia.Ident == "" {
continue
}
lookupKey := mk(keyPrefix, def.ID, ia.Ident)
// Check if the key already exists. If so, just return it.
alias, err := redis.String(conn.Do("GET", lookupKey))
// Exists.
if err == nil {
ia.Alias = alias
ia.Status = StatusExists
continue
}
if err != nil && err != redis.ErrNil {
return nil, err
}
var attempt int
for {
if attempt == MaxAttempts {
s.Log.Printf("max attempts reached for '%s' in '%s'", lookupKey, def.Name)
// TODO: auto-increase minlenth if this occurs.
return nil, ErrMaxAttemptsReached
}
attempt++
// Generate new key.
alias, err = gen.New()
if err != nil {
return nil, err
}
// Check if it exists, otherwise set it.
checkKey := mk(aliasPrefix, def.ID, alias)
ok, err := redis.Bool(conn.Do("EXISTS", checkKey))
if err != nil {
return nil, err
}
// Does not exist, set it.
if !ok {
_, err := conn.Do("MSET", lookupKey, alias, checkKey, true)
if err != nil {
return nil, err
}
ia.Alias = alias
ia.Status = StatusCreated
// TODO: add metric for number of attempts. this is an indicator
// to whether the min length should be increased.
break
}
}
}
return idents, nil
}
// Get retrieves existing aliases for a slice of identities in a given alias definition.
func (s *Server) Get(def *Def, idents []*IdentAlias) ([]*IdentAlias, error) {
conn := s.Pool.Get()
defer s.handleClose(conn)
for _, ia := range idents {
lookupKey := mk(keyPrefix, def.ID, ia.Ident)
// Check if the key already exists. If so, just return it.
alias, err := redis.String(conn.Do("GET", lookupKey))
// Exists.
if err == nil {
ia.Alias = alias
ia.Status = StatusExists
continue
}
if err != nil && err != redis.ErrNil {
return nil, err
}
ia.Status = StatusMissing
}
return idents, nil
}
// Put explicitly sets a set of IDs with an alias.
func (s *Server) Put(def *Def, idents []*IdentAlias) error {
conn := s.Pool.Get()
defer s.handleClose(conn)
for _, ia := range idents {
if ia.Ident == "" {
continue
}
if ia.Alias == "" {
return errors.New("empty alias")
}
// key to alias
lookupKey := mk(keyPrefix, def.ID, ia.Ident)
// alias entry for existence check.
checkKey := mk(aliasPrefix, def.ID, ia.Alias)
_, err := conn.Do("MSET", lookupKey, ia.Alias, checkKey, true)
if err != nil {
return err
}
}
s.Log.Printf("put %d keys", len(idents))
return nil
}
// Del deletes a slice of identities from an alias generation definition.
func (s *Server) Del(def *Def, idents []string) error {
conn := s.Pool.Get()
defer s.handleClose(conn)
var (
removedCount int
skippedCount int
conflictCount int
internalCount int
)
for _, ident := range idents {
lookupKey := mk(keyPrefix, def.ID, ident)
// Get the corresponding alias.
alias, err := redis.String(conn.Do("GET", lookupKey))
if err == redis.ErrNil {
skippedCount++
continue
}
if err != nil {
return err
}
removedCount++
checkKey := mk(aliasPrefix, def.ID, alias)
n, err := redis.Int64(conn.Do("DEL", lookupKey, checkKey))
if err != nil {
return err
}
internalCount += int(n)
}
s.Log.Printf("%d removed", removedCount)
s.Log.Printf("%d skipped", skippedCount)
s.Log.Printf("%d conflicts", conflictCount)
s.Log.Printf("%d internal", internalCount)
return nil
}