Skip to content

Commit

Permalink
Merge #91323 #91742
Browse files Browse the repository at this point in the history
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
3 people committed Nov 11, 2022
3 parents a6ab8da + cb938ed + 55af138 commit 111f018
Show file tree
Hide file tree
Showing 13 changed files with 849 additions and 149 deletions.
3 changes: 3 additions & 0 deletions pkg/ccl/serverccl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ go_test(
size = "enormous",
srcs = [
"admin_test.go",
"api_v2_tenant_test.go",
"chart_catalog_test.go",
"main_test.go",
"role_authentication_test.go",
Expand All @@ -41,6 +42,7 @@ go_test(
"tenant_vars_test.go",
],
args = ["-test.timeout=3595s"],
data = glob(["testdata/**"]),
embed = [":serverccl"],
deps = [
"//pkg/base",
Expand Down Expand Up @@ -78,6 +80,7 @@ go_test(
"//pkg/util/protoutil",
"//pkg/util/randutil",
"//pkg/util/timeutil",
"@com_github_cockroachdb_datadriven//:datadriven",
"@com_github_elastic_gosigar//:gosigar",
"@com_github_lib_pq//:pq",
"@com_github_prometheus_client_model//go",
Expand Down
88 changes: 88 additions & 0 deletions pkg/ccl/serverccl/api_v2_tenant_test.go
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)
},
)
}
4 changes: 4 additions & 0 deletions pkg/ccl/serverccl/tenant_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ func (c *httpClient) PostJSONChecked(
return httputil.PostJSON(c.client, c.baseURL+path, request, response)
}

func (c *httpClient) PostJSONRawChecked(path string, request []byte) (*http.Response, error) {
return httputil.PostJSONRaw(c.client, c.baseURL+path, request)
}

func (c *httpClient) Close() {
c.client.CloseIdleConnections()
}
Loading

0 comments on commit 111f018

Please sign in to comment.