1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/lib/cloud"
16 "git.arvados.org/arvados.git/sdk/go/arvados"
17 "git.arvados.org/arvados.git/sdk/go/stats"
18 "github.com/sirupsen/logrus"
23 maxPingFailTime = 10 * time.Minute
26 // State indicates whether a worker is available to do work, and (if
27 // not) whether/when it is expected to become ready.
31 StateUnknown State = iota // might be running a container already
32 StateBooting // instance is booting
33 StateIdle // instance booted, no containers are running
34 StateRunning // instance is running one or more containers
35 StateShutdown // worker has stopped monitoring the instance
38 var stateString = map[State]string{
39 StateUnknown: "unknown",
40 StateBooting: "booting",
42 StateRunning: "running",
43 StateShutdown: "shutdown",
46 // String implements fmt.Stringer.
47 func (s State) String() string {
51 // MarshalText implements encoding.TextMarshaler so a JSON encoding of
52 // map[State]anything uses the state's string representation.
53 func (s State) MarshalText() ([]byte, error) {
54 return []byte(stateString[s]), nil
57 // IdleBehavior indicates the behavior desired when a node becomes idle.
58 type IdleBehavior string
61 IdleBehaviorRun IdleBehavior = "run" // run containers, or shutdown on idle timeout
62 IdleBehaviorHold IdleBehavior = "hold" // don't shutdown or run more containers
63 IdleBehaviorDrain IdleBehavior = "drain" // shutdown immediately when idle
66 var validIdleBehavior = map[IdleBehavior]bool{
67 IdleBehaviorRun: true,
68 IdleBehaviorHold: true,
69 IdleBehaviorDrain: true,
73 logger logrus.FieldLogger
77 mtx sync.Locker // must be wp's Locker.
79 idleBehavior IdleBehavior
80 instance cloud.Instance
81 instType arvados.InstanceType
90 running map[string]*remoteRunner // remember to update state idle<->running when this changes
91 starting map[string]*remoteRunner // remember to update state idle<->running when this changes
95 func (wkr *worker) onUnkillable(uuid string) {
97 defer wkr.mtx.Unlock()
98 logger := wkr.logger.WithField("ContainerUUID", uuid)
99 if wkr.idleBehavior == IdleBehaviorHold {
100 logger.Warn("unkillable container, but worker has IdleBehavior=Hold")
103 logger.Warn("unkillable container, draining worker")
104 wkr.setIdleBehavior(IdleBehaviorDrain)
107 func (wkr *worker) onKilled(uuid string) {
109 defer wkr.mtx.Unlock()
110 wkr.closeRunner(uuid)
114 // caller must have lock.
115 func (wkr *worker) setIdleBehavior(idleBehavior IdleBehavior) {
116 wkr.logger.WithField("IdleBehavior", idleBehavior).Info("set idle behavior")
117 wkr.idleBehavior = idleBehavior
122 // caller must have lock.
123 func (wkr *worker) startContainer(ctr arvados.Container) {
124 logger := wkr.logger.WithFields(logrus.Fields{
125 "ContainerUUID": ctr.UUID,
126 "Priority": ctr.Priority,
128 logger.Debug("starting container")
129 rr := newRemoteRunner(ctr.UUID, wkr)
130 wkr.starting[ctr.UUID] = rr
131 if wkr.state != StateRunning {
132 wkr.state = StateRunning
138 defer wkr.mtx.Unlock()
142 delete(wkr.starting, ctr.UUID)
143 wkr.running[ctr.UUID] = rr
144 wkr.lastUUID = ctr.UUID
148 // ProbeAndUpdate conducts appropriate boot/running probes (if any)
149 // for the worker's curent state. If a previous probe is still
150 // running, it does nothing.
152 // It should be called in a new goroutine.
153 func (wkr *worker) ProbeAndUpdate() {
155 case wkr.probing <- struct{}{}:
159 wkr.logger.Debug("still waiting for last probe to finish")
163 // probeAndUpdate calls probeBooted and/or probeRunning if needed, and
164 // updates state accordingly.
166 // In StateUnknown: Call both probeBooted and probeRunning.
167 // In StateBooting: Call probeBooted; if successful, call probeRunning.
168 // In StateRunning: Call probeRunning.
169 // In StateIdle: Call probeRunning.
170 // In StateShutdown: Do nothing.
172 // If both probes succeed, wkr.state changes to
173 // StateIdle/StateRunning.
175 // If probeRunning succeeds, wkr.running is updated. (This means
176 // wkr.running might be non-empty even in StateUnknown, if the boot
179 // probeAndUpdate should be called in a new goroutine.
180 func (wkr *worker) probeAndUpdate() {
182 updated := wkr.updated
183 initialState := wkr.state
190 stderr []byte // from probeBooted
193 switch initialState {
196 case StateIdle, StateRunning:
198 case StateUnknown, StateBooting:
200 panic(fmt.Sprintf("unknown state %s", initialState))
203 probeStart := time.Now()
204 logger := wkr.logger.WithField("ProbeStart", probeStart)
207 booted, stderr = wkr.probeBooted()
209 // Pretend this probe succeeded if another
210 // concurrent attempt succeeded.
212 booted = wkr.state == StateRunning || wkr.state == StateIdle
216 logger.Info("instance booted; will try probeRunning")
219 reportedBroken := false
220 if booted || wkr.state == StateUnknown {
221 ctrUUIDs, reportedBroken, ok = wkr.probeRunning()
224 defer wkr.mtx.Unlock()
225 if reportedBroken && wkr.idleBehavior == IdleBehaviorRun {
226 logger.Info("probe reported broken instance")
227 wkr.setIdleBehavior(IdleBehaviorDrain)
229 if !ok || (!booted && len(ctrUUIDs) == 0 && len(wkr.running) == 0) {
230 if wkr.state == StateShutdown && wkr.updated.After(updated) {
231 // Skip the logging noise if shutdown was
232 // initiated during probe.
235 // Using the start time of the probe as the timeout
236 // threshold ensures we always initiate at least one
237 // probe attempt after the boot/probe timeout expires
238 // (otherwise, a slow probe failure could cause us to
239 // shutdown an instance even though it did in fact
240 // boot/recover before the timeout expired).
241 dur := probeStart.Sub(wkr.probed)
242 if wkr.shutdownIfBroken(dur) {
243 // stderr from failed run-probes will have
244 // been logged already, but boot-probe
245 // failures are normal so they are logged only
246 // at Debug level. This is our chance to log
247 // some evidence about why the node never
248 // booted, even in non-debug mode.
250 logger.WithFields(logrus.Fields{
252 "stderr": string(stderr),
253 }).Info("boot failed")
259 updateTime := time.Now()
260 wkr.probed = updateTime
262 if updated != wkr.updated {
263 // Worker was updated after the probe began, so
264 // wkr.running might have a container UUID that was
265 // not yet running when ctrUUIDs was generated. Leave
266 // wkr.running alone and wait for the next probe to
267 // catch up on any changes.
271 if len(ctrUUIDs) > 0 {
272 wkr.busy = updateTime
273 wkr.lastUUID = ctrUUIDs[0]
274 } else if len(wkr.running) > 0 {
275 // Actual last-busy time was sometime between wkr.busy
276 // and now. Now is the earliest opportunity to take
277 // advantage of the non-busy state, though.
278 wkr.busy = updateTime
281 changed := wkr.updateRunning(ctrUUIDs)
283 // Update state if this was the first successful boot-probe.
284 if booted && (wkr.state == StateUnknown || wkr.state == StateBooting) {
285 // Note: this will change again below if
286 // len(wkr.starting)+len(wkr.running) > 0.
287 wkr.state = StateIdle
291 // If wkr.state and wkr.running aren't changing then there's
292 // no need to log anything, notify the scheduler, move state
293 // back and forth between idle/running, etc.
298 // Log whenever a run-probe reveals crunch-run processes
299 // appearing/disappearing before boot-probe succeeds.
300 if wkr.state == StateUnknown && changed {
301 logger.WithFields(logrus.Fields{
302 "RunningContainers": len(wkr.running),
304 }).Info("crunch-run probe succeeded, but boot probe is still failing")
307 if wkr.state == StateIdle && len(wkr.starting)+len(wkr.running) > 0 {
308 wkr.state = StateRunning
309 } else if wkr.state == StateRunning && len(wkr.starting)+len(wkr.running) == 0 {
310 wkr.state = StateIdle
312 wkr.updated = updateTime
313 if booted && (initialState == StateUnknown || initialState == StateBooting) {
314 logger.WithFields(logrus.Fields{
315 "RunningContainers": len(wkr.running),
317 }).Info("probes succeeded, instance is in service")
322 func (wkr *worker) probeRunning() (running []string, reportsBroken, ok bool) {
323 cmd := wkr.wp.runnerCmd + " --list"
324 if u := wkr.instance.RemoteUser(); u != "root" {
327 stdout, stderr, err := wkr.executor.Execute(nil, cmd, nil)
329 wkr.logger.WithFields(logrus.Fields{
331 "stdout": string(stdout),
332 "stderr": string(stderr),
333 }).WithError(err).Warn("probe failed")
337 for _, s := range strings.Split(string(stdout), "\n") {
341 running = append(running, s)
347 func (wkr *worker) probeBooted() (ok bool, stderr []byte) {
348 cmd := wkr.wp.bootProbeCommand
352 stdout, stderr, err := wkr.executor.Execute(nil, cmd, nil)
353 logger := wkr.logger.WithFields(logrus.Fields{
355 "stdout": string(stdout),
356 "stderr": string(stderr),
359 logger.WithError(err).Debug("boot probe failed")
362 logger.Info("boot probe succeeded")
363 if err = wkr.wp.loadRunnerData(); err != nil {
364 wkr.logger.WithError(err).Warn("cannot boot worker: error loading runner binary")
366 } else if len(wkr.wp.runnerData) == 0 {
367 // Assume crunch-run is already installed
368 } else if _, stderr2, err := wkr.copyRunnerData(); err != nil {
369 wkr.logger.WithError(err).WithField("stderr", string(stderr2)).Warn("error copying runner binary")
370 return false, stderr2
372 stderr = append(stderr, stderr2...)
377 func (wkr *worker) copyRunnerData() (stdout, stderr []byte, err error) {
378 hash := fmt.Sprintf("%x", wkr.wp.runnerMD5)
379 dstdir, _ := filepath.Split(wkr.wp.runnerCmd)
380 logger := wkr.logger.WithFields(logrus.Fields{
382 "path": wkr.wp.runnerCmd,
385 stdout, stderr, err = wkr.executor.Execute(nil, `md5sum `+wkr.wp.runnerCmd, nil)
386 if err == nil && len(stderr) == 0 && bytes.Equal(stdout, []byte(hash+" "+wkr.wp.runnerCmd+"\n")) {
387 logger.Info("runner binary already exists on worker, with correct hash")
391 // Note touch+chmod come before writing data, to avoid the
392 // possibility of md5 being correct while file mode is
394 cmd := `set -e; dstdir="` + dstdir + `"; dstfile="` + wkr.wp.runnerCmd + `"; mkdir -p "$dstdir"; touch "$dstfile"; chmod 0755 "$dstdir" "$dstfile"; cat >"$dstfile"`
395 if wkr.instance.RemoteUser() != "root" {
396 cmd = `sudo sh -c '` + strings.Replace(cmd, "'", "'\\''", -1) + `'`
398 logger.WithField("cmd", cmd).Info("installing runner binary on worker")
399 stdout, stderr, err = wkr.executor.Execute(nil, cmd, bytes.NewReader(wkr.wp.runnerData))
403 // caller must have lock.
404 func (wkr *worker) shutdownIfBroken(dur time.Duration) bool {
405 if wkr.idleBehavior == IdleBehaviorHold {
409 label, threshold := "", wkr.wp.timeoutProbe
410 if wkr.state == StateUnknown || wkr.state == StateBooting {
411 label, threshold = "new ", wkr.wp.timeoutBooting
416 wkr.logger.WithFields(logrus.Fields{
420 }).Warnf("%sinstance unresponsive, shutting down", label)
425 // Returns true if the instance is eligible for shutdown: either it's
426 // been idle too long, or idleBehavior=Drain and nothing is running.
428 // caller must have lock.
429 func (wkr *worker) eligibleForShutdown() bool {
430 if wkr.idleBehavior == IdleBehaviorHold {
433 draining := wkr.idleBehavior == IdleBehaviorDrain
438 return draining || time.Since(wkr.busy) >= wkr.wp.timeoutIdle
443 for _, rr := range wkr.running {
448 for _, rr := range wkr.starting {
453 // draining, and all remaining runners are just trying
454 // to force-kill their crunch-run procs
461 // caller must have lock.
462 func (wkr *worker) shutdownIfIdle() bool {
463 if !wkr.eligibleForShutdown() {
466 wkr.logger.WithFields(logrus.Fields{
468 "IdleDuration": stats.Duration(time.Since(wkr.busy)),
469 "IdleBehavior": wkr.idleBehavior,
470 }).Info("shutdown worker")
475 // caller must have lock.
476 func (wkr *worker) shutdown() {
480 wkr.state = StateShutdown
483 err := wkr.instance.Destroy()
485 wkr.logger.WithError(err).Warn("shutdown failed")
491 // Save worker tags to cloud provider metadata, if they don't already
492 // match. Caller must have lock.
493 func (wkr *worker) saveTags() {
494 instance := wkr.instance
495 tags := instance.Tags()
496 update := cloud.InstanceTags{
497 wkr.wp.tagKeyPrefix + tagKeyInstanceType: wkr.instType.Name,
498 wkr.wp.tagKeyPrefix + tagKeyIdleBehavior: string(wkr.idleBehavior),
501 for k, v := range update {
509 err := instance.SetTags(tags)
511 wkr.wp.logger.WithField("Instance", instance.ID()).WithError(err).Warnf("error updating tags")
517 func (wkr *worker) Close() {
518 // This might take time, so do it after unlocking mtx.
519 defer wkr.executor.Close()
522 defer wkr.mtx.Unlock()
523 for uuid, rr := range wkr.running {
524 wkr.logger.WithField("ContainerUUID", uuid).Info("crunch-run process abandoned")
527 for uuid, rr := range wkr.starting {
528 wkr.logger.WithField("ContainerUUID", uuid).Info("crunch-run process abandoned")
533 // Add/remove entries in wkr.running to match ctrUUIDs returned by a
534 // probe. Returns true if anything was added or removed.
536 // Caller must have lock.
537 func (wkr *worker) updateRunning(ctrUUIDs []string) (changed bool) {
538 alive := map[string]bool{}
539 for _, uuid := range ctrUUIDs {
541 if _, ok := wkr.running[uuid]; ok {
543 } else if rr, ok := wkr.starting[uuid]; ok {
544 wkr.running[uuid] = rr
545 delete(wkr.starting, uuid)
548 // We didn't start it -- it must have been
549 // started by a previous dispatcher process.
550 wkr.logger.WithField("ContainerUUID", uuid).Info("crunch-run process detected")
551 wkr.running[uuid] = newRemoteRunner(uuid, wkr)
555 for uuid := range wkr.running {
557 wkr.closeRunner(uuid)
564 // caller must have lock.
565 func (wkr *worker) closeRunner(uuid string) {
566 rr := wkr.running[uuid]
570 wkr.logger.WithField("ContainerUUID", uuid).Info("crunch-run process ended")
571 delete(wkr.running, uuid)
576 wkr.wp.exited[uuid] = now
577 if wkr.state == StateRunning && len(wkr.running)+len(wkr.starting) == 0 {
578 wkr.state = StateIdle