-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsub.go
323 lines (282 loc) · 7.72 KB
/
sub.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
package main
import (
"fmt"
"os"
schema "github.com/glycerine/goq/schema"
)
// Submitter represents all other queries beside those from workers.
// Their principle purpose is to supply jobs to-be-done using JOBMSG_INITIALSUBMIT.
//
// However, submitters can do many other miscelanous other things.
// They can query the server for a snapshot of status using
// JOBMSG_TAKESNAPSHOT, for instance. The 'goq stat' command issues that query.
type Submitter struct {
Name string
Addr string
Cli *ClientRpc
ServerName string
ServerAddr string
ToServerSubmit chan *Job
// set Cfg *once*, before any goroutines start, then
// treat it as immutable and never changing.
Cfg Config
LastSentMsg []byte
HomeOnSubmitter string
}
func NewSubmitter(cfg *Config, infWait bool) (*Submitter, error) {
cli, err := NewClientRpc("sub", cfg, infWait)
if err != nil {
panic(err)
}
localAddr := cli.LocalAddr()
vv("localAddr = '%v'", localAddr)
if localAddr == "" {
panic("must have a localAddr")
}
sub := &Submitter{
Name: fmt.Sprintf("submitter.pid.%d", os.Getpid()),
Addr: localAddr,
Cli: cli,
Cfg: *CopyConfig(cfg),
HomeOnSubmitter: os.Getenv("HOME"),
}
return sub, nil
}
func (sub *Submitter) Bye() {
if sub.Cli != nil {
sub.Cli.Close()
sub.Cli = nil // allow 2x Bye() during shutdown.
}
}
func (sub *Submitter) SubmitJob(j *Job) {
j.Msg = schema.JOBMSG_INITIALSUBMIT
j.Submitaddr = sub.Addr
if sub.Addr != "" {
errsend := sub.Cli.AsyncSend(j)
if errsend != nil {
panic(fmt.Errorf("err during submit job: %s\n", errsend))
}
//sub.LastSentMsg = cy
} else {
sub.ToServerSubmit <- j
}
}
func (sub *Submitter) SubmitJobGetReply(j *Job) (*Job, []byte, error) {
j.Msg = schema.JOBMSG_INITIALSUBMIT
j.Submitaddr = sub.Addr
j.HomeOnSubmitter = sub.HomeOnSubmitter
// don't pass the env along anymore, let that be set locally.
// used to be: grab the local env, without any GOQ stuff.
// j.Env = GetNonGOQEnv(os.Environ(), sub.Cfg.ClusterId)
if sub.Addr != "" {
return sub.Cli.DoSyncCall(j)
} else {
sub.ToServerSubmit <- j
}
return nil, nil, nil
}
// returns only after the request for job has been registered (assuming error is nil)
// i.e. if error is non-nil, request might not have gone through.
// If there was an error, the chan will supply a job with Id == -1 and
// an error message in Out[0]
func (sub *Submitter) WaitForJob(jobidToWaitFor int64) (chan *Job, error) {
res := make(chan *Job)
j := NewJob()
j.Msg = schema.JOBMSG_OBSERVEJOBFINISH
j.Submitaddr = sub.Addr
j.Aboutjid = jobidToWaitFor
if sub.Addr != "" {
_, _, err := sub.Cli.DoSyncCall(j)
panicOn(err)
go func() {
for {
in := <-sub.Cli.ReadIncomingCh
j, err := sub.Cfg.bytesToJob(in.JobSerz)
panicOn(err)
vv("WaitForJob() background goro got j = '%#v'", j)
if j.Msg == schema.JOBMSG_JOBFINISHEDNOTICE {
vv("we see JOBMSG_JOBFINISHEDNOTICE")
res <- j
break
}
}
}()
return res, err
} else {
sub.ToServerSubmit <- j
}
return res, nil
}
func (sub *Submitter) SubmitShutdownJob() error {
j := NewJob()
j.Msg = schema.JOBMSG_SHUTDOWNSERV
j.Submitaddr = sub.Addr
j.Serveraddr = sub.ServerAddr
// try to speed up the timeout, when its already down.
//sub.SetServerPushTimeoutMsec(100)
if sub.Addr != "" {
//_, _, err := sub.Cli.DoSyncCall(j) // shutdown stuck here, like never gets to server.
// maybe the shutdown response is just so fast
// with quic that we'll never see a reply from the dying server.
// prefer async send. For TestSubmitShutdownToRemoteJobServ; in
// point, we are atg with this so it seems okay; under quic.
err := sub.Cli.AsyncSend(j) // shutdown stuck here, like never gets to server.
return err
} else {
sub.ToServerSubmit <- j
}
return nil
}
func (sub *Submitter) SubmitSnapJob(maxShow int) ([]string, error) {
j := NewJob()
j.Msg = schema.JOBMSG_TAKESNAPSHOT
j.Submitaddr = sub.Addr
j.Serveraddr = sub.ServerAddr
j.MaxShow = int64(maxShow)
//if AesOff {
// j.Out = append(j.Out, "clusterid:"+sub.Cfg.ClusterId)
//}
if sub.Addr != "" {
//sub.Cli.SetRecvTimeout(60000 * time.Millisecond) // wait 60 seconds
jstat, _, err := sub.Cli.DoSyncCall(j)
// return to normal? sub.Cli.SetRecvTimeout(time.Duration(cfg.RecvTimeoutMsec) * time.Millisecond)
if err == nil {
return jstat.Out, nil
}
fmt.Printf("\n err in SubmitSnapJob on receiving reply: '%s'\n", err)
return []string{}, err
} else {
fmt.Printf("local server stat not implemented.\n")
//sub.ToServerSubmit <- j
}
return []string{}, nil
}
/*
func (sub *Submitter) setServerPrivate(pushaddr string) error {
var err error
var pushsock *ClientRpc
if pushaddr != "" {
pushsock, err = NewClientRpc(pushaddr, &sub.Cfg, false)
if err != nil {
panic(err)
//return err
}
sub.ServerAddr = pushaddr
if sub.Cli != nil {
sub.Cli.Close()
}
sub.Cli = pushsock
sub.ServerName = "JSERV"
}
return nil
}
func (sub *Submitter) SetServerPushTimeoutMsec(msec int) error {
// crashing, so disable for now.
//return sub.Cli.SetSendTimeout(time.Duration(msec) * time.Millisecond)
return nil
}
*/
func NewLocalSubmitter(js *JobServ) (*Submitter, error) {
sub := &Submitter{
ToServerSubmit: js.Submit,
}
return sub, nil
}
func SendKill(cfg *Config, jid int64) {
sub, err := NewSubmitter(cfg, false)
if err != nil {
panic(err)
}
sub.SubmitKillJob(jid)
}
func (sub *Submitter) SubmitKillJob(jid int64) {
j := NewJob()
j.Msg = schema.JOBMSG_CANCELSUBMIT
j.Submitaddr = sub.Addr
j.Serveraddr = sub.ServerAddr
j.Aboutjid = jid
//sub.SetServerPushTimeoutMsec(100)
if sub.Addr != "" {
jconfirm, _, err := sub.Cli.DoSyncCall(j)
if err == nil {
if jconfirm.Msg == schema.JOBMSG_ACKCANCELSUBMIT {
VPrintf("[pid %d] cancellation of job %d at '%s' succeeded.\n", os.Getpid(), jid, sub.ServerAddr)
}
}
} else {
sub.ToServerSubmit <- j
}
}
func (sub *Submitter) SubmitImmoJob() error {
j := NewJob()
j.Msg = schema.JOBMSG_IMMOLATEAWORKERS
j.Submitaddr = sub.Addr
j.Serveraddr = sub.ServerAddr
//if AesOff {
// j.Out = append(j.Out, "clusterid:"+sub.Cfg.ClusterId)
//}
if sub.Addr != "" {
jimmoack, _, err := sub.Cli.DoSyncCall(j)
if err != nil {
return err
}
if jimmoack.Msg != schema.JOBMSG_IMMOLATEACK {
panic(fmt.Sprintf("expected JOBMSG_IMMOLATEACK but got: %s", jimmoack))
}
return nil
} else {
fmt.Printf("local server 'immolate workers' not implemented.\n")
}
return nil
}
func (sub *Submitter) SubmitResetJob() error {
j := NewJob()
j.Msg = schema.JOBMSG_RESETSERVER
j.Submitaddr = sub.Addr
j.Serveraddr = sub.ServerAddr
//if AesOff {
// j.Out = append(j.Out, "clusterid:"+sub.Cfg.ClusterId)
//}
if sub.Addr != "" {
jimmoack, _, err := sub.Cli.DoSyncCall(j)
if err != nil {
return err
}
if jimmoack.Msg != schema.JOBMSG_RESETSERVER_ACK {
panic(fmt.Sprintf("expected JOBMSG_RESETSERVER_ACK but got: %s", jimmoack))
}
return nil
} else {
fmt.Printf("local server 'immolate workers' not implemented.\n")
}
return nil
}
func (sub *Submitter) SubmitCancelJob(jid int64) error {
j := NewJob()
j.Msg = schema.JOBMSG_CANCELSUBMIT
j.Aboutjid = jid
j.Submitaddr = sub.Addr
j.Serveraddr = sub.ServerAddr
//if AesOff {
// j.Out = append(j.Out, "clusterid:"+sub.Cfg.ClusterId)
//}
if sub.Addr != "" {
jimmoack, _, err := sub.Cli.DoSyncCall(j)
_ = jimmoack
_ = err
/* might timeout
jimmoack, err := recvZjob(sub.Cli, &sub.Cfg)
if err != nil {
fmt.Printf("error during receiving confirmation of cancel job: '%s'\n", err)
return err
}
if jimmoack.Msg != schema.JOBMSG_ACKCANCELSUBMIT {
panic(fmt.Sprintf("expected JOBMSG_ACKCANCELSUBMIT but got: %s", jimmoack))
}
*/
return nil
} else {
fmt.Printf("local server 'cancelsubmit' not implemented.\n")
}
return nil
}