-
Notifications
You must be signed in to change notification settings - Fork 21
/
client.go
414 lines (355 loc) · 9.76 KB
/
client.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
package core
import (
"encoding/json"
"fmt"
"github.com/MG-RAST/AWE/lib/core/uuid"
"github.com/MG-RAST/AWE/lib/logger"
"io/ioutil"
"time"
)
const (
CLIENT_STAT_ACTIVE_BUSY = "active-busy"
CLIENT_STAT_ACTIVE_IDLE = "active-idle"
CLIENT_STAT_SUSPEND = "suspend"
CLIENT_STAT_DELETED = "deleted"
)
// this is the Worker
type Client struct {
coAckChannel chan CoAck `bson:"-" json:"-"` //workunit checkout item including data and err (qmgr.Handler -> WorkController)
RWMutex
Id string `bson:"id" json:"id"` // this is a uuid (the only relevant identifier)
Name string `bson:"name" json:"name"` // this can be anything you want
Group string `bson:"group" json:"group"`
User string `bson:"user" json:"user"`
Domain string `bson:"domain" json:"domain"`
InstanceId string `bson:"instance_id" json:"instance_id"` // Openstack specific
InstanceType string `bson:"instance_type" json:"instance_type"` // Openstack specific
Host string `bson:"host" json:"host"` // deprecated
Hostname string `bson:"hostname" json:"hostname"`
Host_ip string `bson:"host_ip" json:"host_ip"` // Host can be physical machine or VM, whatever is helpful for management
CPUs int `bson:"cores" json:"cores"`
Apps []string `bson:"apps" json:"apps"`
RegTime time.Time `bson:"regtime" json:"regtime"`
LastCompleted time.Time `bson:"lastcompleted" json:"lastcompleted"` // time of last time a job was completed (can be used to compute idle time)
Serve_time string `bson:"serve_time" json:"serve_time"`
//Idle_time int `bson:"idle_time" json:"idle_time"`
Status string `bson:"Status" json:"Status"`
Total_checkout int `bson:"total_checkout" json:"total_checkout"`
Total_completed int `bson:"total_completed" json:"total_completed"`
Total_failed int `bson:"total_failed" json:"total_failed"`
Current_work map[string]bool `bson:"current_work" json:"current_work"` // the bool in the mapping is deprecated. It used to indicate completed work that could not be returned to server
Skip_work []string `bson:"skip_work" json:"skip_work"`
Last_failed int `bson:"-" json:"-"`
Tag bool `bson:"-" json:"-"`
Proxy bool `bson:"proxy" json:"proxy"`
SubClients int `bson:"subclients" json:"subclients"`
GitCommitHash string `bson:"git_commit_hash" json:"git_commit_hash"`
Version string `bson:"version" json:"version"`
}
// invoked by NewClient or manually after unmarshalling
func (client *Client) Init() {
client.coAckChannel = make(chan CoAck)
client.Tag = true
if client.Id == "" {
client.Id = uuid.New()
}
client.RWMutex.Init("client_" + client.Id)
if client.RegTime.IsZero() {
client.RegTime = time.Now()
}
if client.Apps == nil {
client.Apps = []string{}
}
if client.Skip_work == nil {
client.Skip_work = []string{}
}
if client.Current_work == nil {
client.Current_work = map[string]bool{}
}
}
func NewClient() (client *Client) {
client = &Client{
Total_checkout: 0,
Total_completed: 0,
Total_failed: 0,
Serve_time: "0",
Last_failed: 0,
Status: CLIENT_STAT_ACTIVE_IDLE,
}
client.Init()
return
}
// create Client object from json file
func NewProfileClient(filepath string) (client *Client, err error) {
jsonstream, err := ioutil.ReadFile(filepath)
if err != nil {
err = fmt.Errorf("(NewProfileClient) error in ioutil.ReadFile(filepath): %s %s", filepath, err.Error())
return nil, err
}
if len(jsonstream) == 0 {
err = fmt.Errorf("filepath %s seems to be empty", filepath)
return
}
client = new(Client)
err = json.Unmarshal(jsonstream, client)
if err != nil {
err = fmt.Errorf("failed to unmashal json stream for client profile (error: %s) (file: %s) json: %s", err.Error(), filepath, string(jsonstream[:]))
return nil, err
}
client.Init()
return
}
func (cl *Client) Get_Ack() (ack CoAck, err error) {
start_time := time.Now()
timeout := make(chan bool, 1)
go func() {
time.Sleep(60 * time.Second)
timeout <- true
}()
select {
case ack = <-cl.coAckChannel:
elapsed_time := time.Since(start_time)
logger.Debug(3, "got ack after %s", elapsed_time)
case <-timeout:
elapsed_time := time.Since(start_time)
err = fmt.Errorf("(CheckoutWorkunits) %s workunit request timed out after %s ", cl.Id, elapsed_time)
return
}
return
}
func (cl *Client) Append_Skip_work(workid string, write_lock bool) (err error) {
if write_lock {
err = cl.LockNamed("Append_Skip_work")
if err != nil {
return
}
}
cl.Skip_work = append(cl.Skip_work, workid)
if write_lock {
cl.Unlock()
}
return
}
func (cl *Client) Contains_Skip_work_nolock(workid string) (c bool) {
c = contains(cl.Skip_work, workid)
return
}
func (cl *Client) Get_Id(do_read_lock bool) (s string, err error) {
if do_read_lock {
read_lock, xerr := cl.RLockNamed("Get_Id")
if xerr != nil {
err = xerr
return
}
defer cl.RUnlockNamed(read_lock)
}
s = cl.Id
return
}
func (cl *Client) Get_Status(do_read_lock bool) (s string, err error) {
if do_read_lock {
read_lock, xerr := cl.RLockNamed("Get_Status")
if xerr != nil {
err = xerr
return
}
defer cl.RUnlockNamed(read_lock)
}
s = cl.Status
return
}
func (cl *Client) Set_Status(s string, write_lock bool) (err error) {
if write_lock {
err = cl.LockNamed("Set_Status")
if err != nil {
return
}
defer cl.Unlock()
}
cl.Status = s
return
}
func (cl *Client) Get_Total_checkout() (count int, err error) {
read_lock, err := cl.RLockNamed("Get_Total_checkout")
if err != nil {
return
}
defer cl.RUnlockNamed(read_lock)
count = cl.Total_checkout
return
}
func (cl *Client) Increment_total_checkout(err error) {
err = cl.LockNamed("Increment_total_checkout")
if err != nil {
return
}
defer cl.Unlock()
cl.Total_checkout += 1
return
}
func (cl *Client) Get_Total_completed() (count int, err error) {
read_lock, err := cl.RLockNamed("Get_Total_completed")
if err != nil {
return
}
defer cl.RUnlockNamed(read_lock)
count = cl.Total_completed
return
}
func (cl *Client) Increment_total_completed() (err error) {
err = cl.LockNamed("Increment_total_completed")
if err != nil {
return
}
defer cl.Unlock()
cl.LastCompleted = time.Now()
cl.Total_completed += 1
cl.Last_failed = 0 //reset last consecutive failures
return
}
func (cl *Client) Get_Total_failed() (count int, err error) {
read_lock, err := cl.RLockNamed("Get_Total_failed")
if err != nil {
return
}
defer cl.RUnlockNamed(read_lock)
count = cl.Total_failed
return
}
func (cl *Client) Increment_total_failed(write_lock bool) (err error) {
if write_lock {
err = cl.LockNamed("Increment_total_failed")
if err != nil {
return
}
defer cl.Unlock()
}
cl.Total_failed += 1
return
}
func (cl *Client) Increment_last_failed(write_lock bool) (value int, err error) {
if write_lock {
err = cl.LockNamed("Increment_last_failed")
if err != nil {
return
}
defer cl.Unlock()
}
cl.Last_failed += 1
value = cl.Last_failed
return
}
func (cl *Client) Get_Last_failed() (count int, err error) {
read_lock, err := cl.RLockNamed("Get_Last_failed")
if err != nil {
return
}
defer cl.RUnlockNamed(read_lock)
count = cl.Last_failed
return
}
func (cl *Client) Current_work_delete(workid string, write_lock bool) (err error) {
if write_lock {
err = cl.LockNamed("Current_work_delete")
defer cl.Unlock()
}
delete(cl.Current_work, workid)
cw_length, err := cl.Current_work_length(false)
if err != nil {
return
}
if cw_length == 0 && cl.Status == CLIENT_STAT_ACTIVE_BUSY {
cl.Status = CLIENT_STAT_ACTIVE_IDLE
}
return
}
func (cl *Client) Current_work_has(workid string) (ok bool, err error) {
err = cl.LockNamed("Current_work_has")
defer cl.Unlock()
_, ok = cl.Current_work[workid]
return
}
func (cl *Client) Get_current_work(do_read_lock bool) (current_work_ids []string, err error) {
current_work_ids = []string{}
if do_read_lock {
read_lock, xerr := cl.RLockNamed("Get_current_work")
if xerr != nil {
err = xerr
return
}
defer cl.RUnlockNamed(read_lock)
}
for id := range cl.Current_work {
current_work_ids = append(current_work_ids, id)
}
return
}
func (cl *Client) Set_current_work(current_work_ids []string, do_write_lock bool) (err error) {
current_work_ids = []string{}
if do_write_lock {
err = cl.LockNamed("Set_current_work")
if err != nil {
return
}
defer cl.Unlock()
}
cl.Current_work = make(map[string]bool)
for _, workid := range current_work_ids {
cl.Current_work[workid] = true
}
return
}
// TODO: Wolfgang: Can we use delete instead ?
//func (cl *Client) Current_work_false(workid string) (err error) {
// err = cl.LockNamed("Current_work_false")
// if err != nil {
// return
// }
// defer cl.Unlock()
// cl.Current_work[workid] = false
// return
//}
// lock always
func (cl *Client) Add_work(workid string) (err error) {
err = cl.LockNamed("Add_work")
if err != nil {
return
}
defer cl.Unlock()
cl.Current_work[workid] = true
cl.Total_checkout += 1
return
}
func (cl *Client) Current_work_length(lock bool) (clength int, err error) {
if lock {
read_lock, xerr := cl.RLockNamed("Current_work_length")
if xerr != nil {
err = xerr
return
}
defer cl.RUnlockNamed(read_lock)
}
clength = len(cl.Current_work)
return
}
func (cl *Client) IsBusy(lock bool) (busy bool, err error) {
cw_length, err := cl.Current_work_length(lock)
if err != nil {
return
}
if cw_length > 0 {
busy = true
return
}
busy = false
return
}
func (cl *Client) Marshal() (result []byte, err error) {
read_lock, err := cl.RLockNamed("Marshal")
if err != nil {
return
}
defer cl.RUnlockNamed(read_lock)
result, err = json.Marshal(cl)
return
}