Skip to content

Commit

Permalink
[query] Add ability to set default query timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
robskillington committed Mar 24, 2020
1 parent c346552 commit c90a671
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
18 changes: 18 additions & 0 deletions src/cmd/services/m3query/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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"`

Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 8 additions & 11 deletions src/query/api/v1/httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package httpd

import (
"encoding/json"
"errors"
"fmt"
"net/http"
_ "net/http/pprof" // needed for pprof handler registration
Expand Down Expand Up @@ -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{
Expand All @@ -144,14 +139,16 @@ func NewHandler(
embeddedDbCfg: embeddedDbCfg,
createdAt: time.Now(),
tagOptions: tagOptions,
timeoutOpts: timeoutOpts,
enforcer: enforcer,
fetchOptionsBuilder: fetchOptionsBuilder,
queryContextOptions: queryContextOptions,
instrumentOpts: instrumentOpts,
cpuProfileDuration: cpuProfileDuration,
placementServiceNames: placementServiceNames,
serviceOptionDefaults: serviceOptionDefaults,
timeoutOpts: &prometheus.TimeoutOpts{
FetchTimeout: timeout,
},
}, nil
}

Expand Down

0 comments on commit c90a671

Please sign in to comment.