Skip to content

Commit

Permalink
Merge #95024 #95463
Browse files Browse the repository at this point in the history
95024: server: ListTenants endpoint on system admin server r=aadityasondhi a=aadityasondhi

This patch adds a new endpoint to the system admin server that returns a
list of active tenants in the cluster. It queries the `system.tenants`
table to enumerate a list of tenants and then it starts those tenants on
the host to be able to return SQL and RPC addresses for those tenants.

Fixes #95014.

Release note: None

95463: rpc: simplify tagging code r=andreimatei a=andreimatei

This patch bumps logtags to get a new helper, and uses it to simplify some code. It also fixes a bug: before, when we wanted to remove a tenant ID from the ctx, we were in fact adding an empty TenantID logtag, which was rendered as "invalid". Now the tag is properly removed.

Release note: None
Epic: None

Co-authored-by: Aaditya Sondhi <[email protected]>
Co-authored-by: Andrei Matei <[email protected]>
  • Loading branch information
3 people committed Jan 19, 2023
3 parents 4066912 + 98797aa + bbcaca7 commit 38d207a
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 21 deletions.
6 changes: 3 additions & 3 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1468,10 +1468,10 @@ def go_deps():
name = "com_github_cockroachdb_logtags",
build_file_proto_mode = "disable_global",
importpath = "github.com/cockroachdb/logtags",
sha256 = "1972c3f171f118add3fd9e64bcea6cbb9959a3b7fa0ada308e8a7310813fea74",
strip_prefix = "github.com/cockroachdb/[email protected]20211118104740-dabe8e521a4f",
sha256 = "ca7776f47e5fecb4c495490a679036bfc29d95bd7625290cfdb9abb0baf97476",
strip_prefix = "github.com/cockroachdb/[email protected]20230118201751-21c54148d20b",
urls = [
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/logtags/com_github_cockroachdb_logtags-v0.0.0-20211118104740-dabe8e521a4f.zip",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/logtags/com_github_cockroachdb_logtags-v0.0.0-20230118201751-21c54148d20b.zip",
],
)
go_repository(
Expand Down
2 changes: 1 addition & 1 deletion build/bazelutil/distdir_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ DISTDIR_FILES = {
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/go-test-teamcity/com_github_cockroachdb_go_test_teamcity-v0.0.0-20191211140407-cff980ad0a55.zip": "bac30148e525b79d004da84d16453ddd2d5cd20528e9187f1d7dac708335674b",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/google-api-go-client/com_github_cockroachdb_google_api_go_client-v0.80.1-0.20221117193156-6a9f7150cb93.zip": "b3378c579f4f4340403038305907d672c86f615f8233118a8873ebe4229c4f39",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/gostdlib/com_github_cockroachdb_gostdlib-v1.19.0.zip": "c4d516bcfe8c07b6fc09b8a9a07a95065b36c2855627cb3514e40c98f872b69e",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/logtags/com_github_cockroachdb_logtags-v0.0.0-20211118104740-dabe8e521a4f.zip": "1972c3f171f118add3fd9e64bcea6cbb9959a3b7fa0ada308e8a7310813fea74",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/logtags/com_github_cockroachdb_logtags-v0.0.0-20230118201751-21c54148d20b.zip": "ca7776f47e5fecb4c495490a679036bfc29d95bd7625290cfdb9abb0baf97476",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/pebble/com_github_cockroachdb_pebble-v0.0.0-20230112184329-90c810757fe6.zip": "dcef948c71b6b6f6e83c41a8c6b9f6bc3df953216534ac4bc7a817d54bcd3515",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/redact/com_github_cockroachdb_redact-v1.1.3.zip": "7778b1e4485e4f17f35e5e592d87eb99c29e173ac9507801d000ad76dd0c261e",
"https://storage.googleapis.com/cockroach-godeps/gomod/github.com/cockroachdb/returncheck/com_github_cockroachdb_returncheck-v0.0.0-20200612231554-92cdbca611dd.zip": "ce92ba4352deec995b1f2eecf16eba7f5d51f5aa245a1c362dfe24c83d31f82b",
Expand Down
56 changes: 56 additions & 0 deletions docs/generated/http/full.md
Original file line number Diff line number Diff line change
Expand Up @@ -7446,3 +7446,59 @@ Support status: [reserved](#support-status)



## ListTenants

`GET /_admin/v1/tenants`

ListTenants returns a list of active tenants in the cluster.

Support status: [reserved](#support-status)

#### Request Parameters













#### Response Parameters







| Field | Type | Label | Description | Support status |
| ----- | ---- | ----- | ----------- | -------------- |
| tenants | [Tenant](#cockroach.server.serverpb.ListTenantsResponse-cockroach.server.serverpb.Tenant) | repeated | | [reserved](#support-status) |






<a name="cockroach.server.serverpb.ListTenantsResponse-cockroach.server.serverpb.Tenant"></a>
#### Tenant



| Field | Type | Label | Description | Support status |
| ----- | ---- | ----- | ----------- | -------------- |
| tenant_id | [cockroach.roachpb.TenantID](#cockroach.server.serverpb.ListTenantsResponse-cockroach.roachpb.TenantID) | | | [reserved](#support-status) |
| tenant_name | [string](#cockroach.server.serverpb.ListTenantsResponse-string) | | | [reserved](#support-status) |
| sql_addr | [string](#cockroach.server.serverpb.ListTenantsResponse-string) | | | [reserved](#support-status) |
| rpc_addr | [string](#cockroach.server.serverpb.ListTenantsResponse-string) | | | [reserved](#support-status) |






2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ require (
github.com/cockroachdb/errors v1.9.0
github.com/cockroachdb/go-test-teamcity v0.0.0-20191211140407-cff980ad0a55
github.com/cockroachdb/gostdlib v1.19.0
github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b
github.com/cockroachdb/pebble v0.0.0-20230112184329-90c810757fe6
github.com/cockroachdb/redact v1.1.3
github.com/cockroachdb/returncheck v0.0.0-20200612231554-92cdbca611dd
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,9 @@ github.com/cockroachdb/google-api-go-client v0.80.1-0.20221117193156-6a9f7150cb9
github.com/cockroachdb/gostdlib v1.19.0 h1:cSISxkVnTlWhTkyple/T6NXzOi5659FkhxvUgZv+Eb0=
github.com/cockroachdb/gostdlib v1.19.0/go.mod h1:+dqqpARXbE/gRDEhCak6dm0l14AaTymPZUKMfURjBtY=
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f h1:6jduT9Hfc0njg5jJ1DdKCFPdMBrp/mdZfCpa5h+WM74=
github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs=
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE=
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs=
github.com/cockroachdb/pebble v0.0.0-20230112184329-90c810757fe6 h1:g4/xNt1/chUSHly60rbkoMxUrzL4DhzJvL9UktSq9Fs=
github.com/cockroachdb/pebble v0.0.0-20230112184329-90c810757fe6/go.mod h1:rWEpkT1ud5qGG2m1HqTBRWbLgi+oodC4+BWC46uVPjw=
github.com/cockroachdb/redact v1.1.3 h1:AKZds10rFSIj7qADf0g46UixK8NNLwWTNdCIGS5wfSQ=
Expand Down
33 changes: 33 additions & 0 deletions pkg/ccl/serverccl/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,36 @@ func TestAdminAPIJobs(t *testing.T) {

require.Equal(t, backups[0], jobRes)
}

func TestListTenants(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

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

db := sqlutils.MakeSQLRunner(sqlDB)
db.Exec(t, "CREATE TENANT test;")

const path = "tenants"
var response serverpb.ListTenantsResponse

if err := getAdminJSONProto(s, path, &response); err != nil {
t.Fatalf("unexpected error: %v\n", err)
}

require.NotEmpty(t, response.Tenants)
appTenantFound := false
for _, tenant := range response.Tenants {
if tenant.TenantName == "test" {
appTenantFound = true
}
require.NotNil(t, tenant.TenantId)
require.NotEmpty(t, tenant.TenantName)
require.NotEmpty(t, tenant.RpcAddr)
require.NotEmpty(t, tenant.SqlAddr)
}
require.True(t, appTenantFound, "test tenant not found")
}
16 changes: 1 addition & 15 deletions pkg/rpc/auth_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,22 +454,8 @@ func validateSpan(tenID roachpb.TenantID, sp roachpb.Span) error {
func contextWithClientTenant(ctx context.Context, tenID roachpb.TenantID) context.Context {
ctx = roachpb.ContextWithClientTenant(ctx, tenID)
const key = "tenant"
// Don't set a log tag if the tenant is not set and there is no existing log
// tag.
if !tenID.IsSet() {
found := false
tags := logtags.FromContext(ctx)
if tags != nil {
for _, t := range tags.Get() {
if t.Key() == key {
found = true
break
}
}
}
if !found {
return ctx
}
return logtags.RemoveTag(ctx, key)
}
ctx = logtags.AddTag(ctx, key, tenID.String())
return ctx
Expand Down
74 changes: 74 additions & 0 deletions pkg/server/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4124,3 +4124,77 @@ func (s *adminServer) SetTraceRecordingType(
})
return &serverpb.SetTraceRecordingTypeResponse{}, nil
}

func (s *adminServer) RecoveryCollectReplicaInfo(
request *serverpb.RecoveryCollectReplicaInfoRequest,
server serverpb.Admin_RecoveryCollectReplicaInfoServer,
) error {
return errors.AssertionFailedf("To be implemented by #93040")
}

func (s *adminServer) RecoveryCollectLocalReplicaInfo(
request *serverpb.RecoveryCollectLocalReplicaInfoRequest,
server serverpb.Admin_RecoveryCollectLocalReplicaInfoServer,
) error {
return errors.AssertionFailedf("To be implemented by #93040")
}

func (s *adminServer) RecoveryStagePlan(
ctx context.Context, request *serverpb.RecoveryStagePlanRequest,
) (*serverpb.RecoveryStagePlanResponse, error) {
return nil, errors.AssertionFailedf("To be implemented by #93044")
}

func (s *adminServer) RecoveryNodeStatus(
ctx context.Context, request *serverpb.RecoveryNodeStatusRequest,
) (*serverpb.RecoveryNodeStatusResponse, error) {
return nil, errors.AssertionFailedf("To be implemented by #93043")
}

func (s *adminServer) RecoveryVerify(
ctx context.Context, request *serverpb.RecoveryVerifyRequest,
) (*serverpb.RecoveryVerifyResponse, error) {
return nil, errors.AssertionFailedf("To be implemented by #93043")
}

// ListTenants returns a list of active tenants in the cluster. Calling this
// function will start in-process tenants if they are not already running.
func (s *systemAdminServer) ListTenants(
ctx context.Context, _ *serverpb.ListTenantsRequest,
) (*serverpb.ListTenantsResponse, error) {
ie := s.internalExecutor
rowIter, err := ie.QueryIterator(ctx, "list-tenants", nil, /* txn */
`SELECT name FROM system.tenants WHERE active = true AND name IS NOT NULL`)
if err != nil {
return nil, err
}
defer func() { _ = rowIter.Close() }()

var tenantNames []roachpb.TenantName
var hasNext bool
for hasNext, err = rowIter.Next(ctx); hasNext && err == nil; hasNext, err = rowIter.Next(ctx) {
row := rowIter.Cur()
tenantName := tree.MustBeDString(row[0])
tenantNames = append(tenantNames, roachpb.TenantName(tenantName))
}

var tenantList []*serverpb.Tenant
for _, tenantName := range tenantNames {
server, err := s.server.serverController.getOrCreateServer(ctx, tenantName)
if err != nil {
log.Errorf(ctx, "unable to get or create a tenant server: %v", err)
continue
}
tenantID := server.getTenantID()
tenantList = append(tenantList, &serverpb.Tenant{
TenantId: &tenantID,
TenantName: string(tenantName),
SqlAddr: server.getSQLAddr(),
RpcAddr: server.getRPCAddr(),
})
}

return &serverpb.ListTenantsResponse{
Tenants: tenantList,
}, nil
}
33 changes: 33 additions & 0 deletions pkg/server/server_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ type onDemandServer interface {

// serveConn handles an incoming SQL connection.
serveConn(ctx context.Context, conn net.Conn, status pgwire.PreServeStatus) error

// getSQLAddr returns the SQL address for this server.
getSQLAddr() string

// getRPCAddr() returns the RPC address for this server.
getRPCAddr() string

// getTenantID returns the TenantID for this server.
getTenantID() roachpb.TenantID
}

type serverEntry struct {
Expand Down Expand Up @@ -695,6 +704,18 @@ func (t *tenantServerWrapper) serveConn(
return t.server.sqlServer.pgServer.ServeConn(pgCtx, conn, status)
}

func (t *tenantServerWrapper) getSQLAddr() string {
return t.server.sqlServer.cfg.SQLAdvertiseAddr
}

func (t *tenantServerWrapper) getRPCAddr() string {
return t.server.sqlServer.cfg.AdvertiseAddr
}

func (t *tenantServerWrapper) getTenantID() roachpb.TenantID {
return t.server.sqlCfg.TenantID
}

// systemServerWrapper implements the onDemandServer interface for Server.
//
// (We can imagine a future where the SQL service for the system
Expand Down Expand Up @@ -734,6 +755,18 @@ func (t *systemServerWrapper) serveConn(
return t.server.sqlServer.pgServer.ServeConn(pgCtx, conn, status)
}

func (s *systemServerWrapper) getSQLAddr() string {
return s.server.sqlServer.cfg.SQLAdvertiseAddr
}

func (s *systemServerWrapper) getRPCAddr() string {
return s.server.sqlServer.cfg.AdvertiseAddr
}

func (s *systemServerWrapper) getTenantID() roachpb.TenantID {
return s.server.cfg.TenantID
}

func (s *Server) makeSharedProcessTenantConfig(
ctx context.Context, tenantID roachpb.TenantID, index int,
) (*stop.Stopper, BaseConfig, SQLConfig, error) {
Expand Down
20 changes: 20 additions & 0 deletions pkg/server/serverpb/admin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import "kv/kvserver/loqrecovery/loqrecoverypb/recovery.proto";
import "kv/kvserver/kvserverpb/range_log.proto";
import "roachpb/api.proto";
import "roachpb/metadata.proto";
import "roachpb/data.proto";
import "ts/catalog/chart_catalog.proto";
import "util/metric/metric.proto";
import "gogoproto/gogo.proto";
Expand Down Expand Up @@ -1223,6 +1224,25 @@ service Admin {
// decommissioned.
rpc RecoveryVerify(RecoveryVerifyRequest) returns (RecoveryVerifyResponse) {}

// ListTenants returns a list of active tenants in the cluster.
rpc ListTenants(ListTenantsRequest) returns (ListTenantsResponse) {
option (google.api.http) = {
get: "/_admin/v1/tenants"
};
}
}

message ListTenantsRequest{}

message ListTenantsResponse {
repeated Tenant tenants = 1;
}

message Tenant {
roachpb.TenantID tenant_id = 1;
string tenant_name = 2;
string sql_addr = 3;
string rpc_addr = 4;
}

message ListTracingSnapshotsRequest {}
Expand Down

0 comments on commit 38d207a

Please sign in to comment.