Skip to content
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

rpc: fix scaling policy get index response when policy is found. #11579

Merged
merged 2 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/11579.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
rpc: Fixed scaling policy get index response when the policy is found
```
22 changes: 11 additions & 11 deletions nomad/scaling_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,18 @@ func (p *Scaling) GetPolicy(args *structs.ScalingPolicySpecificRequest,

reply.Policy = p

// Use the last index that affected the policy table
index, err := state.Index("scaling_policy")
if err != nil {
return err
}

// Ensure we never set the index to zero, otherwise a blocking query cannot be used.
// We floor the index at one, since realistically the first write must have a higher index.
if index == 0 {
index = 1
// If the state lookup returned a policy object, use the modify
// index for the response. Otherwise, use the index table to supply
// this, ensuring a non-zero value.
if p != nil {
reply.Index = p.ModifyIndex
} else {
index, err := state.Index("scaling_policy")
if err != nil {
return err
}
reply.Index = helper.Uint64Max(1, index)
}
reply.Index = index
return nil
}}
return p.srv.blockingRPC(&opts)
Expand Down
8 changes: 6 additions & 2 deletions nomad/scaling_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ func TestScalingEndpoint_GetPolicy(t *testing.T) {
p2 := mock.ScalingPolicy()
s1.fsm.State().UpsertScalingPolicies(1000, []*structs.ScalingPolicy{p1, p2})

// Lookup the policy
// Add another policy at a higher index.
p3 := mock.ScalingPolicy()
require.NoError(s1.fsm.State().UpsertScalingPolicies(2000, []*structs.ScalingPolicy{p3}))

// Lookup the first policy and perform assertions.
get := &structs.ScalingPolicySpecificRequest{
ID: p1.ID,
QueryOptions: structs.QueryOptions{
Expand All @@ -54,7 +58,7 @@ func TestScalingEndpoint_GetPolicy(t *testing.T) {
resp = structs.SingleScalingPolicyResponse{}
err = msgpackrpc.CallWithCodec(codec, "Scaling.GetPolicy", get, &resp)
require.NoError(err)
require.EqualValues(1000, resp.Index)
require.EqualValues(2000, resp.Index)
require.Nil(resp.Policy)
}

Expand Down