-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcluster.go
426 lines (385 loc) · 10.9 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2015-present Basho Technologies, Inc.
//
// 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 riak
import (
"fmt"
"sync"
"time"
)
// Constants identifying Cluster state
const (
clusterCreated state = iota
clusterRunning
clusterShuttingDown
clusterShutdown
clusterError
)
// ClusterOptions object contains your pool of Node objects and the NodeManager
// If the NodeManager is not defined, the defaultNodeManager is used
type ClusterOptions struct {
Nodes []*Node
NoDefaultNode bool
NodeManager NodeManager
ExecutionAttempts byte
QueueMaxDepth uint16
QueueExecutionInterval time.Duration
}
// Cluster object contains your pool of Node objects, the NodeManager and the
// current stateData object of the cluster
type Cluster struct {
stopChan chan struct{}
nodes []*Node
nodeManager NodeManager
executionAttempts byte
queueCommands bool
cq *queue
commandQueueTicker *time.Ticker
sync.Mutex
stateData
}
// Cluster errors
var (
ErrClusterNodesMustBeNonNil = newClientError("[Cluster] all nodes must be non-nil", nil)
ErrClusterCommandRequired = newClientError("[Cluster] Command must be non-nil", nil)
ErrClusterAsyncRequiresChannelOrWaitGroup = newClientError("[Cluster] ExecuteAsync argument requires a channel or sync.WaitGroup to indicate completion", nil)
ErrClusterEnqueueWhileShuttingDown = newClientError("[Cluster] will not enqueue command, shutting down", nil)
ErrClusterShuttingDown = newClientError("[Cluster] will not execute command, shutting down", nil)
ErrClusterNodeMustBeNonNil = newClientError("[Cluster] node argument must be non-nil", nil)
)
const ErrClusterNoNodesAvailable = "[Cluster] all retries exhausted and/or no nodes available to execute command"
var defaultClusterOptions = &ClusterOptions{
Nodes: make([]*Node, 0),
NodeManager: &defaultNodeManager{},
ExecutionAttempts: defaultExecutionAttempts,
}
// NewCluster generates a new Cluster object using the provided ClusterOptions object
func NewCluster(options *ClusterOptions) (*Cluster, error) {
if options == nil {
options = defaultClusterOptions
}
if options.NodeManager == nil {
options.NodeManager = &defaultNodeManager{}
}
if options.ExecutionAttempts == 0 {
options.ExecutionAttempts = defaultExecutionAttempts
}
c := &Cluster{
executionAttempts: options.ExecutionAttempts,
nodeManager: options.NodeManager,
}
c.initStateData("clusterCreated", "clusterRunning", "clusterShuttingDown", "clusterShutdown", "clusterError")
if options.Nodes == nil {
c.nodes = make([]*Node, 0)
} else {
c.nodes = options.Nodes
}
if options.NoDefaultNode == false && len(c.nodes) == 0 {
defaultNode, nerr := NewNode(nil)
if nerr != nil {
return nil, nerr
}
c.nodes = append(c.nodes, defaultNode)
}
for _, node := range c.nodes {
if node == nil {
return nil, ErrClusterNodesMustBeNonNil
}
}
if options.QueueMaxDepth > 0 {
if options.QueueExecutionInterval == 0 {
options.QueueExecutionInterval = defaultQueueExecutionInterval
}
c.queueCommands = true
c.stopChan = make(chan struct{})
c.cq = newQueue(options.QueueMaxDepth)
c.commandQueueTicker = time.NewTicker(options.QueueExecutionInterval)
go c.executeEnqueuedCommands()
}
c.setState(clusterCreated)
return c, nil
}
// String returns a formatted string that lists status information for the Cluster
func (c *Cluster) String() string {
return fmt.Sprintf("%v", c.nodes)
}
// Start opens connections with your configured nodes and adds them to
// the active pool
func (c *Cluster) Start() error {
if c.isCurrentState(clusterRunning) {
logWarnln("[Cluster]", "cluster already running.")
return nil
}
if err := c.stateCheck(clusterCreated); err != nil {
return err
}
logDebug("[Cluster]", "starting")
c.Lock()
defer c.Unlock()
for _, node := range c.nodes {
if err := node.start(); err != nil {
return err
}
}
c.setState(clusterRunning)
logDebug("[Cluster]", "cluster started")
return nil
}
// Stop closes the connections with your configured nodes and removes them from
// the active pool
func (c *Cluster) Stop() (err error) {
if err = c.stateCheck(clusterRunning); err != nil {
return
}
logDebug("[Cluster]", "shutting down")
c.setState(clusterShuttingDown)
if c.queueCommands {
close(c.stopChan)
c.commandQueueTicker.Stop()
qc := c.cq.count()
if qc > 0 {
logWarn("[Cluster]", "commands in queue during shutdown: %d", qc)
var f = func(v interface{}) (bool, bool) {
if v == nil {
return true, false
}
if a, ok := v.(*Async); ok {
a.done(ErrClusterShuttingDown)
}
return false, false
}
if qerr := c.cq.iterate(f); qerr != nil {
logErr("[Cluster]", qerr)
}
}
c.cq.destroy()
}
c.Lock()
defer c.Unlock()
for _, node := range c.nodes {
err = node.stop()
if err != nil {
logErr("[Cluster]", err)
}
}
allStopped := true
logDebug("[Cluster]", "checking to see if nodes are shut down")
for _, node := range c.nodes {
nodeState := node.getState()
if nodeState != nodeShutdown {
allStopped = false
break
}
}
if allStopped {
c.setState(clusterShutdown)
logDebug("[Cluster]", "cluster shut down")
} else {
panic("[Cluster] nodes still running when all should be stopped")
}
return
}
// Adds a node to the cluster and starts it
func (c *Cluster) AddNode(n *Node) error {
if n == nil {
return ErrClusterNodeMustBeNonNil
}
c.Lock()
defer c.Unlock()
for _, node := range c.nodes {
if n == node {
return nil
}
}
if c.isCurrentState(clusterRunning) {
if err := n.start(); err != nil {
return err
}
}
c.nodes = append(c.nodes, n)
return nil
}
// Stops the node and removes from the cluster
func (c *Cluster) RemoveNode(n *Node) error {
if n == nil {
return ErrClusterNodeMustBeNonNil
}
c.Lock()
defer c.Unlock()
cn := c.nodes
for i, node := range c.nodes {
if n == node {
l := len(cn) - 1
cn[i], cn[l], c.nodes = cn[l], nil, cn[:l]
if !node.isCurrentState(nodeCreated) {
if err := node.stop(); err != nil {
return err
}
}
return nil
}
}
return nil
}
// Execute (asynchronously) the provided Command against the active pooled Nodes using the NodeManager
func (c *Cluster) ExecuteAsync(async *Async) error {
if async.Command == nil {
return ErrClusterCommandRequired
}
if async.Done == nil && async.Wait == nil {
return ErrClusterAsyncRequiresChannelOrWaitGroup
}
if async.Wait != nil {
async.Wait.Add(1)
}
go c.execute(async)
return nil
}
// Execute (synchronously) the provided Command against the active pooled Nodes using the NodeManager
func (c *Cluster) Execute(command Command) error {
if command == nil {
return ErrClusterCommandRequired
}
async := &Async{
Command: command,
}
c.execute(async)
if async.Error != nil {
return async.Error
}
if cerr := command.Error(); cerr != nil {
return cerr
}
return nil
}
// NB: will be executed in a goroutine
func (c *Cluster) execute(async *Async) {
if c == nil {
panic("[Cluster] nil cluster argument")
}
if async == nil {
panic("[Cluster] nil async argument")
}
var err error
executed := false
enqueued := false
cmd := async.Command
tries := byte(1)
var lastExeNode *Node
if rc, ok := cmd.(retryableCommand); ok {
tries = c.executionAttempts
lastExeNode = rc.getLastNode()
}
async.onExecute()
for tries > 0 {
if err = c.stateCheck(clusterRunning); err != nil {
break
}
executed, err = c.nodeManager.ExecuteOnNode(c.nodes, cmd, lastExeNode)
// NB: do *not* call cmd.onError here as it will have been called in connection
if executed {
// NB: "executed" means that a node sent the data to Riak and received a response
if err == nil {
// No need to re-try
logDebug("[Cluster]", "successfully executed cmd '%s'", cmd.Name())
break
} else {
// NB: retry since error occurred
logDebug("[Cluster]", "executed cmd '%s': re-try due to error '%v'", cmd.Name(), err)
}
} else {
// Command did NOT execute
if err == nil {
logDebug("[Cluster]", "did NOT execute cmd '%s', nil err", cmd.Name())
// Command did not execute but there was no error, so enqueue it
// TODO FUTURE should this only happen if retries exhausted?
if c.queueCommands {
if err = c.enqueueCommand(async); err == nil {
enqueued = true
}
break
}
} else {
// NB: retry since error occurred
logDebug("[Cluster]", "did NOT execute cmd '%s': re-try due to error '%v'", cmd.Name(), err)
}
}
tries--
logDebug("[Cluster]", "cmd %s tries: %d", cmd.Name(), tries)
if tries > 0 {
cmd.onRetry()
async.onRetry()
} else {
err = newClientError(ErrClusterNoNodesAvailable, err)
}
}
if !enqueued {
async.done(err)
}
}
func (c *Cluster) enqueueCommand(async *Async) error {
var err error
if c.isStateLessThan(clusterShuttingDown) {
command := async.Command
logDebug("[Cluster]", "enqueuing command '%s'", command.Name())
async.onEnqueued()
err = c.cq.enqueue(async)
if err != nil {
async.done(err)
}
} else {
err = ErrClusterEnqueueWhileShuttingDown
async.done(err)
}
return err
}
func (c *Cluster) executeEnqueuedCommands() {
logDebug("[Cluster]", "(%v) command queue routine is starting", c)
for {
select {
case <-c.stopChan:
logDebug("[Cluster]", "(%v) command queue routine is quitting", c)
return
case t := <-c.commandQueueTicker.C:
// NB: ensure we're not already shutting down
if c.isStateLessThan(clusterShuttingDown) {
var f = func(v interface{}) (bool, bool) {
if !c.isStateLessThan(clusterShuttingDown) {
logDebug("[Cluster]", "(%v) shutting down, command queue routine is quitting")
return true, false
}
if v == nil {
return true, false
}
var re_enqueue bool
async := v.(*Async)
if t.After(async.executeAt) {
re_enqueue = false
logDebug("[Cluster]", "(%v) executing queued command '%s' at %v", c, async.Command.Name(), t)
go c.execute(async) // NB: *may* re-enqueue, so goroutine required
} else {
re_enqueue = true
logDebug("[Cluster]", "(%v) skipping queued command '%s'", c, async.Command.Name())
}
return false, re_enqueue
}
if qerr := c.cq.iterate(f); qerr != nil {
logErr("[Cluster]", qerr)
}
} else {
logDebug("[Cluster]", "(%v) shutting down, command queue routine is quitting")
return
}
}
}
}