-
Notifications
You must be signed in to change notification settings - Fork 808
/
Copy pathpool.go
216 lines (186 loc) · 5.51 KB
/
pool.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
package client
import (
"context"
"flag"
"fmt"
"io"
"sync"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"google.golang.org/grpc/health/grpc_health_v1"
"github.com/cortexproject/cortex/pkg/ring"
"github.com/cortexproject/cortex/pkg/util"
"github.com/weaveworks/common/user"
)
var clients = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "cortex",
Name: "distributor_ingester_clients",
Help: "The current number of ingester clients.",
})
// Factory defines the signature for an ingester client factory.
type Factory func(addr string) (grpc_health_v1.HealthClient, error)
// PoolConfig is config for creating a Pool.
type PoolConfig struct {
ClientCleanupPeriod time.Duration `yaml:"client_cleanup_period,omitempty"`
HealthCheckIngesters bool `yaml:"health_check_ingesters,omitempty"`
RemoteTimeout time.Duration
}
// RegisterFlags adds the flags required to config this to the given FlagSet.
func (cfg *PoolConfig) RegisterFlags(f *flag.FlagSet) {
f.DurationVar(&cfg.ClientCleanupPeriod, "distributor.client-cleanup-period", 15*time.Second, "How frequently to clean up clients for ingesters that have gone away.")
f.BoolVar(&cfg.HealthCheckIngesters, "distributor.health-check-ingesters", false, "Run a health check on each ingester client during periodic cleanup.")
}
// Pool holds a cache of grpc_health_v1 clients.
type Pool struct {
cfg PoolConfig
ring ring.ReadRing
factory Factory
logger log.Logger
quit chan struct{}
done sync.WaitGroup
sync.RWMutex
clients map[string]grpc_health_v1.HealthClient
}
// NewPool creates a new Pool.
func NewPool(cfg PoolConfig, ring ring.ReadRing, factory Factory, logger log.Logger) *Pool {
p := &Pool{
cfg: cfg,
ring: ring,
factory: factory,
logger: logger,
quit: make(chan struct{}),
clients: map[string]grpc_health_v1.HealthClient{},
}
p.done.Add(1)
go p.loop()
return p
}
func (p *Pool) loop() {
defer p.done.Done()
cleanupClients := time.NewTicker(p.cfg.ClientCleanupPeriod)
defer cleanupClients.Stop()
for {
select {
case <-cleanupClients.C:
p.removeStaleClients()
if p.cfg.HealthCheckIngesters {
p.cleanUnhealthy()
}
case <-p.quit:
return
}
}
}
// Stop the pool's background cleanup goroutine.
func (p *Pool) Stop() {
close(p.quit)
p.done.Wait()
}
func (p *Pool) fromCache(addr string) (grpc_health_v1.HealthClient, bool) {
p.RLock()
defer p.RUnlock()
client, ok := p.clients[addr]
return client, ok
}
// GetClientFor gets the client for the specified address. If it does not exist it will make a new client
// at that address
func (p *Pool) GetClientFor(addr string) (grpc_health_v1.HealthClient, error) {
client, ok := p.fromCache(addr)
if ok {
return client, nil
}
p.Lock()
defer p.Unlock()
client, ok = p.clients[addr]
if ok {
return client, nil
}
client, err := p.factory(addr)
if err != nil {
return nil, err
}
p.clients[addr] = client
clients.Add(1)
return client, nil
}
// RemoveClientFor removes the client with the specified address
func (p *Pool) RemoveClientFor(addr string) {
p.Lock()
defer p.Unlock()
client, ok := p.clients[addr]
if ok {
delete(p.clients, addr)
clients.Add(-1)
// Close in the background since this operation may take awhile and we have a mutex
go func(addr string, closer io.Closer) {
if err := closer.Close(); err != nil {
level.Error(p.logger).Log("msg", "error closing connection to ingester", "ingester", addr, "err", err)
}
}(addr, client.(io.Closer))
}
}
// RegisteredAddresses returns all the addresses that a client is cached for
func (p *Pool) RegisteredAddresses() []string {
result := []string{}
p.RLock()
defer p.RUnlock()
for addr := range p.clients {
result = append(result, addr)
}
return result
}
// Count returns how many clients are in the cache
func (p *Pool) Count() int {
p.RLock()
defer p.RUnlock()
return len(p.clients)
}
func (p *Pool) removeStaleClients() {
clients := map[string]struct{}{}
replicationSet, err := p.ring.GetAll()
if err != nil {
level.Error(util.Logger).Log("msg", "error removing stale clients", "err", err)
return
}
for _, ing := range replicationSet.Ingesters {
clients[ing.Addr] = struct{}{}
}
for _, addr := range p.RegisteredAddresses() {
if _, ok := clients[addr]; ok {
continue
}
level.Info(util.Logger).Log("msg", "removing stale client", "addr", addr)
p.RemoveClientFor(addr)
}
}
// cleanUnhealthy loops through all ingesters and deletes any that fails a healtcheck.
func (p *Pool) cleanUnhealthy() {
for _, addr := range p.RegisteredAddresses() {
client, ok := p.fromCache(addr)
// not ok means someone removed a client between the start of this loop and now
if ok {
err := healthCheck(client, p.cfg.RemoteTimeout)
if err != nil {
level.Warn(util.Logger).Log("msg", "removing ingester failing healtcheck", "addr", addr, "reason", err)
p.RemoveClientFor(addr)
}
}
}
}
// healthCheck will check if the client is still healthy, returning an error if it is not
func healthCheck(client grpc_health_v1.HealthClient, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
ctx = user.InjectOrgID(ctx, "0")
resp, err := client.Check(ctx, &grpc_health_v1.HealthCheckRequest{})
if err != nil {
return err
}
if resp.Status != grpc_health_v1.HealthCheckResponse_SERVING {
return fmt.Errorf("Failing healthcheck status: %s", resp.Status)
}
return nil
}