Skip to content
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

engine: Make ReadOnly a toggleable atomic.Bool. #1961

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 12 additions & 14 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"

"github.com/dolthub/vitess/go/sqltypes"
querypb "github.com/dolthub/vitess/go/vt/proto/query"
Expand Down Expand Up @@ -136,7 +137,7 @@ type Engine struct {
ProcessList sql.ProcessList
MemoryManager *sql.MemoryManager
BackgroundThreads *sql.BackgroundThreads
IsReadOnly bool
ReadOnly atomic.Bool
IsServerLocked bool
PreparedDataCache *PreparedDataCache
mu *sync.Mutex
Expand Down Expand Up @@ -168,17 +169,18 @@ func New(a *analyzer.Analyzer, cfg *Config) *Engine {
})
a.Catalog.RegisterFunction(emptyCtx, function.GetLockingFuncs(ls)...)

return &Engine{
ret := &Engine{
Analyzer: a,
MemoryManager: sql.NewMemoryManager(sql.ProcessMemory),
ProcessList: NewProcessList(),
LS: ls,
BackgroundThreads: sql.NewBackgroundThreads(),
IsReadOnly: cfg.IsReadOnly,
IsServerLocked: cfg.IsServerLocked,
PreparedDataCache: NewPreparedDataCache(),
mu: &sync.Mutex{},
}
ret.ReadOnly.Store(cfg.IsReadOnly)
return ret
}

// NewDefault creates a new default Engine.
Expand Down Expand Up @@ -635,19 +637,15 @@ func (e *Engine) WithBackgroundThreads(b *sql.BackgroundThreads) *Engine {
return e
}

func (e *Engine) IsReadOnly() bool {
return e.ReadOnly.Load()
}

// readOnlyCheck checks to see if the query is valid with the modification setting of the engine.
func (e *Engine) readOnlyCheck(node sql.Node) error {
if plan.IsDDLNode(node) {
if e.IsReadOnly {
return sql.ErrReadOnly.New()
} else if e.IsServerLocked {
return sql.ErrDatabaseWriteLocked.New()
}
}
switch node.(type) {
case
*plan.DeleteFrom, *plan.InsertInto, *plan.Update, *plan.LockTables, *plan.UnlockTables:
if e.IsReadOnly {
nodeIsReadOnly := plan.IsReadOnly(node)
if !nodeIsReadOnly {
if e.IsReadOnly() {
reltuk marked this conversation as resolved.
Show resolved Hide resolved
return sql.ErrReadOnly.New()
} else if e.IsServerLocked {
return sql.ErrDatabaseWriteLocked.New()
Expand Down
7 changes: 6 additions & 1 deletion enginetest/enginetests.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ func TestOrderByGroupBy(t *testing.T, harness Harness) {
func TestReadOnly(t *testing.T, harness Harness) {
harness.Setup(setup.Mytable...)
e := mustNewEngine(t, harness)
e.IsReadOnly = true
e.ReadOnly.Store(true)
defer e.Close()

RunQuery(t, e, harness, `SELECT i FROM mytable`)
Expand All @@ -634,6 +634,11 @@ func TestReadOnly(t *testing.T, harness Harness) {
`INSERT INTO mytable (i, s) VALUES(42, 'yolo')`,
`CREATE VIEW myview3 AS SELECT i FROM mytable`,
`DROP VIEW myview`,
`DROP DATABASE mydb`,
`CREATE DATABASE newdb`,
`CREATE USER tester@localhost`,
`CREATE ROLE test_role`,
`GRANT SUPER ON * TO 'root'@'localhost'`,
}

for _, query := range writingQueries {
Expand Down
46 changes: 46 additions & 0 deletions sql/plan/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,49 @@ func IsShowNode(node sql.Node) bool {
func IsNoRowNode(node sql.Node) bool {
return IsDDLNode(node) || IsShowNode(node)
}

func IsReadOnly(node sql.Node) bool {
isExplain := false
transform.Inspect(node, func(n sql.Node) bool {
switch n.(type) {
case *DescribeQuery:
isExplain = true
return false
}
return true
})
if isExplain {
return true
}

if IsDDLNode(node) {
return false
}

switch node.(type) {
case *DeleteFrom, *InsertInto, *Update, *LockTables, *UnlockTables:
return false
}

isPrivNodeP := func(n sql.Node) bool {
switch node.(type) {
case *CreateUser, *DropUser, *RenameUser, *CreateRole, *DropRole, *Grant, *GrantRole, *GrantProxy, *Revoke, *RevokeRole, *RevokeAll, *RevokeProxy:
return true
}
return false
}
isPrivNode := false
transform.Inspect(node, func(n sql.Node) bool {
if isPrivNodeP(n) {
isPrivNode = true
return false
}
return true
})

if isPrivNode {
return false
}

return true
}