surrealpatch/db/executor.go

567 lines
12 KiB
Go
Raw Normal View History

2017-02-23 10:13:13 +00:00
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
2017-11-16 20:53:39 +00:00
"time"
"context"
"runtime/debug"
"github.com/abcum/surreal/cnf"
2017-02-23 10:13:13 +00:00
"github.com/abcum/surreal/kvs"
2017-11-16 20:53:39 +00:00
"github.com/abcum/surreal/log"
2017-02-23 10:13:13 +00:00
"github.com/abcum/surreal/mem"
"github.com/abcum/surreal/sql"
)
2017-11-16 20:53:39 +00:00
type executor struct {
2019-01-31 10:03:50 +00:00
id string
ns string
db string
2018-09-28 21:06:07 +00:00
dbo *mem.Cache
time int64
lock *mutex
2019-01-23 01:27:30 +00:00
opts *options
2018-09-28 21:06:07 +00:00
send chan *Response
cache *cache
2017-02-23 10:13:13 +00:00
}
2019-01-31 10:03:50 +00:00
func newExecutor(id, ns, db string) (e *executor) {
2017-11-16 20:53:39 +00:00
e = executorPool.Get().(*executor)
2019-01-31 10:03:50 +00:00
e.id = id
e.ns = ns
e.db = db
2017-11-16 20:53:39 +00:00
e.dbo = mem.New()
2019-01-23 01:27:30 +00:00
e.opts = newOptions()
2017-11-16 20:53:39 +00:00
e.send = make(chan *Response)
2018-09-28 21:06:07 +00:00
e.cache = new(cache)
return
2017-11-16 20:53:39 +00:00
}
func (e *executor) execute(ctx context.Context, ast *sql.Query) {
// Ensure that the executor is added back into
// the executor pool when the executor has
// finished processing the request.
defer executorPool.Put(e)
// Ensure that the query responses channel is
// closed when the full query has been processed
// and dealt with.
defer close(e.send)
// If we are making use of a global transaction
// which is not committed at the end of the
// query set, then cancel the transaction.
defer func() {
if e.dbo.TX != nil {
e.dbo.Cancel()
2019-01-31 10:03:50 +00:00
clear(e.id)
2017-11-16 20:53:39 +00:00
}
}()
// If we have panicked during query execution
// then ensure that we recover from the error
// and print the error to the log.
defer func() {
if err := recover(); err != nil {
2019-01-31 10:03:50 +00:00
log.WithPrefix(logKeyDB).WithFields(map[string]interface{}{
logKeyId: e.id, logKeyStack: string(debug.Stack()),
2017-11-16 20:53:39 +00:00
}).Errorln(err)
}
}()
// Loop over the defined query statements and
// process them, while listening for the quit
// channel to see if the client has gone away.
for _, stm := range ast.Statements {
select {
case <-ctx.Done():
return
default:
2019-01-31 10:03:50 +00:00
e.conduct(ctx, stm)
}
}
2017-11-16 20:53:39 +00:00
2019-01-31 10:03:50 +00:00
}
2017-11-16 20:53:39 +00:00
2019-01-31 10:03:50 +00:00
func (e *executor) conduct(ctx context.Context, stm sql.Statement) {
2017-11-16 20:53:39 +00:00
2019-01-31 10:03:50 +00:00
var err error
var now time.Time
var rsp *Response
var buf []*Response
var res []interface{}
2017-11-16 20:53:39 +00:00
2019-01-31 10:03:50 +00:00
// When in debugging mode, log every sql
// query, along with the query execution
// speed, so we can analyse slow queries.
2017-11-16 20:53:39 +00:00
2019-01-31 10:03:50 +00:00
log := log.WithPrefix(logKeySql).WithFields(map[string]interface{}{
logKeyId: e.id,
logKeyKind: ctx.Value(ctxKeyKind),
logKeyVars: ctx.Value(ctxKeyVars),
})
2017-11-16 20:53:39 +00:00
2019-01-31 10:03:50 +00:00
if len(e.ns) != 0 {
log = log.WithField(logKeyNS, e.ns)
}
2017-11-16 20:53:39 +00:00
2019-01-31 10:03:50 +00:00
if len(e.db) != 0 {
log = log.WithField(logKeyDB, e.db)
}
2017-11-16 20:53:39 +00:00
2019-01-31 10:03:50 +00:00
// If we are not inside a global transaction
// then reset the error to nil so that the
// next statement is not ignored.
if e.dbo.TX == nil {
err, now = nil, time.Now()
}
2018-05-02 01:31:31 +00:00
2019-01-31 10:03:50 +00:00
// Check to see if the current statement is
// a TRANSACTION statement, and if it is
// then deal with it and move on to the next.
2017-11-16 20:53:39 +00:00
2019-01-31 10:03:50 +00:00
switch stm.(type) {
case *sql.BeginStatement:
e.lock = new(mutex)
err = e.begin(ctx, true)
return
case *sql.CancelStatement:
err, buf = e.cancel(buf, err, e.send)
if err != nil {
clear(e.id)
} else {
clear(e.id)
}
return
case *sql.CommitStatement:
err, buf = e.commit(buf, err, e.send)
if err != nil {
clear(e.id)
} else {
flush(e.id)
}
return
}
2017-11-16 20:53:39 +00:00
2019-01-31 10:03:50 +00:00
// If an error has occured and we are inside
// a global transaction, then ignore all
// subsequent statements in the transaction.
2017-11-16 20:53:39 +00:00
2019-01-31 10:03:50 +00:00
if err == nil {
res, err = e.operate(ctx, stm)
} else {
res, err = []interface{}{}, errQueryNotExecuted
}
2017-11-16 20:53:39 +00:00
2019-01-31 10:03:50 +00:00
rsp = &Response{
Time: time.Since(now).String(),
Status: status(err),
Detail: detail(err),
Result: append([]interface{}{}, res...),
}
// Log the sql statement along with the
// query duration time, and mark it as
// an error if the query failed.
switch err.(type) {
default:
log.WithFields(map[string]interface{}{
logKeyTime: time.Since(now).String(),
}).Debugln(stm)
case error:
log.WithFields(map[string]interface{}{
logKeyTime: time.Since(now).String(),
logKeyError: detail(err),
}).Errorln(stm)
}
2017-11-16 20:53:39 +00:00
2019-01-31 10:03:50 +00:00
// If we are not inside a global transaction
// then we can output the statement response
// immediately to the channel.
if e.dbo.TX == nil {
e.send <- rsp
}
// If we are inside a global transaction we
// must buffer the responses for output at
// the end of the transaction.
if e.dbo.TX != nil {
switch stm.(type) {
case *sql.ReturnStatement:
buf = groupd(buf, rsp)
default:
buf = append(buf, rsp)
}
2017-11-16 20:53:39 +00:00
}
}
func (e *executor) operate(ctx context.Context, stm sql.Statement) (res []interface{}, err error) {
var loc bool
var trw bool
var canc context.CancelFunc
// If we are not inside a global transaction
// then grab a new transaction, ensuring that
// it is closed at the end.
if e.dbo.TX == nil {
loc = true
switch stm := stm.(type) {
case sql.WriteableStatement:
trw = stm.Writeable()
default:
trw = false
}
err = e.begin(ctx, trw)
2017-11-16 20:53:39 +00:00
if err != nil {
return
}
defer e.dbo.Cancel()
// Let's create a new mutex for just this
// local transaction, so we can track any
// recursive queries and race errors.
e.lock = new(mutex)
2017-11-16 20:53:39 +00:00
}
// Mark the beginning of this statement so we
// can monitor the running time, and ensure
// it runs no longer than specified.
if cnf.Settings.Query.Timeout > 0 {
2019-01-31 10:03:50 +00:00
if perm(ctx) != cnf.AuthKV {
ctx, canc = context.WithTimeout(ctx, cnf.Settings.Query.Timeout)
defer func() {
if tim := ctx.Err(); err == nil && tim != nil {
res, err = nil, &TimerError{timer: cnf.Settings.Query.Timeout}
}
canc()
}()
}
}
// Mark the beginning of this statement so we
// can monitor the running time, and ensure
// it runs no longer than specified.
2017-11-16 20:53:39 +00:00
if stm, ok := stm.(sql.KillableStatement); ok {
if stm.Duration() > 0 {
ctx, canc = context.WithTimeout(ctx, stm.Duration())
defer func() {
if tim := ctx.Err(); err == nil && tim != nil {
res, err = nil, &TimerError{timer: stm.Duration()}
}
canc()
2017-11-16 20:53:39 +00:00
}()
}
}
// Specify a new time for the current executor
// iteration, so that all subqueries and async
// events are saved with the same version time.
e.time = time.Now().UnixNano()
2017-11-16 20:53:39 +00:00
// Execute the defined statement, receiving the
// result set, and any errors which occured
// while processing the query.
switch stm := stm.(type) {
2019-01-31 10:03:50 +00:00
case *sql.OptStatement:
res, err = e.executeOpt(ctx, stm)
case *sql.UseStatement:
res, err = e.executeUse(ctx, stm)
2017-11-16 20:53:39 +00:00
case *sql.RunStatement:
res, err = e.executeRun(ctx, stm)
2017-11-16 20:53:39 +00:00
case *sql.InfoStatement:
res, err = e.executeInfo(ctx, stm)
case *sql.LetStatement:
res, err = e.executeLet(ctx, stm)
case *sql.ReturnStatement:
res, err = e.executeReturn(ctx, stm)
case *sql.LiveStatement:
res, err = e.executeLive(ctx, stm)
case *sql.KillStatement:
res, err = e.executeKill(ctx, stm)
2019-01-31 10:03:50 +00:00
case *sql.IfelseStatement:
res, err = e.executeIfelse(ctx, stm)
2017-11-16 20:53:39 +00:00
case *sql.SelectStatement:
res, err = e.executeSelect(ctx, stm)
case *sql.CreateStatement:
res, err = e.executeCreate(ctx, stm)
case *sql.UpdateStatement:
res, err = e.executeUpdate(ctx, stm)
case *sql.DeleteStatement:
res, err = e.executeDelete(ctx, stm)
case *sql.RelateStatement:
res, err = e.executeRelate(ctx, stm)
case *sql.InsertStatement:
res, err = e.executeInsert(ctx, stm)
case *sql.UpsertStatement:
res, err = e.executeUpsert(ctx, stm)
case *sql.DefineNamespaceStatement:
res, err = e.executeDefineNamespace(ctx, stm)
case *sql.RemoveNamespaceStatement:
res, err = e.executeRemoveNamespace(ctx, stm)
case *sql.DefineDatabaseStatement:
res, err = e.executeDefineDatabase(ctx, stm)
case *sql.RemoveDatabaseStatement:
res, err = e.executeRemoveDatabase(ctx, stm)
case *sql.DefineLoginStatement:
res, err = e.executeDefineLogin(ctx, stm)
case *sql.RemoveLoginStatement:
res, err = e.executeRemoveLogin(ctx, stm)
case *sql.DefineTokenStatement:
res, err = e.executeDefineToken(ctx, stm)
case *sql.RemoveTokenStatement:
res, err = e.executeRemoveToken(ctx, stm)
case *sql.DefineScopeStatement:
res, err = e.executeDefineScope(ctx, stm)
case *sql.RemoveScopeStatement:
res, err = e.executeRemoveScope(ctx, stm)
case *sql.DefineTableStatement:
res, err = e.executeDefineTable(ctx, stm)
case *sql.RemoveTableStatement:
res, err = e.executeRemoveTable(ctx, stm)
case *sql.DefineEventStatement:
res, err = e.executeDefineEvent(ctx, stm)
case *sql.RemoveEventStatement:
res, err = e.executeRemoveEvent(ctx, stm)
case *sql.DefineFieldStatement:
res, err = e.executeDefineField(ctx, stm)
case *sql.RemoveFieldStatement:
res, err = e.executeRemoveField(ctx, stm)
case *sql.DefineIndexStatement:
res, err = e.executeDefineIndex(ctx, stm)
case *sql.RemoveIndexStatement:
res, err = e.executeRemoveIndex(ctx, stm)
}
2018-04-14 17:02:58 +00:00
// If the context is already closed or failed,
// then ignore this result, clear all queued
// changes, and reset the transaction.
2017-11-16 20:53:39 +00:00
select {
case <-ctx.Done():
e.dbo.Cancel()
e.dbo.Reset()
2019-01-31 10:03:50 +00:00
clear(e.id)
2017-11-16 20:53:39 +00:00
default:
// If this is a local transaction for only the
// current statement, then commit or cancel
// depending on the result error.
2017-11-16 20:53:39 +00:00
if loc && e.dbo.Closed() == false {
// As this is a local transaction then
// make sure we reset the transaction
// context.
defer e.dbo.Reset()
// If there was an error with the query
// then clear the queued changes and
// return immediately.
if err != nil {
2017-11-16 20:53:39 +00:00
e.dbo.Cancel()
2019-01-31 10:03:50 +00:00
clear(e.id)
return
}
// Otherwise check if this is a read or
// a write transaction, and attempt to
// Cancel or Commit, returning any errors.
if !trw {
if err = e.dbo.Cancel(); err != nil {
2019-01-31 10:03:50 +00:00
clear(e.id)
} else {
2019-01-31 10:03:50 +00:00
clear(e.id)
}
2017-11-16 20:53:39 +00:00
} else {
if err = e.dbo.Commit(); err != nil {
2019-01-31 10:03:50 +00:00
clear(e.id)
} else {
2019-01-31 10:03:50 +00:00
flush(e.id)
}
2017-11-16 20:53:39 +00:00
}
2017-11-16 20:53:39 +00:00
}
}
return
}
func (e *executor) begin(ctx context.Context, rw bool) (err error) {
2017-11-16 20:53:39 +00:00
if e.dbo.TX == nil {
e.dbo = mem.New()
e.dbo.TX, err = db.Begin(ctx, rw)
2017-11-16 20:53:39 +00:00
}
return
}
func (e *executor) cancel(buf []*Response, err error, chn chan<- *Response) (error, []*Response) {
defer e.dbo.Reset()
2017-11-16 20:53:39 +00:00
if e.dbo.TX == nil {
return nil, buf
}
err = e.dbo.Cancel()
2017-11-16 20:53:39 +00:00
for _, v := range buf {
v.Status = "ERR"
v.Result = []interface{}{}
v.Detail = "Transaction cancelled"
2017-11-16 20:53:39 +00:00
chn <- v
}
for i := len(buf) - 1; i >= 0; i-- {
buf[len(buf)-1] = nil
buf = buf[:len(buf)-1]
}
return err, buf
2017-11-16 20:53:39 +00:00
}
func (e *executor) commit(buf []*Response, err error, chn chan<- *Response) (error, []*Response) {
defer e.dbo.Reset()
2017-11-16 20:53:39 +00:00
if e.dbo.TX == nil {
return nil, buf
}
if err != nil {
err = e.dbo.Cancel()
2017-11-16 20:53:39 +00:00
} else {
err = e.dbo.Commit()
2017-11-16 20:53:39 +00:00
}
for _, v := range buf {
if err != nil {
v.Status = "ERR"
v.Result = []interface{}{}
v.Detail = "Transaction failed: " + err.Error()
2017-11-16 20:53:39 +00:00
}
chn <- v
}
for i := len(buf) - 1; i >= 0; i-- {
buf[len(buf)-1] = nil
buf = buf[:len(buf)-1]
}
return err, buf
2017-11-16 20:53:39 +00:00
}
2017-11-16 20:53:39 +00:00
func status(e error) (s string) {
switch e.(type) {
default:
return "OK"
case *kvs.DBError:
return "ERR_DB"
case *kvs.KVError:
return "ERR_KV"
case *PermsError:
return "ERR_PE"
case *ExistError:
return "ERR_EX"
2017-11-16 20:53:39 +00:00
case *FieldError:
return "ERR_FD"
case *IndexError:
return "ERR_IX"
2018-05-09 21:51:23 +00:00
case *TimerError:
return "ERR_TO"
2017-11-16 20:53:39 +00:00
case error:
return "ERR"
}
2017-02-23 10:13:13 +00:00
}
2017-11-16 20:53:39 +00:00
func detail(e error) (s string) {
switch err := e.(type) {
default:
return
case error:
return err.Error()
}
2017-02-23 10:13:13 +00:00
}
2017-11-16 20:53:39 +00:00
func groupd(buf []*Response, rsp *Response) []*Response {
for i := len(buf) - 1; i >= 0; i-- {
buf[len(buf)-1] = nil
buf = buf[:len(buf)-1]
}
return append(buf, rsp)
2017-02-23 10:13:13 +00:00
}