From 74f35dd1af3872f7eee5fa111ff2fe46c3af7422 Mon Sep 17 00:00:00 2001 From: Chelsea Holland Komlo Date: Fri, 4 Aug 2017 20:14:41 +0000 Subject: [PATCH] if no context is specified, set maximum index for available contexts --- command/agent/resources_endpoint_test.go | 5 +++-- nomad/resources_endpoint.go | 12 +++++++----- nomad/resources_endpoint_test.go | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/command/agent/resources_endpoint_test.go b/command/agent/resources_endpoint_test.go index ab85b56e3..375afb8a4 100644 --- a/command/agent/resources_endpoint_test.go +++ b/command/agent/resources_endpoint_test.go @@ -276,8 +276,7 @@ func TestHTTP_Resources_NoContext(t *testing.T) { state := s.Agent.server.State() eval1 := mock.Eval() eval1.ID = testJobID - err := state.UpsertEvals(9000, - []*structs.Evaluation{eval1}) + err := state.UpsertEvals(8000, []*structs.Evaluation{eval1}) assert.Nil(err) data := structs.ResourcesRequest{Prefix: testJobPrefix} @@ -299,5 +298,7 @@ func TestHTTP_Resources_NoContext(t *testing.T) { assert.Equal(matchedJobs[0], testJobID) assert.Equal(matchedEvals[0], eval1.ID) + + assert.Equal("8000", respW.HeaderMap.Get("X-Nomad-Index")) }) } diff --git a/nomad/resources_endpoint.go b/nomad/resources_endpoint.go index 5b4b1c075..5681ef8b9 100644 --- a/nomad/resources_endpoint.go +++ b/nomad/resources_endpoint.go @@ -109,16 +109,18 @@ func (r *Resources) List(args *structs.ResourcesRequest, // Set the index for the context. If the context has been specified, it // is the only non-empty match set, and the index is set for it. - // If the context was not specified, we set the index of the first - // non-empty match set. + // If the context was not specified, we set the index to be the max index + // from available contexts + reply.Index = 0 for k, v := range reply.Matches { - if len(v) != 0 { + if len(v) != 0 { // make sure matches exist for this context index, err := state.Index(k) if err != nil { return err } - reply.Index = index - break + if index > reply.Index { + reply.Index = index + } } } diff --git a/nomad/resources_endpoint_test.go b/nomad/resources_endpoint_test.go index 4f031d890..2f67b2280 100644 --- a/nomad/resources_endpoint_test.go +++ b/nomad/resources_endpoint_test.go @@ -264,7 +264,7 @@ func TestResourcesEndpoint_List_NoContext(t *testing.T) { assert.Equal(node.ID, resp.Matches["nodes"][0]) assert.Equal(eval1.ID, resp.Matches["evals"][0]) - assert.NotEqual(uint64(0), resp.Index) + assert.Equal(uint64(1000), resp.Index) } //// Tests that the top 20 matches are returned when no prefix is set