diff --git a/src/cmd/services/m3query/config/config.go b/src/cmd/services/m3query/config/config.go index 61bfec3b70..a3ac3e5de2 100644 --- a/src/cmd/services/m3query/config/config.go +++ b/src/cmd/services/m3query/config/config.go @@ -58,6 +58,8 @@ const ( errNoIDGenerationScheme = "error: a recent breaking change means that an ID " + "generation scheme is required in coordinator configuration settings. " + "More information is available here: %s" + + defaultQueryTimeout = 30 * time.Second ) var ( @@ -125,6 +127,9 @@ type Configuration struct { // Carbon is the carbon configuration. Carbon *CarbonConfiguration `yaml:"carbon"` + // Query is the query configuration. + Query QueryConfiguration `yaml:"query"` + // Limits specifies limits on per-query resource usage. Limits LimitsConfiguration `yaml:"limits"` @@ -188,6 +193,19 @@ type ResultOptions struct { KeepNans bool `yaml:"keepNans"` } +// QueryConfiguration is the query configuration. +type QueryConfiguration struct { + Timeout *time.Duration `yaml:"timeout"` +} + +// TimeoutOrDefault returns the configured timeout or default value. +func (c QueryConfiguration) TimeoutOrDefault() time.Duration { + if v := c.Timeout; v != nil { + return *v + } + return defaultQueryTimeout +} + // LimitsConfiguration represents limitations on resource usage in the query // instance. Limits are split between per-query and global limits. type LimitsConfiguration struct { diff --git a/src/query/api/v1/httpd/handler.go b/src/query/api/v1/httpd/handler.go index 5bee02005f..71b478f329 100644 --- a/src/query/api/v1/httpd/handler.go +++ b/src/query/api/v1/httpd/handler.go @@ -22,7 +22,6 @@ package httpd import ( "encoding/json" - "errors" "fmt" "net/http" _ "net/http/pprof" // needed for pprof handler registration @@ -121,15 +120,11 @@ func NewHandler( handlerWithMiddleware := applyMiddleware(r, opentracing.GlobalTracer()) - var timeoutOpts = &prometheus.TimeoutOpts{} - if embeddedDbCfg == nil || embeddedDbCfg.Client.FetchTimeout == nil { - timeoutOpts.FetchTimeout = defaultTimeout - } else { - if *embeddedDbCfg.Client.FetchTimeout <= 0 { - return nil, errors.New("m3db client fetch timeout should be > 0") - } - - timeoutOpts.FetchTimeout = *embeddedDbCfg.Client.FetchTimeout + timeout := cfg.Query.TimeoutOrDefault() + if embeddedDbCfg != nil && + embeddedDbCfg.Client.FetchTimeout != nil && + *embeddedDbCfg.Client.FetchTimeout > timeout { + timeout = *embeddedDbCfg.Client.FetchTimeout } return &Handler{ @@ -144,7 +139,6 @@ func NewHandler( embeddedDbCfg: embeddedDbCfg, createdAt: time.Now(), tagOptions: tagOptions, - timeoutOpts: timeoutOpts, enforcer: enforcer, fetchOptionsBuilder: fetchOptionsBuilder, queryContextOptions: queryContextOptions, @@ -152,6 +146,9 @@ func NewHandler( cpuProfileDuration: cpuProfileDuration, placementServiceNames: placementServiceNames, serviceOptionDefaults: serviceOptionDefaults, + timeoutOpts: &prometheus.TimeoutOpts{ + FetchTimeout: timeout, + }, }, nil }