Files
nomad/client/heartbeatstop_test.go
Tim Gross 48b1b01e69 prevent client deadlock and incorrect timing on stop_on_client_after (#25946)
The `disconnect.stop_on_client_after` feature is implemented as a loop on the
client that's intended to wait on the shortest timeout of all the allocations on
the node and then check whether the interval since the last heartbeat has been
longer than the timeout. It uses a buffered channel of allocations written and
read from the same goroutine to push "stops" from the timeout expiring to the
next pass through the loop. Unfortunately if there are multiple allocations that
need to be stopped in the same timeout event, or even if a previous event has
not yet been dequeued, then sending on the channel will block and the entire
goroutine deadlocks itself.

While fixing this, I also discovered that the `stop_on_client_after` and
heartbeat loops can synchronize in a pathological way that extends the
`stop_on_client_after` window. If a heartbeat fails close to the beginning of
the shortest `stop_on_client_after` window, the loop will end up waiting until
almost 2x the intended wait period.

While fixing both of those issues, I discovered that the existing tests had a
bug such that we were asserting that an allocrunner was being destroyed when it
had already exited.

This commit includes the following:
* Rework the watch loop so that we handle the stops in the same case as the
  timer expiration, rather than using a channel in the method scope.
* Remove the alloc intervals map field from the struct and keep it in the
  method scope, in order to discourage writing racy tests that read its value.
* Reset the timer whenever we receive a heartbeat, which forces the two
  intervals to synchronize correctly.
* Minor refactoring of the disconnect timeout lookup to improve brevity.

Fixes: https://github.com/hashicorp/nomad/issues/24679
Ref: https://hashicorp.atlassian.net/browse/NMD-407
2025-05-29 15:05:33 -04:00

143 lines
3.7 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package client
import (
"errors"
"sync"
"testing"
"time"
"github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
"github.com/hashicorp/nomad/helper/pointer"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/shoenig/test/must"
"github.com/shoenig/test/wait"
)
func TestHeartbeatStop(t *testing.T) {
ci.Parallel(t)
shutdownCh := make(chan struct{})
t.Cleanup(func() { close(shutdownCh) })
destroyers := map[string]*mockAllocRunnerDestroyer{}
stopper := newHeartbeatStop(func(id string) (interfaces.AllocRunner, error) {
return destroyers[id], nil
},
time.Hour, // start grace, ignored in this test
testlog.HCLogger(t),
shutdownCh)
// an allocation, with a tiny lease
alloc1 := &structs.Allocation{
ID: uuid.Generate(),
TaskGroup: "foo",
Job: &structs.Job{
TaskGroups: []*structs.TaskGroup{
{
Name: "foo",
Disconnect: &structs.DisconnectStrategy{
StopOnClientAfter: pointer.Of(time.Microsecond),
},
},
},
},
}
// an alloc with a longer lease
alloc2 := alloc1.Copy()
alloc2.ID = uuid.Generate()
alloc2.Job.TaskGroups[0].Disconnect.StopOnClientAfter = pointer.Of(500 * time.Millisecond)
// an alloc with no disconnect config
alloc3 := alloc1.Copy()
alloc3.ID = uuid.Generate()
alloc3.Job.TaskGroups[0].Disconnect = nil
destroyers[alloc1.ID] = &mockAllocRunnerDestroyer{}
destroyers[alloc2.ID] = &mockAllocRunnerDestroyer{}
destroyers[alloc3.ID] = &mockAllocRunnerDestroyer{}
go stopper.watch()
stopper.allocHook(alloc1)
stopper.allocHook(alloc2)
stopper.allocHook(alloc3)
must.Wait(t, wait.InitialSuccess(
wait.Timeout(time.Second),
wait.Gap(10*time.Millisecond),
wait.ErrorFunc(func() error {
if destroyers[alloc1.ID].checkCalls() != 1 {
return errors.New("first alloc was not destroyed as expected")
}
if destroyers[alloc2.ID].checkCalls() != 0 {
return errors.New("second alloc was unexpectedly destroyed")
}
if destroyers[alloc3.ID].checkCalls() != 0 {
return errors.New("third alloc should never be destroyed")
}
return nil
})))
// send a heartbeat and make sure nothing changes
stopper.setLastOk(time.Now())
must.Wait(t, wait.ContinualSuccess(
wait.Timeout(200*time.Millisecond),
wait.Gap(10*time.Millisecond),
wait.ErrorFunc(func() error {
if destroyers[alloc1.ID].checkCalls() != 1 {
return errors.New("first alloc should no longer be tracked")
}
if destroyers[alloc2.ID].checkCalls() != 0 {
return errors.New("second alloc was unexpectedly destroyed")
}
if destroyers[alloc3.ID].checkCalls() != 0 {
return errors.New("third alloc should never be destroyed")
}
return nil
})))
// skip the next heartbeat
must.Wait(t, wait.InitialSuccess(
wait.Timeout(1*time.Second),
wait.Gap(10*time.Millisecond),
wait.ErrorFunc(func() error {
if destroyers[alloc1.ID].checkCalls() != 1 {
return errors.New("first alloc should no longer be tracked")
}
if destroyers[alloc2.ID].checkCalls() != 1 {
return errors.New("second alloc should have been destroyed")
}
if destroyers[alloc3.ID].checkCalls() != 0 {
return errors.New("third alloc should never be destroyed")
}
return nil
})))
}
type mockAllocRunnerDestroyer struct {
callsLock sync.Mutex
calls int
interfaces.AllocRunner
}
func (ar *mockAllocRunnerDestroyer) checkCalls() int {
ar.callsLock.Lock()
defer ar.callsLock.Unlock()
return ar.calls
}
func (ar *mockAllocRunnerDestroyer) Destroy() {
ar.callsLock.Lock()
defer ar.callsLock.Unlock()
ar.calls++
}