2018-09-15 00:08:26 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2018-10-04 23:22:01 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
2018-09-15 00:08:26 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2018-12-11 20:27:50 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-12-21 19:23:21 +00:00
|
|
|
bstructs "github.com/hashicorp/nomad/plugins/base/structs"
|
2018-09-15 00:08:26 +00:00
|
|
|
)
|
|
|
|
|
2018-09-25 17:56:12 +00:00
|
|
|
// StatsUpdater is the interface required by the StatsHook to update stats.
|
|
|
|
// Satisfied by TaskRunner.
|
2018-09-15 00:08:26 +00:00
|
|
|
type StatsUpdater interface {
|
|
|
|
UpdateStats(*cstructs.TaskResourceUsage)
|
|
|
|
}
|
|
|
|
|
2018-09-25 17:56:12 +00:00
|
|
|
// statsHook manages the task stats collection goroutine.
|
2018-09-15 00:08:26 +00:00
|
|
|
type statsHook struct {
|
|
|
|
updater StatsUpdater
|
|
|
|
interval time.Duration
|
|
|
|
|
2018-12-11 20:27:50 +00:00
|
|
|
// cancel is called by Exited or Canceled
|
|
|
|
cancel context.CancelFunc
|
2018-09-15 00:08:26 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
|
|
|
logger hclog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStatsHook(su StatsUpdater, interval time.Duration, logger hclog.Logger) *statsHook {
|
|
|
|
h := &statsHook{
|
|
|
|
updater: su,
|
|
|
|
interval: interval,
|
|
|
|
}
|
|
|
|
h.logger = logger.Named(h.Name())
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*statsHook) Name() string {
|
|
|
|
return "stats_hook"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *statsHook) Poststart(ctx context.Context, req *interfaces.TaskPoststartRequest, _ *interfaces.TaskPoststartResponse) error {
|
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
|
|
|
// This shouldn't happen, but better safe than risk leaking a goroutine
|
2018-12-11 20:27:50 +00:00
|
|
|
if h.cancel != nil {
|
2018-09-15 00:08:26 +00:00
|
|
|
h.logger.Debug("poststart called twice without exiting between")
|
2018-12-11 20:27:50 +00:00
|
|
|
h.cancel()
|
2018-09-15 00:08:26 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 20:27:50 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
h.cancel = cancel
|
|
|
|
go h.collectResourceUsageStats(ctx, req.DriverStats)
|
2018-09-15 00:08:26 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *statsHook) Exited(context.Context, *interfaces.TaskExitedRequest, *interfaces.TaskExitedResponse) error {
|
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
2018-12-11 20:27:50 +00:00
|
|
|
if h.cancel == nil {
|
2018-09-15 00:08:26 +00:00
|
|
|
// No stats running
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-11 20:27:50 +00:00
|
|
|
// Call cancel to stop stats collection
|
|
|
|
h.cancel()
|
2018-09-15 00:08:26 +00:00
|
|
|
|
2018-12-11 20:27:50 +00:00
|
|
|
// Clear cancel func so we don't double call for any reason
|
|
|
|
h.cancel = nil
|
2018-09-15 00:08:26 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// collectResourceUsageStats starts collecting resource usage stats of a Task.
|
|
|
|
// Collection ends when the passed channel is closed
|
2018-12-11 20:27:50 +00:00
|
|
|
func (h *statsHook) collectResourceUsageStats(ctx context.Context, handle interfaces.DriverStats) {
|
|
|
|
|
|
|
|
ch, err := handle.Stats(ctx, h.interval)
|
|
|
|
if err != nil {
|
|
|
|
// Check if the driver doesn't implement stats
|
|
|
|
if err.Error() == cstructs.DriverStatsNotImplemented.Error() {
|
|
|
|
h.logger.Debug("driver does not support stats")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.logger.Error("failed to start stats collection for task", "error", err)
|
|
|
|
}
|
|
|
|
|
2019-01-12 17:16:12 +00:00
|
|
|
var backoff time.Duration
|
|
|
|
var retry int
|
|
|
|
limit := time.Second * 5
|
2018-09-15 00:08:26 +00:00
|
|
|
for {
|
2019-01-12 17:16:12 +00:00
|
|
|
time.Sleep(backoff)
|
2018-09-15 00:08:26 +00:00
|
|
|
select {
|
2018-12-11 20:27:50 +00:00
|
|
|
case ru, ok := <-ch:
|
|
|
|
// Channel is closed
|
|
|
|
if !ok {
|
2019-01-12 17:16:12 +00:00
|
|
|
var re *structs.RecoverableError
|
2018-12-11 20:27:50 +00:00
|
|
|
ch, err = handle.Stats(ctx, h.interval)
|
|
|
|
if err == nil {
|
2019-01-12 17:16:12 +00:00
|
|
|
goto RETRY
|
2018-09-15 00:08:26 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 22:47:49 +00:00
|
|
|
// We do not log when the plugin is shutdown since this is
|
|
|
|
// likely because the driver plugin has unexpectedly exited,
|
|
|
|
// in which case sleeping and trying again or returning based
|
2019-01-03 23:00:52 +00:00
|
|
|
// on the stop channel is the correct behavior
|
2018-12-21 19:23:21 +00:00
|
|
|
if err != bstructs.ErrPluginShutdown {
|
2018-09-15 00:08:26 +00:00
|
|
|
h.logger.Debug("error fetching stats of task", "error", err)
|
2019-01-12 17:16:12 +00:00
|
|
|
goto RETRY
|
2018-09-15 00:08:26 +00:00
|
|
|
}
|
2018-12-11 20:27:50 +00:00
|
|
|
// check if the error is terminal otherwise it's likely a
|
|
|
|
// transport error and we should retry
|
2019-01-12 17:16:12 +00:00
|
|
|
re, ok = err.(*structs.RecoverableError)
|
|
|
|
if ok && re.IsUnrecoverable() {
|
2018-12-11 20:27:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
h.logger.Warn("stats collection for task failed", "error", err)
|
2019-01-12 17:16:12 +00:00
|
|
|
RETRY:
|
|
|
|
// Calculate the new backoff
|
|
|
|
backoff = (1 << (2 * uint64(retry))) * time.Second
|
|
|
|
if backoff > limit {
|
|
|
|
backoff = limit
|
|
|
|
}
|
|
|
|
// Increment retry counter
|
|
|
|
retry++
|
|
|
|
|
2018-09-15 00:08:26 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update stats on TaskRunner and emit them
|
|
|
|
h.updater.UpdateStats(ru)
|
2018-12-11 20:27:50 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2018-09-15 00:08:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 18:29:07 +00:00
|
|
|
|
|
|
|
func (h *statsHook) Shutdown() {
|
|
|
|
h.mu.Lock()
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
2018-12-11 20:27:50 +00:00
|
|
|
if h.cancel == nil {
|
2018-11-14 18:29:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-11 20:27:50 +00:00
|
|
|
h.cancel()
|
2018-11-14 18:29:07 +00:00
|
|
|
}
|