-
Notifications
You must be signed in to change notification settings - Fork 796
/
util.go
82 lines (66 loc) · 1.74 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
package ring
import (
"context"
"math/rand"
"time"
"github.com/cortexproject/cortex/pkg/util"
)
// GenerateTokens make numTokens unique random tokens, none of which clash
// with takenTokens.
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)
for _, v := range takenTokens {
used[v] = true
}
tokens := []uint32{}
for i := 0; i < numTokens; {
candidate := r.Uint32()
if used[candidate] {
continue
}
used[candidate] = true
tokens = append(tokens, candidate)
i++
}
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 IngesterState) 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()
}