Skip to content

Commit

Permalink
Add option in prepared query to include all nodes
Browse files Browse the repository at this point in the history
- Add option IncludeAll in Service Query section, defaulted to false,
which enable the possibility to fetched all nodes whatever the health check
- Add more tests on prepared query

JIRA: DISCO-890
  • Loading branch information
mougams committed Nov 4, 2024
1 parent cc16756 commit 7494246
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
6 changes: 4 additions & 2 deletions agent/consul/prepared_query_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,11 @@ func (p *PreparedQuery) execute(query *structs.PreparedQuery,

// Filter out any unhealthy nodes.
filterType := structs.HealthFilterExcludeCritical
if query.Service.OnlyPassing {
filterType = structs.HealthFilterIncludeOnlyPassing
if query.Service.IncludeAll {
filterType = structs.HealthFilterIncludeAll

} else if query.Service.OnlyPassing {
filterType = structs.HealthFilterIncludeOnlyPassing
}

nodes = nodes.Filter(structs.CheckServiceNodeFilterOptions{FilterType: filterType,
Expand Down
27 changes: 27 additions & 0 deletions agent/consul/prepared_query_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2178,6 +2178,33 @@ func TestPreparedQuery_Execute(t *testing.T) {
expectNodes(t, &query, &reply, 0)
})

// Critical nodes should also be returned
query.Query.Service.IncludeAll = true
// remove tags filtering
query.Query.Service.Tags = nil
setHealth(t, es.server.codec, "dc1", 1, api.HealthPassing)
setHealth(t, es.server.codec, "dc1", 2, api.HealthPassing)
setHealth(t, es.server.codec, "dc1", 4, api.HealthWarning)
require.NoError(t, msgpackrpc.CallWithCodec(es.server.codec, "PreparedQuery.Apply", &query, &query.Query.ID))
t.Run("see 10 nodes despite all nodes being critical", func(t *testing.T) {
req := structs.PreparedQueryExecuteRequest{
Datacenter: "dc1",
QueryIDOrName: query.Query.ID,
QueryOptions: structs.QueryOptions{Token: es.execToken},
}

var reply structs.PreparedQueryExecuteResponse
require.NoError(t, msgpackrpc.CallWithCodec(es.server.codec, "PreparedQuery.Execute", &req, &reply))

expectNodes(t, &query, &reply, 10)
})
// Undo that so all the following tests aren't broken!
query.Query.Service.IncludeAll = false
query.Query.Service.Tags = []string{"!tag3"}
setHealth(t, es.server.codec, "dc1", 1, api.HealthCritical)
setHealth(t, es.server.codec, "dc1", 2, api.HealthCritical)
require.NoError(t, msgpackrpc.CallWithCodec(es.server.codec, "PreparedQuery.Apply", &query, &query.Query.ID))

// Modify the query to have it fail over to a bogus DC and then dc2.
query.Query.Service.Failover.Datacenters = []string{"bogus", "dc2"}
require.NoError(t, msgpackrpc.CallWithCodec(es.server.codec, "PreparedQuery.Apply", &query, &query.Query.ID))
Expand Down
4 changes: 4 additions & 0 deletions agent/structs/prepared_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ type ServiceQuery struct {
// local datacenter.
Failover QueryFailoverOptions

// If IncludeAll is true then we will only include all nodes
// whatever the health checks
IncludeAll bool

// If OnlyPassing is true then we will only include nodes with passing
// health checks (critical AND warning checks will cause a node to be
// discarded)
Expand Down

0 comments on commit 7494246

Please sign in to comment.