-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrink.go
More file actions
193 lines (163 loc) · 4.46 KB
/
rink.go
File metadata and controls
193 lines (163 loc) · 4.46 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
package rink
import (
"context"
"time"
"github.com/luno/jettison/errors"
"github.com/luno/jettison/j"
"github.com/luno/jettison/log"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/concurrency"
"golang.org/x/sync/errgroup"
)
type noopLogger struct{}
func (noopLogger) Debug(context.Context, string, ...log.Option) {}
func (noopLogger) Info(context.Context, string, ...log.Option) {}
func (noopLogger) Error(context.Context, error, ...log.Option) {}
type options struct {
Log log.Interface
ClusterOptions ClusterOptions
RolesOptions RolesOptions
}
type Option func(*options)
// WithClusterOptions passes through specific options to the Cluster.
// See ClusterOptions for more details.
func WithClusterOptions(opts ClusterOptions) Option {
return func(o *options) {
o.ClusterOptions = opts
}
}
// WithRolesOptions passes through options to the Roles interface.
// See RolesOptions for more details.
func WithRolesOptions(opts RolesOptions) Option {
return func(o *options) {
o.RolesOptions = opts
}
}
// WithLogger sets a logger to be used for logging in Rink.
// It is also used for the Roles and Cluster logging if not specified in
// ClusterOptions or RolesOptions
func WithLogger(l log.Interface) Option {
return func(o *options) {
o.Log = l
}
}
func buildOptions(opts []Option) options {
var o options
for _, opt := range opts {
opt(&o)
}
if o.RolesOptions.Log == nil {
o.RolesOptions.Log = o.Log
}
if o.ClusterOptions.Log == nil {
o.ClusterOptions.Log = o.Log
}
if o.Log == nil {
o.Log = noopLogger{}
}
return o
}
// Rink manages roles for members of a cluster.
// Each role is assigned to a single member of the cluster at any one time.
//
// Should the connection to the etcd client fail, roles will be revoked until
// it can be re-established.
//
// The Roles' struct is used to manage individual roles.
type Rink struct {
cli *clientv3.Client
ctx context.Context
cancel context.CancelFunc
finished chan struct{}
name string
options options
Roles *Roles
}
// New starts Rink and runs the Cluster.
// Call Shutdown to stop and clean up rink roles.
// Use Roles to access individual roles.
func New(cli *clientv3.Client, name string, opts ...Option) *Rink {
s := Create(cli, name, opts...)
go s.Run()
return s
}
// Create creates a Rink client, ready to join the cluster.
// Call Run to join the cluster and assign roles.
func Create(cli *clientv3.Client, name string, opts ...Option) *Rink {
ctx, cancel := context.WithCancel(context.Background())
s := &Rink{
cli: cli,
ctx: ctx,
cancel: cancel,
finished: make(chan struct{}),
name: name,
options: buildOptions(opts),
}
s.Roles = NewRoles(name, s.options.RolesOptions)
return s
}
// Shutdown will synchronously release all locked roles, shutdown the cluster, and
// revoke the current Session.
func (s *Rink) Shutdown(ctx context.Context) {
s.cancel()
s.options.Log.Debug(ctx, "shutting down rink")
select {
case <-s.finished:
case <-ctx.Done():
}
}
func (s *Rink) Run() {
ctx := s.ctx
s.options.Log.Debug(ctx, "running rink")
defer s.options.Log.Debug(ctx, "stopped rink")
defer close(s.finished)
for ctx.Err() == nil {
err := s.runOnce(ctx)
if err != nil && !errors.IsAny(err, context.Canceled) {
s.options.Log.Error(ctx, errors.Wrap(err, "running rink"))
}
select {
case <-ctx.Done():
case <-time.After(10 * time.Second):
}
}
}
func (s *Rink) runOnce(ctx context.Context) error {
s.options.Log.Debug(ctx, "creating new session")
sess, err := concurrency.NewSession(s.cli)
if err != nil {
return err
}
s.options.Log.Debug(ctx, "created session", j.KV("etcd_lease", sess.Lease()))
defer s.options.Log.Debug(ctx, "ended session", j.KV("etcd_lease", sess.Lease()))
defer func() {
err := sess.Close()
if err != nil {
// NoReturnErr: Nowhere to return it
s.options.Log.Error(ctx, errors.Wrap(err, "session close"))
}
}()
eg, ctx := errgroup.WithContext(ctx)
ctx = clientv3.WithRequireLeader(ctx)
eg.Go(func() error {
return Run(ctx, sess, s.name, s.Roles.updateRank, s.options.ClusterOptions)
})
eg.Go(func() error {
return watchSession(ctx, sess)
})
eg.Go(func() error {
return watchForExpiredKeys(ctx, s.cli, s.name)
})
eg.Go(func() error {
return s.Roles.assignRoles(ctx, sess)
})
return eg.Wait()
}
func watchSession(ctx context.Context, sess *concurrency.Session) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-sess.Done():
return errors.New("etcd lease expired")
}
}