-
Notifications
You must be signed in to change notification settings - Fork 1
/
controllers_master.go
265 lines (230 loc) · 7.37 KB
/
controllers_master.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
/*
Copyright (C) 2003-2011 Institute for Systems Biology
Seattle, Washington, USA.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
)
type MasterJobController struct {
master *Master
apikey string
}
// GET /jobs
func (this MasterJobController) Index(rw http.ResponseWriter) {
logger.Debug("Index()")
items := make([]JobDetails, 0, 0)
logger.Debug("for loop")
this.master.subMu.RLock()
for _, s := range this.master.subMap {
if s != nil {
items = append(items, s.SniffDetails())
}
}
this.master.subMu.RUnlock()
logger.Debug("for loop done")
jobDetails := JobDetailsList{Items: items, NumberOfItems: len(items)}
if err := json.NewEncoder(rw).Encode(jobDetails); err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
}
}
// POST /jobs
func (this MasterJobController) Create(rw http.ResponseWriter, r *http.Request) {
logger.Debug("Create()")
if CheckApiKey(this.apikey, r) == false {
http.Error(rw, "api key required in header", http.StatusForbidden)
return
}
tasks := make([]Task, 0, 100)
if err := LoadTasksFromJson(r, &tasks); err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
jobId := GetHeader(r, "x-golem-job-preassigned-id", "")
if jobId == "" {
jobId = UniqueId()
}
this.master.subMu.RLock()
_, isin := this.master.subMap[jobId]
this.master.subMu.RUnlock()
if isin {
msg := fmt.Sprintf("job already exists: %v", jobId)
logger.Debug(msg)
http.Error(rw, msg, http.StatusConflict)
return
}
owner := GetHeader(r, "x-golem-job-owner", "Anonymous")
label := GetHeader(r, "x-golem-job-label", jobId)
jobtype := GetHeader(r, "x-golem-job-type", "Unspecified")
jd := NewJobDetails(jobId, owner, label, jobtype, TotalTasks(tasks), SCHEDULED, READY)
logger.Debug("creating: %v", jobId)
this.master.subMu.Lock()
this.master.subMap[jobId] = NewSubmission(jd, tasks, this.master.jobChan)
this.master.subMu.Unlock()
logger.Debug("created: %v", jobId)
if err := json.NewEncoder(rw).Encode(jd); err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
}
}
// GET /jobs/id
func (this MasterJobController) Find(rw http.ResponseWriter, id string) {
logger.Debug("Find(%v)", id)
this.master.subMu.RLock()
s, isin := this.master.subMap[id]
this.master.subMu.RUnlock()
if isin == false {
logger.Debug("job not found: %v", id)
http.Error(rw, "job "+id+" not found", http.StatusNotFound)
return
}
logger.Debug("job found: %v", id)
if err := json.NewEncoder(rw).Encode(s.SniffDetails()); err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
}
}
// POST /jobs/id/stop or POST /jobs/id/kill
func (this MasterJobController) Act(rw http.ResponseWriter, parts []string, r *http.Request) {
logger.Debug("Act(%v)", r.URL.Path)
if CheckApiKey(this.apikey, r) == false {
http.Error(rw, "api key required in header", http.StatusForbidden)
return
}
if len(parts) < 2 {
http.Error(rw, "POST /jobs/id/stop or POST /jobs/id/kill", http.StatusBadRequest)
return
}
jobId := parts[0]
logger.Debug("finding job: %v", jobId)
this.master.subMu.RLock()
job, isin := this.master.subMap[jobId]
this.master.subMu.RUnlock()
if isin == false {
logger.Debug("job not found: %v", jobId)
http.Error(rw, "job "+jobId+" not found", http.StatusNotFound)
return
}
if parts[1] == "stop" {
logger.Debug("stopping: %v", jobId)
if job.Stop() == false {
logger.Debug("job seems to have already been stopped: %v", jobId)
}
if job.SniffDetails().IsRunning() {
logger.Debug("job seems to still be running: %v", jobId)
http.Error(rw, "unable to stop", http.StatusExpectationFailed)
}
} else if parts[1] == "kill" {
logger.Debug("killing: %v", jobId)
if job.Stop() == false {
logger.Debug("job seems to have already been stopped: %v", jobId)
}
if job.SniffDetails().IsRunning() {
logger.Debug("job seems to still be running: %v", jobId)
http.Error(rw, "unable to stop", http.StatusExpectationFailed)
}
this.master.Broadcast(&WorkerMessage{Type: KILL, SubId: jobId})
} else if parts[1] == "archive" {
logger.Debug("archiving: %v", jobId)
dtls := job.SniffDetails()
if dtls.State == COMPLETE {
go func() {
<-time.After(time.Duration(900)*time.Second)
this.master.subMu.Lock()
_, isin := this.master.subMap[jobId]
if isin {
delete(this.master.subMap, jobId)
}
this.master.subMu.Unlock()
}()
} else {
http.Error(rw, fmt.Sprintf("unable to archive:%v:%v", jobId, dtls), http.StatusConflict)
}
} else {
http.Error(rw, r.URL.Path, http.StatusNotImplemented)
}
logger.Debug("Act(): completed")
}
type MasterNodeController struct {
master *Master
apikey string
}
// GET /nodes
func (this MasterNodeController) Index(rw http.ResponseWriter) {
logger.Debug("Index()")
items := make([]WorkerNode, 0, 0)
logger.Debug("for loop")
this.master.nodeMu.RLock()
for _, n := range this.master.NodeHandles {
items = append(items, NewWorkerNode(n))
}
this.master.nodeMu.RUnlock()
logger.Debug("for loop done")
workerNodes := WorkerNodeList{Items: items, NumberOfItems: len(items)}
if err := json.NewEncoder(rw).Encode(workerNodes); err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
}
}
// GET /nodes/id
func (this MasterNodeController) Find(rw http.ResponseWriter, nodeId string) {
logger.Debug("Find(%v)", nodeId)
this.master.nodeMu.RLock()
nh, isin := this.master.NodeHandles[nodeId]
this.master.nodeMu.RUnlock()
if isin == false {
logger.Debug("node not found: %v", nodeId)
http.Error(rw, "node "+nodeId+" not found", http.StatusNotFound)
return
}
logger.Debug("node found: %v", nodeId)
if err := json.NewEncoder(rw).Encode(NewWorkerNode(nh)); err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
}
}
// POST /nodes/restart or POST /nodes/die or POST /nodes/id/resize/new-size
func (this MasterNodeController) Act(rw http.ResponseWriter, parts []string, r *http.Request) {
logger.Debug("Act(%v):%v", r.URL.Path, parts)
if CheckApiKey(this.apikey, r) == false {
http.Error(rw, "api key required in header", http.StatusForbidden)
return
}
if parts[0] == "restart" {
this.master.Broadcast(&WorkerMessage{Type: RESTART})
go RestartIn(10)
return
}
if parts[0] == "die" {
this.master.Broadcast(&WorkerMessage{Type: DIE})
go DieIn(10)
return
}
if parts[1] == "resize" {
nodeId := parts[0]
numberOfThreads, err := strconv.Atoi(parts[2])
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
this.master.nodeMu.RLock()
node, isin := this.master.NodeHandles[nodeId]
this.master.nodeMu.RUnlock()
if isin == false {
http.Error(rw, "node "+nodeId+" not found", http.StatusNotFound)
return
}
node.ReSize(numberOfThreads)
}
}