2018-08-23 19:03:17 +00:00
|
|
|
package allocwatcher
|
2017-08-10 17:56:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-08-14 23:02:28 +00:00
|
|
|
"sync"
|
2017-12-13 00:58:27 +00:00
|
|
|
"syscall"
|
2017-08-10 17:56:51 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/lib"
|
2018-08-29 22:05:03 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2017-08-10 17:56:51 +00:00
|
|
|
nomadapi "github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2018-06-11 20:33:18 +00:00
|
|
|
const (
|
|
|
|
// getRemoteRetryIntv is minimum interval on which we retry
|
|
|
|
// to fetch remote objects. We pick a value between this and 2x this.
|
|
|
|
getRemoteRetryIntv = 30 * time.Second
|
|
|
|
)
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
// RPCer is the interface needed by a prevAllocWatcher to make RPC calls.
|
|
|
|
type RPCer interface {
|
2017-08-10 17:56:51 +00:00
|
|
|
// RPC allows retrieving remote allocs.
|
|
|
|
RPC(method string, args interface{}, reply interface{}) error
|
|
|
|
}
|
|
|
|
|
2017-08-11 17:27:21 +00:00
|
|
|
// terminated is the interface needed by a prevAllocWatcher to check if an
|
|
|
|
// alloc is terminated.
|
|
|
|
type terminated interface {
|
|
|
|
Terminated() bool
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:03:17 +00:00
|
|
|
// AllocRunnerMeta provides metadata about an AllocRunner such as its alloc and
|
|
|
|
// alloc dir.
|
|
|
|
type AllocRunnerMeta interface {
|
|
|
|
GetAllocDir() *allocdir.AllocDir
|
|
|
|
Listener() *cstructs.AllocListener
|
|
|
|
Alloc() *structs.Allocation
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrevAllocWatcher allows AllocRunners to wait for a previous allocation to
|
2018-12-06 11:15:59 +00:00
|
|
|
// terminate whether or not the previous allocation is local or remote.
|
|
|
|
// See `PrevAllocMigrator` for migrating workloads.
|
2018-08-23 19:03:17 +00:00
|
|
|
type PrevAllocWatcher interface {
|
2017-08-10 17:56:51 +00:00
|
|
|
// Wait for previous alloc to terminate
|
|
|
|
Wait(context.Context) error
|
|
|
|
|
2017-08-14 23:02:28 +00:00
|
|
|
// IsWaiting returns true if a concurrent caller is blocked in Wait
|
|
|
|
IsWaiting() bool
|
2018-12-06 11:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrevAllocMigrator allows AllocRunners to migrate a previous allocation
|
|
|
|
// whether or not the previous allocation is local or remote.
|
|
|
|
type PrevAllocMigrator interface {
|
|
|
|
PrevAllocWatcher
|
2017-08-14 23:02:28 +00:00
|
|
|
|
|
|
|
// IsMigrating returns true if a concurrent caller is in Migrate
|
|
|
|
IsMigrating() bool
|
2018-12-06 11:15:59 +00:00
|
|
|
|
|
|
|
// Migrate data from previous alloc
|
|
|
|
Migrate(ctx context.Context, dest *allocdir.AllocDir) error
|
2017-08-10 17:56:51 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
type Config struct {
|
|
|
|
// Alloc is the current allocation which may need to block on its
|
|
|
|
// previous allocation stopping.
|
|
|
|
Alloc *structs.Allocation
|
|
|
|
|
2018-12-06 11:15:59 +00:00
|
|
|
// PreviousRunner is non-nil if Alloc has a PreviousAllocation and it is
|
2018-08-29 22:05:03 +00:00
|
|
|
// running locally.
|
|
|
|
PreviousRunner AllocRunnerMeta
|
|
|
|
|
2018-12-06 11:15:59 +00:00
|
|
|
// PreemptedRunners is non-nil if Alloc has one or more PreemptedAllocations.
|
|
|
|
PreemptedRunners map[string]AllocRunnerMeta
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
// RPC allows the alloc watcher to monitor remote allocations.
|
|
|
|
RPC RPCer
|
|
|
|
|
|
|
|
// Config is necessary for using the RPC.
|
|
|
|
Config *config.Config
|
|
|
|
|
|
|
|
// MigrateToken is used to migrate remote alloc dirs when ACLs are
|
|
|
|
// enabled.
|
|
|
|
MigrateToken string
|
|
|
|
|
|
|
|
Logger hclog.Logger
|
|
|
|
}
|
|
|
|
|
2018-12-11 12:09:37 +00:00
|
|
|
func newMigratorForAlloc(c Config, tg *structs.TaskGroup, watchedAllocID string, m AllocRunnerMeta) PrevAllocMigrator {
|
2018-12-11 17:03:45 +00:00
|
|
|
logger := c.Logger.Named("alloc_migrator").With("alloc_id", c.Alloc.ID).With("previous_alloc", watchedAllocID)
|
2018-12-06 11:15:59 +00:00
|
|
|
|
2018-12-11 12:09:37 +00:00
|
|
|
tasks := tg.Tasks
|
|
|
|
sticky := tg.EphemeralDisk != nil && tg.EphemeralDisk.Sticky
|
|
|
|
migrate := tg.EphemeralDisk != nil && tg.EphemeralDisk.Migrate
|
2017-08-10 17:56:51 +00:00
|
|
|
|
2018-12-06 11:15:59 +00:00
|
|
|
if m != nil {
|
|
|
|
// Local Allocation because there's no meta
|
2017-08-10 17:56:51 +00:00
|
|
|
return &localPrevAlloc{
|
2018-08-29 22:05:03 +00:00
|
|
|
allocID: c.Alloc.ID,
|
2018-12-06 11:15:59 +00:00
|
|
|
prevAllocID: watchedAllocID,
|
|
|
|
tasks: tasks,
|
|
|
|
sticky: sticky,
|
|
|
|
prevAllocDir: m.GetAllocDir(),
|
|
|
|
prevListener: m.Listener(),
|
|
|
|
prevStatus: m.Alloc(),
|
2018-08-29 22:05:03 +00:00
|
|
|
logger: logger,
|
2017-08-10 17:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &remotePrevAlloc{
|
2018-08-29 22:05:03 +00:00
|
|
|
allocID: c.Alloc.ID,
|
|
|
|
prevAllocID: c.Alloc.PreviousAllocation,
|
2018-12-06 11:15:59 +00:00
|
|
|
tasks: tasks,
|
2018-08-29 22:05:03 +00:00
|
|
|
config: c.Config,
|
2018-12-06 11:15:59 +00:00
|
|
|
migrate: migrate,
|
2018-08-29 22:05:03 +00:00
|
|
|
rpc: c.RPC,
|
|
|
|
migrateToken: c.MigrateToken,
|
|
|
|
logger: logger,
|
2017-08-10 17:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 12:09:37 +00:00
|
|
|
func newWatcherForAlloc(c Config, watchedAllocID string, m AllocRunnerMeta) PrevAllocWatcher {
|
2018-12-11 17:03:45 +00:00
|
|
|
logger := c.Logger.Named("alloc_watcher").With("alloc_id", c.Alloc.ID).With("previous_alloc", watchedAllocID)
|
2018-12-11 12:09:37 +00:00
|
|
|
|
|
|
|
if m != nil {
|
|
|
|
// Local Allocation because there's no meta
|
|
|
|
return &localPrevAlloc{
|
|
|
|
allocID: c.Alloc.ID,
|
|
|
|
prevAllocID: watchedAllocID,
|
|
|
|
prevAllocDir: m.GetAllocDir(),
|
|
|
|
prevListener: m.Listener(),
|
|
|
|
prevStatus: m.Alloc(),
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &remotePrevAlloc{
|
|
|
|
allocID: c.Alloc.ID,
|
|
|
|
prevAllocID: c.Alloc.PreviousAllocation,
|
|
|
|
config: c.Config,
|
|
|
|
rpc: c.RPC,
|
|
|
|
migrateToken: c.MigrateToken,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:15:59 +00:00
|
|
|
// NewAllocWatcher creates a PrevAllocWatcher appropriate for whether this
|
|
|
|
// alloc's previous allocation was local or remote. If this alloc has no
|
|
|
|
// previous alloc then a noop implementation is returned.
|
|
|
|
func NewAllocWatcher(c Config) (PrevAllocWatcher, PrevAllocMigrator) {
|
|
|
|
if c.Alloc.PreviousAllocation == "" && c.PreemptedRunners == nil {
|
|
|
|
return NoopPrevAlloc{}, NoopPrevAlloc{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var prevAllocWatchers []PrevAllocWatcher
|
|
|
|
var prevAllocMigrator PrevAllocMigrator = NoopPrevAlloc{}
|
|
|
|
|
|
|
|
// We have a previous allocation, add its listener to the watchers, and
|
|
|
|
// use a migrator.
|
|
|
|
if c.Alloc.PreviousAllocation != "" {
|
|
|
|
tg := c.Alloc.Job.LookupTaskGroup(c.Alloc.TaskGroup)
|
2018-12-11 12:09:37 +00:00
|
|
|
m := newMigratorForAlloc(c, tg, c.Alloc.PreviousAllocation, c.PreviousRunner)
|
|
|
|
prevAllocWatchers = append(prevAllocWatchers, m)
|
|
|
|
prevAllocMigrator = m
|
2018-12-06 11:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We are preempting allocations, add their listeners to the watchers.
|
|
|
|
if c.PreemptedRunners != nil {
|
|
|
|
for aid, r := range c.PreemptedRunners {
|
2018-12-11 12:09:37 +00:00
|
|
|
w := newWatcherForAlloc(c, aid, r)
|
2018-12-06 11:15:59 +00:00
|
|
|
prevAllocWatchers = append(prevAllocWatchers, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
groupWatcher := &groupPrevAllocWatcher{
|
|
|
|
prevAllocs: prevAllocWatchers,
|
|
|
|
}
|
|
|
|
|
|
|
|
return groupWatcher, prevAllocMigrator
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:56:51 +00:00
|
|
|
// localPrevAlloc is a prevAllocWatcher for previous allocations on the same
|
|
|
|
// node as an updated allocation.
|
|
|
|
type localPrevAlloc struct {
|
2017-08-11 23:14:58 +00:00
|
|
|
// allocID is the ID of the alloc being blocked
|
|
|
|
allocID string
|
|
|
|
|
|
|
|
// prevAllocID is the ID of the alloc being replaced
|
2017-08-10 17:56:51 +00:00
|
|
|
prevAllocID string
|
|
|
|
|
2017-08-11 23:14:58 +00:00
|
|
|
// tasks on the new alloc
|
|
|
|
tasks []*structs.Task
|
|
|
|
|
|
|
|
// sticky is true if data should be moved
|
|
|
|
sticky bool
|
|
|
|
|
|
|
|
// prevAllocDir is the alloc dir for the previous alloc
|
2017-08-10 17:56:51 +00:00
|
|
|
prevAllocDir *allocdir.AllocDir
|
2017-08-11 23:14:58 +00:00
|
|
|
|
|
|
|
// prevListener allows blocking for updates to the previous alloc
|
2017-08-10 17:56:51 +00:00
|
|
|
prevListener *cstructs.AllocListener
|
2017-08-11 23:14:58 +00:00
|
|
|
|
|
|
|
// prevStatus allows checking if the previous alloc has already
|
|
|
|
// terminated (and therefore won't send updates to the listener)
|
|
|
|
prevStatus terminated
|
|
|
|
|
2017-08-14 23:02:28 +00:00
|
|
|
// waiting and migrating are true when alloc runner is waiting on the
|
|
|
|
// prevAllocWatcher. Writers must acquire the waitingLock and readers
|
|
|
|
// should use the helper methods IsWaiting and IsMigrating.
|
|
|
|
waiting bool
|
|
|
|
migrating bool
|
|
|
|
waitingLock sync.RWMutex
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
logger hclog.Logger
|
2017-08-10 17:56:51 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 23:02:28 +00:00
|
|
|
// IsWaiting returns true if there's a concurrent call inside Wait
|
|
|
|
func (p *localPrevAlloc) IsWaiting() bool {
|
|
|
|
p.waitingLock.RLock()
|
|
|
|
b := p.waiting
|
|
|
|
p.waitingLock.RUnlock()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsMigrating returns true if there's a concurrent call inside Migrate
|
|
|
|
func (p *localPrevAlloc) IsMigrating() bool {
|
|
|
|
p.waitingLock.RLock()
|
|
|
|
b := p.migrating
|
|
|
|
p.waitingLock.RUnlock()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:56:51 +00:00
|
|
|
// Wait on a local alloc to become terminal, exit, or the context to be done.
|
|
|
|
func (p *localPrevAlloc) Wait(ctx context.Context) error {
|
2017-08-14 23:02:28 +00:00
|
|
|
p.waitingLock.Lock()
|
|
|
|
p.waiting = true
|
|
|
|
p.waitingLock.Unlock()
|
|
|
|
defer func() {
|
|
|
|
p.waitingLock.Lock()
|
|
|
|
p.waiting = false
|
|
|
|
p.waitingLock.Unlock()
|
|
|
|
}()
|
|
|
|
|
2017-08-10 17:56:51 +00:00
|
|
|
defer p.prevListener.Close()
|
2017-08-11 17:27:21 +00:00
|
|
|
|
2018-11-20 00:13:04 +00:00
|
|
|
// Don't bother blocking for updates from the previous alloc if it has
|
|
|
|
// already terminated.
|
|
|
|
if p.prevStatus.Terminated() {
|
|
|
|
p.logger.Trace("previous allocation already terminated")
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-17 01:29:25 +00:00
|
|
|
|
2017-08-11 17:27:21 +00:00
|
|
|
// Block until previous alloc exits
|
2018-08-29 22:05:03 +00:00
|
|
|
p.logger.Debug("waiting for previous alloc to terminate")
|
2017-08-10 17:56:51 +00:00
|
|
|
for {
|
|
|
|
select {
|
2018-11-17 01:29:25 +00:00
|
|
|
case prevAlloc, ok := <-p.prevListener.Ch():
|
2017-08-11 23:14:58 +00:00
|
|
|
if !ok || prevAlloc.Terminated() {
|
2017-08-10 17:56:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate from previous local alloc dir to destination alloc dir.
|
|
|
|
func (p *localPrevAlloc) Migrate(ctx context.Context, dest *allocdir.AllocDir) error {
|
|
|
|
if !p.sticky {
|
|
|
|
// Not a sticky volume, nothing to migrate
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-14 23:02:28 +00:00
|
|
|
|
|
|
|
p.waitingLock.Lock()
|
|
|
|
p.migrating = true
|
|
|
|
p.waitingLock.Unlock()
|
|
|
|
defer func() {
|
|
|
|
p.waitingLock.Lock()
|
|
|
|
p.migrating = false
|
|
|
|
p.waitingLock.Unlock()
|
|
|
|
}()
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
p.logger.Debug("copying previous alloc")
|
2017-08-10 17:56:51 +00:00
|
|
|
|
2017-08-14 23:48:56 +00:00
|
|
|
moveErr := dest.Move(p.prevAllocDir, p.tasks)
|
|
|
|
|
|
|
|
// Always cleanup previous alloc
|
2017-08-10 17:56:51 +00:00
|
|
|
if err := p.prevAllocDir.Destroy(); err != nil {
|
2018-08-29 22:05:03 +00:00
|
|
|
p.logger.Error("error destroying alloc dir",
|
|
|
|
"error", err, "previous_alloc_dir", p.prevAllocDir.AllocDir)
|
2017-08-10 17:56:51 +00:00
|
|
|
}
|
2017-08-14 23:48:56 +00:00
|
|
|
|
|
|
|
return moveErr
|
2017-08-10 17:56:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 17:36:34 +00:00
|
|
|
// remotePrevAlloc is a prevAllocWatcher for previous allocations on remote
|
2017-08-10 17:56:51 +00:00
|
|
|
// nodes as an updated allocation.
|
|
|
|
type remotePrevAlloc struct {
|
2017-08-11 23:14:58 +00:00
|
|
|
// allocID is the ID of the alloc being blocked
|
|
|
|
allocID string
|
|
|
|
|
|
|
|
// prevAllocID is the ID of the alloc being replaced
|
2017-08-10 17:56:51 +00:00
|
|
|
prevAllocID string
|
|
|
|
|
2017-08-11 23:14:58 +00:00
|
|
|
// tasks on the new alloc
|
|
|
|
tasks []*structs.Task
|
|
|
|
|
2017-10-12 23:38:02 +00:00
|
|
|
// config for the Client to get AllocDir, Region, and Node.SecretID
|
2017-08-11 23:14:58 +00:00
|
|
|
config *config.Config
|
|
|
|
|
|
|
|
// migrate is true if data should be moved between nodes
|
2017-08-10 17:56:51 +00:00
|
|
|
migrate bool
|
2017-08-11 23:14:58 +00:00
|
|
|
|
|
|
|
// rpc provides an RPC method for watching for updates to the previous
|
|
|
|
// alloc and determining what node it was on.
|
2018-08-29 22:05:03 +00:00
|
|
|
rpc RPCer
|
2017-08-10 17:56:51 +00:00
|
|
|
|
|
|
|
// nodeID is the node the previous alloc. Set by Wait() for use in
|
|
|
|
// Migrate() iff the previous alloc has not already been GC'd.
|
|
|
|
nodeID string
|
|
|
|
|
2017-08-14 23:02:28 +00:00
|
|
|
// waiting and migrating are true when alloc runner is waiting on the
|
|
|
|
// prevAllocWatcher. Writers must acquire the waitingLock and readers
|
|
|
|
// should use the helper methods IsWaiting and IsMigrating.
|
|
|
|
waiting bool
|
|
|
|
migrating bool
|
|
|
|
waitingLock sync.RWMutex
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
logger hclog.Logger
|
2017-10-03 17:53:32 +00:00
|
|
|
|
|
|
|
// migrateToken allows a client to migrate data in an ACL-protected remote
|
|
|
|
// volume
|
|
|
|
migrateToken string
|
2017-08-10 17:56:51 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 23:02:28 +00:00
|
|
|
// IsWaiting returns true if there's a concurrent call inside Wait
|
|
|
|
func (p *remotePrevAlloc) IsWaiting() bool {
|
|
|
|
p.waitingLock.RLock()
|
|
|
|
b := p.waiting
|
|
|
|
p.waitingLock.RUnlock()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsMigrating returns true if there's a concurrent call inside Migrate
|
|
|
|
func (p *remotePrevAlloc) IsMigrating() bool {
|
|
|
|
p.waitingLock.RLock()
|
|
|
|
b := p.migrating
|
|
|
|
p.waitingLock.RUnlock()
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:38:23 +00:00
|
|
|
// Wait until the remote previous allocation has terminated.
|
2017-08-10 17:56:51 +00:00
|
|
|
func (p *remotePrevAlloc) Wait(ctx context.Context) error {
|
2017-08-14 23:02:28 +00:00
|
|
|
p.waitingLock.Lock()
|
|
|
|
p.waiting = true
|
|
|
|
p.waitingLock.Unlock()
|
|
|
|
defer func() {
|
|
|
|
p.waitingLock.Lock()
|
|
|
|
p.waiting = false
|
|
|
|
p.waitingLock.Unlock()
|
|
|
|
}()
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
p.logger.Debug("waiting for remote previous alloc to terminate")
|
2017-08-10 17:56:51 +00:00
|
|
|
req := structs.AllocSpecificRequest{
|
|
|
|
AllocID: p.prevAllocID,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: p.config.Region,
|
|
|
|
AllowStale: true,
|
2017-10-12 23:38:02 +00:00
|
|
|
AuthToken: p.config.Node.SecretID,
|
2017-08-10 17:56:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
done := func() bool {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for !done() {
|
|
|
|
resp := structs.SingleAllocResponse{}
|
|
|
|
err := p.rpc.RPC("Alloc.GetAlloc", &req, &resp)
|
|
|
|
if err != nil {
|
2018-08-29 22:05:03 +00:00
|
|
|
p.logger.Error("error querying previous alloc", "error", err)
|
2018-06-11 20:33:18 +00:00
|
|
|
retry := getRemoteRetryIntv + lib.RandomStagger(getRemoteRetryIntv)
|
2017-08-10 17:56:51 +00:00
|
|
|
select {
|
|
|
|
case <-time.After(retry):
|
|
|
|
continue
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if resp.Alloc == nil {
|
2018-08-29 22:05:03 +00:00
|
|
|
p.logger.Debug("blocking alloc was GC'd")
|
2017-08-10 17:56:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if resp.Alloc.Terminated() {
|
|
|
|
// Terminated!
|
|
|
|
p.nodeID = resp.Alloc.NodeID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the query index and requery.
|
|
|
|
if resp.Index > req.MinQueryIndex {
|
|
|
|
req.MinQueryIndex = resp.Index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 23:59:03 +00:00
|
|
|
return ctx.Err()
|
2017-08-10 17:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate alloc data from a remote node if the new alloc has migration enabled
|
|
|
|
// and the old alloc hasn't been GC'd.
|
|
|
|
func (p *remotePrevAlloc) Migrate(ctx context.Context, dest *allocdir.AllocDir) error {
|
|
|
|
if !p.migrate {
|
|
|
|
// Volume wasn't configured to be migrated, return early
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-14 23:02:28 +00:00
|
|
|
|
|
|
|
p.waitingLock.Lock()
|
|
|
|
p.migrating = true
|
|
|
|
p.waitingLock.Unlock()
|
|
|
|
defer func() {
|
|
|
|
p.waitingLock.Lock()
|
|
|
|
p.migrating = false
|
|
|
|
p.waitingLock.Unlock()
|
|
|
|
}()
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
p.logger.Debug("copying from remote previous alloc")
|
2017-08-10 17:56:51 +00:00
|
|
|
|
|
|
|
if p.nodeID == "" {
|
|
|
|
// NodeID couldn't be found; likely alloc was GC'd
|
2018-08-29 22:05:03 +00:00
|
|
|
p.logger.Warn("unable to migrate data from previous alloc; previous alloc may have been GC'd")
|
2017-08-10 17:56:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := p.getNodeAddr(ctx, p.nodeID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
prevAllocDir, err := p.migrateAllocDir(ctx, addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := dest.Move(prevAllocDir, p.tasks); err != nil {
|
|
|
|
// cleanup on error
|
|
|
|
prevAllocDir.Destroy()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := prevAllocDir.Destroy(); err != nil {
|
2018-08-29 22:05:03 +00:00
|
|
|
p.logger.Error("error destroying alloc dir",
|
|
|
|
"error", err, "previous_alloc_dir", prevAllocDir.AllocDir)
|
2017-08-10 17:56:51 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getNodeAddr gets the node from the server with the given Node ID
|
|
|
|
func (p *remotePrevAlloc) getNodeAddr(ctx context.Context, nodeID string) (string, error) {
|
|
|
|
req := structs.NodeSpecificRequest{
|
|
|
|
NodeID: nodeID,
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: p.config.Region,
|
|
|
|
AllowStale: true,
|
2017-10-12 23:38:02 +00:00
|
|
|
AuthToken: p.config.Node.SecretID,
|
2017-08-10 17:56:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := structs.SingleNodeResponse{}
|
|
|
|
for {
|
|
|
|
err := p.rpc.RPC("Node.GetNode", &req, &resp)
|
|
|
|
if err != nil {
|
2018-08-29 22:05:03 +00:00
|
|
|
p.logger.Error("failed to query node", "error", err, "node", nodeID)
|
2018-06-11 20:33:18 +00:00
|
|
|
retry := getRemoteRetryIntv + lib.RandomStagger(getRemoteRetryIntv)
|
2017-08-10 17:56:51 +00:00
|
|
|
select {
|
|
|
|
case <-time.After(retry):
|
|
|
|
continue
|
|
|
|
case <-ctx.Done():
|
|
|
|
return "", ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Node == nil {
|
|
|
|
return "", fmt.Errorf("node %q not found", nodeID)
|
|
|
|
}
|
|
|
|
|
|
|
|
scheme := "http://"
|
|
|
|
if resp.Node.TLSEnabled {
|
|
|
|
scheme = "https://"
|
|
|
|
}
|
|
|
|
return scheme + resp.Node.HTTPAddr, nil
|
|
|
|
}
|
|
|
|
|
2017-08-14 23:59:03 +00:00
|
|
|
// migrate a remote alloc dir to local node. Caller is responsible for calling
|
|
|
|
// Destroy on the returned allocdir if no error occurs.
|
2017-08-10 17:56:51 +00:00
|
|
|
func (p *remotePrevAlloc) migrateAllocDir(ctx context.Context, nodeAddr string) (*allocdir.AllocDir, error) {
|
|
|
|
// Create the previous alloc dir
|
|
|
|
prevAllocDir := allocdir.NewAllocDir(p.logger, filepath.Join(p.config.AllocDir, p.prevAllocID))
|
|
|
|
if err := prevAllocDir.Build(); err != nil {
|
|
|
|
return nil, fmt.Errorf("error building alloc dir for previous alloc %q: %v", p.prevAllocID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an API client
|
|
|
|
apiConfig := nomadapi.DefaultConfig()
|
|
|
|
apiConfig.Address = nodeAddr
|
|
|
|
apiConfig.TLSConfig = &nomadapi.TLSConfig{
|
2018-09-06 00:06:30 +00:00
|
|
|
CACert: p.config.TLSConfig.CAFile,
|
|
|
|
ClientCert: p.config.TLSConfig.CertFile,
|
|
|
|
ClientKey: p.config.TLSConfig.KeyFile,
|
|
|
|
TLSServerName: fmt.Sprintf("client.%s.nomad", p.config.Region),
|
2017-08-10 17:56:51 +00:00
|
|
|
}
|
|
|
|
apiClient, err := nomadapi.NewClient(apiConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
url := fmt.Sprintf("/v1/client/allocation/%v/snapshot", p.prevAllocID)
|
2017-10-12 22:16:33 +00:00
|
|
|
qo := &nomadapi.QueryOptions{AuthToken: p.migrateToken}
|
2017-10-03 17:53:32 +00:00
|
|
|
resp, err := apiClient.Raw().Response(url, qo)
|
2017-08-10 17:56:51 +00:00
|
|
|
if err != nil {
|
|
|
|
prevAllocDir.Destroy()
|
|
|
|
return nil, fmt.Errorf("error getting snapshot from previous alloc %q: %v", p.prevAllocID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.streamAllocDir(ctx, resp, prevAllocDir.AllocDir); err != nil {
|
|
|
|
prevAllocDir.Destroy()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return prevAllocDir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// stream remote alloc to dir to a local path. Caller should cleanup dest on
|
|
|
|
// error.
|
|
|
|
func (p *remotePrevAlloc) streamAllocDir(ctx context.Context, resp io.ReadCloser, dest string) error {
|
2018-08-29 22:05:03 +00:00
|
|
|
p.logger.Debug("streaming snapshot of previous alloc", "destination", dest)
|
2017-08-10 17:56:51 +00:00
|
|
|
tr := tar.NewReader(resp)
|
|
|
|
defer resp.Close()
|
|
|
|
|
2017-12-13 19:50:12 +00:00
|
|
|
// Cache effective uid as we only run Chown if we're root
|
|
|
|
euid := syscall.Geteuid()
|
|
|
|
|
2017-08-10 17:56:51 +00:00
|
|
|
canceled := func() bool {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2018-08-29 22:05:03 +00:00
|
|
|
p.logger.Info("migration of previous alloc canceled")
|
2017-08-10 17:56:51 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 01:48:06 +00:00
|
|
|
// if we see this file, there was an error on the remote side
|
|
|
|
errorFilename := allocdir.SnapshotErrorFilename(p.prevAllocID)
|
|
|
|
|
2017-08-10 17:56:51 +00:00
|
|
|
buf := make([]byte, 1024)
|
|
|
|
for !canceled() {
|
|
|
|
// Get the next header
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
|
|
|
|
// Snapshot has ended
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-08-15 17:37:02 +00:00
|
|
|
return fmt.Errorf("error streaming previous alloc %q for new alloc %q: %v",
|
2017-08-10 17:56:51 +00:00
|
|
|
p.prevAllocID, p.allocID, err)
|
|
|
|
}
|
|
|
|
|
2017-11-17 01:48:06 +00:00
|
|
|
if hdr.Name == errorFilename {
|
|
|
|
// Error snapshotting on the remote side, try to read
|
|
|
|
// the message out of the file and return it.
|
|
|
|
errBuf := make([]byte, int(hdr.Size))
|
2018-03-27 18:14:53 +00:00
|
|
|
if _, err := tr.Read(errBuf); err != nil && err != io.EOF {
|
2017-11-17 01:48:06 +00:00
|
|
|
return fmt.Errorf("error streaming previous alloc %q for new alloc %q; failed reading error message: %v",
|
|
|
|
p.prevAllocID, p.allocID, err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("error streaming previous alloc %q for new alloc %q: %s",
|
|
|
|
p.prevAllocID, p.allocID, string(errBuf))
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:56:51 +00:00
|
|
|
// If the header is for a directory we create the directory
|
|
|
|
if hdr.Typeflag == tar.TypeDir {
|
2018-01-05 23:45:41 +00:00
|
|
|
name := filepath.Join(dest, hdr.Name)
|
|
|
|
os.MkdirAll(name, os.FileMode(hdr.Mode))
|
|
|
|
|
|
|
|
// Can't change owner if not root or on Windows.
|
|
|
|
if euid == 0 {
|
|
|
|
if err := os.Chown(name, hdr.Uid, hdr.Gid); err != nil {
|
|
|
|
return fmt.Errorf("error chowning directory %v", err)
|
|
|
|
}
|
|
|
|
}
|
2017-08-10 17:56:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// If the header is for a symlink we create the symlink
|
|
|
|
if hdr.Typeflag == tar.TypeSymlink {
|
|
|
|
if err = os.Symlink(hdr.Linkname, filepath.Join(dest, hdr.Name)); err != nil {
|
|
|
|
return fmt.Errorf("error creating symlink: %v", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// If the header is a file, we write to a file
|
|
|
|
if hdr.Typeflag == tar.TypeReg {
|
|
|
|
f, err := os.Create(filepath.Join(dest, hdr.Name))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setting the permissions of the file as the origin.
|
|
|
|
if err := f.Chmod(os.FileMode(hdr.Mode)); err != nil {
|
|
|
|
f.Close()
|
|
|
|
return fmt.Errorf("error chmoding file %v", err)
|
|
|
|
}
|
2017-12-13 00:58:27 +00:00
|
|
|
|
2018-01-05 23:45:41 +00:00
|
|
|
// Can't change owner if not root or on Windows.
|
2017-12-13 19:50:12 +00:00
|
|
|
if euid == 0 {
|
2017-12-13 00:58:27 +00:00
|
|
|
if err := f.Chown(hdr.Uid, hdr.Gid); err != nil {
|
|
|
|
f.Close()
|
|
|
|
return fmt.Errorf("error chowning file %v", err)
|
|
|
|
}
|
2017-08-10 17:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We write in chunks so that we can test if the client
|
|
|
|
// is still alive
|
|
|
|
for !canceled() {
|
|
|
|
n, err := tr.Read(buf)
|
2018-03-27 18:14:53 +00:00
|
|
|
if n > 0 && (err == nil || err == io.EOF) {
|
|
|
|
if _, err := f.Write(buf[:n]); err != nil {
|
|
|
|
f.Close()
|
|
|
|
return fmt.Errorf("error writing to file %q: %v", f.Name(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:56:51 +00:00
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
if err != io.EOF {
|
|
|
|
return fmt.Errorf("error reading snapshot: %v", err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if canceled() {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-11 20:33:18 +00:00
|
|
|
// NoopPrevAlloc does not block or migrate on a previous allocation and never
|
2017-08-10 17:56:51 +00:00
|
|
|
// returns an error.
|
2018-06-11 20:33:18 +00:00
|
|
|
type NoopPrevAlloc struct{}
|
2017-08-10 17:56:51 +00:00
|
|
|
|
|
|
|
// Wait returns nil immediately.
|
2018-06-11 20:33:18 +00:00
|
|
|
func (NoopPrevAlloc) Wait(context.Context) error { return nil }
|
2017-08-10 17:56:51 +00:00
|
|
|
|
|
|
|
// Migrate returns nil immediately.
|
2018-06-11 20:33:18 +00:00
|
|
|
func (NoopPrevAlloc) Migrate(context.Context, *allocdir.AllocDir) error { return nil }
|
2017-08-14 23:02:28 +00:00
|
|
|
|
2018-06-11 20:33:18 +00:00
|
|
|
func (NoopPrevAlloc) IsWaiting() bool { return false }
|
|
|
|
func (NoopPrevAlloc) IsMigrating() bool { return false }
|