Skip to content

Commit

Permalink
consul: allow stale namespace results
Browse files Browse the repository at this point in the history
Nomad reconciles services it expects to be registered in Consul with
what is actually registered in the local Consul agent. This is necessary
to prevent leaking service registrations if Nomad crashes at certain
points (or if there are bugs).

When Consul has namespaces enabled, we must iterate over each available
namespace to be sure no services were leaked into non-default
namespaces.

Since this reconciliation happens often, there's no need to require
results from the Consul leader server. In large clusters this creates
far more load than the "freshness" of the response is worth.

Therefore this patch switches the request to AllowStale=true
  • Loading branch information
schmichael committed Aug 26, 2022
1 parent e23b47e commit f298877
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/12953.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
consul: Reduce load on Consul leader server by allowing stale results when listing namespaces.
```
9 changes: 6 additions & 3 deletions command/agent/consul/namespaces_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"sort"
"sync"
"time"

"github.com/hashicorp/consul/api"
)

const (
Expand Down Expand Up @@ -66,16 +68,17 @@ func (ns *NamespacesClient) allowable(now time.Time) bool {
}

// List returns a list of Consul Namespaces.
//
// TODO(shoenig): return empty string instead of "default" when namespaces are not enabled. (Coming in followup PR).
func (ns *NamespacesClient) List() ([]string, error) {
if !ns.allowable(time.Now()) {
// TODO(shoenig): lets return the empty string instead, that way we do not
// need to normalize at call sites later on
return []string{"default"}, nil
}

namespaces, _, err := ns.namespacesAPI.List(nil)
qo := &api.QueryOptions{
AllowStale: true,
}
namespaces, _, err := ns.namespacesAPI.List(qo)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit f298877

Please sign in to comment.