-
Notifications
You must be signed in to change notification settings - Fork 786
/
util.go
154 lines (127 loc) · 3.87 KB
/
util.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
package ring
import (
"context"
"math/rand"
"sort"
"time"
"github.com/cortexproject/cortex/pkg/util"
)
// GenerateTokens make numTokens unique random tokens, none of which clash
// with takenTokens. Generated tokens are sorted.
func GenerateTokens(numTokens int, takenTokens []uint32) []uint32 {
if numTokens <= 0 {
return []uint32{}
}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
used := make(map[uint32]bool, len(takenTokens))
for _, v := range takenTokens {
used[v] = true
}
tokens := make([]uint32, 0, numTokens)
for i := 0; i < numTokens; {
candidate := r.Uint32()
if used[candidate] {
continue
}
used[candidate] = true
tokens = append(tokens, candidate)
i++
}
// Ensure returned tokens are sorted.
sort.Slice(tokens, func(i, j int) bool {
return tokens[i] < tokens[j]
})
return tokens
}
// GetInstanceAddr returns the address to use to register the instance
// in the ring.
func GetInstanceAddr(configAddr string, netInterfaces []string) (string, error) {
if configAddr != "" {
return configAddr, nil
}
addr, err := util.GetFirstAddressOf(netInterfaces)
if err != nil {
return "", err
}
return addr, nil
}
// GetInstancePort returns the port to use to register the instance
// in the ring.
func GetInstancePort(configPort, listenPort int) int {
if configPort > 0 {
return configPort
}
return listenPort
}
// WaitInstanceState waits until the input instanceID is registered within the
// ring matching the provided state. A timeout should be provided within the context.
func WaitInstanceState(ctx context.Context, r *Ring, instanceID string, state InstanceState) error {
backoff := util.NewBackoff(ctx, util.BackoffConfig{
MinBackoff: 100 * time.Millisecond,
MaxBackoff: time.Second,
MaxRetries: 0,
})
for backoff.Ongoing() {
if actualState, err := r.GetInstanceState(instanceID); err == nil && actualState == state {
return nil
}
backoff.Wait()
}
return backoff.Err()
}
// WaitRingStability monitors the ring topology for the provided operation and waits until it
// keeps stable for at least minStability.
func WaitRingStability(ctx context.Context, r *Ring, op Operation, minStability, maxWaiting time.Duration) error {
// Configure the max waiting time as a context deadline.
ctx, cancel := context.WithTimeout(ctx, maxWaiting)
defer cancel()
// Get the initial ring state.
ringLastState, _ := r.GetAllHealthy(op) // nolint:errcheck
ringLastStateTs := time.Now()
const pollingFrequency = time.Second
pollingTicker := time.NewTicker(pollingFrequency)
defer pollingTicker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-pollingTicker.C:
// We ignore the error because in case of error it will return an empty
// replication set which we use to compare with the previous state.
currRingState, _ := r.GetAllHealthy(op) // nolint:errcheck
if HasReplicationSetChanged(ringLastState, currRingState) {
ringLastState = currRingState
ringLastStateTs = time.Now()
} else if time.Since(ringLastStateTs) >= minStability {
return nil
}
}
}
}
// MakeBuffersForGet returns buffers to use with Ring.Get().
func MakeBuffersForGet() (bufDescs []InstanceDesc, bufHosts, bufZones []string) {
bufDescs = make([]InstanceDesc, 0, GetBufferSize)
bufHosts = make([]string, 0, GetBufferSize)
bufZones = make([]string, 0, GetBufferSize)
return
}
// getZones return the list zones from the provided tokens. The returned list
// is guaranteed to be sorted.
func getZones(tokens map[string][]uint32) []string {
var zones []string
for zone := range tokens {
zones = append(zones, zone)
}
sort.Strings(zones)
return zones
}
// searchToken returns the offset of the tokens entry holding the range for the provided key.
func searchToken(tokens []uint32, key uint32) int {
i := sort.Search(len(tokens), func(x int) bool {
return tokens[x] > key
})
if i >= len(tokens) {
i = 0
}
return i
}