Skip to content

Commit

Permalink
server: suppress spurious blocking query returns where multiple confi…
Browse files Browse the repository at this point in the history
…g entries are involved

Starting from and extending the mechanism introduced in #12110 we can
specially handle the following special Consul RPC endpoints that react to
many config entries in a single blocking query in Connect:

- DiscoveryChain.Get
- ConfigEntry.ResolveServiceConfig
- Intentions.Match
- ConfigEntry.List
- Internal.IntentionUpstreams

All of these will internally watch for many config entries, and at least
one of those will likely be not found in any given query. Because these
are blends of multiple reads the exact solution from #12110 isn't
perfectly aligned, but we can tweak the approach slightly and regain the
utility of that mechanism.

(A) No Config Entries Found

In this case, despite looking for many config entries none may be found
at all. Unlike #12110 in this scenario we do not return an empty reply
to the caller, but instead synthesize a struct from default values to
return. This can be handled nearly identically to #12110 with the first
1-2 replies being non-empty payloads followed by the standard spurious
wakeup suppression mechanism from #12110.

(B) No Change Since Last Wakeup

Once a blocking query loop on the server has completed and slept at
least once, there is a further optimization we can make here to detect
if any of the config entries that were present at specific versions for
the prior execution of the loop are identical for the loop we just woke
up for. In that scenario we can return a slightly different internal
sentinel error and basically externally handle it similar to #12110.

This would mean that even if 20 discovery chain read RPC handling
goroutines wakeup due to the creation of an unrelated config entry, the
only ones that will terminate and reply with a blob of data are those
that genuinely have new data to report.
  • Loading branch information
rboyer committed Feb 18, 2022
1 parent 9bddf5b commit ec7b8ff
Show file tree
Hide file tree
Showing 16 changed files with 1,265 additions and 256 deletions.
4 changes: 4 additions & 0 deletions agent/configentry/config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ func NewKindName(kind, name string, entMeta *structs.EnterpriseMeta) KindName {
ret.Normalize()
return ret
}

func NewKindNameForEntry(entry structs.ConfigEntry) KindName {
return NewKindName(entry.GetKind(), entry.GetName(), entry.GetEnterpriseMeta())
}
57 changes: 57 additions & 0 deletions agent/configentry/service_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package configentry

import (
"github.com/hashicorp/consul/agent/structs"
)

// ResolvedServiceConfigSet is a wrapped set of raw cross-referenced config
// entries necessary for the ConfigEntry.ResolveServiceConfig RPC process.
//
// None of these are defaulted.
type ResolvedServiceConfigSet struct {
ServiceDefaults map[structs.ServiceID]*structs.ServiceConfigEntry
ProxyDefaults map[string]*structs.ProxyConfigEntry
}

func (r *ResolvedServiceConfigSet) IsEmpty() bool {
return len(r.ServiceDefaults) == 0 && len(r.ProxyDefaults) == 0
}

func (r *ResolvedServiceConfigSet) GetServiceDefaults(sid structs.ServiceID) *structs.ServiceConfigEntry {
if r.ServiceDefaults == nil {
return nil
}
return r.ServiceDefaults[sid]
}

func (r *ResolvedServiceConfigSet) GetProxyDefaults(partition string) *structs.ProxyConfigEntry {
if r.ProxyDefaults == nil {
return nil
}
return r.ProxyDefaults[partition]
}

func (r *ResolvedServiceConfigSet) AddServiceDefaults(entry *structs.ServiceConfigEntry) {
if entry == nil {
return
}

if r.ServiceDefaults == nil {
r.ServiceDefaults = make(map[structs.ServiceID]*structs.ServiceConfigEntry)
}

sid := structs.NewServiceID(entry.Name, &entry.EnterpriseMeta)
r.ServiceDefaults[sid] = entry
}

func (r *ResolvedServiceConfigSet) AddProxyDefaults(entry *structs.ProxyConfigEntry) {
if entry == nil {
return
}

if r.ProxyDefaults == nil {
r.ProxyDefaults = make(map[string]*structs.ProxyConfigEntry)
}

r.ProxyDefaults[entry.PartitionOrDefault()] = entry
}

0 comments on commit ec7b8ff

Please sign in to comment.