/
runJob.go
837 lines (676 loc) · 22.6 KB
/
runJob.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package main
import (
"errors"
"sort"
"strconv"
"time"
"github.com/openmpp/go/ompp/helper"
"github.com/openmpp/go/ompp/omppLog"
)
// get model run job from the queue
// return job to run, bool job found flag, queue job file path, list of server names to run the job, list of resources to use on each server
func (rsc *RunCatalog) selectJobFromQueue() (*RunJob, bool, string, []computeUse, hostIni, error) {
rsc.rscLock.Lock()
defer rsc.rscLock.Unlock()
if rsc.IsQueuePaused || len(rsc.queueKeys) <= 0 {
return nil, false, "", []computeUse{}, hostIni{}, nil // queue is paused or empty
}
// resource available to rum MPI jobs from global MPI queue and form localhost queue of this oms instance jobs
qLocal := RunRes{
Cpu: rsc.LocalRes.Cpu - rsc.LocalActiveRes.Cpu,
Mem: rsc.LocalRes.Mem - rsc.LocalActiveRes.Mem,
}
qMpi := RunRes{}
isMpiLimit := false
if len(rsc.computeState) <= 0 {
qMpi = qLocal // no computational servers, use localhost to run MPI jobs
isMpiLimit = qLocal.Cpu > 0 // MPI cores limited if locahost CPU is limited
} else {
isMpiLimit = true // MPI cores are limited by sum of ready to use servers CPU cores
for _, cs := range rsc.computeState {
if cs.state == "ready" {
qMpi.Cpu += cs.totalRes.Cpu
qMpi.Mem += cs.totalRes.Mem
}
}
qMpi.Cpu = qMpi.Cpu - rsc.ActiveTotalRes.Cpu
qMpi.Mem = qMpi.Mem - rsc.ActiveTotalRes.Mem
}
// first job in localhost non-MPI queue or global MPI queue where zero resources required for previous jobs
// check if there are enough available resources to run the job
stamp := ""
for _, qKey := range rsc.queueKeys {
jc, ok := rsc.queueJobs[qKey]
if !ok || jc.isError || jc.IsOverLimit || jc.preRes.Cpu > 0 || jc.preRes.Mem > 0 {
continue // skip invalid job or if required resources exceeding limits or if it is not a first job in global queue
}
isSel := false
for j := 0; !isSel && j < len(rsc.selectedKeys); j++ {
isSel = rsc.selectedKeys[j] == qKey
}
if isSel {
continue // this job is already selected to run
}
// check avaliable resource: cpu cores and memory
if !jc.IsMpi {
if rsc.LocalRes.Cpu > 0 && jc.Res.Cpu > 0 && qLocal.Cpu < jc.Res.Cpu+jc.preRes.Cpu {
continue // localhost cpu cores limited and job cpu limit set to non zero and localhost available cpu less than required cpu
}
if rsc.LocalRes.Mem > 0 && jc.Res.Mem > 0 && qLocal.Mem < jc.Res.Mem+jc.preRes.Mem {
continue // localhost memory limited and job memory limit set to non zero and localhost available memory less than required memory
}
} else { // check resources available for MPI jobs
if isMpiLimit && qMpi.Cpu < jc.Res.Cpu+jc.preRes.Cpu {
continue // job cpu limit set to non zero and MPI cluster available cpu less than required cpu
}
if qMpi.Mem > 0 && jc.Res.Mem > 0 && qMpi.Mem < jc.Res.Mem+jc.preRes.Mem {
continue // MPI cluster memory limited and job memory limit set to non zero and MPI cluster available memory less than required memory
}
}
stamp = qKey // first job in queue which not yet selected to run and enough resources available to run
}
if stamp == "" {
return nil, false, "", []computeUse{}, hostIni{}, nil // queue is empty or all jobs already selected to run
}
qj := rsc.queueJobs[stamp] // job found
// if this is MPI job then
// select servers where state is ready and max cpu cores available
// until all required cores assigned to the servers
compUse := []computeUse{}
if qj.IsMpi && len(rsc.computeState) > 0 {
nCpu := qj.Res.Cpu
nMem := qj.Res.Mem
for nCpu > 0 {
// find serever with max cpu cores available
cUse := computeUse{}
for name, cs := range rsc.computeState {
if cs.state != "ready" {
continue // this server is not ready
}
isUse := false
for k := 0; !isUse && k < len(compUse); k++ {
isUse = name == compUse[k].name
}
if isUse {
continue // this server already selected for tha model run
}
if cUse.Cpu < cs.totalRes.Cpu-cs.usedRes.Cpu {
cUse.name = name
cUse.Cpu = cs.totalRes.Cpu - cs.usedRes.Cpu
cUse.Mem = cs.totalRes.Mem - cs.usedRes.Mem
}
}
if cUse.name == "" {
if len(compUse) <= 0 {
qj.isError = true
rsc.queueJobs[stamp] = qj
e := errors.New("ERROR: resources not found to run the model, CPU: " + strconv.Itoa(qj.Res.Cpu) + ": " + stamp)
return &qj.RunJob, false, qj.filePath, []computeUse{}, hostIni{}, e
}
// else assign the rest of the job to the last server
compUse[len(compUse)-1].Cpu = compUse[len(compUse)-1].Cpu + nCpu
omppLog.Log("WARNING: oversubscribe resources run the model, CPU: ", qj.Res.Cpu, ": ", stamp)
break // oversubscribe last server
}
if cUse.Cpu > nCpu {
cUse.Cpu = nCpu
}
if cUse.Mem > nMem {
cUse.Mem = nMem
}
if cUse.Mem < 0 {
cUse.Mem = 0
}
compUse = append(compUse, cUse)
nCpu = nCpu - cUse.Cpu
if nMem > 0 {
nMem = nMem - (rsc.computeState[cUse.name].totalRes.Mem - rsc.computeState[cUse.name].usedRes.Mem)
}
}
}
// job found: copy run request from the queue
rsc.selectedKeys = append(rsc.selectedKeys, stamp)
job := qj.RunJob
job.Opts = make(map[string]string, len(qj.Opts))
for key, val := range qj.Opts {
job.Opts[key] = val
}
job.Env = make(map[string]string, len(qj.Env))
for key, val := range qj.Env {
job.Env[key] = val
}
job.Tables = make([]string, len(qj.Tables))
copy(job.Tables, qj.Tables)
job.RunNotes = make(
[]struct {
LangCode string // model language code
Note string // run notes
},
len(qj.RunNotes))
copy(job.RunNotes, qj.RunNotes)
return &job, true, qj.filePath, compUse, rsc.hostFile, nil
}
// return copy of submission stamps and job control items for queue, active and history model run jobs
func (rsc *RunCatalog) getRunJobs() (JobServiceState, []string, []RunJob, []string, []RunJob, []string, []historyJobFile, []computeItem) {
rsc.rscLock.Lock()
defer rsc.rscLock.Unlock()
// jobs queue: sort in order of submission stamps, which user may change through UI
qKeys := make([]string, len(rsc.queueKeys))
qJobs := make([]RunJob, len(rsc.queueKeys))
for k, stamp := range rsc.queueKeys {
qKeys[k] = stamp
qJobs[k] = rsc.queueJobs[stamp].RunJob
}
// active jobs: sort by submission time
aKeys := make([]string, len(rsc.activeJobs))
n := 0
for stamp := range rsc.activeJobs {
aKeys[n] = stamp
n++
}
sort.Strings(aKeys)
aJobs := make([]RunJob, len(aKeys))
for k, stamp := range aKeys {
aJobs[k] = rsc.activeJobs[stamp].RunJob
}
// history jobs: sort by submission time
hKeys := make([]string, len(rsc.historyJobs))
n = 0
for stamp := range rsc.historyJobs {
hKeys[n] = stamp
n++
}
sort.Strings(hKeys)
hJobs := make([]historyJobFile, len(hKeys))
for k, stamp := range hKeys {
hJobs[k] = rsc.historyJobs[stamp]
}
// get computational servers state, sorted by name
cN := make([]string, len(rsc.computeState))
np := 0
for name := range rsc.computeState {
cN[np] = name
np++
}
sort.Strings(cN)
cState := make([]computeItem, len(rsc.computeState))
for k := 0; k < len(cN); k++ {
cs := rsc.computeState[cN[k]]
cState[k] = cs
cState[k].startArgs = []string{}
cState[k].stopArgs = []string{}
}
return rsc.JobServiceState, qKeys, qJobs, aKeys, aJobs, hKeys, hJobs, cState
}
// return active job control item and is found boolean flag
func (rsc *RunCatalog) getActiveJobItem(submitStamp string) (runJobFile, bool) {
if submitStamp == "" {
return runJobFile{}, false // empty job submission stamp: return empty result
}
rsc.rscLock.Lock()
defer rsc.rscLock.Unlock()
if aj, ok := rsc.activeJobs[submitStamp]; ok {
return aj, true
}
return runJobFile{}, false // not found
}
// return queue job control item and is found boolean flag
func (rsc *RunCatalog) getQueueJobItem(submitStamp string) (queueJobFile, bool) {
if submitStamp == "" {
return queueJobFile{}, false // empty job submission stamp: return empty result
}
rsc.rscLock.Lock()
defer rsc.rscLock.Unlock()
if qj, ok := rsc.queueJobs[submitStamp]; ok {
return qj, true
}
return queueJobFile{}, false // not found
}
// return history job control item and is found boolean flag
func (rsc *RunCatalog) getHistoryJobItem(submitStamp string) (historyJobFile, bool) {
if submitStamp == "" {
return historyJobFile{}, false // empty job submission stamp: return empty result
}
rsc.rscLock.Lock()
defer rsc.rscLock.Unlock()
if hj, ok := rsc.historyJobs[submitStamp]; ok {
return hj, true
}
return historyJobFile{}, false // not found
}
// write new run request into job queue file, return queue job file path
func (rsc *RunCatalog) addJobToQueue(job *RunJob) (string, error) {
if !theCfg.isJobControl {
return "", nil // job control disabled
}
fp := jobQueuePath(job.SubmitStamp, job.ModelName, job.ModelDigest, job.IsMpi, rsc.nextJobPosition(), job.Res.Cpu, job.Res.Mem)
err := helper.ToJsonIndentFile(fp, job)
if err != nil {
omppLog.Log(err)
fileDeleteAndLog(true, fp) // on error remove file, if any file created
return "", err
}
return "", nil
}
// return next job position in the queue, it is not a queue index but "ticket number" to establish queue jobs order
func (rsc *RunCatalog) nextJobPosition() int {
if !theCfg.isJobControl {
return 0 // job control disabled
}
rsc.rscLock.Lock()
defer rsc.rscLock.Unlock()
rsc.jobLastPosition++
if rsc.jobLastPosition <= jobPositionDefault {
rsc.jobLastPosition = jobPositionDefault + 1
}
return rsc.jobLastPosition
}
// move job into the specified queue index position.
// Top of the queue position is zero, negative position treated as zero.
// If position number exceeds queue length then job moved to the bottom of the queue.
// Return false if job not found in the queue
func (rsc *RunCatalog) moveJobInQueue(submitStamp string, index int) (bool, [][2]string) {
if submitStamp == "" {
return false, [][2]string{} // empty job submission stamp: return empty result
}
rsc.rscLock.Lock()
defer rsc.rscLock.Unlock()
// find current job position in the queue, excluding jobs selected to run
isFound := false
n := 0
for n = range rsc.queueKeys {
isFound = rsc.queueKeys[n] == submitStamp
if isFound {
break
}
}
if isFound {
isSel := false
for j := 0; !isSel && j < len(rsc.selectedKeys); j++ {
isSel = rsc.selectedKeys[j] == submitStamp
}
isFound = !isSel
}
if !isFound {
return false, [][2]string{} // job not found in the queue
}
// position must be between zero at the last position in the queue
nPos := index
fPos := jobPositionDefault
isFirst := nPos <= 0
if isFirst {
nPos = 0
rsc.jobFirstPosition--
fPos = rsc.jobFirstPosition
}
isLast := !isFirst && nPos >= len(rsc.queueKeys)-1
if isLast {
nPos = len(rsc.queueKeys) - 1
fPos = rsc.jobLastPosition + 1
rsc.jobLastPosition = fPos + 1
}
if nPos == n {
return true, [][2]string{} // job is already at this position
}
// list of files to rename
moveLst := [][2]string{}
fJ, isFrom := rsc.queueJobs[submitStamp]
toJ, isTo := rsc.queueJobs[rsc.queueKeys[nPos]]
if !isFirst && !isLast {
if isFrom && isTo {
moveLst = append(moveLst, [2]string{
fJ.filePath,
jobQueuePath(fJ.SubmitStamp, fJ.ModelName, fJ.ModelDigest, fJ.IsMpi, toJ.position, fJ.Res.Cpu, fJ.Res.Mem),
})
}
} else { // move source file to the top (before the first position) or to the bottom (after last postion)
moveLst = append(moveLst, [2]string{
fJ.filePath,
jobQueuePath(fJ.SubmitStamp, fJ.ModelName, fJ.ModelDigest, fJ.IsMpi, fPos, fJ.Res.Cpu, fJ.Res.Mem),
})
}
// move down to the queue: shift items up
if nPos > n {
for k := n; k < nPos && k < len(rsc.queueKeys)-1; k++ {
// skip files selected to run
fromKey := rsc.queueKeys[k+1]
isSel := false
for j := 0; !isSel && j < len(rsc.selectedKeys); j++ {
isSel = rsc.selectedKeys[j] == fromKey
}
if isSel {
continue
}
if !isFirst && !isLast {
fJ, isFrom = rsc.queueJobs[fromKey]
toJ, isTo = rsc.queueJobs[rsc.queueKeys[k]]
if isFrom && isTo {
moveLst = append(moveLst, [2]string{
fJ.filePath,
jobQueuePath(fJ.SubmitStamp, fJ.ModelName, fJ.ModelDigest, fJ.IsMpi, toJ.position, fJ.Res.Cpu, fJ.Res.Mem),
})
}
}
rsc.queueKeys[k] = fromKey
}
} else { // move up in the queue: shift items down
for k := n - 1; k >= nPos && k >= 0; k-- {
// skip files selected to run
fromKey := rsc.queueKeys[k]
isSel := false
for j := 0; !isSel && j < len(rsc.selectedKeys); j++ {
isSel = rsc.selectedKeys[j] == fromKey
}
if isSel {
continue
}
if !isFirst && !isLast {
fJ, isFrom = rsc.queueJobs[fromKey]
toJ, isTo = rsc.queueJobs[rsc.queueKeys[k+1]]
if isFrom && isTo {
moveLst = append(moveLst, [2]string{
fJ.filePath,
jobQueuePath(fJ.SubmitStamp, fJ.ModelName, fJ.ModelDigest, fJ.IsMpi, toJ.position, fJ.Res.Cpu, fJ.Res.Mem),
})
}
}
rsc.queueKeys[k+1] = fromKey
}
}
rsc.queueKeys[nPos] = submitStamp
return true, moveLst
}
// select additional computational servers or clusters to start,
// return server names, startup timeout and for each server startup exe names and startup arguments
func (rsc *RunCatalog) selectToStartCompute() ([]string, int64, []string, [][]string) {
rsc.rscLock.Lock()
defer rsc.rscLock.Unlock()
// do not start anything if:
// this instance is not a leader oms instance or queue is paused or no computational servers or MPI queue is empty
nowTs := time.Now().UnixMilli()
if !rsc.isLeader || rsc.IsQueuePaused || len(rsc.computeState) <= 0 ||
rsc.topQueueRes.Cpu <= 0 && rsc.topQueueRes.Mem <= 0 ||
rsc.lastStartStopTs+computeStartStopInterval > nowTs {
return []string{}, 1, []string{}, [][]string{}
}
res := rsc.topQueueRes
// remove from startup list servers which are no longer exist
// substract from required resources servers which are starting now
n := 0
for _, name := range rsc.startupNames {
if cs, ok := rsc.computeState[name]; ok {
rsc.startupNames[n] = name // server stil exist
n++
res.Cpu -= cs.totalRes.Cpu // server is starting, reduce cpu and memory required to run the queue
res.Mem -= cs.totalRes.Mem
}
}
rsc.startupNames = rsc.startupNames[:n]
// substract from required resources servers which are ready and not used now
readyRes := RunRes{}
for _, cs := range rsc.computeState {
if cs.state == "ready" {
readyRes.Cpu += cs.totalRes.Cpu
readyRes.Mem += cs.totalRes.Mem
}
}
res.Cpu = res.Cpu - (readyRes.Cpu - rsc.ActiveTotalRes.Cpu)
res.Mem = res.Mem - (readyRes.Mem - rsc.ActiveTotalRes.Mem)
if res.Cpu <= 0 && res.Mem <= 0 {
return []string{}, 1, []string{}, [][]string{} // do not start any additional servers
}
// select servers to start
// where state is power off and with min error count and longest unused time
// until it is enough cpu and memory to run first MPI job in the queue
srvLst := []string{}
exeLst := []string{}
argLst := [][]string{}
for res.Cpu > 0 || res.Mem > 0 { // until not enough servers to run first MPI job in the queue
name := ""
minE := 2 * rsc.maxComputeErrors
minTs := nowTs + 100
for _, cs := range rsc.computeState {
if cs.state != "" {
continue // server not in power off state
}
isAct := false
for k := 0; !isAct && k < len(rsc.startupNames); k++ {
isAct = rsc.startupNames[k] == cs.name
}
for k := 0; !isAct && k < len(srvLst); k++ {
isAct = srvLst[k] == cs.name
}
for k := 0; !isAct && k < len(rsc.shutdownNames); k++ {
isAct = rsc.shutdownNames[k] == cs.name
}
if isAct {
continue // this server already in startup or shutdown list
}
if name == "" || cs.errorCount < minE || cs.errorCount == minE && cs.lastUsedTs < minTs {
name = cs.name
minE = cs.errorCount
minTs = cs.lastUsedTs
}
}
if name == "" {
break // no servers available
}
// add server to startup list
srvLst = append(srvLst, name)
exeLst = append(exeLst, rsc.computeState[name].startExe)
args := make([]string, len(rsc.computeState[name].startArgs))
copy(args, rsc.computeState[name].startArgs)
argLst = append(argLst, args)
res.Cpu -= rsc.computeState[name].totalRes.Cpu
res.Mem -= rsc.computeState[name].totalRes.Mem
}
// check results: if not enough servers to run first MPI job in the queue then return empty result
if res.Cpu > 0 || res.Mem > 0 {
return []string{}, 1, []string{}, [][]string{}
}
// append to servers starup list and return list of servers to start
rsc.startupNames = append(rsc.startupNames, srvLst...)
return srvLst, rsc.maxStartTime, exeLst, argLst
}
// select computational servers or clusters to shutdown,
// return server names, shutdown timeout and for each server shutdown exe names and shutdown arguments
func (rsc *RunCatalog) selectToStopCompute() ([]string, int64, []string, [][]string) {
rsc.rscLock.Lock()
defer rsc.rscLock.Unlock()
// do not stop anything: if idle time is unlimited or this instance is not a leader oms instance
nowTs := time.Now().UnixMilli()
if rsc.maxIdleTime <= 0 || !rsc.isLeader || rsc.lastStartStopTs+computeStartStopInterval > nowTs {
return []string{}, 1, []string{}, [][]string{}
}
// remove from shutdown list servers which are no longer exist
n := 0
for _, name := range rsc.shutdownNames {
if _, ok := rsc.computeState[name]; ok {
rsc.shutdownNames[n] = name // server stil exist
n++
}
}
rsc.shutdownNames = rsc.shutdownNames[:n]
// find servers where there no model runs for more than idle time interval
srvLst := []string{}
exeLst := []string{}
argLst := [][]string{}
for _, cs := range rsc.computeState {
isAct := false
for k := 0; !isAct && k < len(rsc.shutdownNames); k++ {
isAct = rsc.shutdownNames[k] == cs.name
}
for k := 0; !isAct && k < len(rsc.startupNames); k++ {
isAct = rsc.startupNames[k] == cs.name
}
if isAct {
continue // this server already in shutdown or startup list
}
// if no model runs for more than idle time in milliseconds and server started more than idle time in milliseconds
if cs.state == "ready" && cs.lastUsedTs+rsc.maxIdleTime < nowTs {
srvLst = append(srvLst, cs.name)
exeLst = append(exeLst, cs.stopExe)
args := make([]string, len(cs.stopArgs))
copy(args, cs.stopArgs)
argLst = append(argLst, args)
rsc.shutdownNames = append(rsc.shutdownNames, cs.name)
}
}
return srvLst, rsc.maxStopTime, exeLst, argLst
}
// on sucess reset server error count or increase it on error and remove server name from startup list
func (rsc *RunCatalog) startupCompleted(isOkStart bool, name string) {
rsc.rscLock.Lock()
defer rsc.rscLock.Unlock()
// update server state
rsc.lastStartStopTs = time.Now().UnixMilli()
if cs, ok := rsc.computeState[name]; ok {
if isOkStart {
cs.lastUsedTs = rsc.lastStartStopTs
}
rsc.computeState[name] = cs
}
// remove server from startup list
n := 0
for k := range rsc.startupNames {
if rsc.startupNames[k] != name {
rsc.startupNames[n] = rsc.startupNames[k]
n++
}
}
rsc.startupNames = rsc.startupNames[:n]
}
// on sucess reset server error count or increase it on error remove server name from shutdown list
func (rsc *RunCatalog) shutdownCompleted(isOkStop bool, name string) {
rsc.rscLock.Lock()
defer rsc.rscLock.Unlock()
// update server state
rsc.lastStartStopTs = time.Now().UnixMilli()
// remove server from shutdown list
n := 0
for k := range rsc.shutdownNames {
if rsc.shutdownNames[k] != name {
rsc.shutdownNames[n] = rsc.shutdownNames[k]
n++
}
}
rsc.shutdownNames = rsc.shutdownNames[:n]
}
// update run catalog with current job control files
func (rsc *RunCatalog) updateRunJobs(
jsState JobServiceState,
computeState map[string]computeItem,
queueJobs map[string]queueJobFile,
activeJobs map[string]runJobFile,
historyJobs map[string]historyJobFile,
) *jobControlState {
rsc.rscLock.Lock()
defer rsc.rscLock.Unlock()
jNextPos := rsc.jobLastPosition
rsc.JobServiceState = jsState
if rsc.jobLastPosition < jNextPos {
rsc.jobLastPosition = jNextPos
}
// copy state of computational resources
for name := range rsc.computeState {
_, ok := computeState[name]
if !ok {
delete(rsc.computeState, name) // remove: server or cluster not does exist anymore
}
}
for name, cs := range computeState {
if cs.lastUsedTs < rsc.computeState[name].lastUsedTs {
cs.lastUsedTs = rsc.computeState[name].lastUsedTs
}
rsc.computeState[name] = cs
}
// update queue jobs and collect all new submission stamps
for stamp := range rsc.queueJobs {
jf, ok := queueJobs[stamp]
if !ok || jf.isError {
delete(rsc.queueJobs, stamp) // remove: job file not exists
}
}
for stamp, jf := range queueJobs {
if _, ok := rsc.models[jf.ModelDigest]; !ok {
continue // skip: model digest is not the models list
}
if jf.isError {
continue // skip: model job error
}
rsc.queueJobs[stamp] = jf
}
// remove queue submission stamps which are no longer exists in the queue
n := 0
for _, stamp := range rsc.queueKeys {
if _, ok := queueJobs[stamp]; ok {
rsc.queueKeys[n] = stamp
n++
}
}
rsc.queueKeys = rsc.queueKeys[:n]
// find new submission stamps from the queue
n = len(queueJobs) - n
if n > 0 {
qKeys := make([]string, n)
k := 0
for stamp := range queueJobs {
isFound := false
for j := 0; !isFound && j < len(rsc.queueKeys); j++ {
isFound = rsc.queueKeys[j] == stamp
}
if !isFound {
qKeys[k] = stamp
k++
}
}
// sort new jobs by time stamps: first come first served and append at the end of existing queue
sort.Strings(qKeys)
rsc.queueKeys = append(rsc.queueKeys, qKeys...)
}
// update active model run jobs
for stamp := range rsc.activeJobs {
jf, ok := activeJobs[stamp]
if !ok || jf.isError {
delete(rsc.activeJobs, stamp) // remove: job file not exists
}
}
for stamp, jf := range activeJobs {
if _, ok := rsc.models[jf.ModelDigest]; !ok {
continue // skip: model digest is not the models list
}
if jf.isError {
continue // skip: model job error or
}
rsc.activeJobs[stamp] = jf
}
// update model run job history
for stamp := range rsc.historyJobs {
jh, ok := historyJobs[stamp]
if !ok || jh.isError {
delete(rsc.historyJobs, stamp) // remove: job file not exist
}
}
for stamp, jh := range historyJobs {
if !jh.isError {
rsc.historyJobs[stamp] = jh
}
}
// cleanup selected to run jobs list: remove if submission stamp not exist in queue files list
n = 0
for _, stamp := range rsc.selectedKeys {
if _, ok := queueJobs[stamp]; ok {
rsc.selectedKeys[n] = stamp // job file still exist in the queue
n++
}
}
rsc.selectedKeys = rsc.selectedKeys[:n]
// return job control state
jsc := jobControlState{
Queue: make([]string, len(rsc.queueKeys)),
}
copy(jsc.Queue, rsc.queueKeys)
return &jsc
}