-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterMember.go
276 lines (234 loc) · 8.11 KB
/
clusterMember.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
/*
Copyright 2016 The Rook Authors. All rights reserved.
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 clusterd
import (
"errors"
"fmt"
"log"
"path"
"sync/atomic"
"time"
etcd "github.com/coreos/etcd/client"
"github.com/coreos/etcd/store"
"github.com/rook/rook/pkg/clusterd/inventory"
"github.com/rook/rook/pkg/util"
ctx "golang.org/x/net/context"
)
const (
SystemLogDir = "/var/log/rook"
leaseVersion = 1
leaseTtlSeconds = 15
leaseRetrySeconds = 5
heartbeatTtlSeconds = 60 * 60
hardwareDiscoveryIntervalSeconds = 120 * 60
watchErrorRetrySeconds = 2
machineTtlMinutes = 5
)
var (
leaseTtlDuration = time.Duration(leaseTtlSeconds) * time.Second
leaseRetryDuration = time.Duration(leaseRetrySeconds) * time.Second
machineTtlDuration = time.Duration(machineTtlMinutes) * time.Minute
hardwareDiscoveryInterval = time.Duration(hardwareDiscoveryIntervalSeconds) * time.Second
)
// Interface defining how a leader reacts to gaining or losing election, new members being added, etc.
type Leader interface {
OnLeadershipAcquired() error
OnLeadershipLost() error
GetLeaseName() string
}
func IsLeader(l Lease, nodeID string) bool {
if l == nil {
return false
}
if l.MachineID() != nodeID {
return false
}
return true
}
type ClusterMember struct {
context *Context
isLeader bool
leaseManager Manager
leader Leader
hardwareDiscoveryLock int32
}
func newClusterMember(context *Context, leaseManager Manager, leader Leader) *ClusterMember {
return &ClusterMember{
context: context,
isLeader: false,
leaseManager: leaseManager,
leader: leader,
hardwareDiscoveryLock: 0,
}
}
func (r *ClusterMember) initialize() error {
err := r.ElectLeader()
if err != nil {
return fmt.Errorf("failed to elect leader. %+v", err)
}
// initialize the hardware inventory
err = r.discoverHardware()
if err != nil {
return fmt.Errorf("failed to detect initial hardware. %+v", err)
}
// in a goroutine, begin the monitor cluster loop for changes in membership, leadership, etc.
go func() {
r.refreshLeader()
}()
go func() {
r.discoverHardwareLoop()
}()
go func() {
r.waitForHardwareChangeNotifications()
}()
return nil
}
func (r *ClusterMember) ElectLeader() error {
// keep our cluster membership up to date
err := r.heartbeat()
if err != nil {
log.Printf("failed to heartbeat, will try again later: err=%v", err)
}
existing, err := r.leaseManager.GetLease(r.leader.GetLeaseName())
if err != nil {
// failed to get the current lease, ensure our leadership status is updated
r.updateLeaderStatus(err)
return err
}
var l Lease
if existing == nil {
// no leader currently exists, try to acquire
l, err = r.leaseManager.AcquireLease(r.leader.GetLeaseName(), r.context.NodeID, leaseVersion, leaseTtlDuration)
if err != nil {
// failed to acquire lease, ensure our leadership status is updated
r.updateLeaderStatus(err)
return err
} else if l == nil {
// failed to acquire lease. This node is simply not the leader
r.updateLeaderStatus(errors.New("another node is leader"))
return nil
}
// succeeded in acquiring lease, ensure our leadership status is updated
r.updateLeaderStatus(nil)
} else if IsLeader(existing, r.context.NodeID) {
// we are the existing leader, attempt to renew the lease then ensure our leadership status is updated
err = existing.Renew(leaseTtlDuration)
r.updateLeaderStatus(err)
return err
} else if r.isLeader {
// we used to be the leader, now we are not
r.isLeader = false
return r.leader.OnLeadershipLost()
}
return nil
}
func (r *ClusterMember) refreshLeader() {
for {
// sleep for a portion of the lease TTL and try again
<-time.After(leaseRetryDuration)
err := r.ElectLeader()
if err != nil {
log.Printf("error while electing leader: %s", err.Error())
}
}
}
func (r *ClusterMember) discoverHardwareLoop() {
for {
// sleep until it's time to detect hardware again
// assume the initial discovery was already done
<-time.After(hardwareDiscoveryInterval)
err := r.discoverHardware()
if err != nil {
log.Printf("error while discovering hardware: %+v", err)
} else {
log.Print("hardware discovery complete")
}
}
}
func (r *ClusterMember) waitForHardwareChangeNotifications() {
hardwareTriggerKey := path.Join(inventory.GetNodeConfigKey(r.context.NodeID), inventory.TriggerHardwareDetectionKey)
hardwareWatcher := r.context.EtcdClient.Watcher(hardwareTriggerKey, nil)
for {
// wait for any changes to the hardware detection trigger key
resp, err := hardwareWatcher.Next(ctx.Background())
if err != nil {
if err == ctx.Canceled {
log.Print("hardware change watching cancelled, bailing out...")
break
} else {
<-time.After(time.Duration(watchErrorRetrySeconds) * time.Second)
continue
}
}
if resp != nil && resp.Node != nil && resp.Action == store.Set {
// the trigger hardware detection key was set, perform a hardware discovery
err := r.discoverHardware()
if err != nil {
log.Printf("error while discovering hardware after a change notification: %+v", err)
} else {
log.Print("hardware discovery after a change notification complete")
}
// clear out the hardware detection trigger key now so it can fire again later
r.context.EtcdClient.Delete(ctx.Background(), hardwareTriggerKey, nil)
}
}
}
func (r *ClusterMember) updateLeaderStatus(err error) error {
if err != nil && r.isLeader {
// there was an error in leader election and we currently think we're the leader
// update our internal state
r.onLeadershipLost()
} else if err == nil && !r.isLeader {
// there was no error in leader election and we currently don't think we're the leader
// update our internal state
return r.onLeadershipAcquired()
}
// Return the error that indicates the original cause for not updating the status
return err
}
func (r *ClusterMember) onLeadershipAcquired() error {
r.isLeader = true
log.Printf("cluster leadership acquired by this machine (%s)", r.context.NodeID)
return r.leader.OnLeadershipAcquired()
}
func (r *ClusterMember) onLeadershipLost() {
log.Print("leadership lost by this machine")
r.isLeader = false
r.leader.OnLeadershipLost()
}
func (r *ClusterMember) heartbeat() error {
machineKey := path.Join(inventory.NodesHealthKey, r.context.NodeID)
// first ensure the machine key (directory) exists
_, err := r.context.EtcdClient.Set(ctx.Background(), machineKey, "", &etcd.SetOptions{Dir: true})
if err != nil && !util.IsEtcdDirAlreadyExists(err) {
// we got a different error than "it already exists", bail out
return err
}
// send a heartbeat by setting the heartbeat etcd key. we are avoiding setting a timestamp
// here since cluster member clock may be skewed from etcd cluster clocks. note that last
// heartbeat time can be indirectly determined by getting the key and subtracting the current
// TTL from the original TTL
key := path.Join(machineKey, inventory.HeartbeatKey)
_, err = r.context.EtcdClient.Set(ctx.Background(), key, "", &etcd.SetOptions{TTL: inventory.HeartbeatTtlDuration})
return err
}
func (r *ClusterMember) discoverHardware() error {
hardwareDiscoveryCount := atomic.AddInt32(&r.hardwareDiscoveryLock, 1)
defer atomic.AddInt32(&r.hardwareDiscoveryLock, -1)
if hardwareDiscoveryCount != 1 {
log.Print("local hardware discovery already in progress, skipping...")
return nil
}
// Discover current state of the disks and other node properties
return inventory.DiscoverHardware(r.context.NodeID, r.context.EtcdClient, r.context.Executor)
}