forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
operationlock.go
170 lines (140 loc) · 4.36 KB
/
operationlock.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
package operationlock
import (
"fmt"
"sync"
"time"
"github.com/lxc/lxd/shared"
)
var instanceOperationsLock sync.Mutex
var instanceOperations = make(map[int]*InstanceOperation)
// InstanceOperation operation locking.
type InstanceOperation struct {
action string
chanDone chan error
chanReset chan bool
err error
id int
reusable bool
}
// Action returns operation's action.
func (op InstanceOperation) Action() string {
return op.action
}
// Create creates a new operation lock for an Instance if one does not already exist and returns it.
// The lock will be released after 30s or when Done() is called, which ever occurs first.
// If reusable is set as true then future lock attempts can specify the reuse argument as true which
// will then trigger a reset of the 30s timeout on the existing lock and return it.
func Create(instanceID int, action string, reusable bool, reuse bool) (*InstanceOperation, error) {
instanceOperationsLock.Lock()
defer instanceOperationsLock.Unlock()
op := instanceOperations[instanceID]
if op != nil {
if op.reusable && reuse {
// Reset operation timeout without releasing lock or deadlocking using Reset() function.
op.chanReset <- true
return op, nil
}
return nil, fmt.Errorf("Instance is busy running a %s operation", op.action)
}
op = &InstanceOperation{}
op.id = instanceID
op.action = action
op.reusable = reusable
op.chanDone = make(chan error, 0)
op.chanReset = make(chan bool, 0)
instanceOperations[instanceID] = op
go func(op *InstanceOperation) {
for {
select {
case <-op.chanDone:
return
case <-op.chanReset:
continue
case <-time.After(time.Second * 30):
op.Done(fmt.Errorf("Instance %s operation timed out after 30 seconds", op.action))
return
}
}
}(op)
return op, nil
}
// CreateWaitGet is a weird function which does what we happen to want most of the time.
//
// If the instance has an operation of the same type and it's not reusable
// or the caller doesn't want to reuse it, the function will wait and
// indicate that it did so.
//
// If the instance has an operation of one of the alternate types, then
// the operation is returned to the user.
//
// If the instance doesn't have an operation, has an operation of a different
// type that is not in the alternate list or has the right type and is
// being reused, then this behaves as a Create call.
func CreateWaitGet(instanceID int, action string, altActions []string, reusable bool, reuse bool) (bool, *InstanceOperation, error) {
op := Get(instanceID)
// No existing operation, call create.
if op == nil {
op, err := Create(instanceID, action, reusable, reuse)
return false, op, err
}
// Operation matches and not reusable or asked to reuse, wait.
if op.action == action && (!reuse || !op.reusable) {
err := op.Wait()
return true, nil, err
}
// Operation matches one the alternate actions, return the operation.
if shared.StringInSlice(op.action, altActions) {
return false, op, nil
}
// Send the rest to Create
op, err := Create(instanceID, action, reusable, reuse)
return false, op, err
}
// Get retrieves an existing lock or returns nil if no lock exists.
func Get(instanceID int) *InstanceOperation {
instanceOperationsLock.Lock()
defer instanceOperationsLock.Unlock()
return instanceOperations[instanceID]
}
// Reset resets the operation timeout.
func (op *InstanceOperation) Reset() error {
instanceOperationsLock.Lock()
defer instanceOperationsLock.Unlock()
// This function can be called on a nil struct.
if op == nil {
return nil
}
// Check if already done
runningOp, ok := instanceOperations[op.id]
if !ok || runningOp != op {
return fmt.Errorf("Operation is already done or expired")
}
op.chanReset <- true
return nil
}
// Wait waits for an operation to finish.
func (op *InstanceOperation) Wait() error {
// This function can be called on a nil struct.
if op == nil {
return nil
}
<-op.chanDone
return op.err
}
// Done indicates the operation has finished.
func (op *InstanceOperation) Done(err error) {
instanceOperationsLock.Lock()
defer instanceOperationsLock.Unlock()
// This function can be called on a nil struct.
if op == nil {
return
}
// Check if already done
runningOp, ok := instanceOperations[op.id]
if !ok || runningOp != op {
return
}
op.err = err
delete(instanceOperations, op.id) // Delete before closing chanDone.
close(op.chanDone)
}