-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
health: detect missing task checks #7366
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,267 @@ | ||
package allochealth | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
consulapi "github.com/hashicorp/consul/api" | ||
"github.com/hashicorp/nomad/client/consul" | ||
cstructs "github.com/hashicorp/nomad/client/structs" | ||
agentconsul "github.com/hashicorp/nomad/command/agent/consul" | ||
"github.com/hashicorp/nomad/helper/testlog" | ||
"github.com/hashicorp/nomad/nomad/mock" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/hashicorp/nomad/testutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTracker_Checks_Healthy(t *testing.T) { | ||
t.Parallel() | ||
|
||
alloc := mock.Alloc() | ||
alloc.Job.TaskGroups[0].Migrate.MinHealthyTime = 1 // let's speed things up | ||
task := alloc.Job.TaskGroups[0].Tasks[0] | ||
|
||
// Synthesize running alloc and tasks | ||
alloc.ClientStatus = structs.AllocClientStatusRunning | ||
alloc.TaskStates = map[string]*structs.TaskState{ | ||
task.Name: { | ||
State: structs.TaskStateRunning, | ||
StartedAt: time.Now(), | ||
}, | ||
} | ||
|
||
// Make Consul response | ||
check := &consulapi.AgentCheck{ | ||
Name: task.Services[0].Checks[0].Name, | ||
Status: consulapi.HealthPassing, | ||
} | ||
taskRegs := map[string]*agentconsul.ServiceRegistrations{ | ||
task.Name: { | ||
Services: map[string]*agentconsul.ServiceRegistration{ | ||
task.Services[0].Name: { | ||
Service: &consulapi.AgentService{ | ||
ID: "foo", | ||
Service: task.Services[0].Name, | ||
}, | ||
Checks: []*consulapi.AgentCheck{check}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
logger := testlog.HCLogger(t) | ||
b := cstructs.NewAllocBroadcaster(logger) | ||
defer b.Close() | ||
|
||
// Don't reply on the first call | ||
var called uint64 | ||
consul := consul.NewMockConsulServiceClient(t, logger) | ||
consul.AllocRegistrationsFn = func(string) (*agentconsul.AllocRegistration, error) { | ||
if atomic.AddUint64(&called, 1) == 1 { | ||
return nil, nil | ||
} | ||
|
||
reg := &agentconsul.AllocRegistration{ | ||
Tasks: taskRegs, | ||
} | ||
|
||
return reg, nil | ||
} | ||
|
||
ctx, cancelFn := context.WithCancel(context.Background()) | ||
defer cancelFn() | ||
|
||
checkInterval := 10 * time.Millisecond | ||
tracker := NewTracker(ctx, logger, alloc, b.Listen(), consul, | ||
time.Millisecond, true) | ||
tracker.checkLookupInterval = checkInterval | ||
tracker.Start() | ||
|
||
select { | ||
case <-time.After(4 * checkInterval): | ||
require.Fail(t, "timed out while waiting for health") | ||
case h := <-tracker.HealthyCh(): | ||
require.True(t, h) | ||
} | ||
} | ||
|
||
func TestTracker_Checks_Unhealthy(t *testing.T) { | ||
t.Parallel() | ||
|
||
alloc := mock.Alloc() | ||
alloc.Job.TaskGroups[0].Migrate.MinHealthyTime = 1 // let's speed things up | ||
task := alloc.Job.TaskGroups[0].Tasks[0] | ||
|
||
newCheck := task.Services[0].Checks[0].Copy() | ||
newCheck.Name = "failing-check" | ||
task.Services[0].Checks = append(task.Services[0].Checks, newCheck) | ||
|
||
// Synthesize running alloc and tasks | ||
alloc.ClientStatus = structs.AllocClientStatusRunning | ||
alloc.TaskStates = map[string]*structs.TaskState{ | ||
task.Name: { | ||
State: structs.TaskStateRunning, | ||
StartedAt: time.Now(), | ||
}, | ||
} | ||
|
||
// Make Consul response | ||
checkHealthy := &consulapi.AgentCheck{ | ||
Name: task.Services[0].Checks[0].Name, | ||
Status: consulapi.HealthPassing, | ||
} | ||
checksUnhealthy := &consulapi.AgentCheck{ | ||
Name: task.Services[0].Checks[1].Name, | ||
Status: consulapi.HealthCritical, | ||
} | ||
taskRegs := map[string]*agentconsul.ServiceRegistrations{ | ||
task.Name: { | ||
Services: map[string]*agentconsul.ServiceRegistration{ | ||
task.Services[0].Name: { | ||
Service: &consulapi.AgentService{ | ||
ID: "foo", | ||
Service: task.Services[0].Name, | ||
}, | ||
Checks: []*consulapi.AgentCheck{checkHealthy, checksUnhealthy}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
logger := testlog.HCLogger(t) | ||
b := cstructs.NewAllocBroadcaster(logger) | ||
defer b.Close() | ||
|
||
// Don't reply on the first call | ||
var called uint64 | ||
consul := consul.NewMockConsulServiceClient(t, logger) | ||
consul.AllocRegistrationsFn = func(string) (*agentconsul.AllocRegistration, error) { | ||
if atomic.AddUint64(&called, 1) == 1 { | ||
return nil, nil | ||
} | ||
|
||
reg := &agentconsul.AllocRegistration{ | ||
Tasks: taskRegs, | ||
} | ||
|
||
return reg, nil | ||
} | ||
|
||
ctx, cancelFn := context.WithCancel(context.Background()) | ||
defer cancelFn() | ||
|
||
checkInterval := 10 * time.Millisecond | ||
tracker := NewTracker(ctx, logger, alloc, b.Listen(), consul, | ||
time.Millisecond, true) | ||
tracker.checkLookupInterval = checkInterval | ||
tracker.Start() | ||
|
||
testutil.WaitForResult(func() (bool, error) { | ||
lookup := atomic.LoadUint64(&called) | ||
return lookup < 4, fmt.Errorf("wait to get more task registration lookups: %v", lookup) | ||
}, func(err error) { | ||
require.NoError(t, err) | ||
}) | ||
|
||
tracker.l.Lock() | ||
require.False(t, tracker.checksHealthy) | ||
tracker.l.Unlock() | ||
|
||
select { | ||
case v := <-tracker.HealthyCh(): | ||
require.Failf(t, "expected no health value", " got %v", v) | ||
default: | ||
// good | ||
} | ||
} | ||
|
||
func TestTracker_Checks_Missing(t *testing.T) { | ||
t.Parallel() | ||
|
||
alloc := mock.Alloc() | ||
alloc.Job.TaskGroups[0].Migrate.MinHealthyTime = 1 // let's speed things up | ||
task := alloc.Job.TaskGroups[0].Tasks[0] | ||
|
||
newCheck := task.Services[0].Checks[0].Copy() | ||
newCheck.Name = "failing-check" | ||
task.Services[0].Checks = append(task.Services[0].Checks, newCheck) | ||
|
||
// Synthesize running alloc and tasks | ||
alloc.ClientStatus = structs.AllocClientStatusRunning | ||
alloc.TaskStates = map[string]*structs.TaskState{ | ||
task.Name: { | ||
State: structs.TaskStateRunning, | ||
StartedAt: time.Now(), | ||
}, | ||
} | ||
|
||
// Make Consul response | ||
checkHealthy := &consulapi.AgentCheck{ | ||
Name: task.Services[0].Checks[0].Name, | ||
Status: consulapi.HealthPassing, | ||
} | ||
taskRegs := map[string]*agentconsul.ServiceRegistrations{ | ||
task.Name: { | ||
Services: map[string]*agentconsul.ServiceRegistration{ | ||
task.Services[0].Name: { | ||
Service: &consulapi.AgentService{ | ||
ID: "foo", | ||
Service: task.Services[0].Name, | ||
}, | ||
// notice missing check | ||
Checks: []*consulapi.AgentCheck{checkHealthy}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
logger := testlog.HCLogger(t) | ||
b := cstructs.NewAllocBroadcaster(logger) | ||
defer b.Close() | ||
|
||
// Don't reply on the first call | ||
var called uint64 | ||
consul := consul.NewMockConsulServiceClient(t, logger) | ||
consul.AllocRegistrationsFn = func(string) (*agentconsul.AllocRegistration, error) { | ||
if atomic.AddUint64(&called, 1) == 1 { | ||
return nil, nil | ||
} | ||
|
||
reg := &agentconsul.AllocRegistration{ | ||
Tasks: taskRegs, | ||
} | ||
|
||
return reg, nil | ||
} | ||
|
||
ctx, cancelFn := context.WithCancel(context.Background()) | ||
defer cancelFn() | ||
|
||
checkInterval := 10 * time.Millisecond | ||
tracker := NewTracker(ctx, logger, alloc, b.Listen(), consul, | ||
time.Millisecond, true) | ||
tracker.checkLookupInterval = checkInterval | ||
tracker.Start() | ||
|
||
testutil.WaitForResult(func() (bool, error) { | ||
lookup := atomic.LoadUint64(&called) | ||
return lookup < 4, fmt.Errorf("wait to get more task registration lookups: %v", lookup) | ||
}, func(err error) { | ||
require.NoError(t, err) | ||
}) | ||
|
||
tracker.l.Lock() | ||
require.False(t, tracker.checksHealthy) | ||
tracker.l.Unlock() | ||
|
||
select { | ||
case v := <-tracker.HealthyCh(): | ||
require.Failf(t, "expected no health value", " got %v", v) | ||
default: | ||
// good | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this logic work when it is a migration for a batch job:
nomad/client/allocrunner/health_hook.go
Line 253 in c346a47
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe so - the assumption this depends on that checks are unique and that job definition has a 1 to 1 mapping with consul agent check registration, regardless of exact parameter or minHealthyTime. This needs to be tweaked for task group level checks, but will address that in a follow up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry should have added more for my thinking. If it is a batch job, the tasks can finish and then the number of checks that will be registered in Consul will be less than those defined in the job spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah - excellent point. Going to drawing board a bit to think this through a bit more. Not sure if I fully understand expectations of service health checks in batch - but it's definitely a problem for service jobs with init containers with task lifecycle/dependencies features. Thanks for raising it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked - and looks like batch jobs don't support migrate or update/deployments [1] - do you know when this code is relevant for batch by any chance?
[1] https://github.com/hashicorp/nomad/blob/v0.10.4/nomad/structs/structs.go#L4997-L5021
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry for the noise! Thought batch supported migrate!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:) You helped found a bug though - addressed it with a different approach in #7383 .