open-nomad/client/util.go
Michael Schurter 0f7dcfdc9a example redis job "runs" on arv2! see below
Tons left to do and lots of churn:
1. No state saving
2. No shutdown or gc
3. Removed AR factory *for now*
4. Made all "Config" structs local to the package they configure
5. Added allocID to GC to avoid a lookup

Really hating how many things use *structs.Allocation. It's not bad
without state saving, but if AllocRunner starts updating its copy things
get racy fast.
2018-10-16 16:53:29 -07:00

66 lines
1.6 KiB
Go

package client
import (
"fmt"
"math/rand"
"github.com/hashicorp/nomad/nomad/structs"
)
// diffResult is used to return the sets that result from a diff
type diffResult struct {
added []*structs.Allocation
removed []string
updated []*structs.Allocation
ignore []string
}
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.
func diffAllocs(existing map[string]uint64, allocs *allocUpdates) *diffResult {
// Scan the existing allocations
result := &diffResult{}
for existID, existIndex := range existing {
// Check if the alloc was updated or filtered because an update wasn't
// needed.
alloc, pulled := allocs.pulled[existID]
_, filtered := allocs.filtered[existID]
// If not updated or filtered, removed
if !pulled && !filtered {
result.removed = append(result.removed, existID)
continue
}
// Check for an update
if pulled && alloc.AllocModifyIndex > existIndex {
result.updated = append(result.updated, alloc)
continue
}
// Ignore this
result.ignore = append(result.ignore, existID)
}
// Scan the updated allocations for any that are new
for id, pulled := range allocs.pulled {
if _, ok := existing[id]; !ok {
result.added = append(result.added, pulled)
}
}
return result
}
// 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]
}
}