forked from infobloxopen/infoblox-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.go
248 lines (220 loc) · 5.62 KB
/
lock.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
package ibclient
import (
"fmt"
"math/rand"
"time"
"github.com/sirupsen/logrus"
)
const (
timeout int32 = 60 // in seconds
freeLockVal string = "Available"
)
type Lock interface {
Lock() error
UnLock(force bool) error
}
type NetworkViewLock struct {
Name string
ObjMgr *ObjectManager
LockEA string
LockTimeoutEA string
}
func (l *NetworkViewLock) createLockRequest() *MultiRequest {
req := NewMultiRequest(
[]*RequestBody{
&RequestBody{
Method: "GET",
Object: "networkview",
Data: map[string]interface{}{
"name": l.Name,
"*" + l.LockEA: freeLockVal,
},
Args: map[string]string{
"_return_fields": "extattrs",
},
AssignState: map[string]string{
"NET_VIEW_REF": "_ref",
},
Discard: true,
},
&RequestBody{
Method: "PUT",
Object: "##STATE:NET_VIEW_REF:##",
Data: map[string]interface{}{
"extattrs+": map[string]interface{}{
l.LockEA: map[string]string{
"value": l.ObjMgr.tenantID,
},
l.LockTimeoutEA: map[string]int32{
"value": int32(time.Now().Unix()),
},
},
},
EnableSubstitution: true,
Discard: true,
},
&RequestBody{
Method: "GET",
Object: "##STATE:NET_VIEW_REF:##",
Args: map[string]string{
"_return_fields": "extattrs",
},
AssignState: map[string]string{
"DOCKER-ID": "*" + l.LockEA,
},
EnableSubstitution: true,
Discard: true,
},
&RequestBody{
Method: "STATE:DISPLAY",
},
},
)
return req
}
func (l *NetworkViewLock) createUnlockRequest(force bool) *MultiRequest {
getData := map[string]interface{}{"name": l.Name}
if !force {
getData["*"+l.LockEA] = l.ObjMgr.tenantID
}
req := NewMultiRequest(
[]*RequestBody{
&RequestBody{
Method: "GET",
Object: "networkview",
Data: getData,
Args: map[string]string{
"_return_fields": "extattrs",
},
AssignState: map[string]string{
"NET_VIEW_REF": "_ref",
},
Discard: true,
},
&RequestBody{
Method: "PUT",
Object: "##STATE:NET_VIEW_REF:##",
Data: map[string]interface{}{
"extattrs+": map[string]interface{}{
l.LockEA: map[string]string{
"value": freeLockVal,
},
},
},
EnableSubstitution: true,
Discard: true,
},
&RequestBody{
Method: "PUT",
Object: "##STATE:NET_VIEW_REF:##",
Data: map[string]interface{}{
"extattrs-": map[string]interface{}{
l.LockTimeoutEA: map[string]interface{}{},
},
},
EnableSubstitution: true,
Discard: true,
},
&RequestBody{
Method: "GET",
Object: "##STATE:NET_VIEW_REF:##",
Args: map[string]string{
"_return_fields": "extattrs",
},
AssignState: map[string]string{
"DOCKER-ID": "*" + l.LockEA,
},
EnableSubstitution: true,
Discard: true,
},
&RequestBody{
Method: "STATE:DISPLAY",
},
},
)
return req
}
func (l *NetworkViewLock) getLock() bool {
logrus.Debugf("Creating lock on network niew %s\n", l.Name)
req := l.createLockRequest()
res, err := l.ObjMgr.CreateMultiObject(req)
if err != nil {
logrus.Debugf("Failed to create lock on network view %s: %s\n", l.Name, err)
//Check for Lock Timeout
nw, err := l.ObjMgr.GetNetworkView(l.Name)
if err != nil {
logrus.Debugf("Failed to get the network view object for %s : %s\n", l.Name, err)
return false
}
if t, ok := nw.Ea[l.LockTimeoutEA]; ok {
if int32(time.Now().Unix())-int32(t.(int)) > timeout {
logrus.Debugln("Lock is timed out. Forcefully acquiring it.")
//remove the lock forcefully and acquire it
l.UnLock(true)
// try to get lock again
return l.getLock()
}
}
return false
}
dockerID := res[0]["DOCKER-ID"]
if dockerID == l.ObjMgr.tenantID {
logrus.Debugln("Got the lock !!!")
return true
}
return false
}
func (l *NetworkViewLock) Lock() error {
// verify if network view exists and has EA for the lock
nw, err := l.ObjMgr.GetNetworkView(l.Name)
if err != nil {
msg := fmt.Sprintf("Failed to get the network view object for %s : %s\n", l.Name, err)
logrus.Debugf(msg)
return fmt.Errorf(msg)
}
if _, ok := nw.Ea[l.LockEA]; !ok {
nw.Ea[l.LockEA] = freeLockVal
_, err = l.ObjMgr.UpdateNetworkView(nw.Ref, "", "", nw.Ea)
if err != nil {
return fmt.Errorf("Failed to Update Network view with Lock EA")
}
}
retryCount := 0
for {
// Get lock on the network view
lock := l.getLock()
if lock == true {
// Got the lock.
logrus.Debugf("Got the lock on Network View %s\n", l.Name)
return nil
}
// Lock is held by some other agent. Wait for some time and retry it again
if retryCount >= 10 {
return fmt.Errorf("Failed to get Lock on Network View %s", l.Name)
}
retryCount++
logrus.Debugf("Lock on Network View %s not free. Retrying again %d out of 10.\n", l.Name, retryCount)
// sleep for random time (between 1 - 10 seconds) to reduce collisions
time.Sleep(time.Duration(rand.Intn(9)+1) * time.Second)
continue
}
}
func (l *NetworkViewLock) UnLock(force bool) error {
// To unlock set the Docker-Plugin-Lock EA of network view to Available and
// remove the Docker-Plugin-Lock-Time EA
req := l.createUnlockRequest(force)
res, err := l.ObjMgr.CreateMultiObject(req)
if err != nil {
msg := fmt.Sprintf("Failed to release lock from Network View %s: %s\n", l.Name, err)
logrus.Errorf(msg)
return fmt.Errorf(msg)
}
dockerID := res[0]["DOCKER-ID"]
if dockerID == freeLockVal {
logrus.Debugln("Removed the lock!")
return nil
}
msg := fmt.Sprintf("Failed to release lock from Network View %s\n", l.Name)
logrus.Errorf(msg)
return fmt.Errorf(msg)
}