-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
91323: tenant: add apiv2 support for sql-over-http r=knz a=dhartunian This commit adds partial support for the `/api/v2` HTTP server on tenants. Currently, we *only* support the SQL endpoint since this functionality is needed by newer DB Console features. The API V2 Server initialization no longer relies on the global `Server` object, and only requires a `SQLServer` instead which makes it easier to play nicely with tenants. However, we still have the problem of incompatible status and admin servers which are in use on other endpoints. Future work will unify the tenant scoped versions of these servers and allow them to be used here, enabling full API V2 compatibility on tenants. Informs: #80789 Release note (ops change): sql tenants now support the HTTP endpoint under `/api/v2/sql` which allows the caller to execute an HTTP request containing SQL statements to execute. The JSON response contains the results. This endpoints works identically as on a non-tenant server, except that it naturally scopes to the target tenant for SQL execution. Epic: CRDB-17356 91742: kvserver: add logStore type with storeEntries method r=tbg a=pavelkalinnikov This change factors out a `logStore` type stub as a foundation for a separated log storage, and moves the log entries persistence code to be its `storeEntries` method. Epic: CRDB-220 Release note: None Co-authored-by: David Hartunian <[email protected]> Co-authored-by: Pavel Kalinnikov <[email protected]>
- Loading branch information
Showing
13 changed files
with
849 additions
and
149 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package serverccl | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/server" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/timeutil" | ||
"github.com/cockroachdb/datadriven" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestExecSQL(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
server.SQLAPIClock = timeutil.NewManualTime(timeutil.FromUnixMicros(0)) | ||
defer func() { | ||
server.SQLAPIClock = timeutil.DefaultTimeSource{} | ||
}() | ||
|
||
ctx := context.Background() | ||
|
||
testHelper := NewTestTenantHelper(t, 3 /* tenantClusterSize */, base.TestingKnobs{}) | ||
defer testHelper.Cleanup(ctx, t) | ||
|
||
tenantCluster := testHelper.TestCluster() | ||
adminClient := tenantCluster.TenantAdminHTTPClient(t, 0) | ||
nonAdminClient := tenantCluster.TenantHTTPClient(t, 0, false) | ||
|
||
datadriven.RunTest(t, "testdata/api_v2_sql", | ||
func(t *testing.T, d *datadriven.TestData) string { | ||
if d.Cmd != "sql" { | ||
t.Fatal("Only sql command is accepted in this test") | ||
} | ||
|
||
var client *httpClient | ||
if d.HasArg("admin") { | ||
client = adminClient | ||
} | ||
if d.HasArg("non-admin") { | ||
client = nonAdminClient | ||
} | ||
|
||
resp, err := client.PostJSONRawChecked( | ||
"/api/v2/sql/", | ||
[]byte(d.Input), | ||
) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
r, err := io.ReadAll(resp.Body) | ||
require.NoError(t, err) | ||
|
||
if d.HasArg("expect-error") { | ||
type jsonError struct { | ||
Code string `json:"code"` | ||
Message string `json:"message"` | ||
} | ||
type errorResp struct { | ||
Error jsonError `json:"error"` | ||
} | ||
|
||
er := errorResp{} | ||
err := json.Unmarshal(r, &er) | ||
require.NoError(t, err) | ||
return fmt.Sprintf("%s|%s", er.Error.Code, er.Error.Message) | ||
} | ||
var u interface{} | ||
err = json.Unmarshal(r, &u) | ||
require.NoError(t, err) | ||
s, err := json.MarshalIndent(u, "", " ") | ||
require.NoError(t, err) | ||
return string(s) | ||
}, | ||
) | ||
} |
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.