Skip to content

feat(flags): Add query timeout as a limit config #7599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dgraph/cmd/alpha/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ they form a Raft group and provide synchronous replication.
Flag("disallow-drop",
"Set disallow-drop to true to block drop-all and drop-data operation. It still"+
" allows dropping attributes and types.").
Flag("query-timeout",
"Maximum time after which a query execution will fail. If set to"+
" 0, the timeout is infinite.").
String())

flag.String("ludicrous", worker.LudicrousDefaults, z.NewSuperFlagHelp(worker.LudicrousDefaults).
Expand Down Expand Up @@ -731,6 +734,7 @@ func run() {
x.Config.LimitMutationsNquad = int(x.Config.Limit.GetInt64("mutations-nquad"))
x.Config.LimitQueryEdge = x.Config.Limit.GetUint64("query-edge")
x.Config.BlockClusterWideDrop = x.Config.Limit.GetBool("disallow-drop")
x.Config.QueryTimeout = x.Config.Limit.GetDuration("query-timeout")

x.Config.GraphQL = z.NewSuperFlag(Alpha.Conf.GetString("graphql")).MergeAndCheckDefault(
worker.GraphQLDefaults)
Expand Down
10 changes: 10 additions & 0 deletions edgraph/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,16 @@ func (s *Server) QueryGraphQL(ctx context.Context, req *api.Request,
// Query handles queries or mutations
func (s *Server) Query(ctx context.Context, req *api.Request) (*api.Response, error) {
ctx = x.AttachJWTNamespace(ctx)
// Add a timeout for queries which don't have a deadline set. We don't want to
// apply a timeout if it's a mutation, that's currently handled by flag
// "abort_older_than".
if req.GetMutations() == nil && x.Config.QueryTimeout != 0 {
if d, _ := ctx.Deadline(); d.IsZero() {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, x.Config.QueryTimeout)
defer cancel()
}
}
return s.doQuery(ctx, &Request{req: req, doAuth: getAuthMode(ctx)})
}

Expand Down
2 changes: 1 addition & 1 deletion worker/server_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const (
CDCDefaults = `file=; kafka=; sasl_user=; sasl_password=; ca_cert=; client_cert=; ` +
`client_key=;`
LimitDefaults = `mutations=allow; query-edge=1000000; normalize-node=10000; ` +
`mutations-nquad=1000000; disallow-drop=false;`
`mutations-nquad=1000000; disallow-drop=false; query-timeout=0ms;`
GraphQLDefaults = `introspection=true; debug=false; extensions=true; poll-interval=1s; ` +
`lambda-url=;`
)
Expand Down
10 changes: 6 additions & 4 deletions x/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ type Options struct {
// normalize directive
// mutations-nquad int - maximum number of nquads that can be inserted in a mutation request
// BlockDropAll bool - if set to true, the drop all operation will be rejected by the server.
Limit *z.SuperFlag
LimitMutationsNquad int
LimitQueryEdge uint64
BlockClusterWideDrop bool
// query-timeout duration - Maximum time after which a query execution will fail.
Limit *z.SuperFlag
LimitMutationsNquad int
LimitQueryEdge uint64
BlockClusterWideDrop bool
QueryTimeout time.Duration

// GraphQL options:
//
Expand Down