-
Notifications
You must be signed in to change notification settings - Fork 21
/
proxymgr.go
306 lines (259 loc) · 7.28 KB
/
proxymgr.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
package core
import (
"errors"
"fmt"
"github.com/MG-RAST/AWE/lib/conf"
e "github.com/MG-RAST/AWE/lib/errors"
"github.com/MG-RAST/AWE/lib/logger"
"github.com/MG-RAST/AWE/lib/logger/event"
"github.com/MG-RAST/AWE/lib/user"
"os"
"time"
)
type ProxyMgr struct {
CQMgr
}
func NewProxyMgr() *ProxyMgr {
return &ProxyMgr{
CQMgr: CQMgr{
clientMap: map[string]*Client{},
workQueue: NewWQueue(),
suspendQueue: false,
coReq: make(chan CoReq),
coAck: make(chan CoAck),
feedback: make(chan Notice),
coSem: make(chan int, 1), //non-blocking buffered channel
},
}
}
func (qm *ProxyMgr) JidHandle() {
return
}
func (qm *ProxyMgr) TaskHandle() {
return
}
func (qm *ProxyMgr) ClientHandle() {
for {
select {
case coReq := <-qm.coReq:
logger.Debug(2, fmt.Sprintf("proxymgr: workunit checkout request received, Req=%v\n", coReq))
var ack CoAck
if qm.suspendQueue {
// queue is suspended, return suspend error
ack = CoAck{workunits: nil, err: errors.New(e.QueueSuspend)}
} else {
works, err := qm.popWorks(coReq)
ack = CoAck{workunits: works, err: err}
}
qm.coAck <- ack
case notice := <-qm.feedback:
logger.Debug(2, fmt.Sprintf("proxymgr: workunit feedback received, workid=%s, status=%s, clientid=%s\n", notice.WorkId, notice.Status, notice.ClientId))
if err := qm.handleWorkStatusChange(notice); err != nil {
logger.Error("handleWorkStatusChange(): " + err.Error())
}
}
}
}
func (qm *ProxyMgr) SuspendQueue() {
return
}
func (qm *ProxyMgr) ResumeQueue() {
return
}
func (qm *ProxyMgr) QueueStatus() string {
return ""
}
func (qm *ProxyMgr) InitMaxJid() (err error) {
return
}
func (qm *ProxyMgr) ShowStatus() string {
return ""
}
//---end of mgr methods
// workunit methods
//handle feedback from a client about the execution of a workunit
func (qm *ProxyMgr) handleWorkStatusChange(notice Notice) (err error) {
//relay the notice to the server
perf := new(WorkPerf)
workid := notice.WorkId
clientid := notice.ClientId
if client, ok := qm.GetClient(clientid); ok {
delete(client.Current_work, workid)
if len(client.Current_work) == 0 {
client.Status = CLIENT_STAT_ACTIVE_IDLE
}
qm.PutClient(client)
}
if work, ok := qm.workQueue.Get(workid); ok {
work.State = notice.Status
if err = proxy_relay_workunit(work, perf); err != nil {
return
}
if work.State == WORK_STAT_DONE {
if client, ok := qm.GetClient(clientid); ok {
client.Total_completed += 1
client.Last_failed = 0 //reset last consecutive failures
qm.PutClient(client)
}
} else if work.State == WORK_STAT_FAIL {
if client, ok := qm.GetClient(clientid); ok {
client.Skip_work = append(client.Skip_work, workid)
client.Total_failed += 1
client.Last_failed += 1 //last consecutive failures
if client.Last_failed == conf.MAX_CLIENT_FAILURE {
client.Status = CLIENT_STAT_SUSPEND
}
qm.PutClient(client)
}
}
qm.workQueue.Put(work)
}
return
}
func (qm *ProxyMgr) FetchDataToken(workid string, clientid string) (token string, err error) {
return
}
func (qm *ProxyMgr) FetchPrivateEnv(workid string, clientid string) (env map[string]string, err error) {
return
}
//end of workunits methods
//client methods
func (qm *ProxyMgr) RegisterNewClient(files FormFiles, cg *ClientGroup) (client *Client, err error) {
if _, ok := files["profile"]; ok {
client, err = NewProfileClient(files["profile"].Path)
os.Remove(files["profile"].Path)
} else {
client = NewClient()
}
if err != nil {
return nil, err
}
// If the name of the clientgroup does not match the name in the client profile, throw an error
if cg != nil && client.Group != cg.Name {
return nil, errors.New("Clientgroup name in token does not match that in the client configuration.")
}
qm.PutClient(client)
if len(client.Current_work) > 0 { //re-registered client
// move already checked-out workunit from waiting queue (workMap) to checked-out list (coWorkMap)
for workid, _ := range client.Current_work {
if qm.workQueue.Has(workid) {
qm.workQueue.StatusChange(workid, WORK_STAT_CHECKOUT)
}
}
}
//proxy specific
Self.SubClients += 1
notifySubClients(Self.Id, Self.SubClients)
return
}
func (qm *ProxyMgr) ClientChecker() {
for {
time.Sleep(30 * time.Second)
for _, client := range qm.GetAllClients() {
if client.Tag == true {
client.Tag = false
total_minutes := int(time.Now().Sub(client.RegTime).Minutes())
hours := total_minutes / 60
minutes := total_minutes % 60
client.Serve_time = fmt.Sprintf("%dh%dm", hours, minutes)
if len(client.Current_work) > 0 {
client.Idle_time = 0
} else {
client.Idle_time += 30
}
qm.PutClient(client)
} else {
//now client must be gone as tag set to false 30 seconds ago and no heartbeat received thereafter
logger.Event(event.CLIENT_UNREGISTER, "clientid="+client.Id+";name="+client.Name)
//requeue unfinished workunits associated with the failed client
qm.ReQueueWorkunitByClient(client.Id)
//delete the client from client map
qm.RemoveClient(client.Id)
//proxy specific
Self.SubClients -= 1
notifySubClients(Self.Id, Self.SubClients)
}
}
}
}
//end of client methods
func (qm *ProxyMgr) EnqueueTasksByJobId(jobid string, tasks []*Task) (err error) {
return
}
func (qm *ProxyMgr) JobRegister() (jid string, err error) {
return
}
func (qm *ProxyMgr) GetActiveJobs() map[string]bool {
return nil
}
func (qm *ProxyMgr) IsJobRegistered(id string) bool {
return false
}
func (qm *ProxyMgr) GetSuspendJobs() map[string]bool {
return nil
}
func (qm *ProxyMgr) SuspendJob(jobid string, reason string, id string) (err error) {
return
}
func (qm *ProxyMgr) ResumeSuspendedJobs() (num int) {
return
}
func (qm *ProxyMgr) ResumeSuspendedJobsByUser(u *user.User) (num int) {
return
}
func (qm *ProxyMgr) DeleteJob(jobid string) (err error) {
return
}
func (qm *ProxyMgr) DeleteJobByUser(jobid string, u *user.User) (err error) {
return
}
func (qm *ProxyMgr) DeleteSuspendedJobs() (num int) {
return
}
func (qm *ProxyMgr) DeleteSuspendedJobsByUser(u *user.User) (num int) {
return
}
func (qm *ProxyMgr) DeleteZombieJobs() (num int) {
return
}
func (qm *ProxyMgr) DeleteZombieJobsByUser(u *user.User) (num int) {
return
}
//resubmit a suspended job
func (qm *ProxyMgr) ResumeSuspendedJob(id string) (err error) {
//Load job by id
return
}
//resubmit a suspended job if user has rights
func (qm *ProxyMgr) ResumeSuspendedJobByUser(id string, u *user.User) (err error) {
//Load job by id
return
}
//re-submit a job in db but not in the queue (caused by server restarting)
func (qm *ProxyMgr) ResubmitJob(id string) (err error) {
return
}
//recover jobs not completed before awe-server restarts
func (qm *ProxyMgr) RecoverJobs() (err error) {
return
}
//recompute jobs from specified task stage
func (qm *ProxyMgr) RecomputeJob(jobid string, stage string) (err error) {
return
}
func (qm *ProxyMgr) UpdateGroup(jobid string, newgroup string) (err error) {
return
}
func (qm *ProxyMgr) UpdatePriority(jobid string, priority int) (err error) {
return
}
func (qm *ProxyMgr) FinalizeWorkPerf(string, string) (err error) {
return
}
func (qm *ProxyMgr) SaveStdLog(string, string, string) (err error) {
return
}
func (qm *ProxyMgr) GetReportMsg(string, string) (report string, err error) {
return
}
//---end of job methods