2015-08-20 23:07:26 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2017-04-28 20:18:04 +00:00
|
|
|
"bytes"
|
2015-08-30 01:03:00 +00:00
|
|
|
"encoding/json"
|
2015-08-20 23:07:26 +00:00
|
|
|
"fmt"
|
2015-08-30 01:03:00 +00:00
|
|
|
"io/ioutil"
|
2015-08-23 21:54:52 +00:00
|
|
|
"math/rand"
|
2015-08-30 01:03:00 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-08-23 21:47:51 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-04-28 20:18:04 +00:00
|
|
|
"github.com/ugorji/go/codec"
|
2015-08-20 23:07:26 +00:00
|
|
|
)
|
|
|
|
|
2015-08-23 21:47:51 +00:00
|
|
|
type allocTuple struct {
|
|
|
|
exist, updated *structs.Allocation
|
|
|
|
}
|
|
|
|
|
|
|
|
// diffResult is used to return the sets that result from a diff
|
|
|
|
type diffResult struct {
|
|
|
|
added []*structs.Allocation
|
|
|
|
removed []*structs.Allocation
|
|
|
|
updated []allocTuple
|
|
|
|
ignore []*structs.Allocation
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *diffResult) GoString() string {
|
|
|
|
return fmt.Sprintf("allocs: (added %d) (removed %d) (updated %d) (ignore %d)",
|
|
|
|
len(d.added), len(d.removed), len(d.updated), len(d.ignore))
|
|
|
|
}
|
|
|
|
|
|
|
|
// diffAllocs is used to diff the existing and updated allocations
|
|
|
|
// to see what has happened.
|
2016-02-01 21:57:35 +00:00
|
|
|
func diffAllocs(existing []*structs.Allocation, allocs *allocUpdates) *diffResult {
|
2015-08-23 21:47:51 +00:00
|
|
|
// Scan the existing allocations
|
2016-02-01 21:57:35 +00:00
|
|
|
result := &diffResult{}
|
2015-08-23 21:47:51 +00:00
|
|
|
existIdx := make(map[string]struct{})
|
|
|
|
for _, exist := range existing {
|
|
|
|
// Mark this as existing
|
|
|
|
existIdx[exist.ID] = struct{}{}
|
|
|
|
|
2016-02-01 21:57:35 +00:00
|
|
|
// Check if the alloc was updated or filtered because an update wasn't
|
|
|
|
// needed.
|
|
|
|
alloc, pulled := allocs.pulled[exist.ID]
|
|
|
|
_, filtered := allocs.filtered[exist.ID]
|
2015-08-23 21:47:51 +00:00
|
|
|
|
2016-02-01 21:57:35 +00:00
|
|
|
// If not updated or filtered, removed
|
|
|
|
if !pulled && !filtered {
|
2015-08-23 21:47:51 +00:00
|
|
|
result.removed = append(result.removed, exist)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for an update
|
2016-02-01 21:57:35 +00:00
|
|
|
if pulled && alloc.AllocModifyIndex > exist.AllocModifyIndex {
|
|
|
|
result.updated = append(result.updated, allocTuple{exist, alloc})
|
2015-08-23 21:47:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore this
|
|
|
|
result.ignore = append(result.ignore, exist)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan the updated allocations for any that are new
|
2016-02-01 21:57:35 +00:00
|
|
|
for id, pulled := range allocs.pulled {
|
|
|
|
if _, ok := existIdx[id]; !ok {
|
|
|
|
result.added = append(result.added, pulled)
|
2015-08-23 21:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2015-08-20 23:07:26 +00:00
|
|
|
// shuffleStrings randomly shuffles the list of strings
|
|
|
|
func shuffleStrings(list []string) {
|
|
|
|
for i := range list {
|
|
|
|
j := rand.Intn(i + 1)
|
|
|
|
list[i], list[j] = list[j], list[i]
|
|
|
|
}
|
|
|
|
}
|
2015-08-30 01:03:00 +00:00
|
|
|
|
|
|
|
// persistState is used to help with saving state
|
|
|
|
func persistState(path string, data interface{}) error {
|
2017-04-28 20:18:04 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
enc := codec.NewEncoder(&buf, structs.JsonHandlePretty)
|
|
|
|
if err := enc.Encode(data); err != nil {
|
|
|
|
return err
|
2015-08-30 01:03:00 +00:00
|
|
|
}
|
2017-04-28 20:18:04 +00:00
|
|
|
|
2015-08-30 01:03:00 +00:00
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
|
|
|
return fmt.Errorf("failed to make dirs for %s: %v", path, err)
|
|
|
|
}
|
2016-06-27 17:07:33 +00:00
|
|
|
tmpPath := path + ".tmp"
|
2017-04-28 20:18:04 +00:00
|
|
|
if err := ioutil.WriteFile(tmpPath, buf.Bytes(), 0600); err != nil {
|
2016-06-27 17:07:33 +00:00
|
|
|
return fmt.Errorf("failed to save state to tmp: %v", err)
|
|
|
|
}
|
|
|
|
if err := os.Rename(tmpPath, path); err != nil {
|
|
|
|
return fmt.Errorf("failed to rename tmp to path: %v", err)
|
2015-08-30 01:03:00 +00:00
|
|
|
}
|
2016-09-02 00:34:40 +00:00
|
|
|
|
|
|
|
// Sanity check since users have reported empty state files on disk
|
|
|
|
if stat, err := os.Stat(path); err != nil {
|
|
|
|
return fmt.Errorf("unable to stat state file %s: %v", path, err)
|
|
|
|
} else if stat.Size() == 0 {
|
2016-12-01 19:22:34 +00:00
|
|
|
return fmt.Errorf("persisted invalid state file %s; see https://github.com/hashicorp/nomad/issues/1367", path)
|
2016-09-02 00:34:40 +00:00
|
|
|
}
|
2015-08-30 01:03:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-02 20:31:56 +00:00
|
|
|
// pre060RestoreState is used to read back in the persisted state for pre v0.6.0
|
|
|
|
// state
|
|
|
|
func pre060RestoreState(path string, data interface{}) error {
|
2015-08-30 01:03:00 +00:00
|
|
|
buf, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
2017-05-02 20:31:56 +00:00
|
|
|
return err
|
2015-08-30 01:03:00 +00:00
|
|
|
}
|
|
|
|
if err := json.Unmarshal(buf, data); err != nil {
|
|
|
|
return fmt.Errorf("failed to decode state: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|