-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Add CLI and API support for forcing rescheduling of failed allocs #4274
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
242cc19
Work in progress - force rescheduling of failed allocs
preetapan 3b7d23f
Fix logic inversion in force rescheduling
preetapan 268a99e
Add unit tests for forced rescheduling
preetapan 1bad719
Added CLI for evaluating job given ID, and modified client API for ev…
preetapan 4f9d92c
fix test comment
preetapan 2d0e273
Documentation for evaluate endpoint
preetapan b5e18b6
Docs for job eval CLI
preetapan 879c2c9
Code review feedback
preetapan 53c05c5
Add support for monitoring evals, and -detach/-verbose support
preetapan e2f13d2
unit test for job eval should detach
preetapan b2006cc
more review feedback
preetapan ae5d8fd
Add new method EvaluateWithOptions to avoid breaking go API client
preetapan 2ea09b8
Fix docs and method documentation in API
preetapan a5ca379
remove extra return
preetapan 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
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
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,128 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/nomad/api" | ||
"github.com/hashicorp/nomad/api/contexts" | ||
"github.com/posener/complete" | ||
) | ||
|
||
type JobEvalCommand struct { | ||
Meta | ||
forceRescheduling bool | ||
} | ||
|
||
func (c *JobEvalCommand) Help() string { | ||
helpText := ` | ||
Usage: nomad job eval [options] <job_id> | ||
|
||
Force an evaluation of the provided job ID. Forcing an evaluation will trigger the scheduler | ||
to re-evaluate the job. The force flags allow operators to force the scheduler to create | ||
new allocations under certain scenarios. | ||
|
||
General Options: | ||
|
||
` + generalOptionsUsage() + ` | ||
|
||
Eval Options: | ||
|
||
-force-reschedule | ||
Force reschedule failed allocations even if they are not currently | ||
eligible for rescheduling. | ||
|
||
-detach | ||
Return immediately instead of entering monitor mode. The ID | ||
of the evaluation created will be printed to the screen, which can be | ||
used to examine the evaluation using the eval-status command. | ||
|
||
-verbose | ||
Display full information. | ||
` | ||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
func (c *JobEvalCommand) Synopsis() string { | ||
return "Force an evaluation for the job" | ||
} | ||
|
||
func (c *JobEvalCommand) AutocompleteFlags() complete.Flags { | ||
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), | ||
complete.Flags{ | ||
"-force-reschedule": complete.PredictNothing, | ||
"-detach": complete.PredictNothing, | ||
"-verbose": complete.PredictNothing, | ||
}) | ||
} | ||
|
||
func (c *JobEvalCommand) AutocompleteArgs() complete.Predictor { | ||
return complete.PredictFunc(func(a complete.Args) []string { | ||
client, err := c.Meta.Client() | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil) | ||
if err != nil { | ||
return []string{} | ||
} | ||
return resp.Matches[contexts.Jobs] | ||
}) | ||
} | ||
|
||
func (c *JobEvalCommand) Name() string { return "job eval" } | ||
|
||
func (c *JobEvalCommand) Run(args []string) int { | ||
var detach, verbose bool | ||
|
||
flags := c.Meta.FlagSet(c.Name(), FlagSetClient) | ||
flags.Usage = func() { c.Ui.Output(c.Help()) } | ||
flags.BoolVar(&c.forceRescheduling, "force-reschedule", false, "") | ||
flags.BoolVar(&detach, "detach", false, "") | ||
flags.BoolVar(&verbose, "verbose", false, "") | ||
|
||
if err := flags.Parse(args); err != nil { | ||
return 1 | ||
} | ||
|
||
// Check that we either got no jobs or exactly one. | ||
args = flags.Args() | ||
if len(args) != 1 { | ||
c.Ui.Error("This command takes one argument: <job>") | ||
c.Ui.Error(commandErrorText(c)) | ||
return 1 | ||
} | ||
|
||
// Get the HTTP client | ||
client, err := c.Meta.Client() | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) | ||
return 1 | ||
} | ||
|
||
// Truncate the id unless full length is requested | ||
length := shortId | ||
if verbose { | ||
length = fullId | ||
} | ||
// Call eval endpoint | ||
jobID := args[0] | ||
|
||
opts := api.EvalOptions{ | ||
ForceReschedule: c.forceRescheduling, | ||
} | ||
evalId, _, err := client.Jobs().EvaluateWithOpts(jobID, opts, nil) | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error evaluating job: %s", err)) | ||
return 1 | ||
} | ||
|
||
if detach { | ||
c.Ui.Output(fmt.Sprintf("Created eval ID: %q ", limit(evalId, length))) | ||
return 0 | ||
} | ||
|
||
mon := newMonitor(c.Ui, client, length) | ||
return mon.monitor(evalId, false) | ||
} |
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,127 @@ | ||
package command | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"fmt" | ||
|
||
"github.com/hashicorp/nomad/nomad/mock" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/hashicorp/nomad/testutil" | ||
"github.com/mitchellh/cli" | ||
"github.com/posener/complete" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestJobEvalCommand_Implements(t *testing.T) { | ||
t.Parallel() | ||
var _ cli.Command = &JobEvalCommand{} | ||
} | ||
|
||
func TestJobEvalCommand_Fails(t *testing.T) { | ||
t.Parallel() | ||
ui := new(cli.MockUi) | ||
cmd := &JobEvalCommand{Meta: Meta{Ui: ui}} | ||
|
||
// Fails on misuse | ||
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { | ||
t.Fatalf("expected exit code 1, got: %d", code) | ||
} | ||
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) { | ||
t.Fatalf("expected help output, got: %s", out) | ||
} | ||
ui.ErrorWriter.Reset() | ||
|
||
// Fails when job ID is not specified | ||
if code := cmd.Run([]string{}); code != 1 { | ||
t.Fatalf("expect exit 1, got: %d", code) | ||
} | ||
if out := ui.ErrorWriter.String(); !strings.Contains(out, "This command takes one argument") { | ||
t.Fatalf("unexpected error: %v", out) | ||
} | ||
ui.ErrorWriter.Reset() | ||
|
||
} | ||
|
||
func TestJobEvalCommand_Run(t *testing.T) { | ||
t.Parallel() | ||
srv, client, url := testServer(t, true, nil) | ||
defer srv.Shutdown() | ||
|
||
// Wait for a node to be ready | ||
testutil.WaitForResult(func() (bool, error) { | ||
nodes, _, err := client.Nodes().List(nil) | ||
if err != nil { | ||
return false, err | ||
} | ||
for _, node := range nodes { | ||
if node.Status == structs.NodeStatusReady { | ||
return true, nil | ||
} | ||
} | ||
return false, fmt.Errorf("no ready nodes") | ||
}, func(err error) { | ||
t.Fatalf("err: %v", err) | ||
}) | ||
|
||
ui := new(cli.MockUi) | ||
cmd := &JobEvalCommand{Meta: Meta{Ui: ui}} | ||
require := require.New(t) | ||
|
||
state := srv.Agent.Server().State() | ||
|
||
// Create a job | ||
job := mock.Job() | ||
err := state.UpsertJob(11, job) | ||
require.Nil(err) | ||
|
||
job, err = state.JobByID(nil, structs.DefaultNamespace, job.ID) | ||
require.Nil(err) | ||
|
||
// Create a failed alloc for the job | ||
alloc := mock.Alloc() | ||
alloc.Job = job | ||
alloc.JobID = job.ID | ||
alloc.TaskGroup = job.TaskGroups[0].Name | ||
alloc.Namespace = job.Namespace | ||
alloc.ClientStatus = structs.AllocClientStatusFailed | ||
err = state.UpsertAllocs(12, []*structs.Allocation{alloc}) | ||
require.Nil(err) | ||
|
||
if code := cmd.Run([]string{"-address=" + url, "-force-reschedule", "-detach", job.ID}); code != 0 { | ||
t.Fatalf("expected exit 0, got: %d", code) | ||
} | ||
|
||
// Lookup alloc again | ||
alloc, err = state.AllocByID(nil, alloc.ID) | ||
require.NotNil(alloc) | ||
require.Nil(err) | ||
require.True(*alloc.DesiredTransition.ForceReschedule) | ||
|
||
} | ||
|
||
func TestJobEvalCommand_AutocompleteArgs(t *testing.T) { | ||
assert := assert.New(t) | ||
t.Parallel() | ||
|
||
srv, _, url := testServer(t, true, nil) | ||
defer srv.Shutdown() | ||
|
||
ui := new(cli.MockUi) | ||
cmd := &JobEvalCommand{Meta: Meta{Ui: ui, flagAddress: url}} | ||
|
||
// Create a fake job | ||
state := srv.Agent.Server().State() | ||
j := mock.Job() | ||
assert.Nil(state.UpsertJob(1000, j)) | ||
|
||
prefix := j.ID[:len(j.ID)-5] | ||
args := complete.Args{Last: prefix} | ||
predictor := cmd.AutocompleteArgs() | ||
|
||
res := predictor.Predict(args) | ||
assert.Equal(1, len(res)) | ||
assert.Equal(j.ID, res[0]) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not add a test that submits a job and runs the force eval against it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
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.
0.10? i guess this code will go out in 0.9 :)