Skip to content

Commit

Permalink
status: set codec from context in table stats requests
Browse files Browse the repository at this point in the history
Replaced usages of `TODOSQLCodec` with the codec from `sqlServer.execCfg`. This
enables the DB and Table stats endpoints to work from tenants.

Resolves: #82879

Relates to: #90261, #90267, #90268, #90264, #89429

Epic: CRDB-12100

Release note: None
  • Loading branch information
dhartunian committed Feb 28, 2023
1 parent b0e5507 commit 4308e91
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
41 changes: 39 additions & 2 deletions pkg/ccl/serverccl/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ import (
var adminPrefix = "/_admin/v1/"

func getAdminJSONProto(
ts serverutils.TestServerInterface, path string, response protoutil.Message,
ts serverutils.TestTenantInterface, path string, response protoutil.Message,
) error {
return getAdminJSONProtoWithAdminOption(ts, path, response, true)
}

func getAdminJSONProtoWithAdminOption(
ts serverutils.TestServerInterface, path string, response protoutil.Message, isAdmin bool,
ts serverutils.TestTenantInterface, path string, response protoutil.Message, isAdmin bool,
) error {
return serverutils.GetJSONProtoWithAdminOption(ts, adminPrefix+path, response, isAdmin)
}
Expand Down Expand Up @@ -195,3 +195,40 @@ func TestListTenants(t *testing.T) {
}
require.True(t, appTenantFound, "test tenant not found")
}

func TestTableAndDatabaseDetailsAndStats(t *testing.T) {
defer leaktest.AfterTest(t)()

ctx := context.Background()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)

st, db := serverutils.StartTenant(t, s, base.TestTenantArgs{
TenantID: serverutils.TestTenantID(),
})
_, err := db.Exec("CREATE TABLE test (id int)")
require.NoError(t, err)
_, err = db.Exec("INSERT INTO test VALUES (1)")
require.NoError(t, err)

// DatabaseDetails
dbResp := &serverpb.DatabaseDetailsResponse{}
err = getAdminJSONProto(st, "databases/defaultdb", dbResp)
require.NoError(t, err)

require.Equal(t, dbResp.TableNames[0], "public.test")

// TableStats
tableStatsResp := &serverpb.TableStatsResponse{}
err = getAdminJSONProto(st, "databases/defaultdb/tables/public.test/stats", tableStatsResp)
require.NoError(t, err)

require.Greater(t, tableStatsResp.Stats.LiveBytes, int64(0))

// TableDetails
tableDetailsResp := &serverpb.TableDetailsResponse{}
err = getAdminJSONProto(st, "databases/defaultdb/tables/public.test", tableDetailsResp)
require.NoError(t, err)

require.Greater(t, tableDetailsResp.DataLiveBytes, int64(0))
}
10 changes: 5 additions & 5 deletions pkg/server/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ func (s *adminServer) getDatabaseTableSpans(
if err != nil {
return nil, err
}
tableSpans[tableName] = generateTableSpan(tableID)
tableSpans[tableName] = generateTableSpan(tableID, s.sqlServer.execCfg.Codec)
}
return tableSpans, nil
}
Expand Down Expand Up @@ -1161,7 +1161,7 @@ func (s *adminServer) tableDetailsHelper(
// Get the number of ranges in the table. We get the key span for the table
// data. Then, we count the number of ranges that make up that key span.
{
tableSpan := generateTableSpan(tableID)
tableSpan := generateTableSpan(tableID, s.sqlServer.execCfg.Codec)
tableRSpan, err := keys.SpanAddr(tableSpan)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1190,8 +1190,8 @@ func (s *adminServer) tableDetailsHelper(
//
// NOTE: this doesn't make sense for interleaved (children) table. As of
// 03/2018, callers around here use it anyway.
func generateTableSpan(tableID descpb.ID) roachpb.Span {
tableStartKey := keys.TODOSQLCodec.TablePrefix(uint32(tableID))
func generateTableSpan(tableID descpb.ID, codec keys.SQLCodec) roachpb.Span {
tableStartKey := codec.TablePrefix(uint32(tableID))
tableEndKey := tableStartKey.PrefixEnd()
return roachpb.Span{Key: tableStartKey, EndKey: tableEndKey}
}
Expand Down Expand Up @@ -1223,7 +1223,7 @@ func (s *adminServer) TableStats(
if err != nil {
return nil, serverError(ctx, err)
}
tableSpan := generateTableSpan(tableID)
tableSpan := generateTableSpan(tableID, s.sqlServer.execCfg.Codec)

r, err := s.statsForSpan(ctx, tableSpan)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/testutils/serverutils/test_server_shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,14 +437,14 @@ func TestTenantID3() roachpb.TenantID {

// GetJSONProto uses the supplied client to GET the URL specified by the parameters
// and unmarshals the result into response.
func GetJSONProto(ts TestServerInterface, path string, response protoutil.Message) error {
func GetJSONProto(ts TestTenantInterface, path string, response protoutil.Message) error {
return GetJSONProtoWithAdminOption(ts, path, response, true)
}

// GetJSONProtoWithAdminOption is like GetJSONProto but the caller can customize
// whether the request is performed with admin privilege
func GetJSONProtoWithAdminOption(
ts TestServerInterface, path string, response protoutil.Message, isAdmin bool,
ts TestTenantInterface, path string, response protoutil.Message, isAdmin bool,
) error {
httpClient, err := ts.GetAuthenticatedHTTPClient(isAdmin)
if err != nil {
Expand All @@ -455,15 +455,15 @@ func GetJSONProtoWithAdminOption(

// PostJSONProto uses the supplied client to POST the URL specified by the parameters
// and unmarshals the result into response.
func PostJSONProto(ts TestServerInterface, path string, request, response protoutil.Message) error {
func PostJSONProto(ts TestTenantInterface, path string, request, response protoutil.Message) error {
return PostJSONProtoWithAdminOption(ts, path, request, response, true)
}

// PostJSONProtoWithAdminOption is like PostJSONProto but the caller
// can customize whether the request is performed with admin
// privilege.
func PostJSONProtoWithAdminOption(
ts TestServerInterface, path string, request, response protoutil.Message, isAdmin bool,
ts TestTenantInterface, path string, request, response protoutil.Message, isAdmin bool,
) error {
httpClient, err := ts.GetAuthenticatedHTTPClient(isAdmin)
if err != nil {
Expand Down

0 comments on commit 4308e91

Please sign in to comment.