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 24, 2017
1 parent fc743e6 commit 9e159f1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 28 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
}
}
5 changes: 4 additions & 1 deletion pkg/sql/logic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,10 @@ func TestLogic(t *testing.T) {
}
var cleanupFuncs []func()
if cfg.defaultDistSQLMode != "" {
cleanupFuncs = append(cleanupFuncs, sql.SetDefaultDistSQLMode(cfg.defaultDistSQLMode))
// TODO(DONOTMERGE): how do we run a SET CLUSTER SETTING distsql.mode = on SQL command here?
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 Down
34 changes: 18 additions & 16 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,27 @@ const (
distSQLAlways
)

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

// 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",
1,
map[string]int64{
"Off": 0,
"Auto": 1,
"On": 2,
"Always": -1,
},
)

// Session contains the state of a SQL client connection.
Expand Down Expand Up @@ -262,9 +263,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 9e159f1

Please sign in to comment.