forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.go
102 lines (89 loc) · 2.94 KB
/
sequence.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
package etcd
import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"github.com/coreos/etcd/clientv3"
)
var initialItemKey []byte
func init() {
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, uint64(0)); err != nil {
// Should never happen
panic(err)
}
initialItemKey = buf.Bytes()
}
// Sequence provides an incrementing sequence ID. The ID is a big-endian
// encoded uint64 value that starts at 0.
//
// If the key provided does not contain a sequence yet, one is created and
// incremented to 1. If the sequence already exists, then the next ID in the
// sequence will be returned.
//
// Because sequence IDs are big-endian encoded strings, they can be ordered
// with lexicographic sorting. This makes them a useful key for ordered
// collections.
//
// Sequence is not transactional, but is safe to use concurrently. If two
// clients are both using Sequence on the same key, they will race to be the
// one who updates the sequence. The loser of the race will execute the routine
// again.
func Sequence(ctx context.Context, kv clientv3.KV, key string) (result string, err error) {
// Get the current key, or initialize it to be the first item key
seqs, err := Sequences(ctx, kv, key, 1)
if err != nil {
return "", err
}
if len(seqs) == 0 {
return "", errors.New("sequences returned no results")
}
return seqs[0], nil
}
// Sequences is like Sequence, but returns a slice of sequences whose length is
// equal to values.
func Sequences(ctx context.Context, kv clientv3.KV, key string, values int) ([]string, error) {
// Get the current key, or initialize it to be the first item key
if values == 0 {
return nil, nil
}
if values < 0 {
return nil, fmt.Errorf("negative values requested")
}
exists := clientv3.Compare(clientv3.Version(key), ">", 0)
put := clientv3.OpPut(key, string(initialItemKey))
get := clientv3.OpGet(key)
resp, err := kv.Txn(context.Background()).If(exists).Then(get).Else(put, get).Commit()
if err != nil {
return nil, fmt.Errorf("sequence error: %s", err)
}
respIdx := len(resp.Responses) - 1
value := resp.Responses[respIdx].GetResponseRange().Kvs[0].Value
// decode the key into an integer
var n uint64
if err := binary.Read(bytes.NewReader(value), binary.BigEndian, &n); err != nil {
return nil, fmt.Errorf("sequence error reading sequence: %s", err)
}
var buf *bytes.Buffer
result := make([]string, values)
for i := range result {
buf = new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, n+1); err != nil {
return nil, fmt.Errorf("sequence error writing sequence: %s", err)
}
result[i] = buf.String()
n++
}
notModified := clientv3.Compare(clientv3.Value(key), "=", string(value))
put = clientv3.OpPut(key, buf.String())
resp, err = kv.Txn(context.Background()).If(notModified).Then(put).Commit()
if err != nil {
return nil, fmt.Errorf("sequence error: %s", err)
}
if !resp.Succeeded {
return Sequences(ctx, kv, key, values)
}
return result, nil
}