forked from hypermodeinc/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
296 lines (259 loc) · 7.1 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
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
/*
* Copyright 2016-2018 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package conn
import (
"context"
"crypto/tls"
"sync"
"time"
"github.com/dgraph-io/dgo/v210/protos/api"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/x"
"github.com/dgraph-io/ristretto/z"
"github.com/golang/glog"
"github.com/pkg/errors"
"go.opencensus.io/plugin/ocgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
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
}
// 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
}
// 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)
}
}
}
func (p *Pools) remove(addr string) {
p.Lock()
defer p.Unlock()
pool, ok := p.all[addr]
if !ok {
return
}
glog.Warningf("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("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("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(&ocgrpc.ClientHandler{}),
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.WithInsecure())
}
conn, err := grpc.Dial(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(), 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("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(context.Background())
defer cancel()
s, err := c.Heartbeat(ctx, &api.Payload{})
if err != nil {
return err
}
go func() {
select {
case <-ctx.Done():
case <-p.closer.HasBeenClosed():
cancel()
}
}()
// This loop can block indefinitely as long as it keeps on receiving pings back.
for {
res, err := s.Recv()
if err != nil || res == nil {
return err
}
// We do this periodic stream receive based approach to defend against network partitions.
p.Lock()
p.lastEcho = time.Now()
p.healthInfo = *res
p.Unlock()
}
}
// MonitorHealth monitors the health of the connection via Echo. This function blocks forever.
func (p *Pool) MonitorHealth() {
defer p.closer.Done()
var lastErr error
for {
select {
case <-p.closer.HasBeenClosed():
return
default:
err := p.listenToHeartbeat()
if lastErr != nil && err == nil {
glog.Infof("Connection re-established with %v\n", p.Addr)
} else if err != nil && lastErr == nil {
glog.Warningf("Connection lost with %v. Error: %v\n", p.Addr, err)
}
lastErr = err
// Sleep for a bit before retrying.
time.Sleep(echoDuration)
}
}
}
// 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 {
p.RLock()
defer p.RUnlock()
p.healthInfo.Status = "healthy"
if !p.IsHealthy() {
p.healthInfo.Status = "unhealthy"
}
p.healthInfo.LastEcho = p.lastEcho.Unix()
return p.healthInfo
}