Skip to content

Commit

Permalink
Do not cancel context for async queries on success
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos <[email protected]>
  • Loading branch information
carlosms committed Oct 25, 2019
1 parent 68b087c commit 6059987
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
12 changes: 12 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ func (e *Engine) Query(
return analyzed.Schema(), iter, nil
}

// Async returns true if the query is async. If there are any errors with the
// query it returns false
func (e *Engine) Async(ctx *sql.Context, query string) bool {
parsed, err := parse.Parse(ctx, query)
if err != nil {
return false
}

asyncNode, ok := parsed.(sql.AsyncNode)
return ok && asyncNode.IsAsync()
}

// AddDatabase adds the given database to the catalog.
func (e *Engine) AddDatabase(db sql.Database) {
e.Catalog.AddDatabase(db)
Expand Down
10 changes: 7 additions & 3 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ func (h *Handler) ComQuery(
callback func(*sqltypes.Result) error,
) (err error) {
ctx := h.sm.NewContextWithQuery(c, query)
newCtx, cancel := context.WithCancel(ctx)
defer cancel()
ctx = ctx.WithContext(newCtx)

if !h.e.Async(ctx, query) {
newCtx, cancel := context.WithCancel(ctx)
ctx = ctx.WithContext(newCtx)

defer cancel()
}

handled, err := h.handleKill(c, query)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions sql/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ type OpaqueNode interface {
Opaque() bool
}

// AsyncNode is a node that can be executed asynchronously.
type AsyncNode interface {
// IsAsync reports whether the node is async or not.
IsAsync() bool
}

// Expressioner is a node that contains expressions.
type Expressioner interface {
// Expressions returns the list of expressions contained by the node.
Expand Down
5 changes: 5 additions & 0 deletions sql/plan/create_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ func (c *CreateIndex) WithChildren(children ...sql.Node) (sql.Node, error) {
return &nc, nil
}

// IsAsync implements the AsyncNode interface.
func (c *CreateIndex) IsAsync() bool {
return c.Async
}

// getColumnsAndPrepareExpressions extracts the unique columns required by all
// those expressions and fixes the indexes of the GetFields in the expressions
// to match a row with only the returned columns in that same order.
Expand Down

0 comments on commit 6059987

Please sign in to comment.