/
handlerRunModel.go
253 lines (213 loc) · 7.89 KB
/
handlerRunModel.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
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package main
import (
"net/http"
"strconv"
"strings"
"golang.org/x/text/language"
"github.com/openmpp/go/ompp/helper"
"github.com/openmpp/go/ompp/omppLog"
)
// runModelHandler run the model identified by model digest-or-name with specified run options.
// POST /api/run
// Json RunRequest structure is posted to specify model digest-or-name, run stamp and othe run options.
// If multiple models with same name exist then result is undefined.
// Model run console output redirected to log file: models/log/modelName.runStamp.console.log
func runModelHandler(w http.ResponseWriter, r *http.Request) {
// decode json request body
var req RunRequest
if !jsonRequestDecode(w, r, true, &req) {
return // error at json decode, response done with http error
}
// if log messages language not specified then use browser preferred language
if _, ok := req.Opts["OpenM.MessageLanguage"]; !ok {
if rqLangTags, _, e := language.ParseAcceptLanguage(r.Header.Get("Accept-Language")); e == nil {
if len(rqLangTags) > 0 && rqLangTags[0] != language.Und {
req.Opts["OpenM.MessageLanguage"] = rqLangTags[0].String()
}
}
}
// find model metadata by digest or name
dn := req.ModelDigest
if dn == "" {
dn = req.ModelName
}
m, ok := theCatalog.ModelDicByDigestOrName(dn)
if !ok {
http.Error(w, "Model not found: "+dn, http.StatusBadRequest)
return // empty result: model digest not found
}
req.ModelDigest = m.Digest
req.ModelName = m.Name
// adjust MPI options:
// IsMpi is the same as number of processes > 0
if req.Mpi.Np < 0 {
req.Mpi.Np = 0
}
if req.Mpi.Np > 0 {
req.IsMpi = true
}
if req.IsMpi && req.Mpi.Np <= 0 {
req.Mpi.Np = 1
}
if req.IsMpi && !theCfg.isJobControl {
req.Mpi.IsNotByJob = true // if job control disabled then model run cannot use job control
}
// get submit stamp
submitStamp, dtNow := theCatalog.getNewTimeStamp()
if req.Env == nil {
req.Env = map[string]string{}
}
job := RunJob{
SubmitStamp: submitStamp,
RunRequest: req,
}
// get number of modelling cpu
// for backward compatibility: check if number of threads specified using run options
job.Res, job.Mpi.IsNotOnRoot, _, job.Threads, ok = resFromRequest(req)
if !ok {
http.Error(w, "Model start failed: "+dn, http.StatusBadRequest)
return
}
// if job control disabled then start model run
if !theCfg.isJobControl {
rs, err := theRunCatalog.runModel(&job, "", hostIni{}, []computeUse{}) // no job control: use empty arguments
if err != nil {
omppLog.Log(err)
http.Error(w, "Model start failed: "+dn, http.StatusBadRequest)
return
}
w.Header().Set("Content-Location", "/api/model/"+job.ModelDigest+"/run/"+rs.RunStamp)
jsonResponse(w, r, rs)
return
}
// else append run request to the queue and return submit stamp
_, err := theRunCatalog.addJobToQueue(&job)
if err != nil {
http.Error(w, "Model run submission failed: "+dn, http.StatusBadRequest)
return
}
rStamp := helper.CleanPath(job.RunStamp)
w.Header().Set("Content-Location", "/api/model/"+job.ModelDigest+"/run/"+rStamp)
jsonResponse(w, r,
&RunState{
ModelName: job.ModelName,
ModelDigest: job.ModelDigest,
RunStamp: rStamp,
SubmitStamp: submitStamp,
UpdateDateTime: helper.MakeDateTime(dtNow),
})
}
// return cpu modelling count, MPI not-on-root flag, number of processes, number of modelling threads per process and error flag
func resFromRequest(req RunRequest) (RunRes, bool, int, int, bool) {
// get number of threads and MPI NotOnRoot flag
nTh := req.Threads
isNotOnRoot := req.Mpi.IsNotOnRoot
for krq, val := range req.Opts {
// backward compatibility: check if number of threads specified using run options
var err error
if req.Threads <= 1 &&
(strings.EqualFold(krq, "-OpenM.Threads") || strings.EqualFold(krq, "OpenM.Threads")) {
nTh, err = strconv.Atoi(val) // must be >= 1
if err != nil || nTh < 1 {
omppLog.Log(err)
return RunRes{}, false, 0, 0, false
}
}
// backward compatibility: check MPI "not on root" specified using run options
if !isNotOnRoot {
// get MPI "not on root" flag: do not run modelling on root MPI process
if strings.EqualFold(krq, "-OpenM.NotOnRoot") || strings.EqualFold(krq, "OpenM.NotOnRoot") {
if val == "" {
isNotOnRoot = true // empty boolean option value treated as true
}
isNotOnRoot, err = strconv.ParseBool(val)
if err != nil {
omppLog.Log(err)
return RunRes{}, false, 0, 0, false
}
}
}
}
// number of modelling processes and requested cpu count
nProc := req.Mpi.Np
if nProc <= 0 {
nProc = 1
}
np := nProc
if np > 1 && isNotOnRoot {
np--
}
if nTh <= 0 {
nTh = 1
}
res := RunRes{
Cpu: np * nTh,
Mem: 0,
}
return res, isNotOnRoot, np, nTh, true
}
// stopModelHandler kill model run by model digest-or-name and run stamp or remove model run request from queue by submit stamp.
// PUT /api/run/stop/model/:model/stamp/:stamp
// If multiple models with same name exist then result is undefined.
func stopModelHandler(w http.ResponseWriter, r *http.Request) {
// url or query parameters: model digest-or-name, page offset and page size
dn := getRequestParam(r, "model")
stamp := getRequestParam(r, "stamp")
// find model metadata by digest or name
m, ok := theCatalog.ModelDicByDigestOrName(dn)
if !ok {
http.Error(w, "Model not found: "+dn, http.StatusBadRequest)
return // empty result: model digest not found
}
modelDigest := m.Digest
// kill model run by run stamp or
// remove run request from the queue by submit stamp or by run stamp
isFound, submitStamp, jobPath, isRunning := theRunCatalog.stopModelRun(modelDigest, stamp)
if !isRunning {
moveJobQueueToFailed(jobPath, submitStamp, m.Name, m.Digest, stamp) // model was not running, move job control file to history
}
// write model run key as response
w.Header().Set("Content-Location", "/api/model/"+modelDigest+"/run/"+stamp+"/"+strconv.FormatBool(isFound))
w.Header().Set("Content-Type", "text/plain")
}
// runLogPageHandler return model run status and log by model digest-or-name and run-or-submit stamp.
// GET /api/run/log/model/:model/stamp/:stamp
// GET /api/run/log/model/:model/stamp/:stamp/start/:start/count/:count
// If multiple models with same name exist then result is undefined.
// Model run log is same as console output and include stdout and stderr.
// Run log can be returned by page defined by zero-based "start" line and line count.
// If count <= 0 then all log lines until eof returned, complete current log: start=0, count=0
func runLogPageHandler(w http.ResponseWriter, r *http.Request) {
// url or query parameters: model digest-or-name, page offset and page size
dn := getRequestParam(r, "model")
stamp := getRequestParam(r, "stamp")
start, ok := getIntRequestParam(r, "start", 0)
if !ok {
http.Error(w, "Invalid value of start log start line "+dn, http.StatusBadRequest)
return
}
count, ok := getIntRequestParam(r, "count", int(theCfg.pageMaxSize))
if !ok {
http.Error(w, "Invalid value of log line count "+dn, http.StatusBadRequest)
return
}
// find model metadata by digest or name
m, ok := theCatalog.ModelDicByDigestOrName(dn)
if !ok {
http.Error(w, "Model not found: "+dn, http.StatusBadRequest)
return // empty result: model digest not found
}
modelDigest := m.Digest
modelName := m.Name
// get current run status and page of log lines
lrp, e := theRunCatalog.readModelRunLog(modelDigest, stamp, start, count)
if e != nil {
omppLog.Log(e)
http.Error(w, "Model run status read failed: "+modelName+": "+dn, http.StatusBadRequest)
return
}
// return model run status and log content
jsonResponse(w, r, lrp)
}