9ed75e1f72
When client nodes are restarted, all allocations that have been scheduled on the node have their modify index updated, including terminal allocations. There are several contributing factors: * The `allocSync` method that updates the servers isn't gated on first contact with the servers. This means that if a server updates the desired state while the client is down, the `allocSync` races with the `Node.ClientGetAlloc` RPC. This will typically result in the client updating the server with "running" and then immediately thereafter "complete". * The `allocSync` method unconditionally sends the `Node.UpdateAlloc` RPC even if it's possible to assert that the server has definitely seen the client state. The allocrunner may queue-up updates even if we gate sending them. So then we end up with a race between the allocrunner updating its internal state to overwrite the previous update and `allocSync` sending the bogus or duplicate update. This changeset adds tracking of server-acknowledged state to the allocrunner. This state gets checked in the `allocSync` before adding the update to the batch, and updated when `Node.UpdateAlloc` returns successfully. To implement this we need to be able to equality-check the updates against the last acknowledged state. We also need to add the last acknowledged state to the client state DB, otherwise we'd drop unacknowledged updates across restarts. The client restart test has been expanded to cover a variety of allocation states, including allocs stopped before shutdown, allocs stopped by the server while the client is down, and allocs that have been completely GC'd on the server while the client is down. I've also bench tested scenarios where the task workload is killed while the client is down, resulting in a failed restore. Fixes #16381
81 lines
2 KiB
Go
81 lines
2 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package client
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"time"
|
|
|
|
"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 (note: AllocModifyIndex is only updated for
|
|
// server updates)
|
|
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]
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|