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

dolt bootstrap refactor #8708

Merged
merged 20 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
9a2d7c1
First pass on forcing .dolt/tmp
macneale4 Oct 30, 2024
9a8d951
dolt init working again
macneale4 Nov 4, 2024
b9281a7
Verify that datadir is passed through correctly in server.go
macneale4 Dec 3, 2024
7d8e2ff
Allow a 'tmp' directory during dolt init
macneale4 Dec 5, 2024
563317d
Handle terminations and status fully in the parse* method
macneale4 Dec 18, 2024
c07aa22
[ga-format-pr] Run go/utils/repofmt/format_repo.sh and go/Godeps/upda…
macneale4 Dec 18, 2024
f5a0c44
Fix several breaking profile tests
macneale4 Dec 18, 2024
2b15957
Pass through dataDirOverride
macneale4 Dec 19, 2024
da3c002
Add the cwd to cliCtx and use it in server config resolution
macneale4 Dec 23, 2024
32f091d
Update temp if rename fails using the real datadir
macneale4 Dec 23, 2024
7d33631
rename to bootstrapConfig and document
macneale4 Dec 26, 2024
911a50f
Never use original args after parsed. Set to nil.
macneale4 Dec 26, 2024
0a8d550
Ensure --data-dir is only specified once
macneale4 Dec 26, 2024
e99494d
[ga-format-pr] Run go/utils/repofmt/format_repo.sh and go/Godeps/upda…
macneale4 Dec 26, 2024
55b1c0f
Clean up. rename, document
macneale4 Dec 26, 2024
736c490
make the argparse insert functionality a little more robust and test it
macneale4 Dec 26, 2024
06f747a
relative config file path from cwd bug
macneale4 Dec 27, 2024
0eeb791
Add test for conflicting data dirs
macneale4 Jan 2, 2025
1ff6428
Minor sanity check
macneale4 Jan 2, 2025
5195111
Update go/cmd/dolt/commands/sqlserver/command_line_config.go
macneale4 Jan 2, 2025
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
16 changes: 14 additions & 2 deletions go/cmd/dolt/cli/cli_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/dolthub/dolt/go/cmd/dolt/errhand"
"github.com/dolthub/dolt/go/libraries/doltcore/env"
"github.com/dolthub/dolt/go/libraries/utils/argparser"
"github.com/dolthub/dolt/go/libraries/utils/filesys"
)

// LateBindQueryist is a function that will be called the first time Queryist is needed for use. Input is a context which
Expand All @@ -39,17 +40,23 @@ type LateBindQueryist func(ctx context.Context) (Queryist, *sql.Context, func(),
type CliContext interface {
// GlobalArgs returns the arguments passed before the subcommand.
GlobalArgs() *argparser.ArgParseResults
WorkingDir() filesys.Filesys
Config() *env.DoltCliConfig
QueryEngine(ctx context.Context) (Queryist, *sql.Context, func(), error)
}

// NewCliContext creates a new CliContext instance. Arguments must not be nil.
func NewCliContext(args *argparser.ArgParseResults, config *env.DoltCliConfig, latebind LateBindQueryist) (CliContext, errhand.VerboseError) {
func NewCliContext(args *argparser.ArgParseResults, config *env.DoltCliConfig, cwd filesys.Filesys, latebind LateBindQueryist) (CliContext, errhand.VerboseError) {
if args == nil || config == nil || latebind == nil {
return nil, errhand.VerboseErrorFromError(errors.New("Invariant violated. args, config, and latebind must be non nil."))
}

return LateBindCliContext{globalArgs: args, config: config, activeContext: &QueryistContext{}, bind: latebind}, nil
return LateBindCliContext{
globalArgs: args,
config: config,
cwd: cwd,
activeContext: &QueryistContext{},
bind: latebind}, nil
}

type QueryistContext struct {
Expand All @@ -62,6 +69,7 @@ type QueryistContext struct {
// created once.
type LateBindCliContext struct {
globalArgs *argparser.ArgParseResults
cwd filesys.Filesys
config *env.DoltCliConfig
activeContext *QueryistContext

Expand Down Expand Up @@ -92,6 +100,10 @@ func (lbc LateBindCliContext) QueryEngine(ctx context.Context) (Queryist, *sql.C
return qryist, sqlCtx, closer, nil
}

func (lbc LateBindCliContext) WorkingDir() filesys.Filesys {
return lbc.cwd
}

// Config returns the dolt config stored in CliContext
func (lbc LateBindCliContext) Config() *env.DoltCliConfig {
return lbc.config
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/dolt/commands/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestInit(t *testing.T) {
gCfg.SetStrings(test.GlobalConfig)
apr := argparser.ArgParseResults{}
latebind := func(ctx context.Context) (cli.Queryist, *sql.Context, func(), error) { return nil, nil, func() {}, nil }
cliCtx, _ := cli.NewCliContext(&apr, dEnv.Config, latebind)
cliCtx, _ := cli.NewCliContext(&apr, dEnv.Config, dEnv.FS, latebind)

result := InitCmd{}.Exec(context.Background(), "dolt init", test.Args, dEnv, cliCtx)
defer dEnv.DoltDB.Close()
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/dolt/commands/signed_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func execCommand(ctx context.Context, wd string, cmd cli.Command, args []string,
return
}

cliCtx, err := cli.NewCliContext(apr, cfg, latebind)
cliCtx, err := cli.NewCliContext(apr, cfg, dEnv.FS, latebind)
if err != nil {
err = fmt.Errorf("error creating cli context: %w", err)
return
Expand Down
22 changes: 11 additions & 11 deletions go/cmd/dolt/commands/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestSqlConsole(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{}
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestSqlBatchMode(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-b", "-q", test.query}
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestSqlSelect(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestSqlShow(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestCreateTable(t *testing.T) {
assert.NoError(t, err)
assert.False(t, has, "table exists before creating it")

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -232,7 +232,7 @@ func TestShowTables(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestAlterTable(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestDropTable(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -420,7 +420,7 @@ func TestInsert(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -504,7 +504,7 @@ func TestUpdate(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -582,7 +582,7 @@ func TestDelete(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down
24 changes: 17 additions & 7 deletions go/cmd/dolt/commands/sqlserver/command_line_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ func DefaultCommandLineServerConfig() *commandLineServerConfig {
}
}

// NewCommandLineConfig returns server config based on the credentials and command line arguments given.
func NewCommandLineConfig(creds *cli.UserPassword, apr *argparser.ArgParseResults) (servercfg.ServerConfig, error) {
// NewCommandLineConfig returns server config based on the credentials and command line arguments given. The dataDirOverride
// parameter is used to override the data dir specified in the command line arguments. This comes up when there are
// situations where there are multiple ways to specify the data dir.
func NewCommandLineConfig(creds *cli.UserPassword, apr *argparser.ArgParseResults, dataDirOverride string) (servercfg.ServerConfig, error) {
config := DefaultCommandLineServerConfig()

if sock, ok := apr.GetValue(socketFlag); ok {
Expand Down Expand Up @@ -144,8 +146,16 @@ func NewCommandLineConfig(creds *cli.UserPassword, apr *argparser.ArgParseResult
config.withDataDir(dataDir)
}

if dataDir, ok := apr.GetValue(commands.DataDirFlag); ok {
config.withDataDir(dataDir)
// We explicitly don't use the dataDir flag from the APR here. The data dif flag is pull out early and converted
macneale4 marked this conversation as resolved.
Show resolved Hide resolved
// to an absolute path. It is read in the GetDataDirPreStart function, which is called early in dolt.go to get the
// data dir for any dolt process. This complexity exists because the server's config.yaml config file can contain the
// dataDir, but we don't execute any server specific logic until after the database environment is initialized.
if dataDirOverride != "" {
config.withDataDir(dataDirOverride)
} else {
if dd, ok := apr.GetValue(commands.DataDirFlag); ok {
config.withDataDir(dd)
}
}

if maxConnections, ok := apr.GetInt(maxConnectionsFlag); ok {
Expand Down Expand Up @@ -466,7 +476,7 @@ type ServerConfigReader interface {
// ReadConfigFile reads a config file and returns a ServerConfig for it
ReadConfigFile(cwdFS filesys.Filesys, file string) (servercfg.ServerConfig, error)
// ReadConfigArgs reads command line arguments and returns a ServerConfig for them
ReadConfigArgs(args *argparser.ArgParseResults) (servercfg.ServerConfig, error)
ReadConfigArgs(args *argparser.ArgParseResults, dataDirOverride string) (servercfg.ServerConfig, error)
}

var _ ServerConfigReader = DoltServerConfigReader{}
Expand All @@ -475,6 +485,6 @@ func (d DoltServerConfigReader) ReadConfigFile(cwdFS filesys.Filesys, file strin
return servercfg.YamlConfigFromFile(cwdFS, file)
}

func (d DoltServerConfigReader) ReadConfigArgs(args *argparser.ArgParseResults) (servercfg.ServerConfig, error) {
return NewCommandLineConfig(nil, args)
func (d DoltServerConfigReader) ReadConfigArgs(args *argparser.ArgParseResults, dataDirOverride string) (servercfg.ServerConfig, error) {
return NewCommandLineConfig(nil, args, dataDirOverride)
}
21 changes: 2 additions & 19 deletions go/cmd/dolt/commands/sqlserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,30 +154,13 @@ func ConfigureServices(
controller.Register(newHeartbeatService(version, dEnv))

fs := dEnv.FS
InitDataDir := &svcs.AnonService{
InitFailsafes := &svcs.AnonService{
InitF: func(ctx context.Context) (err error) {
if len(serverConfig.DataDir()) > 0 && serverConfig.DataDir() != "." {
fs, err = dEnv.FS.WithWorkingDir(serverConfig.DataDir())
if err != nil {
return err
}
// If datadir has changed, then reload the DoltEnv to ensure its local
// configuration store gets configured correctly
dEnv.FS = fs
dEnv = env.Load(ctx, dEnv.GetUserHomeDir, fs, doltdb.LocalDirDoltDB, dEnv.Version)

// If the datadir has changed, then we need to load any persisted global variables
// from the new datadir's local configuration store
err = dsess.InitPersistedSystemVars(dEnv)
if err != nil {
logrus.Errorf("failed to load persisted global variables: %s\n", err.Error())
}
}
dEnv.Config.SetFailsafes(env.DefaultFailsafeConfig)
return nil
},
}
controller.Register(InitDataDir)
controller.Register(InitFailsafes)

var mrEnv *env.MultiRepoEnv
InitMultiEnv := &svcs.AnonService{
Expand Down
8 changes: 4 additions & 4 deletions go/cmd/dolt/commands/sqlserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestServerArgs(t *testing.T) {
"-t", "5",
"-l", "info",
"-r",
}, dEnv, controller)
}, dEnv, dEnv.FS, controller)
}()
err = controller.WaitForStart()
require.NoError(t, err)
Expand Down Expand Up @@ -118,7 +118,7 @@ listener:
dEnv.FS.WriteFile("config.yaml", []byte(yamlConfig), os.ModePerm)
StartServer(context.Background(), "0.0.0", "dolt sql-server", []string{
"--config", "config.yaml",
}, dEnv, controller)
}, dEnv, dEnv.FS, controller)
}()
err = controller.WaitForStart()
require.NoError(t, err)
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestServerBadArgs(t *testing.T) {
t.Run(strings.Join(test, " "), func(t *testing.T) {
controller := svcs.NewController()
go func() {
StartServer(context.Background(), "test", "dolt sql-server", test, env, controller)
StartServer(context.Background(), "test", "dolt sql-server", test, env, env.FS, controller)
}()
if !assert.Error(t, controller.WaitForStart()) {
controller.Stop()
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestServerFailsIfPortInUse(t *testing.T) {
"-t", "5",
"-l", "info",
"-r",
}, dEnv, controller)
}, dEnv, dEnv.FS, controller)
}()

err = controller.WaitForStart()
Expand Down
Loading
Loading