2022-09-09 17:47:22 +00:00
|
|
|
package serviceregistration
|
2017-09-13 06:15:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-09-14 22:17:21 +00:00
|
|
|
"fmt"
|
2019-07-18 15:22:14 +00:00
|
|
|
"sync"
|
2017-09-13 06:15:46 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-04-17 19:36:50 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2017-09-13 06:15:46 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2019-07-18 15:22:14 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2022-09-09 17:47:22 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2017-09-13 06:15:46 +00:00
|
|
|
)
|
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
// restartRecord is used by a fakeWorkloadRestarter to record when restarts occur
|
2017-09-14 22:17:21 +00:00
|
|
|
// due to a watched check.
|
2022-09-09 17:47:22 +00:00
|
|
|
type restartRecord struct {
|
2017-09-14 22:17:21 +00:00
|
|
|
timestamp time.Time
|
|
|
|
source string
|
|
|
|
reason string
|
|
|
|
failure bool
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
// fakeWorkloadRestarter is a test implementation of TaskRestarter.
|
|
|
|
type fakeWorkloadRestarter struct {
|
2017-09-14 22:17:21 +00:00
|
|
|
// restarts is a slice of all of the restarts triggered by the checkWatcher
|
2022-09-09 17:47:22 +00:00
|
|
|
restarts []restartRecord
|
2017-09-14 22:17:21 +00:00
|
|
|
|
|
|
|
// need the checkWatcher to re-Watch restarted tasks like TaskRunner
|
2022-09-09 17:47:22 +00:00
|
|
|
watcher *CheckWatcher
|
2017-09-14 22:17:21 +00:00
|
|
|
|
|
|
|
// check to re-Watch on restarts
|
|
|
|
check *structs.ServiceCheck
|
|
|
|
allocID string
|
|
|
|
taskName string
|
|
|
|
checkName string
|
2019-07-18 15:22:14 +00:00
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
lock sync.Mutex
|
2017-09-14 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
// newFakeCheckRestart creates a new mock WorkloadRestarter.
|
|
|
|
func newFakeWorkloadRestarter(w *CheckWatcher, allocID, taskName, checkName string, c *structs.ServiceCheck) *fakeWorkloadRestarter {
|
|
|
|
return &fakeWorkloadRestarter{
|
2017-09-14 22:17:21 +00:00
|
|
|
watcher: w,
|
|
|
|
check: c,
|
|
|
|
allocID: allocID,
|
|
|
|
taskName: taskName,
|
|
|
|
checkName: checkName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
// Restart implements part of the TaskRestarter interface needed for check watching
|
|
|
|
// and is normally fulfilled by a TaskRunner.
|
2017-09-14 22:17:21 +00:00
|
|
|
//
|
|
|
|
// Restarts are recorded in the []restarts field and re-Watch the check.
|
2022-09-09 17:47:22 +00:00
|
|
|
func (c *fakeWorkloadRestarter) Restart(_ context.Context, event *structs.TaskEvent, failure bool) error {
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
|
|
restart := restartRecord{
|
2018-10-05 02:36:40 +00:00
|
|
|
timestamp: time.Now(),
|
|
|
|
source: event.Type,
|
|
|
|
reason: event.DisplayMessage,
|
|
|
|
failure: failure,
|
|
|
|
}
|
|
|
|
c.restarts = append(c.restarts, restart)
|
2017-09-14 22:17:21 +00:00
|
|
|
|
|
|
|
// Re-Watch the check just like TaskRunner
|
|
|
|
c.watcher.Watch(c.allocID, c.taskName, c.checkName, c.check, c)
|
2018-10-05 02:36:40 +00:00
|
|
|
return nil
|
2017-09-14 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
// String is useful for debugging.
|
|
|
|
func (c *fakeWorkloadRestarter) String() string {
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2019-07-18 15:22:14 +00:00
|
|
|
|
2017-09-14 22:17:21 +00:00
|
|
|
s := fmt.Sprintf("%s %s %s restarts:\n", c.allocID, c.taskName, c.checkName)
|
|
|
|
for _, r := range c.restarts {
|
|
|
|
s += fmt.Sprintf("%s - %s: %s (failure: %t)\n", r.timestamp, r.source, r.reason, r.failure)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
// GetRestarts for testing in a thread-safe way
|
|
|
|
func (c *fakeWorkloadRestarter) GetRestarts() []restartRecord {
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2019-07-18 15:22:14 +00:00
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
o := make([]restartRecord, len(c.restarts))
|
2019-07-18 15:22:14 +00:00
|
|
|
copy(o, c.restarts)
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
// response is a response returned by fakeCheckStatusGetter after a certain time
|
|
|
|
type response struct {
|
2017-09-13 06:15:46 +00:00
|
|
|
at time.Time
|
|
|
|
id string
|
|
|
|
status string
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
// fakeCheckStatusGetter is a mock implementation of CheckStatusGetter
|
|
|
|
type fakeCheckStatusGetter struct {
|
|
|
|
lock sync.Mutex
|
|
|
|
responses map[string][]response
|
2017-09-13 06:15:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
func (g *fakeCheckStatusGetter) Get() (map[string]CheckStatus, error) {
|
|
|
|
g.lock.Lock()
|
|
|
|
defer g.lock.Unlock()
|
2017-09-13 06:15:46 +00:00
|
|
|
|
|
|
|
now := time.Now()
|
2022-09-09 17:47:22 +00:00
|
|
|
result := make(map[string]CheckStatus)
|
|
|
|
// use the newest response after now for the response
|
|
|
|
for k, vs := range g.responses {
|
2017-09-13 06:15:46 +00:00
|
|
|
for _, v := range vs {
|
|
|
|
if v.at.After(now) {
|
|
|
|
break
|
|
|
|
}
|
2022-09-09 17:47:22 +00:00
|
|
|
result[k] = CheckStatus{Status: v.status}
|
2017-09-13 06:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
func (g *fakeCheckStatusGetter) add(checkID, status string, at time.Time) {
|
|
|
|
g.lock.Lock()
|
|
|
|
defer g.lock.Unlock()
|
|
|
|
if g.responses == nil {
|
|
|
|
g.responses = make(map[string][]response)
|
|
|
|
}
|
|
|
|
g.responses[checkID] = append(g.responses[checkID], response{at, checkID, status})
|
2017-09-13 06:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testCheck() *structs.ServiceCheck {
|
|
|
|
return &structs.ServiceCheck{
|
|
|
|
Name: "testcheck",
|
|
|
|
Interval: 100 * time.Millisecond,
|
|
|
|
Timeout: 100 * time.Millisecond,
|
|
|
|
CheckRestart: &structs.CheckRestart{
|
|
|
|
Limit: 3,
|
|
|
|
Grace: 100 * time.Millisecond,
|
|
|
|
IgnoreWarnings: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
// testWatcherSetup sets up a fakeChecksAPI and a real checkWatcher with a test
|
|
|
|
// logger and faster poll frequency.
|
|
|
|
func testWatcherSetup(t *testing.T) (*fakeCheckStatusGetter, *CheckWatcher) {
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
getter := new(fakeCheckStatusGetter)
|
|
|
|
cw := NewCheckWatcher(logger, getter)
|
|
|
|
cw.pollFrequency = 10 * time.Millisecond
|
|
|
|
return getter, cw
|
|
|
|
}
|
|
|
|
|
|
|
|
func before() time.Time {
|
|
|
|
return time.Now().Add(-10 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCheckWatcher_SkipUnwatched asserts unwatched checks are ignored.
|
|
|
|
func TestCheckWatcher_SkipUnwatched(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-09-13 06:15:46 +00:00
|
|
|
|
|
|
|
// Create a check with restarting disabled
|
|
|
|
check := testCheck()
|
|
|
|
check.CheckRestart = nil
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
2022-09-09 17:47:22 +00:00
|
|
|
getter := new(fakeCheckStatusGetter)
|
2021-03-16 18:22:21 +00:00
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
cw := NewCheckWatcher(logger, getter)
|
|
|
|
restarter1 := newFakeWorkloadRestarter(cw, "testalloc1", "testtask1", "testcheck1", check)
|
2017-09-13 06:15:46 +00:00
|
|
|
cw.Watch("testalloc1", "testtask1", "testcheck1", check, restarter1)
|
|
|
|
|
|
|
|
// Check should have been dropped as it's not watched
|
2022-09-09 17:47:22 +00:00
|
|
|
enqueued := len(cw.checkUpdateCh)
|
|
|
|
must.Zero(t, enqueued, must.Sprintf("expected 0 checks to be enqueued for watching but found %d", enqueued))
|
2017-09-13 06:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestCheckWatcher_Healthy asserts healthy tasks are not restarted.
|
|
|
|
func TestCheckWatcher_Healthy(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-09-13 06:15:46 +00:00
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
now := before()
|
|
|
|
getter, cw := testWatcherSetup(t)
|
|
|
|
|
|
|
|
// Make both checks healthy from the beginning
|
|
|
|
getter.add("testcheck1", "passing", now)
|
|
|
|
getter.add("testcheck2", "passing", now)
|
2017-09-13 06:15:46 +00:00
|
|
|
|
|
|
|
check1 := testCheck()
|
2022-09-09 17:47:22 +00:00
|
|
|
restarter1 := newFakeWorkloadRestarter(cw, "testalloc1", "testtask1", "testcheck1", check1)
|
2017-09-13 06:15:46 +00:00
|
|
|
cw.Watch("testalloc1", "testtask1", "testcheck1", check1, restarter1)
|
|
|
|
|
|
|
|
check2 := testCheck()
|
|
|
|
check2.CheckRestart.Limit = 1
|
|
|
|
check2.CheckRestart.Grace = 0
|
2022-09-09 17:47:22 +00:00
|
|
|
restarter2 := newFakeWorkloadRestarter(cw, "testalloc2", "testtask2", "testcheck2", check2)
|
2017-09-13 06:15:46 +00:00
|
|
|
cw.Watch("testalloc2", "testtask2", "testcheck2", check2, restarter2)
|
|
|
|
|
2017-09-14 06:13:34 +00:00
|
|
|
// Run
|
2017-09-14 05:42:43 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
2017-09-13 06:15:46 +00:00
|
|
|
defer cancel()
|
|
|
|
cw.Run(ctx)
|
|
|
|
|
2017-09-14 06:13:34 +00:00
|
|
|
// Ensure restart was never called
|
2022-09-09 17:47:22 +00:00
|
|
|
must.Empty(t, restarter1.restarts, must.Sprint("expected check 1 to not be restarted"))
|
|
|
|
must.Empty(t, restarter2.restarts, must.Sprint("expected check 2 to not be restarted"))
|
2017-09-13 06:15:46 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 22:22:21 +00:00
|
|
|
// TestCheckWatcher_Unhealthy asserts unhealthy tasks are restarted exactly once.
|
|
|
|
func TestCheckWatcher_Unhealthy(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-07-17 22:22:21 +00:00
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
now := before()
|
|
|
|
getter, cw := testWatcherSetup(t)
|
|
|
|
|
|
|
|
// Check has always been failing
|
|
|
|
getter.add("testcheck1", "critical", now)
|
2019-07-17 22:22:21 +00:00
|
|
|
|
|
|
|
check1 := testCheck()
|
2022-09-09 17:47:22 +00:00
|
|
|
restarter1 := newFakeWorkloadRestarter(cw, "testalloc1", "testtask1", "testcheck1", check1)
|
2019-07-17 22:22:21 +00:00
|
|
|
cw.Watch("testalloc1", "testtask1", "testcheck1", check1, restarter1)
|
|
|
|
|
|
|
|
// Run
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
cw.Run(ctx)
|
|
|
|
|
|
|
|
// Ensure restart was called exactly once
|
2022-09-09 17:47:22 +00:00
|
|
|
must.Len(t, 1, restarter1.restarts, must.Sprint("expected check to be restarted once"))
|
2019-07-17 22:22:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 06:15:46 +00:00
|
|
|
// TestCheckWatcher_HealthyWarning asserts checks in warning with
|
|
|
|
// ignore_warnings=true do not restart tasks.
|
|
|
|
func TestCheckWatcher_HealthyWarning(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-09-13 06:15:46 +00:00
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
now := before()
|
|
|
|
getter, cw := testWatcherSetup(t)
|
|
|
|
|
|
|
|
// Check is always in warning but that's ok
|
|
|
|
getter.add("testcheck1", "warning", now)
|
2017-09-13 06:15:46 +00:00
|
|
|
|
|
|
|
check1 := testCheck()
|
|
|
|
check1.CheckRestart.Limit = 1
|
|
|
|
check1.CheckRestart.Grace = 0
|
|
|
|
check1.CheckRestart.IgnoreWarnings = true
|
2022-09-09 17:47:22 +00:00
|
|
|
restarter1 := newFakeWorkloadRestarter(cw, "testalloc1", "testtask1", "testcheck1", check1)
|
2017-09-13 06:15:46 +00:00
|
|
|
cw.Watch("testalloc1", "testtask1", "testcheck1", check1, restarter1)
|
|
|
|
|
2017-09-14 06:13:34 +00:00
|
|
|
// Run
|
2017-09-14 05:42:43 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
|
2017-09-13 06:15:46 +00:00
|
|
|
defer cancel()
|
|
|
|
cw.Run(ctx)
|
|
|
|
|
|
|
|
// Ensure restart was never called on check 1
|
2022-09-09 17:47:22 +00:00
|
|
|
must.Empty(t, restarter1.restarts, must.Sprint("expected check 1 to not be restarted"))
|
2017-09-13 06:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestCheckWatcher_Flapping asserts checks that flap from healthy to unhealthy
|
|
|
|
// before the unhealthy limit is reached do not restart tasks.
|
|
|
|
func TestCheckWatcher_Flapping(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-09-13 06:15:46 +00:00
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
getter, cw := testWatcherSetup(t)
|
2017-09-13 06:15:46 +00:00
|
|
|
|
|
|
|
check1 := testCheck()
|
|
|
|
check1.CheckRestart.Grace = 0
|
2022-09-09 17:47:22 +00:00
|
|
|
restarter1 := newFakeWorkloadRestarter(cw, "testalloc1", "testtask1", "testcheck1", check1)
|
2017-09-13 06:15:46 +00:00
|
|
|
cw.Watch("testalloc1", "testtask1", "testcheck1", check1, restarter1)
|
|
|
|
|
|
|
|
// Check flaps and is never failing for the full 200ms needed to restart
|
|
|
|
now := time.Now()
|
2022-09-09 17:47:22 +00:00
|
|
|
getter.add("testcheck1", "passing", now)
|
|
|
|
getter.add("testcheck1", "critical", now.Add(100*time.Millisecond))
|
|
|
|
getter.add("testcheck1", "passing", now.Add(250*time.Millisecond))
|
|
|
|
getter.add("testcheck1", "critical", now.Add(300*time.Millisecond))
|
|
|
|
getter.add("testcheck1", "passing", now.Add(450*time.Millisecond))
|
2017-09-13 06:15:46 +00:00
|
|
|
|
2017-09-14 05:42:43 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 600*time.Millisecond)
|
2017-09-13 06:15:46 +00:00
|
|
|
defer cancel()
|
|
|
|
cw.Run(ctx)
|
|
|
|
|
|
|
|
// Ensure restart was never called on check 1
|
2022-09-09 17:47:22 +00:00
|
|
|
must.Empty(t, restarter1.restarts, must.Sprint("expected check 1 to not be restarted"))
|
2017-09-13 06:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestCheckWatcher_Unwatch asserts unwatching checks prevents restarts.
|
|
|
|
func TestCheckWatcher_Unwatch(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-09-13 06:15:46 +00:00
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
now := before()
|
|
|
|
getter, cw := testWatcherSetup(t)
|
|
|
|
|
|
|
|
// Always failing
|
|
|
|
getter.add("testcheck1", "critical", now)
|
2017-09-13 06:15:46 +00:00
|
|
|
|
|
|
|
// Unwatch immediately
|
|
|
|
check1 := testCheck()
|
|
|
|
check1.CheckRestart.Limit = 1
|
|
|
|
check1.CheckRestart.Grace = 100 * time.Millisecond
|
2022-09-09 17:47:22 +00:00
|
|
|
restarter1 := newFakeWorkloadRestarter(cw, "testalloc1", "testtask1", "testcheck1", check1)
|
2017-09-13 06:15:46 +00:00
|
|
|
cw.Watch("testalloc1", "testtask1", "testcheck1", check1, restarter1)
|
|
|
|
cw.Unwatch("testcheck1")
|
|
|
|
|
2017-09-14 05:42:43 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond)
|
2017-09-13 06:15:46 +00:00
|
|
|
defer cancel()
|
|
|
|
cw.Run(ctx)
|
|
|
|
|
|
|
|
// Ensure restart was never called on check 1
|
2022-09-09 17:47:22 +00:00
|
|
|
must.Empty(t, restarter1.restarts, must.Sprint("expected check 1 to not be restarted"))
|
2017-09-13 06:15:46 +00:00
|
|
|
}
|
2017-09-14 06:13:34 +00:00
|
|
|
|
|
|
|
// TestCheckWatcher_MultipleChecks asserts that when there are multiple checks
|
|
|
|
// for a single task, all checks should be removed when any of them restart the
|
|
|
|
// task to avoid multiple restarts.
|
|
|
|
func TestCheckWatcher_MultipleChecks(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-09-14 06:13:34 +00:00
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
getter, cw := testWatcherSetup(t)
|
|
|
|
|
|
|
|
// check is critical, 3 passing; should only be 1 net restart
|
|
|
|
now := time.Now()
|
|
|
|
getter.add("testcheck1", "critical", before())
|
|
|
|
getter.add("testcheck1", "passing", now.Add(150*time.Millisecond))
|
|
|
|
getter.add("testcheck2", "critical", before())
|
|
|
|
getter.add("testcheck2", "passing", now.Add(150*time.Millisecond))
|
|
|
|
getter.add("testcheck3", "passing", time.Time{})
|
2017-09-14 06:13:34 +00:00
|
|
|
|
|
|
|
check1 := testCheck()
|
2022-09-09 17:47:22 +00:00
|
|
|
check1.Name = "testcheck1"
|
2017-09-14 06:13:34 +00:00
|
|
|
check1.CheckRestart.Limit = 1
|
2022-09-09 17:47:22 +00:00
|
|
|
restarter1 := newFakeWorkloadRestarter(cw, "testalloc1", "testtask1", "testcheck1", check1)
|
2017-09-14 06:13:34 +00:00
|
|
|
cw.Watch("testalloc1", "testtask1", "testcheck1", check1, restarter1)
|
|
|
|
|
|
|
|
check2 := testCheck()
|
2022-09-09 17:47:22 +00:00
|
|
|
check2.Name = "testcheck2"
|
2017-09-14 06:13:34 +00:00
|
|
|
check2.CheckRestart.Limit = 1
|
2022-09-09 17:47:22 +00:00
|
|
|
restarter2 := newFakeWorkloadRestarter(cw, "testalloc1", "testtask1", "testcheck2", check2)
|
2017-09-14 06:13:34 +00:00
|
|
|
cw.Watch("testalloc1", "testtask1", "testcheck2", check2, restarter2)
|
|
|
|
|
|
|
|
check3 := testCheck()
|
2022-09-09 17:47:22 +00:00
|
|
|
check3.Name = "testcheck3"
|
2017-09-14 06:13:34 +00:00
|
|
|
check3.CheckRestart.Limit = 1
|
2022-09-09 17:47:22 +00:00
|
|
|
restarter3 := newFakeWorkloadRestarter(cw, "testalloc1", "testtask1", "testcheck3", check3)
|
2017-09-14 06:13:34 +00:00
|
|
|
cw.Watch("testalloc1", "testtask1", "testcheck3", check3, restarter3)
|
|
|
|
|
|
|
|
// Run
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
cw.Run(ctx)
|
|
|
|
|
2017-09-14 17:36:32 +00:00
|
|
|
// Ensure that restart was only called once on check 1 or 2. Since
|
|
|
|
// checks are in a map it's random which check triggers the restart
|
|
|
|
// first.
|
|
|
|
if n := len(restarter1.restarts) + len(restarter2.restarts); n != 1 {
|
|
|
|
t.Errorf("expected check 1 & 2 to be restarted 1 time but found %d\ncheck 1:\n%s\ncheck 2:%s",
|
|
|
|
n, restarter1, restarter2)
|
2017-09-14 06:13:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if n := len(restarter3.restarts); n != 0 {
|
|
|
|
t.Errorf("expected check 3 to not be restarted but found %d:\n%s", n, restarter3)
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 15:22:14 +00:00
|
|
|
|
|
|
|
// TestCheckWatcher_Deadlock asserts that check watcher will not deadlock when
|
|
|
|
// attempting to restart a task even if its update queue is full.
|
|
|
|
// https://github.com/hashicorp/nomad/issues/5395
|
|
|
|
func TestCheckWatcher_Deadlock(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-07-18 15:22:14 +00:00
|
|
|
|
2022-09-09 17:47:22 +00:00
|
|
|
getter, cw := testWatcherSetup(t)
|
2019-07-18 15:22:14 +00:00
|
|
|
|
|
|
|
// If TR.Restart blocks, restarting len(checkUpdateCh)+1 checks causes
|
|
|
|
// a deadlock due to checkWatcher.Run being blocked in
|
|
|
|
// checkRestart.apply and unable to process updates from the chan!
|
|
|
|
n := cap(cw.checkUpdateCh) + 1
|
|
|
|
checks := make([]*structs.ServiceCheck, n)
|
2022-09-09 17:47:22 +00:00
|
|
|
restarters := make([]*fakeWorkloadRestarter, n)
|
2019-07-18 15:22:14 +00:00
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
c := testCheck()
|
2022-09-09 17:47:22 +00:00
|
|
|
r := newFakeWorkloadRestarter(cw,
|
2019-07-18 15:22:14 +00:00
|
|
|
fmt.Sprintf("alloc%d", i),
|
|
|
|
fmt.Sprintf("task%d", i),
|
|
|
|
fmt.Sprintf("check%d", i),
|
|
|
|
c,
|
|
|
|
)
|
|
|
|
checks[i] = c
|
|
|
|
restarters[i] = r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
go cw.Run(ctx)
|
|
|
|
|
|
|
|
// Watch
|
|
|
|
for _, r := range restarters {
|
|
|
|
cw.Watch(r.allocID, r.taskName, r.checkName, r.check, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make them all fail
|
|
|
|
for _, r := range restarters {
|
2022-09-09 17:47:22 +00:00
|
|
|
getter.add(r.checkName, "critical", time.Time{})
|
2019-07-18 15:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that restart was called exactly once on all checks
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
for _, r := range restarters {
|
|
|
|
if n := len(r.GetRestarts()); n != 1 {
|
|
|
|
return false, fmt.Errorf("expected 1 restart but found %d", n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
2022-09-09 17:47:22 +00:00
|
|
|
must.NoError(t, err)
|
2019-07-18 15:22:14 +00:00
|
|
|
})
|
|
|
|
}
|