-
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
Preemption for system jobs #4794
Changes from 27 commits
5f27e00
8004160
bf7192c
fc266f7
715d869
b5cbd73
13e314c
fd6bff2
51c5bae
784b96c
2143fa2
6966e3c
24b3934
9f35923
a960cce
655689a
c4e0e66
c3b8e4f
12af2ae
c4a04eb
191b862
21432d6
4cc21fb
35635ba
8800585
17344a7
f2b0277
22d156f
993b6a2
3ad7b3f
06ad182
35d31f8
b3738a0
0015095
8235919
1380acb
c49a3e2
97cf4e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package api | ||
|
||
import "strconv" | ||
|
||
// Operator can be used to perform low-level operator tasks for Nomad. | ||
type Operator struct { | ||
c *Client | ||
|
@@ -106,3 +108,61 @@ func (op *Operator) RaftRemovePeerByID(id string, q *WriteOptions) error { | |
resp.Body.Close() | ||
return nil | ||
} | ||
|
||
type SchedulerConfiguration struct { | ||
// PreemptionConfig specifies whether to enable eviction of lower | ||
// priority jobs to place higher priority jobs. | ||
PreemptionConfig PreemptionConfig | ||
|
||
// CreateIndex/ModifyIndex store the create/modify indexes of this configuration. | ||
CreateIndex uint64 | ||
ModifyIndex uint64 | ||
} | ||
|
||
// SchedulerConfigurationResponse is the response object that wraps SchedulerConfiguration | ||
type SchedulerConfigurationResponse struct { | ||
// SchedulerConfig contains scheduler config options | ||
SchedulerConfig SchedulerConfiguration | ||
|
||
// CreateIndex/ModifyIndex store the create/modify indexes of this configuration. | ||
CreateIndex uint64 | ||
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. Should be WriteMeta and QueryMeta: https://github.com/hashicorp/nomad/blob/master/api/jobs.go#L1039-L1046 |
||
ModifyIndex uint64 | ||
} | ||
|
||
// PreemptionConfig specifies whether preemption is enabled based on scheduler type | ||
type PreemptionConfig struct { | ||
SystemSchedulerEnabled bool | ||
} | ||
|
||
// SchedulerGetConfiguration is used to query the current Scheduler configuration. | ||
func (op *Operator) SchedulerGetConfiguration(q *QueryOptions) (*SchedulerConfigurationResponse, *QueryMeta, error) { | ||
var resp SchedulerConfigurationResponse | ||
qm, err := op.c.query("/v1/operator/scheduler/config", &resp, q) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return &resp, qm, nil | ||
} | ||
|
||
// SchedulerSetConfiguration is used to set the current Scheduler configuration. | ||
func (op *Operator) SchedulerSetConfiguration(conf *SchedulerConfiguration, q *WriteOptions) (*WriteMeta, error) { | ||
var out bool | ||
wm, err := op.c.write("/v1/operator/scheduler/config", conf, &out, q) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return wm, nil | ||
} | ||
|
||
// SchedulerCASConfiguration is used to perform a Check-And-Set update on the | ||
// Scheduler configuration. The ModifyIndex value will be respected. Returns | ||
// true on success or false on failures. | ||
func (op *Operator) SchedulerCASConfiguration(conf *SchedulerConfiguration, q *WriteOptions) (bool, *WriteMeta, error) { | ||
var out bool | ||
wm, err := op.c.write("/v1/operator/scheduler/config?cas="+strconv.FormatUint(conf.ModifyIndex, 10), conf, &out, q) | ||
if err != nil { | ||
return false, nil, err | ||
} | ||
|
||
return out, wm, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,3 +208,73 @@ func (s *HTTPServer) OperatorServerHealth(resp http.ResponseWriter, req *http.Re | |
|
||
return out, nil | ||
} | ||
|
||
// OperatorSchedulerConfiguration is used to inspect the current Scheduler configuration. | ||
// This supports the stale query mode in case the cluster doesn't have a leader. | ||
func (s *HTTPServer) OperatorSchedulerConfiguration(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
// Switch on the method | ||
switch req.Method { | ||
case "GET": | ||
var args structs.GenericRequest | ||
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. Generally split the methods up so that it only handles one type of HTTP verb: https://github.com/hashicorp/nomad/blob/master/command/agent/job_endpoint.go#L15 |
||
if done := s.parse(resp, req, &args.Region, &args.QueryOptions); done { | ||
return nil, nil | ||
} | ||
|
||
var reply structs.SchedulerConfigurationResponse | ||
if err := s.agent.RPC("Operator.SchedulerGetConfiguration", &args, &reply); err != nil { | ||
return nil, err | ||
} | ||
|
||
out := api.SchedulerConfiguration{ | ||
PreemptionConfig: api.PreemptionConfig{SystemSchedulerEnabled: reply.SchedulerConfig.PreemptionConfig.SystemSchedulerEnabled}, | ||
CreateIndex: reply.CreateIndex, | ||
ModifyIndex: reply.ModifyIndex, | ||
} | ||
|
||
resp := api.SchedulerConfigurationResponse{ | ||
SchedulerConfig: out, | ||
CreateIndex: out.CreateIndex, | ||
ModifyIndex: out.ModifyIndex, | ||
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. |
||
} | ||
|
||
return resp, nil | ||
|
||
case "PUT": | ||
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. We accept POST and PUT |
||
var args structs.SchedulerSetConfigRequest | ||
s.parseWriteRequest(req, &args.WriteRequest) | ||
|
||
var conf api.SchedulerConfiguration | ||
if err := decodeBody(req, &conf); err != nil { | ||
return nil, CodedError(http.StatusBadRequest, fmt.Sprintf("Error parsing scheduler config: %v", err)) | ||
} | ||
|
||
args.Config = structs.SchedulerConfiguration{ | ||
PreemptionConfig: structs.PreemptionConfig{SystemSchedulerEnabled: conf.PreemptionConfig.SystemSchedulerEnabled}, | ||
} | ||
|
||
// Check for cas value | ||
params := req.URL.Query() | ||
if _, ok := params["cas"]; ok { | ||
casVal, err := strconv.ParseUint(params.Get("cas"), 10, 64) | ||
if err != nil { | ||
return nil, CodedError(http.StatusBadRequest, fmt.Sprintf("Error parsing cas value: %v", err)) | ||
} | ||
args.Config.ModifyIndex = casVal | ||
args.CAS = true | ||
} | ||
|
||
var reply bool | ||
if err := s.agent.RPC("Operator.SchedulerSetConfiguration", &args, &reply); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Only use the out value if this was a CAS | ||
if !args.CAS { | ||
return true, nil | ||
} | ||
return reply, nil | ||
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. |
||
|
||
default: | ||
return nil, CodedError(404, ErrInvalidMethod) | ||
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. 404 -> 405 |
||
} | ||
} |
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.
Need to update this as you changed it in the structs