From 50c046e8e996a63827955f53dec5a619cb300064 Mon Sep 17 00:00:00 2001 From: James Rasell Date: Fri, 26 Nov 2021 10:40:27 +0100 Subject: [PATCH 1/2] rpc: fix scaling policy get index response when policy is found. When GetPolicy is called within the scaling handler, the index table was being used to populate the reply index irregardless of whether the policy was found or not. This change fixes that behaviour so that the policy modify index is used when the policy lookup is successful. --- nomad/scaling_endpoint.go | 22 +++++++++++----------- nomad/scaling_endpoint_test.go | 8 ++++++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/nomad/scaling_endpoint.go b/nomad/scaling_endpoint.go index c3363e45a92..bb87769a234 100644 --- a/nomad/scaling_endpoint.go +++ b/nomad/scaling_endpoint.go @@ -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) diff --git a/nomad/scaling_endpoint_test.go b/nomad/scaling_endpoint_test.go index 73311b1f6c1..6dabf308753 100644 --- a/nomad/scaling_endpoint_test.go +++ b/nomad/scaling_endpoint_test.go @@ -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{ @@ -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) } From 30fc0d0149df60fcd869beca904f38f4f7901569 Mon Sep 17 00:00:00 2001 From: James Rasell Date: Fri, 26 Nov 2021 11:16:17 +0100 Subject: [PATCH 2/2] changelog: add entry for #11579 --- .changelog/11579.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/11579.txt diff --git a/.changelog/11579.txt b/.changelog/11579.txt new file mode 100644 index 00000000000..bf878ae1987 --- /dev/null +++ b/.changelog/11579.txt @@ -0,0 +1,3 @@ +```release-note:bug +rpc: Fixed scaling policy get index response when the policy is found +```