if no context is specified, set maximum index for available contexts

This commit is contained in:
Chelsea Holland Komlo 2017-08-04 20:14:41 +00:00
parent 0b38704392
commit 424e475333
3 changed files with 11 additions and 8 deletions

View File

@ -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"))
})
}

View File

@ -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
}
}
}

View File

@ -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