2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-08-20 23:07:26 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-08-23 21:54:52 +00:00
|
|
|
"math/rand"
|
2019-04-19 13:12:50 +00:00
|
|
|
"time"
|
2015-08-23 21:47:51 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-08-20 23:07:26 +00:00
|
|
|
)
|
|
|
|
|
2015-08-23 21:47:51 +00:00
|
|
|
// diffResult is used to return the sets that result from a diff
|
|
|
|
type diffResult struct {
|
|
|
|
added []*structs.Allocation
|
2018-06-29 00:01:05 +00:00
|
|
|
removed []string
|
|
|
|
updated []*structs.Allocation
|
|
|
|
ignore []string
|
2015-08-23 21:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2018-06-29 00:01:05 +00:00
|
|
|
func diffAllocs(existing map[string]uint64, 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{}
|
2018-06-29 00:01:05 +00:00
|
|
|
for existID, existIndex := range existing {
|
2016-02-01 21:57:35 +00:00
|
|
|
// Check if the alloc was updated or filtered because an update wasn't
|
|
|
|
// needed.
|
2018-06-29 00:01:05 +00:00
|
|
|
alloc, pulled := allocs.pulled[existID]
|
|
|
|
_, filtered := allocs.filtered[existID]
|
2015-08-23 21:47:51 +00:00
|
|
|
|
2016-02-01 21:57:35 +00:00
|
|
|
// If not updated or filtered, removed
|
2020-06-19 19:39:44 +00:00
|
|
|
if !pulled && !filtered {
|
2018-06-29 00:01:05 +00:00
|
|
|
result.removed = append(result.removed, existID)
|
2015-08-23 21:47:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-05-11 13:05:24 +00:00
|
|
|
// Check for an update (note: AllocModifyIndex is only updated for
|
|
|
|
// server updates)
|
2018-06-29 00:01:05 +00:00
|
|
|
if pulled && alloc.AllocModifyIndex > existIndex {
|
|
|
|
result.updated = append(result.updated, alloc)
|
2015-08-23 21:47:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore this
|
2018-06-29 00:01:05 +00:00
|
|
|
result.ignore = append(result.ignore, existID)
|
2015-08-23 21:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan the updated allocations for any that are new
|
2016-02-01 21:57:35 +00:00
|
|
|
for id, pulled := range allocs.pulled {
|
2018-06-29 00:01:05 +00:00
|
|
|
if _, ok := existing[id]; !ok {
|
2016-02-01 21:57:35 +00:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
2019-04-19 13:12:50 +00:00
|
|
|
|
|
|
|
// stoppedTimer returns a timer that's stopped and wouldn't fire until
|
|
|
|
// it's reset
|
|
|
|
func stoppedTimer() *time.Timer {
|
|
|
|
timer := time.NewTimer(0)
|
|
|
|
if !timer.Stop() {
|
|
|
|
<-timer.C
|
|
|
|
}
|
|
|
|
return timer
|
|
|
|
}
|