-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql,externalconn: introduce CREATE EXTERNAL CONNECTION
This change introduces the `CREATE EXTERNAL CONNECTION` syntax to CockroachDB. This statement can be used to create an External Connection object that represents an external resource. Majority of this change is introducing the required interfaces to persist an External Connection object in the `system.external_connections` table. We only register `nodelocal` as a supported External Connection to allow for end to end testing of functionality. A user can now execute: `CREATE EXTERNAL CONNECTION foo AS 'nodelocal://1/foo';` All the other providers will be registered as part of #84228. Furthermore, none of the permission model outlined in the RFC #84209 has been implemented, to minimize the scope of this change. Release note (sql change): introduce `CREATE EXTERNAL CONNECTION` syntax that can be used to create an External Connection representing a resource that resides outside of CockroachDB. The only supported resource at the moment is a `nodelocal` URI that can be represented as an External Connection object using: `CREATE EXTERNAL CONNECTION foo AS 'nodelocal://1/foo'`; Fixes: #84225
- Loading branch information
1 parent
b21322b
commit 066577b
Showing
33 changed files
with
1,172 additions
and
42 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
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,29 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_test") | ||
|
||
go_test( | ||
name = "externalconnccl_test", | ||
srcs = [ | ||
"datadriven_test.go", | ||
"main_test.go", | ||
], | ||
data = glob(["testdata/**"]), | ||
deps = [ | ||
"//pkg/base", | ||
"//pkg/ccl/kvccl/kvtenantccl", | ||
"//pkg/cloud/externalconn/externalconntestutils", | ||
"//pkg/cloud/externalconn/impl", | ||
"//pkg/jobs", | ||
"//pkg/roachpb", | ||
"//pkg/security/securityassets", | ||
"//pkg/security/securitytest", | ||
"//pkg/server", | ||
"//pkg/testutils", | ||
"//pkg/testutils/serverutils", | ||
"//pkg/testutils/sqlutils", | ||
"//pkg/testutils/testcluster", | ||
"//pkg/util/leaktest", | ||
"//pkg/util/randutil", | ||
"@com_github_cockroachdb_datadriven//:datadriven", | ||
"@com_github_stretchr_testify//require", | ||
], | ||
) |
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,97 @@ | ||
// Copyright 2022 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 externalconnccl_test | ||
|
||
import ( | ||
"context" | ||
gosql "database/sql" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/cloud/externalconn/externalconntestutils" | ||
_ "github.com/cockroachdb/cockroach/pkg/cloud/externalconn/impl" // register all the concrete External Connection implementations | ||
"github.com/cockroachdb/cockroach/pkg/jobs" | ||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/testutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/datadriven" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDataDriven(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
ctx := context.Background() | ||
datadriven.Walk(t, testutils.TestDataPath(t), func(t *testing.T, path string) { | ||
tc := testcluster.StartTestCluster(t, 1, base.TestClusterArgs{ | ||
ServerArgs: base.TestServerArgs{ | ||
Knobs: base.TestingKnobs{ | ||
JobsTestingKnobs: jobs.NewTestingKnobsWithShortIntervals(), // speeds up test | ||
}, | ||
}, | ||
}) | ||
defer tc.Stopper().Stop(ctx) | ||
|
||
externalConnTestCluster := externalconntestutils.NewHandle(t, tc) | ||
defer externalConnTestCluster.Cleanup() | ||
|
||
externalConnTestCluster.InitializeTenant(ctx, roachpb.SystemTenantID) | ||
datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) string { | ||
tenantID := roachpb.SystemTenantID | ||
if d.HasArg("tenant") { | ||
var id uint64 | ||
d.ScanArgs(t, "tenant", &id) | ||
tenantID = roachpb.MakeTenantID(id) | ||
} | ||
|
||
tenant, found := externalConnTestCluster.LookupTenant(tenantID) | ||
if d.Cmd != "initialize" { | ||
require.Truef(t, found, "tenant %s not found (was it initialized?)", tenantID) | ||
} | ||
|
||
switch d.Cmd { | ||
case "initialize": | ||
externalConnTestCluster.InitializeTenant(ctx, tenantID) | ||
|
||
case "exec-sql": | ||
if err := tenant.ExecWithErr(d.Input); err != nil { | ||
return fmt.Sprint(err.Error()) | ||
} | ||
|
||
case "query-sql": | ||
var rows *gosql.Rows | ||
var err error | ||
if rows, err = tenant.QueryWithErr(d.Input); err != nil { | ||
return fmt.Sprint(err.Error()) | ||
} | ||
output, err := sqlutils.RowsToDataDrivenOutput(rows) | ||
require.NoError(t, err) | ||
return output | ||
|
||
case "inspect-system-table": | ||
rows := tenant.Query(` | ||
SELECT connection_name, connection_type, crdb_internal.pb_to_json('cockroach.cloud.externalconn.connectionpb.ConnectionDetails', connection_details) | ||
FROM system.external_connections; | ||
`) | ||
output, err := sqlutils.RowsToDataDrivenOutput(rows) | ||
require.NoError(t, err) | ||
return output | ||
|
||
default: | ||
t.Fatalf("unknown command: %s", d.Cmd) | ||
} | ||
return "" | ||
}) | ||
}) | ||
} |
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,34 @@ | ||
// Copyright 2022 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 externalconnccl_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
_ "github.com/cockroachdb/cockroach/pkg/ccl/kvccl/kvtenantccl" | ||
"github.com/cockroachdb/cockroach/pkg/security/securityassets" | ||
"github.com/cockroachdb/cockroach/pkg/security/securitytest" | ||
"github.com/cockroachdb/cockroach/pkg/server" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster" | ||
"github.com/cockroachdb/cockroach/pkg/util/randutil" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
securityassets.SetLoader(securitytest.EmbeddedAssets) | ||
randutil.SeedForTests() | ||
serverutils.InitTestServerFactory(server.TestServerFactory) | ||
serverutils.InitTestClusterFactory(testcluster.TestClusterFactory) | ||
os.Exit(m.Run()) | ||
} | ||
|
||
//go:generate ../../util/leaktest/add-leaktest.sh *_test.go |
27 changes: 27 additions & 0 deletions
27
pkg/ccl/cloudccl/externalconnccl/testdata/create_external_connection
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,27 @@ | ||
subtest basic-nodelocal | ||
|
||
exec-sql | ||
CREATE EXTERNAL CONNECTION foo AS 'nodelocal://1/foo/bar'; | ||
---- | ||
|
||
inspect-system-table | ||
---- | ||
foo STORAGE {"nodelocal": {"cfg": {"nodeId": 1, "path": "/foo/bar"}}, "provider": "nodelocal"} | ||
|
||
# Try to create another External Connection with the same name. | ||
exec-sql | ||
CREATE EXTERNAL CONNECTION foo AS 'nodelocal://1/foo'; | ||
---- | ||
pq: external connection with connection name 'foo' already exists | ||
|
||
# Create another External Connection with a unique name. | ||
exec-sql | ||
CREATE EXTERNAL CONNECTION bar123 AS 'nodelocal://1/baz'; | ||
---- | ||
|
||
inspect-system-table | ||
---- | ||
bar123 STORAGE {"nodelocal": {"cfg": {"nodeId": 1, "path": "/baz"}}, "provider": "nodelocal"} | ||
foo STORAGE {"nodelocal": {"cfg": {"nodeId": 1, "path": "/foo/bar"}}, "provider": "nodelocal"} | ||
|
||
subtest end |
30 changes: 30 additions & 0 deletions
30
pkg/ccl/cloudccl/externalconnccl/testdata/multi-tenant/create_external_connection
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,30 @@ | ||
subtest basic-nodelocal | ||
|
||
initialize tenant=10 | ||
---- | ||
|
||
exec-sql | ||
CREATE EXTERNAL CONNECTION foo AS 'nodelocal://1/foo/bar'; | ||
---- | ||
|
||
inspect-system-table | ||
---- | ||
foo STORAGE {"nodelocal": {"cfg": {"nodeId": 1, "path": "/foo/bar"}}, "provider": "nodelocal"} | ||
|
||
# Try to create another External Connection with the same name. | ||
exec-sql | ||
CREATE EXTERNAL CONNECTION foo AS 'nodelocal://1/foo'; | ||
---- | ||
pq: external connection with connection name 'foo' already exists | ||
|
||
# Create another External Connection with a unique name. | ||
exec-sql | ||
CREATE EXTERNAL CONNECTION bar123 AS 'nodelocal://1/baz'; | ||
---- | ||
|
||
inspect-system-table | ||
---- | ||
bar123 STORAGE {"nodelocal": {"cfg": {"nodeId": 1, "path": "/baz"}}, "provider": "nodelocal"} | ||
foo STORAGE {"nodelocal": {"cfg": {"nodeId": 1, "path": "/foo/bar"}}, "provider": "nodelocal"} | ||
|
||
subtest end |
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,25 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "externalconn", | ||
srcs = [ | ||
"connection.go", | ||
"connection_record.go", | ||
"impl_registry.go", | ||
], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/cloud/externalconn", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/cloud/externalconn/connectionpb", | ||
"//pkg/kv", | ||
"//pkg/security/username", | ||
"//pkg/sql/catalog/colinfo", | ||
"//pkg/sql/pgwire/pgcode", | ||
"//pkg/sql/pgwire/pgerror", | ||
"//pkg/sql/sem/tree", | ||
"//pkg/sql/sessiondata", | ||
"//pkg/sql/sqlutil", | ||
"//pkg/util/protoutil", | ||
"@com_github_cockroachdb_errors//:errors", | ||
], | ||
) |
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,32 @@ | ||
// Copyright 2022 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 externalconn | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/cloud/externalconn/connectionpb" | ||
) | ||
|
||
// ConnectionDetails is the interface to the external resource represented by an | ||
// External Connection object. | ||
type ConnectionDetails interface { | ||
// ConnectionProto prepares the ConnectionDetails for serialization. | ||
ConnectionProto() *connectionpb.ConnectionDetails | ||
// ConnectionType returns the type of the connection. | ||
ConnectionType() connectionpb.ConnectionType | ||
} | ||
|
||
// ConnectionDetailsFromURIFactory is the factory method that takes in an | ||
// endpoint URI for an external resource, and returns a ConnectionDetails | ||
// interface to interact with it. | ||
type ConnectionDetailsFromURIFactory func(ctx context.Context, uri *url.URL) (ConnectionDetails, error) |
Oops, something went wrong.