Skip to content

Commit

Permalink
distsql: add setting "sql.defaults.distsql"
Browse files Browse the repository at this point in the history
This cluster setting replaces the environment variable. The session
variable accessible through `SET distsql = ?` is still available, and
defaults to the cluster setting upon session creation. Closes #15045.
  • Loading branch information
Arjun Narayan committed Apr 25, 2017
1 parent f1f8c98 commit 7cfc639
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 34 deletions.
10 changes: 0 additions & 10 deletions pkg/sql/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,3 @@ func (m *LeaseManager) ExpireLeases(clock *hlc.Clock) {
}
m.tableNames.mu.Unlock()
}

// SetDefaultDistSQLMode changes the default DistSQL mode; returns a function
// that can be used to restore the previous mode.
func SetDefaultDistSQLMode(mode string) func() {
prevMode := defaultDistSQLMode
defaultDistSQLMode = distSQLExecModeFromString(mode)
return func() {
defaultDistSQLMode = prevMode
}
}
17 changes: 13 additions & 4 deletions pkg/sql/logic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1321,9 +1321,8 @@ func (t *logicTest) success(file string) {
}
}

func (t *logicTest) setupAndRunFile(path string, config testClusterConfig) {
func (t *logicTest) runFile(path string, config testClusterConfig) {
defer t.close()
t.setup(config.numNodes, config.useFakeSpanResolver)

defer func() {
if r := recover(); r != nil {
Expand Down Expand Up @@ -1438,9 +1437,15 @@ func TestLogic(t *testing.T) {
if len(paths) == 0 {
continue
}
var startupFuncs []func(lt *logicTest)
var cleanupFuncs []func()
if cfg.defaultDistSQLMode != "" {
cleanupFuncs = append(cleanupFuncs, sql.SetDefaultDistSQLMode(cfg.defaultDistSQLMode))
startupFuncs = append(startupFuncs, func(lt *logicTest) {
// TODO(DONOTMERGE)
})
cleanupFuncs = append(cleanupFuncs, func() {
// TODO(DONOTMERGE): how we run a SET CLUSTER SETTING distsql.mode = off SQL command here?
})
}
// Top-level test: one per test configuration.
t.Run(cfg.name, func(t *testing.T) {
Expand All @@ -1466,7 +1471,11 @@ func TestLogic(t *testing.T) {
if *printErrorSummary {
defer lt.printErrorSummary()
}
lt.setupAndRunFile(path, cfg)
lt.setup(cfg.numNodes, cfg.useFakeSpanResolver)
for _, startupFunc := range startupFuncs {
startupFunc(&lt)
}
lt.runFile(path, cfg)

progress.Lock()
defer progress.Unlock()
Expand Down
32 changes: 13 additions & 19 deletions pkg/sql/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package sql
import (
"fmt"
"net"
"strings"
"time"

opentracing "github.com/opentracing/opentracing-go"
Expand Down Expand Up @@ -93,25 +92,19 @@ const (
distSQLAlways
)

func distSQLExecModeFromString(val string) distSQLExecMode {
switch strings.ToUpper(val) {
case "OFF":
return distSQLOff
case "AUTO":
return distSQLAuto
case "ON":
return distSQLOn
case "ALWAYS":
return distSQLAlways
default:
panic(fmt.Sprintf("unknown DistSQL mode %s", val))
}
func distSQLExecModeFromInt(val int64) distSQLExecMode {
return distSQLExecMode(val)
}

// defaultDistSQLMode controls the default DistSQL mode (see above). It can
// still be overridden per-session using `SET DIST_SQL = ...`.
var defaultDistSQLMode = distSQLExecModeFromString(
envutil.EnvOrDefaultString("COCKROACH_DISTSQL_MODE", "OFF"),
var distSQLClusterExecMode = settings.RegisterEnumSetting(
"sql.defaults.distsql",
"Default distributed SQL execution mode",
"Off",
map[string]int64{
"Off": int64(distSQLOff),
"Auto": int64(distSQLAuto),
"On": int64(distSQLOn),
},
)

// Session contains the state of a SQL client connection.
Expand Down Expand Up @@ -262,9 +255,10 @@ func NewSession(
ctx context.Context, args SessionArgs, e *Executor, remote net.Addr, memMetrics *MemoryMetrics,
) *Session {
ctx = e.AnnotateCtx(ctx)

s := &Session{
Database: args.Database,
DistSQLMode: defaultDistSQLMode,
DistSQLMode: distSQLExecModeFromInt(distSQLClusterExecMode.Get()),
SearchPath: parser.SearchPath{"pg_catalog"},
Location: time.UTC,
User: args.User,
Expand Down
4 changes: 3 additions & 1 deletion pkg/sql/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ var varGen = map[string]sessionVar{
p.session.DistSQLMode = distSQLOff
case parser.ReNormalizeName("on"):
p.session.DistSQLMode = distSQLOn
case parser.ReNormalizeName("auto"):
p.session.DistSQLMode = distSQLAuto
case parser.ReNormalizeName("always"):
p.session.DistSQLMode = distSQLAlways
default:
Expand All @@ -141,7 +143,7 @@ var varGen = map[string]sessionVar{
return "auto"
},
Reset: func(p *planner) error {
p.session.DistSQLMode = defaultDistSQLMode
p.session.DistSQLMode = distSQLExecModeFromInt(distSQLClusterExecMode.Get())
return nil
},
},
Expand Down

0 comments on commit 7cfc639

Please sign in to comment.