-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcluster.go
350 lines (298 loc) · 9.47 KB
/
cluster.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
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
package cluster
import (
"context"
"path"
"sync"
"time"
"github.com/ab180/lrmr/cluster/node"
"github.com/ab180/lrmr/coordinator"
"github.com/ab180/lrmr/pkg/retry"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
clientv3 "go.etcd.io/etcd/client/v3"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
)
const nodeNs = "nodes"
// ErrNotFound is returned when an node with given host is not found.
var ErrNotFound = errors.New("node not found")
// State is cluster-wide state in coordinator.
// It is ensured to be permanent and consistent in distributed environment.
type State coordinator.Coordinator
type Cluster interface {
// Register registers node to the coordinator and makes it discoverable.
// registration will be automatically deleted if cluster's context is cancelled.
Register(context.Context, *node.Node) (node.Registration, error)
// Connect tries to connect the host and returns gRPC connection.
// The connection can be pooled and cached, and only one connection per host is maintained.
Connect(ctx context.Context, host string) (*grpc.ClientConn, error)
// List returns a list of available nodes.
List(context.Context, ...ListOption) ([]*node.Node, error)
// Get returns an information of node with the host.
// It returns ErrNotFound if node with given host does not exist.
Get(ctx context.Context, host string) (*node.Node, error)
// States returns a cluster-wide state.
States() State
// Close unregisters registered nodes and closes all connections.
Close() error
}
type cluster struct {
ctx context.Context
cancel context.CancelFunc
clusterState State
grpcOptions []grpc.DialOption
grpcConns map[string]*grpc.ClientConn
grpcConnsMu sync.Mutex
options Options
}
func OpenRemote(clusterState coordinator.Coordinator, opt Options) (Cluster, error) {
defaultCallOptions := []grpc.CallOption{
grpc.MaxCallRecvMsgSize(opt.MaxMessageSize),
grpc.MaxCallSendMsgSize(opt.MaxMessageSize),
}
if opt.Compressor != "" {
defaultCallOptions = append(defaultCallOptions, grpc.UseCompressor(opt.Compressor))
}
grpcOpts := []grpc.DialOption{
grpc.WithBlock(),
grpc.WithDefaultCallOptions(defaultCallOptions...),
}
if opt.TLSCertPath != "" {
cert, err := credentials.NewClientTLSFromFile(opt.TLSCertPath, opt.TLSCertServerName)
if err != nil {
return nil, errors.Wrapf(err, "load TLS cert in %s", opt.TLSCertPath)
}
grpcOpts = append(grpcOpts, grpc.WithTransportCredentials(cert))
} else {
// log.Warn("inter-node RPC is in insecure mode. we recommend configuring TLS credentials.")
grpcOpts = append(grpcOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
ctx, cancel := context.WithCancel(context.Background())
return &cluster{
ctx: ctx,
cancel: cancel,
grpcOptions: grpcOpts,
grpcConns: make(map[string]*grpc.ClientConn),
clusterState: clusterState,
options: opt,
}, nil
}
// Register registers node to the coordinator and makes it discoverable.
// registration will be automatically deleted if cluster's context is cancelled.
func (c *cluster) Register(ctx context.Context, n *node.Node) (node.Registration, error) {
return newNodeRegistration(ctx, n, c, c.options.LivenessProbeInterval)
}
// Connect tries to connect the host and returns gRPC connection.
// The connection can be pooled and cached, and only one connection per host is maintained.
func (c *cluster) Connect(ctx context.Context, host string) (*grpc.ClientConn, error) {
c.grpcConnsMu.Lock()
conn, ok := c.grpcConns[host]
c.grpcConnsMu.Unlock()
if ok && conn.GetState() == connectivity.Ready {
return conn, nil
}
return c.establishNewConnection(ctx, host)
}
// establishNewConnection creates a new connection to given host. the context is only used for
// dialing the host, and cancelling the context after the method return does not affect the connection.
//
// this method is not race-protected; you need to acquire lock before calling the method.
func (c *cluster) establishNewConnection(ctx context.Context, host string) (*grpc.ClientConn, error) {
log.Info().
Str("host", host).
Msg("establish new connection")
return retry.DoWithResult(
func() (*grpc.ClientConn, error) {
dialCtx, cancel := context.WithTimeout(ctx, c.options.ConnectTimeout)
defer cancel()
newConn, err := grpc.DialContext(
dialCtx,
host,
c.grpcOptions...,
)
if err != nil {
return nil, err
}
c.grpcConnsMu.Lock()
defer c.grpcConnsMu.Unlock()
oldConn, ok := c.grpcConns[host]
if ok && oldConn.GetState() == connectivity.Ready {
err := newConn.Close()
if err != nil {
log.Warn().
Err(err).
Str("host", host).
Msg("failed to close connection")
}
return oldConn, nil
}
c.grpcConns[host] = newConn
return newConn, nil
},
retry.WithRetryCount(c.options.ConnectRetryCount),
retry.WithDelay(c.options.ConnectRetryDelay),
)
}
// List returns a list of available nodes.
func (c *cluster) List(ctx context.Context, option ...ListOption) ([]*node.Node, error) {
var opt ListOption
if len(option) > 0 {
opt = option[0]
}
items, err := c.clusterState.Scan(ctx, nodeNs)
if err != nil {
return nil, errors.Wrap(err, "scan etcd")
}
var nodes []*node.Node
for _, item := range items {
n := new(node.Node)
if err := item.Unmarshal(n); err != nil {
return nil, errors.Wrapf(err, "unmarshal item %s", item.Key)
}
if opt.Tag != nil && !n.TagMatches(opt.Tag) {
continue
}
nodes = append(nodes, n)
}
return nodes, nil
}
// Get returns an information of node with the host.
// It returns cluster.ErrNotFound if node with given host does not exist.
func (c *cluster) Get(ctx context.Context, host string) (*node.Node, error) {
n := new(node.Node)
if err := c.clusterState.Get(ctx, path.Join(nodeNs, host), n); err != nil {
if err == coordinator.ErrNotFound {
return nil, ErrNotFound
}
return nil, errors.Wrapf(err, "get etcd")
}
return n, nil
}
// States returns a cluster-wide state.
func (c *cluster) States() State {
return c.clusterState
}
// Close unregisters registered nodes and closes all connections.
func (c *cluster) Close() (err error) {
c.cancel()
c.grpcConnsMu.Lock()
defer c.grpcConnsMu.Unlock()
for host, conn := range c.grpcConns {
if closeErr := conn.Close(); err == nil {
if status.Code(closeErr) == codes.Canceled {
continue
}
err = errors.Wrapf(closeErr, "close connection to %s", host)
}
}
return err
}
// nodeRegistration implements node.Registration.
type nodeRegistration struct {
mu sync.RWMutex
ctx context.Context
cancel context.CancelFunc
node *node.Node
cluster Cluster
livenessProbeInterval time.Duration
livenessLease clientv3.LeaseID
sig <-chan struct{}
}
func newNodeRegistration(ctx context.Context, n *node.Node, c *cluster, ttl time.Duration) (*nodeRegistration, error) {
nodeCtx, cancel := context.WithCancel(c.ctx)
nodeReg := &nodeRegistration{
ctx: nodeCtx,
cancel: cancel,
node: n,
cluster: c,
livenessProbeInterval: c.options.LivenessProbeInterval,
}
lease, err := c.clusterState.GrantLease(ctx, c.options.LivenessProbeInterval)
if err != nil {
return nil, errors.Wrap(err, "grant TTL")
}
nodeReg.livenessLease = lease
sig, err := c.clusterState.KeepAlive(nodeCtx, lease)
if err != nil {
return nil, errors.Wrap(err, "start liveness prove")
}
nodeReg.sig = sig
if err := nodeReg.States().Put(ctx, path.Join(nodeNs, n.Host), n); err != nil {
return nil, errors.Wrap(err, "register node info")
}
log.Info().
Str("host", n.Host).
Interface("tag", n.Tag).
Msg("executor node registered")
go nodeReg.keepRegistered()
return nodeReg, nil
}
func (n *nodeRegistration) keepRegistered() {
for n.ctx.Err() == nil {
select {
case <-n.ctx.Done():
return
case <-n.sig:
}
timeoutCtx, cancel := context.WithTimeout(n.ctx, n.livenessProbeInterval)
func() {
n.mu.Lock()
defer n.mu.Unlock()
log.Info().
Str("host", n.node.Host).
Interface("tag", n.node.Tag).
Msg("re-register node")
lease, err := n.cluster.States().GrantLease(timeoutCtx, n.livenessProbeInterval)
if err != nil {
log.Warn().
Err(err).
Msg("failed to get lease TTL")
return
}
n.livenessLease = lease
sig, err := n.cluster.States().KeepAlive(n.ctx, n.livenessLease)
if err != nil {
log.Warn().
Err(err).
Msg("failed to keep alive")
return
}
n.sig = sig
err = n.states().Put(timeoutCtx, path.Join(nodeNs, n.node.Host), n.node)
if err != nil {
log.Warn().
Err(err).
Msg("failed to put node")
return
}
}()
cancel()
}
}
// Info returns a node's information.
func (n *nodeRegistration) Info() *node.Node {
return n.node
}
func (n *nodeRegistration) states() node.State {
return n.cluster.States().WithOptions(coordinator.WithLease(n.livenessLease))
}
// States returns an NodeState, which is ephemeral.
func (n *nodeRegistration) States() node.State {
n.mu.RLock()
defer n.mu.RUnlock()
return n.states()
}
// Unregister removes node from the cluster's node list, and clears all NodeState.
func (n *nodeRegistration) Unregister() {
_, err := n.cluster.States().Delete(n.ctx, path.Join(nodeNs, n.node.Host))
if err != nil {
log.Warn().
Err(err).
Msg("failed to delete node")
}
n.cancel()
}