forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: add ./cockroach mt start-sql
This adds a CLI command to start a SQL tenant in a standalone process. The tenant currently communicates with the KV layer "as a node"; this will only change with cockroachdb#47898. As is, the tenant has full access to the KV layer and so a few things may work that shouldn't as a result of that. Additionally, the tenant runs a Gossip client as a temporary stopgap until we have removed the remaining dependencies on it (cockroachdb#49693 has the details on what those are). Apart from that, though, this is the real thing and can be used to start setting up end-to-end testing of ORMs as well as the deploy- ments. A word on the choice of the `mt` command: `mt` is an abbreviation for `multi-tenant` which was considered too long; just `tenant` was considered misleading - there will be multiple additional subcommands housing the other services required for running the whole infrastructure including certs generation, the KV auth broker server, and the SQL proxy. Should a lively bikeshed around naming break out we can rename the commands later when consensus has been reached. For those curious to try it out the following will be handy: ```bash rm -rf ~/.cockroach-certs cockroach-data && killall -9 cockroach && \ export COCKROACH_CA_KEY=$HOME/.cockroach-certs/ca.key && \ ./cockroach cert create-ca && \ ./cockroach cert create-client root && \ ./cockroach cert create-node 127.0.0.1 && \ ./cockroach start --host 127.0.0.1 --background && \ ./cockroach sql --host 127.0.0.1 -e 'select crdb_internal.create_tenant(123);' ./cockroach mt start-sql --tenant-id 123 --kv-addrs 127.0.0.1:26257 --sql-addr :5432 & sleep 1 && \ ./cockroach sql --host 127.0.0.1 --port 5432 ``` This roughly matches what the newly introduced `acceptance/multitenant` roachtest does as well. Release note: None
- Loading branch information
Showing
8 changed files
with
305 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
func init() { | ||
cockroachCmd.AddCommand(mtCmd) | ||
mtCmd.AddCommand(mtStartSQLCmd) | ||
} | ||
|
||
// mtCmd is the base command for functionality related to multi-tenancy. | ||
var mtCmd = &cobra.Command{ | ||
Use: "mt [command]", | ||
Short: "commands related to multi-tenancy", | ||
RunE: usageAndErr, | ||
Hidden: true, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/signal" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/clusterversion" | ||
"github.com/cockroachdb/cockroach/pkg/server" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/cockroachdb/cockroach/pkg/util/stop" | ||
"github.com/cockroachdb/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var mtStartSQLCmd = &cobra.Command{ | ||
Use: "start-sql", | ||
Short: "start a standalone SQL server", | ||
Args: cobra.NoArgs, | ||
RunE: MaybeDecorateGRPCError(runStartSQL), | ||
} | ||
|
||
func init() { | ||
mtCmd.AddCommand( | ||
mtStartSQLCmd, | ||
) | ||
} | ||
|
||
func runStartSQL(cmd *cobra.Command, args []string) error { | ||
ctx := context.Background() | ||
const clusterName = "" | ||
stopper := stop.NewStopper() | ||
defer stopper.Stop(ctx) | ||
|
||
st := serverCfg.BaseConfig.Settings | ||
|
||
// TODO(tbg): this has to be passed in. See the upgrade strategy in: | ||
// https://github.com/cockroachdb/cockroach/issues/47919 | ||
if err := clusterversion.Initialize(ctx, st.Version.BinaryVersion(), &st.SV); err != nil { | ||
return err | ||
} | ||
|
||
tempStorageMaxSizeBytes := int64(base.DefaultInMemTempStorageMaxSizeBytes) | ||
if err := diskTempStorageSizeValue.Resolve( | ||
&tempStorageMaxSizeBytes, memoryPercentResolver, | ||
); err != nil { | ||
return err | ||
} | ||
|
||
serverCfg.SQLConfig.TempStorageConfig = base.TempStorageConfigFromEnv( | ||
ctx, | ||
st, | ||
base.StoreSpec{InMemory: true}, | ||
"", // parentDir | ||
tempStorageMaxSizeBytes, | ||
) | ||
|
||
addr, err := server.StartTenant( | ||
ctx, | ||
stopper, | ||
clusterName, | ||
serverCfg.BaseConfig, | ||
serverCfg.SQLConfig, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
log.Infof(ctx, "SQL server for tenant %s listening at %s", serverCfg.SQLConfig.TenantID, addr) | ||
|
||
// TODO(tbg): make the other goodies in `./cockroach start` reusable, such as | ||
// logging to files, periodic memory output, heap and goroutine dumps, debug | ||
// server, graceful drain. Then use them here. | ||
|
||
ch := make(chan os.Signal) | ||
signal.Notify(ch, drainSignals...) | ||
sig := <-ch | ||
return errors.Newf("received signal %v", sig) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cliflags | ||
|
||
// Flags specific to multi-tenancy commands. | ||
var ( | ||
TenantID = FlagInfo{ | ||
Name: "tenant-id", | ||
EnvVar: "COCKROACH_TENANT_ID", | ||
Description: `The tenant ID under which to start the SQL server.`, | ||
} | ||
|
||
KVAddrs = FlagInfo{ | ||
Name: "kv-addrs", | ||
Shorthand: "", | ||
Description: `A comma-separated list of KV endpoints (load balancers allowed).`, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
gosql "database/sql" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func runAcceptanceMultitenant(ctx context.Context, t *test, c *cluster) { | ||
c.Put(ctx, cockroach, "./cockroach") | ||
c.Start(ctx, t, c.All()) | ||
|
||
_, err := c.Conn(ctx, 1).Exec(`SELECT crdb_internal.create_tenant(123)`) | ||
require.NoError(t, err) | ||
|
||
kvAddrs := c.ExternalAddr(ctx, c.All()) | ||
|
||
errCh := make(chan error) | ||
go func() { | ||
errCh <- c.RunE(ctx, c.Node(1), | ||
"./cockroach", "mt", "start-sql", | ||
// TODO(tbg): make this test secure. | ||
// "--certs-dir", "certs", | ||
"--insecure", | ||
"--tenant-id", "123", | ||
"--kv-addrs", strings.Join(kvAddrs, ","), | ||
"--sql-addr", "0.0.0.0:36257", | ||
) | ||
}() | ||
u, err := url.Parse(c.ExternalPGUrl(ctx, c.Node(1))[0]) | ||
require.NoError(t, err) | ||
u.Host = c.ExternalIP(ctx, c.Node(1))[0] + ":36257" | ||
url := u.String() | ||
c.l.Printf("sql server should be running at %s", url) | ||
|
||
time.Sleep(time.Second) | ||
|
||
select { | ||
case err := <-errCh: | ||
t.Fatal(err) | ||
default: | ||
} | ||
|
||
db, err := gosql.Open("postgres", url) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer db.Close() | ||
_, err = db.Exec(`CREATE TABLE foo (id INT PRIMARY KEY, v STRING)`) | ||
require.NoError(t, err) | ||
|
||
_, err = db.Exec(`INSERT INTO foo VALUES($1, $2)`, 1, "bar") | ||
require.NoError(t, err) | ||
|
||
var id int | ||
var v string | ||
require.NoError(t, db.QueryRow(`SELECT * FROM foo LIMIT 1`).Scan(&id, &v)) | ||
require.Equal(t, 1, id) | ||
require.Equal(t, "bar", v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.