-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathpool.go
More file actions
376 lines (333 loc) · 8.88 KB
/
pool.go
File metadata and controls
376 lines (333 loc) · 8.88 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* SPDX-FileCopyrightText: © 2017-2025 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package conn
import (
"context"
"crypto/tls"
"fmt"
"sync"
"time"
"github.com/golang/glog"
"github.com/pkg/errors"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"github.com/dgraph-io/dgo/v250/protos/api"
"github.com/dgraph-io/dgraph/v25/protos/pb"
"github.com/dgraph-io/dgraph/v25/x"
"github.com/dgraph-io/ristretto/v2/z"
)
var (
// ErrNoConnection indicates no connection exists to a node.
ErrNoConnection = errors.New("No connection exists")
// ErrUnhealthyConnection indicates the connection to a node is unhealthy.
ErrUnhealthyConnection = errors.New("Unhealthy connection")
echoDuration = 500 * time.Millisecond
)
// Pool is used to manage the grpc client connection(s) for communicating with other
// worker instances. Right now it just holds one of them.
type Pool struct {
sync.RWMutex
// A pool now consists of one connection. gRPC uses HTTP2 transport to combine
// messages in the same TCP stream.
conn *grpc.ClientConn
lastEcho time.Time
Addr string
closer *z.Closer
healthInfo pb.HealthInfo
dialOpts []grpc.DialOption
}
// Pools manages a concurrency-safe set of Pool.
type Pools struct {
sync.RWMutex
all map[string]*Pool
}
var pi *Pools
func init() {
pi = new(Pools)
pi.all = make(map[string]*Pool)
}
// GetPools returns the list of pools.
func GetPools() *Pools {
return pi
}
// Get returns the list for the given address.
func (p *Pools) Get(addr string) (*Pool, error) {
p.RLock()
defer p.RUnlock()
pool, ok := p.all[addr]
if !ok {
return nil, ErrNoConnection
}
if !pool.IsHealthy() {
return nil, ErrUnhealthyConnection
}
return pool, nil
}
// GetAll returns all pool entries.
func (p *Pools) GetAll() []*Pool {
p.RLock()
defer p.RUnlock()
var pool []*Pool
for _, v := range p.all {
pool = append(pool, v)
}
return pool
}
// RemoveAll removes all pool entries.
func (p *Pools) RemoveAll() {
p.Lock()
defer p.Unlock()
for k, pool := range p.all {
glog.Warningf("CONN: Disconnecting from %s\n", k)
delete(p.all, k)
pool.shutdown()
}
}
// RemoveInvalid removes invalid nodes from the list of pools.
func (p *Pools) RemoveInvalid(state *pb.MembershipState) {
// Keeps track of valid IP addresses, assigned to active nodes. We do this
// to avoid removing valid IP addresses from the Removed list.
validAddr := make(map[string]struct{})
for _, group := range state.Groups {
for _, member := range group.Members {
validAddr[member.Addr] = struct{}{}
}
}
for _, member := range state.Zeros {
validAddr[member.Addr] = struct{}{}
}
for _, member := range state.Removed {
// Some nodes could have the same IP address. So, check before disconnecting.
if _, valid := validAddr[member.Addr]; !valid {
p.remove(member.Addr)
}
}
}
// Remove disconnects and removes the pool for addr. It is a no-op if addr is
// not in the pool. Used to clean up stale connections after an address change.
func (p *Pools) Remove(addr string) {
p.remove(addr)
}
func (p *Pools) remove(addr string) {
p.Lock()
defer p.Unlock()
pool, ok := p.all[addr]
if !ok {
return
}
glog.Warningf("CONN: Disconnecting from %s\n", addr)
delete(p.all, addr)
pool.shutdown()
}
func (p *Pools) getPool(addr string) (*Pool, bool) {
p.RLock()
defer p.RUnlock()
existingPool, has := p.all[addr]
return existingPool, has
}
// Connect creates a Pool instance for the node with the given address or returns the existing one.
func (p *Pools) Connect(addr string, tlsClientConf *tls.Config) *Pool {
existingPool, has := p.getPool(addr)
if has {
return existingPool
}
pool, err := newPool(addr, tlsClientConf)
if err != nil {
glog.Errorf("CONN: Unable to connect to host: %s", addr)
return nil
}
p.Lock()
defer p.Unlock()
existingPool, has = p.all[addr]
if has {
go pool.shutdown() // Not being used, so release the resources.
return existingPool
}
glog.Infof("CONN: Connecting to %s\n", addr)
p.all[addr] = pool
return pool
}
// newPool creates a new "pool" with one gRPC connection, refcount 0.
func newPool(addr string, tlsClientConf *tls.Config) (*Pool, error) {
conOpts := []grpc.DialOption{
grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(x.GrpcMaxSize),
grpc.MaxCallSendMsgSize(x.GrpcMaxSize),
grpc.UseCompressor((snappyCompressor{}).Name())),
grpc.WithBackoffMaxDelay(time.Second),
}
if tlsClientConf != nil {
conOpts = append(conOpts, grpc.WithTransportCredentials(credentials.NewTLS(tlsClientConf)))
} else {
conOpts = append(conOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
conn, err := grpc.NewClient(addr, conOpts...)
if err != nil {
glog.Errorf("unable to connect with %s : %s", addr, err)
return nil, err
}
pl := &Pool{
conn: conn,
Addr: addr,
lastEcho: time.Now(),
dialOpts: conOpts,
closer: z.NewCloser(1),
}
go pl.MonitorHealth()
return pl, nil
}
// Get returns the connection to use from the pool of connections.
func (p *Pool) Get() *grpc.ClientConn {
p.RLock()
defer p.RUnlock()
return p.conn
}
func (p *Pool) shutdown() {
glog.Warningf("CONN: Shutting down extra connection to %s", p.Addr)
p.closer.SignalAndWait()
if err := p.conn.Close(); err != nil {
glog.Warningf("Could not close pool connection with error: %s", err)
}
}
// SetUnhealthy marks a pool as unhealthy.
func (p *Pool) SetUnhealthy() {
p.Lock()
defer p.Unlock()
p.lastEcho = time.Time{}
}
func (p *Pool) listenToHeartbeat() error {
conn := p.Get()
c := pb.NewRaftClient(conn)
ctx, cancel := context.WithCancel(p.closer.Ctx())
defer cancel()
s, err := c.Heartbeat(ctx, &api.Payload{})
if err != nil {
return err
}
go func() {
for {
res, err := s.Recv()
if err != nil || res == nil {
cancel()
return
}
// We do this periodic stream receive based approach to defend against network partitions.
p.Lock()
p.lastEcho = time.Now()
p.healthInfo = *res
p.Unlock()
}
}()
threshold := time.Now().Add(10 * time.Second)
ticker := time.Tick(time.Second)
for {
select {
case <-ticker:
// Don't check before at least 10s since start.
if time.Now().Before(threshold) {
continue
}
p.RLock()
lastEcho := p.lastEcho
p.RUnlock()
if dur := time.Since(lastEcho); dur > 30*time.Second {
glog.Warningf("CONN: No echo to %s for %s. Cancelling connection heartbeats.\n",
p.Addr, dur.Round(time.Second))
cancel()
return fmt.Errorf("too long since last echo")
}
case <-s.Context().Done():
return s.Context().Err()
case <-ctx.Done():
return ctx.Err()
case <-p.closer.HasBeenClosed():
cancel()
return p.closer.Ctx().Err()
}
}
}
// MonitorHealth monitors the health of the connection via Echo. This function blocks forever.
func (p *Pool) MonitorHealth() {
defer p.closer.Done()
// We might have lost connection to the destination. In that case, re-dial
// the connection.
// Returns true, if reconnection was successful
reconnect := func() bool {
reconnectionTicker := time.Tick(time.Second)
for {
select {
case <-p.closer.HasBeenClosed():
glog.Infof("CONN: Returning from MonitorHealth for %s", p.Addr)
return false
case <-reconnectionTicker:
}
if err := p.closer.Ctx().Err(); err != nil {
return false
}
ctx, cancel := context.WithTimeout(p.closer.Ctx(), 10*time.Second)
conn, err := grpc.NewClient(p.Addr, p.dialOpts...)
if err == nil {
// Make a dummy request to test out the connection.
client := pb.NewRaftClient(conn)
_, err = client.IsPeer(ctx, &pb.RaftContext{})
}
cancel()
if err == nil {
p.Lock()
if err := p.conn.Close(); err != nil {
glog.Warningf("error while closing connection: %v", err)
}
p.conn = conn
p.Unlock()
return true
}
glog.Errorf("CONN: Unable to connect with %s : %s\n", p.Addr, err)
if conn != nil {
if err := conn.Close(); err != nil {
glog.Warningf("error while closing connection: %v", err)
}
}
}
}
ticker := time.Tick(time.Second)
for {
select {
case <-p.closer.HasBeenClosed():
glog.Infof("CONN: Returning from MonitorHealth for %s", p.Addr)
return
case <-ticker:
}
err := p.listenToHeartbeat()
if err != nil {
if reconnect() {
glog.Infof("CONN: Re-established connection with %s.\n", p.Addr)
}
}
}
}
// IsHealthy returns whether the pool is healthy.
func (p *Pool) IsHealthy() bool {
if p == nil {
return false
}
p.RLock()
defer p.RUnlock()
return time.Since(p.lastEcho) < 4*echoDuration
}
// HealthInfo returns the healthinfo.
func (p *Pool) HealthInfo() pb.HealthInfo {
ok := p.IsHealthy()
p.Lock()
defer p.Unlock()
p.healthInfo.Status = "healthy"
if !ok {
p.healthInfo.Status = "unhealthy"
}
p.healthInfo.LastEcho = p.lastEcho.Unix()
return p.healthInfo
}