2018-07-17 00:19:56 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-04-20 17:15:56 +00:00
|
|
|
"sync"
|
2018-07-17 00:19:56 +00:00
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-10-04 23:22:01 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
|
|
|
ti "github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces"
|
2022-05-03 22:38:32 +00:00
|
|
|
ci "github.com/hashicorp/nomad/client/interfaces"
|
2018-07-17 00:19:56 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// artifactHook downloads artifacts for a task.
|
|
|
|
type artifactHook struct {
|
|
|
|
eventEmitter ti.EventEmitter
|
|
|
|
logger log.Logger
|
2022-05-03 22:38:32 +00:00
|
|
|
getter ci.ArtifactGetter
|
2018-07-17 00:19:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-03 22:38:32 +00:00
|
|
|
func newArtifactHook(e ti.EventEmitter, getter ci.ArtifactGetter, logger log.Logger) *artifactHook {
|
2018-07-17 00:19:56 +00:00
|
|
|
h := &artifactHook{
|
|
|
|
eventEmitter: e,
|
2022-05-03 22:38:32 +00:00
|
|
|
getter: getter,
|
2018-07-17 00:19:56 +00:00
|
|
|
}
|
|
|
|
h.logger = logger.Named(h.Name())
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2022-04-20 17:15:56 +00:00
|
|
|
func (h *artifactHook) doWork(req *interfaces.TaskPrestartRequest, resp *interfaces.TaskPrestartResponse, jobs chan *structs.TaskArtifact, errorChannel chan error, wg *sync.WaitGroup, responseStateMutex *sync.Mutex) {
|
|
|
|
defer wg.Done()
|
|
|
|
for artifact := range jobs {
|
2019-02-20 16:41:51 +00:00
|
|
|
aid := artifact.Hash()
|
2019-02-21 23:37:22 +00:00
|
|
|
if req.PreviousState[aid] != "" {
|
2019-02-20 16:41:51 +00:00
|
|
|
h.logger.Trace("skipping already downloaded artifact", "artifact", artifact.GetterSource)
|
2022-04-20 17:15:56 +00:00
|
|
|
responseStateMutex.Lock()
|
2019-02-21 23:37:22 +00:00
|
|
|
resp.State[aid] = req.PreviousState[aid]
|
2022-04-20 17:15:56 +00:00
|
|
|
responseStateMutex.Unlock()
|
2019-02-20 16:41:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-20 17:15:56 +00:00
|
|
|
h.logger.Debug("downloading artifact", "artifact", artifact.GetterSource, "aid", aid)
|
2018-07-17 00:19:56 +00:00
|
|
|
//XXX add ctx to GetArtifact to allow cancelling long downloads
|
2022-05-03 22:38:32 +00:00
|
|
|
if err := h.getter.GetArtifact(req.TaskEnv, artifact); err != nil {
|
2020-12-14 17:56:34 +00:00
|
|
|
|
2019-02-13 16:26:23 +00:00
|
|
|
wrapped := structs.NewRecoverableError(
|
|
|
|
fmt.Errorf("failed to download artifact %q: %v", artifact.GetterSource, err),
|
|
|
|
true,
|
|
|
|
)
|
2018-12-13 17:21:32 +00:00
|
|
|
herr := NewHookError(wrapped, structs.NewTaskEvent(structs.TaskArtifactDownloadFailed).SetDownloadError(wrapped))
|
|
|
|
|
2022-04-20 17:15:56 +00:00
|
|
|
errorChannel <- herr
|
|
|
|
continue
|
2018-07-17 00:19:56 +00:00
|
|
|
}
|
2019-02-20 16:41:51 +00:00
|
|
|
|
|
|
|
// Mark artifact as downloaded to avoid re-downloading due to
|
|
|
|
// retries caused by subsequent artifacts failing. Any
|
|
|
|
// non-empty value works.
|
2022-04-20 17:15:56 +00:00
|
|
|
responseStateMutex.Lock()
|
2019-02-21 23:37:22 +00:00
|
|
|
resp.State[aid] = "1"
|
2022-04-20 17:15:56 +00:00
|
|
|
responseStateMutex.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*artifactHook) Name() string {
|
|
|
|
// Copied in client/state when upgrading from <0.9 schemas, so if you
|
|
|
|
// change it here you also must change it there.
|
|
|
|
return "artifacts"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *artifactHook) Prestart(ctx context.Context, req *interfaces.TaskPrestartRequest, resp *interfaces.TaskPrestartResponse) error {
|
|
|
|
if len(req.Task.Artifacts) == 0 {
|
|
|
|
resp.Done = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize hook state to store download progress
|
|
|
|
resp.State = make(map[string]string, len(req.Task.Artifacts))
|
|
|
|
|
|
|
|
// responseStateMutex is a lock used to guard against concurrent writes to the above resp.State map
|
|
|
|
responseStateMutex := &sync.Mutex{}
|
|
|
|
|
|
|
|
h.eventEmitter.EmitEvent(structs.NewTaskEvent(structs.TaskDownloadingArtifacts))
|
|
|
|
|
|
|
|
// maxConcurrency denotes the number of workers that will download artifacts in parallel
|
|
|
|
maxConcurrency := 3
|
|
|
|
|
|
|
|
// jobsChannel is a buffered channel which will have all the artifacts that needs to be processed
|
|
|
|
jobsChannel := make(chan *structs.TaskArtifact, maxConcurrency)
|
|
|
|
|
|
|
|
// errorChannel is also a buffered channel that will be used to signal errors
|
|
|
|
errorChannel := make(chan error, maxConcurrency)
|
|
|
|
|
|
|
|
// create workers and process artifacts
|
|
|
|
go func() {
|
|
|
|
defer close(errorChannel)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < maxConcurrency; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go h.doWork(req, resp, jobsChannel, errorChannel, &wg, responseStateMutex)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Push all artifact requests to job channel
|
|
|
|
go func() {
|
|
|
|
defer close(jobsChannel)
|
|
|
|
for _, artifact := range req.Task.Artifacts {
|
|
|
|
jobsChannel <- artifact
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Iterate over the errorChannel and if there is an error, store it to a variable for future return
|
|
|
|
var err error
|
|
|
|
for e := range errorChannel {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
|
|
|
|
// once error channel is closed, we can check and return the error
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-07-17 00:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.Done = true
|
|
|
|
return nil
|
|
|
|
}
|