Skip to content

Commit

Permalink
agent: add option to disable agent cache for HTTP endpoints (#8023)
Browse files Browse the repository at this point in the history
This allows the operator to disable agent caching for the http endpoint.
It is on by default for backwards compatibility and if disabled will
ignore the url parameter `cached`.
  • Loading branch information
hanshasselberg committed Jun 8, 2020
1 parent 07ca9eb commit e86c4ff
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 7 deletions.
6 changes: 3 additions & 3 deletions agent/catalog_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (s *HTTPServer) CatalogDatacenters(resp http.ResponseWriter, req *http.Requ
parseCacheControl(resp, req, &args.QueryOptions)
var out []string

if args.QueryOptions.UseCache {
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
raw, m, err := s.agent.cache.Get(cachetype.CatalogDatacentersName, &args)
if err != nil {
metrics.IncrCounterWithLabels([]string{"client", "rpc", "error", "catalog_datacenters"}, 1,
Expand Down Expand Up @@ -155,7 +155,7 @@ func (s *HTTPServer) CatalogServices(resp http.ResponseWriter, req *http.Request
var out structs.IndexedServices
defer setMeta(resp, &out.QueryMeta)

if args.QueryOptions.UseCache {
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
raw, m, err := s.agent.cache.Get(cachetype.CatalogListServicesName, &args)
if err != nil {
metrics.IncrCounterWithLabels([]string{"client", "rpc", "error", "catalog_services"}, 1,
Expand Down Expand Up @@ -240,7 +240,7 @@ func (s *HTTPServer) catalogServiceNodes(resp http.ResponseWriter, req *http.Req
var out structs.IndexedServiceNodes
defer setMeta(resp, &out.QueryMeta)

if args.QueryOptions.UseCache {
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
raw, m, err := s.agent.cache.Get(cachetype.CatalogServicesName, &args)
if err != nil {
metrics.IncrCounterWithLabels([]string{"client", "rpc", "error", "catalog_service_nodes"}, 1,
Expand Down
1 change: 1 addition & 0 deletions agent/config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
HTTPBlockEndpoints: c.HTTPConfig.BlockEndpoints,
HTTPResponseHeaders: c.HTTPConfig.ResponseHeaders,
AllowWriteHTTPFrom: b.cidrsVal("allow_write_http_from", c.HTTPConfig.AllowWriteHTTPFrom),
HTTPUseCache: b.boolValWithDefault(c.HTTPConfig.UseCache, true),

// Telemetry
Telemetry: lib.TelemetryConfig{
Expand Down
1 change: 1 addition & 0 deletions agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ type HTTPConfig struct {
BlockEndpoints []string `json:"block_endpoints,omitempty" hcl:"block_endpoints" mapstructure:"block_endpoints"`
AllowWriteHTTPFrom []string `json:"allow_write_http_from,omitempty" hcl:"allow_write_http_from" mapstructure:"allow_write_http_from"`
ResponseHeaders map[string]string `json:"response_headers,omitempty" hcl:"response_headers" mapstructure:"response_headers"`
UseCache *bool `json:"use_cache,omitempty" hcl:"use_cache" mapstructure:"use_cache"`
}

type Performance struct {
Expand Down
6 changes: 6 additions & 0 deletions agent/config/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ type RuntimeConfig struct {
// hcl: dns_config { cache_max_age = "duration" }
DNSCacheMaxAge time.Duration

// HTTPUseCache whether or not to use cache for http queries. Defaults
// to true.
//
// hcl: http_config { use_cache = (true|false) }
HTTPUseCache bool

// HTTPBlockEndpoints is a list of endpoint prefixes to block in the
// HTTP API. Any requests to these will get a 403 response.
//
Expand Down
54 changes: 53 additions & 1 deletion agent/config/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,54 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
`},
err: "Serf Advertise WAN address 10.0.0.1:1000 already configured for RPC Advertise",
},
{
desc: "http use_cache defaults to true",
args: []string{
`-data-dir=` + dataDir,
},
json: []string{`{
"http_config": {}
}`},
hcl: []string{`
http_config = {}
`},
patch: func(rt *RuntimeConfig) {
rt.DataDir = dataDir
rt.HTTPUseCache = true
},
},
{
desc: "http use_cache is enabled when true",
args: []string{
`-data-dir=` + dataDir,
},
json: []string{`{
"http_config": { "use_cache": true }
}`},
hcl: []string{`
http_config = { use_cache = true }
`},
patch: func(rt *RuntimeConfig) {
rt.DataDir = dataDir
rt.HTTPUseCache = true
},
},
{
desc: "http use_cache is disabled when false",
args: []string{
`-data-dir=` + dataDir,
},
json: []string{`{
"http_config": { "use_cache": false }
}`},
hcl: []string{`
http_config = { use_cache = false }
`},
patch: func(rt *RuntimeConfig) {
rt.DataDir = dataDir
rt.HTTPUseCache = false
},
},
{
desc: "sidecar_service can't have ID",
args: []string{
Expand Down Expand Up @@ -3721,7 +3769,8 @@ func TestFullConfig(t *testing.T) {
"response_headers": {
"M6TKa9NP": "xjuxjOzQ",
"JRCrHZed": "rl0mTx81"
}
},
"use_cache": false
},
"key_file": "IEkkwgIA",
"leave_on_terminate": true,
Expand Down Expand Up @@ -4324,6 +4373,7 @@ func TestFullConfig(t *testing.T) {
"M6TKa9NP" = "xjuxjOzQ"
"JRCrHZed" = "rl0mTx81"
}
use_cache = false
}
key_file = "IEkkwgIA"
leave_on_terminate = true
Expand Down Expand Up @@ -5018,6 +5068,7 @@ func TestFullConfig(t *testing.T) {
HTTPMaxConnsPerClient: 9283,
HTTPSHandshakeTimeout: 2391 * time.Millisecond,
HTTPSPort: 15127,
HTTPUseCache: false,
KeyFile: "IEkkwgIA",
KVMaxValueSize: 1234567800000000,
LeaveDrainTime: 8265 * time.Second,
Expand Down Expand Up @@ -5873,6 +5924,7 @@ func TestSanitize(t *testing.T) {
"HTTPMaxConnsPerClient": 0,
"HTTPPort": 0,
"HTTPResponseHeaders": {},
"HTTPUseCache": false,
"HTTPSAddrs": [],
"HTTPSHandshakeTimeout": "0s",
"HTTPSPort": 0,
Expand Down
2 changes: 1 addition & 1 deletion agent/discovery_chain_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *HTTPServer) DiscoveryChainRead(resp http.ResponseWriter, req *http.Requ
var out structs.DiscoveryChainResponse
defer setMeta(resp, &out.QueryMeta)

if args.QueryOptions.UseCache {
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
raw, m, err := s.agent.cache.Get(cachetype.CompiledDiscoveryChainName, &args)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion agent/health_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (s *HTTPServer) healthServiceNodes(resp http.ResponseWriter, req *http.Requ
var out structs.IndexedCheckServiceNodes
defer setMeta(resp, &out.QueryMeta)

if args.QueryOptions.UseCache {
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
raw, m, err := s.agent.cache.Get(cachetype.HealthServicesName, &args)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion agent/prepared_query_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (s *HTTPServer) preparedQueryExecute(id string, resp http.ResponseWriter, r
var reply structs.PreparedQueryExecuteResponse
defer setMeta(resp, &reply.QueryMeta)

if args.QueryOptions.UseCache {
if s.agent.config.HTTPUseCache && args.QueryOptions.UseCache {
raw, m, err := s.agent.cache.Get(cachetype.PreparedQueryName, &args)
if err != nil {
// Don't return error if StaleIfError is set and we are within it and had
Expand Down

0 comments on commit e86c4ff

Please sign in to comment.