-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestexample.go
316 lines (280 loc) · 7.9 KB
/
testexample.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
package main
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
qacct "github.com/hpc-gridware/go-clusterscheduler/pkg/qacct/v9.0"
qstat "github.com/hpc-gridware/go-clusterscheduler/pkg/qstat/v9.0"
"google.golang.org/protobuf/types/known/timestamppb"
"go.uber.org/zap"
)
var qacctClient qacct.QAcct
var qstatClient qstat.QStat
var newlyFinishedJobs <-chan qacct.JobDetail
var log *zap.Logger
func init() {
var err error
log, _ = zap.NewProduction()
qstatClient, err = qstat.NewCommandLineQstat(qstat.CommandLineQStatConfig{})
if err != nil {
log.Fatal("Failed to initialize qstat client", zap.String("error",
err.Error()))
}
qacctClient, err = qacct.NewCommandLineQAcct(qacct.CommandLineQAcctConfig{})
if err != nil {
log.Fatal("Failed to initialize qacct client", zap.String("error",
err.Error()))
}
// watch for newly finished jobs
newlyFinishedJobs, err = qacct.WatchFile(context.Background(),
qacct.GetDefaultQacctFile(), 1024)
if err != nil {
log.Fatal("Failed to initialize job watcher",
zap.String("error", err.Error()))
}
}
func main() {
run(context.Background())
}
func run(ctx context.Context) {
defer log.Sync()
alreadySent := map[string]struct{}{}
for {
select {
case <-ctx.Done():
log.Info("Context cancelled, stopping ClusterScheduler")
return
default:
finishedJobs, err := GetFinishedJobsWithWatcher()
if err != nil {
log.Error("Error getting finished jobs", zap.String("error",
err.Error()))
}
runningJobs, err := GetRunningJobs()
if err != nil {
log.Error("Error getting running jobs", zap.String("error",
err.Error()))
}
allJobs := append(finishedJobs, runningJobs...)
var protoJobs []*SimpleJob
for _, job := range allJobs {
if _, ok := alreadySent[job.JobId]; ok {
continue
}
protoJobs = append(protoJobs, job)
}
_, err = SendJobs(ctx, protoJobs)
if err != nil {
log.Warn("Error ingesting jobs", zap.String("error",
err.Error()))
}
for _, job := range finishedJobs {
alreadySent[job.JobId] = struct{}{}
}
select {
case <-ctx.Done():
log.Info("Context cancelled, stopping")
return
case <-time.After(10 * time.Second):
}
}
}
}
type SimpleJob struct {
JobId string `json:"job_id"`
// Cluster represents the queue name
Cluster string `json:"cluster"`
JobName string `json:"job_name"`
// Partition represents the parallel environment
Partition string `json:"partition"`
Account string `json:"account"`
User string `json:"user"`
State string `json:"state"`
ExitCode string `json:"exit_code"`
Submit *timestamppb.Timestamp `json:"submit"`
Start *timestamppb.Timestamp `json:"start"`
End *timestamppb.Timestamp `json:"end"`
MasterNode string `json:"master_node"`
}
func GetFinishedJobsWithWatcher() ([]*SimpleJob, error) {
jobs := []*SimpleJob{}
for {
// get next job or timeout after 0.1s of there is no new job
select {
case fjob := <-newlyFinishedJobs:
state := fmt.Sprintf("%d", fjob.ExitStatus)
if state == "0" {
state = "done"
} else {
state = "failed"
}
simpleJob := SimpleJob{
// ignore job arrays for now
JobId: fmt.Sprintf("%d", fjob.JobNumber),
Cluster: fjob.QName,
JobName: fjob.JobName,
Partition: fjob.GrantedPE,
Account: fjob.Account,
User: fjob.Owner,
State: state,
ExitCode: fmt.Sprintf("%d", fjob.ExitStatus),
Submit: parseTimestampInt64(fjob.SubmitTime),
Start: parseTimestampInt64(fjob.StartTime),
End: parseTimestampInt64(fjob.EndTime),
MasterNode: fjob.HostName,
}
jobs = append(jobs, &simpleJob)
case <-time.After(100 * time.Millisecond):
return jobs, nil
}
}
return jobs, nil
}
func GetFinishedJobs() ([]*SimpleJob, error) {
// Use qacct NativeSpecification to get finished jobs
qacctOutput, err := qacctClient.NativeSpecification([]string{"-j", "*"})
if err != nil {
// no job are command failed
return nil, fmt.Errorf("error running qacct command: %v", err)
}
jobs, err := qacct.ParseQAcctJobOutput(qacctOutput)
if err != nil {
return nil, fmt.Errorf("error parsing qacct output: %v", err)
}
// convert to SimpleJob format
simpleJobs := make([]*SimpleJob, len(jobs))
for i, job := range jobs {
state := fmt.Sprintf("%d", job.ExitStatus)
if state == "0" {
state = "done"
} else {
state = "failed"
}
simpleJobs[i] = &SimpleJob{
// ignore job arrays for now
JobId: fmt.Sprintf("%d", job.JobNumber),
Cluster: job.QName,
JobName: job.JobName,
Partition: job.GrantedPE,
Account: job.Account,
User: job.Owner,
State: state,
ExitCode: fmt.Sprintf("%d", job.ExitStatus),
Submit: parseTimestampInt64(job.SubmitTime),
Start: parseTimestampInt64(job.StartTime),
End: parseTimestampInt64(job.EndTime),
MasterNode: job.HostName,
}
}
return simpleJobs, nil
}
func GetRunningJobs() ([]*SimpleJob, error) {
qstatOverview, err := qstatClient.NativeSpecification([]string{"-g", "t"})
if err != nil {
// no jobs running
return nil, nil
}
jobsByTask, err := qstat.ParseGroupByTask(qstatOverview)
if err != nil {
return nil, fmt.Errorf("error parsing qstat output: %v", err)
}
type State struct {
QueueName string
State string
MasterNode string
}
stateMap := map[int]State{}
for _, job := range jobsByTask {
// we are only interested in serial and parallel jobs (no arrays)
jq := strings.Split(job.Queue, "@")
if len(jq) == 2 {
js, exists := stateMap[job.JobID]
if !exists {
js = State{
QueueName: jq[0],
State: job.State,
MasterNode: jq[1],
}
}
if job.Master == "MASTER" {
// this is the master task of a parallel job
js.MasterNode = jq[1]
stateMap[job.JobID] = js
}
continue
}
stateMap[job.JobID] = State{
QueueName: job.Queue,
State: job.State,
}
}
// get running jobs
qstatOutput, err := qstatClient.NativeSpecification([]string{"-j", "*"})
if err != nil {
// no jobs running; qstat -j * found 0 jobs (TODO)
return nil, nil
}
jobs, err := qstat.ParseSchedulerJobInfo(qstatOutput)
if err != nil {
return nil, fmt.Errorf("error parsing qstat output: %v", err)
}
// convert to SimpleJob format
simpleJobs := make([]*SimpleJob, len(jobs))
for i, job := range jobs {
state := stateMap[job.JobNumber].State
if state == "" {
state = "running"
}
simpleJobs[i] = &SimpleJob{
JobId: fmt.Sprintf("%d", job.JobNumber),
Cluster: stateMap[job.JobNumber].QueueName,
JobName: job.JobName,
Partition: strings.Split(job.ParallelEnvironment, " ")[0], // PE
Account: job.Account,
User: job.Owner,
State: state,
ExitCode: "",
MasterNode: stateMap[job.JobNumber].MasterNode,
}
if strings.Contains(stateMap[job.JobNumber].State, "q") {
simpleJobs[i].Submit = parseTimestamp(job.SubmissionTime)
} else {
simpleJobs[i].Start = parseTimestamp(job.SubmissionTime)
}
}
return simpleJobs, nil
}
func SendJobs(ctx context.Context, jobs []*SimpleJob) (int, error) {
log.Info("Sending jobs", zap.Int("jobs", len(jobs)))
// Print the jobs
for _, job := range jobs {
// pretty print JSON
json, err := json.MarshalIndent(job, "", " ")
if err != nil {
return 0, fmt.Errorf("error marshalling job: %v", err)
}
fmt.Println(string(json))
}
return len(jobs), nil
}
func parseTimestampInt64(ts int64) *timestamppb.Timestamp {
// ts is 6 digits behind the second (microseconds)
sec := ts / 1e6
nsec := (ts - sec*1e6) * 1e3
t := time.Unix(sec, nsec)
return timestamppb.New(t)
}
// 2024-10-24 09:49:59.911136
func parseTimestamp(s string) *timestamppb.Timestamp {
loc, err := time.LoadLocation("Local")
if err != nil {
return nil
}
t, err := time.ParseInLocation("2006-01-02 15:04:05.999999", s, loc)
if err != nil {
return nil
}
return timestamppb.New(t)
}