-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
id.go
90 lines (73 loc) · 1.74 KB
/
id.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
package bolt
import (
"errors"
"fmt"
"math/rand"
bolt "github.com/coreos/bbolt"
platform "github.com/influxdata/influxdb/v2"
"go.uber.org/zap"
)
var (
idsBucket = []byte("idsv1")
idKey = []byte("id")
errIDNotFound = errors.New("source not found")
)
var _ platform.IDGenerator = (*Client)(nil)
func (c *Client) initializeID(tx *bolt.Tx) error {
if _, err := tx.CreateBucketIfNotExists(idsBucket); err != nil {
return err
}
_, err := c.getID(tx)
if err != nil && err != errIDNotFound {
return err
}
if err == errIDNotFound {
if err := c.generateID(tx); err != nil {
return err
}
}
return nil
}
// ID retrieves the unique ID for this influx instance.
func (c *Client) ID() platform.ID {
// if any error occurs return a random number
id := platform.ID(rand.Int63())
err := c.db.View(func(tx *bolt.Tx) error {
val, err := c.getID(tx)
if err != nil {
return err
}
id = val
return nil
})
if err != nil {
c.log.Error("Unable to load id", zap.Error(err))
}
return id
}
func (c *Client) getID(tx *bolt.Tx) (platform.ID, error) {
v := tx.Bucket(idsBucket).Get(idKey)
if len(v) == 0 {
return platform.InvalidID(), errIDNotFound
}
return decodeID(v)
}
func decodeID(val []byte) (platform.ID, error) {
if len(val) < platform.IDLength {
// This should not happen.
return platform.InvalidID(), fmt.Errorf("provided value is too short to contain an ID. Please report this error")
}
var id platform.ID
if err := id.Decode(val[:platform.IDLength]); err != nil {
return platform.InvalidID(), err
}
return id, nil
}
func (c *Client) generateID(tx *bolt.Tx) error {
id := c.IDGenerator.ID()
encodedID, err := id.Encode()
if err != nil {
return err
}
return tx.Bucket(idsBucket).Put(idKey, encodedID)
}