Skip to content

Commit

Permalink
roachpb,server,spanconfig: introduce RPCs for SystemSpanConfigs
Browse files Browse the repository at this point in the history
This patch adds two new RPCs -- `UpdateSystemSpanConfigs` and
`GetSystemSpanConfigs` to interact with system span configurations.
They're available to tenants via the `Connector`; we ensure secondary
tenants do not target other tenants.

The `KVAccessor` returns unimplemented errors for now. Pulling this
mechanical change into its own patch for ease of review.

Release note: None
  • Loading branch information
arulajmani committed Jan 28, 2022
1 parent 0e932c3 commit 5177417
Show file tree
Hide file tree
Showing 16 changed files with 303 additions and 5 deletions.
33 changes: 33 additions & 0 deletions pkg/ccl/kvccl/kvtenantccl/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,39 @@ func (c *Connector) WithTxn(context.Context, *kv.Txn) spanconfig.KVAccessor {
panic("not applicable")
}

// GetSystemSpanConfigEntries implements the spanconfig.KVAccessor interface.
func (c *Connector) GetSystemSpanConfigEntries(
ctx context.Context,
) (entries []roachpb.SystemSpanConfigEntry, _ error) {
if err := c.withClient(ctx, func(ctx context.Context, c *client) error {
resp, err := c.GetSystemSpanConfigs(ctx, &roachpb.GetSystemSpanConfigsRequest{})
if err != nil {
return err
}

entries = resp.SystemSpanConfigEntries
return nil
}); err != nil {
return nil, err
}
return entries, nil
}

// UpdateSystemSpanConfigEntries implements the spanconfig.KVAccessor interface.
func (c *Connector) UpdateSystemSpanConfigEntries(
ctx context.Context,
toDelete []roachpb.SystemSpanConfigTarget,
toUpsert []roachpb.SystemSpanConfigEntry,
) error {
return c.withClient(ctx, func(ctx context.Context, c *client) error {
_, err := c.UpdateSystemSpanConfigs(ctx, &roachpb.UpdateSystemSpanConfigsRequest{
ToDelete: toDelete,
ToUpsert: toUpsert,
})
return err
})
}

// withClient is a convenience wrapper that executes the given closure while
// papering over InternalClient retrieval errors.
func (c *Connector) withClient(
Expand Down
12 changes: 12 additions & 0 deletions pkg/ccl/kvccl/kvtenantccl/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ func (m *mockServer) UpdateSpanConfigs(
panic("unimplemented")
}

func (m *mockServer) GetSystemSpanConfigs(
context.Context, *roachpb.GetSystemSpanConfigsRequest,
) (*roachpb.GetSystemSpanConfigsResponse, error) {
panic("unimplemented")
}

func (m *mockServer) UpdateSystemSpanConfigs(
context.Context, *roachpb.UpdateSystemSpanConfigsRequest,
) (*roachpb.UpdateSystemSpanConfigsResponse, error) {
panic("unimplemented")
}

func (m *mockServer) TenantSettings(
*roachpb.TenantSettingsRequest, roachpb.Internal_TenantSettingsServer,
) error {
Expand Down
12 changes: 12 additions & 0 deletions pkg/kv/kvclient/kvcoord/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ func (n Node) UpdateSpanConfigs(
panic("unimplemented")
}

func (n Node) GetSystemSpanConfigs(
_ context.Context, _ *roachpb.GetSystemSpanConfigsRequest,
) (*roachpb.GetSystemSpanConfigsResponse, error) {
panic("unimplemented")
}

func (n Node) UpdateSystemSpanConfigs(
_ context.Context, _ *roachpb.UpdateSystemSpanConfigsRequest,
) (*roachpb.UpdateSystemSpanConfigsResponse, error) {
panic("unimplemented")
}

func (n Node) TenantSettings(
*roachpb.TenantSettingsRequest, roachpb.Internal_TenantSettingsServer,
) error {
Expand Down
12 changes: 12 additions & 0 deletions pkg/kv/kvclient/kvcoord/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ func (m *mockInternalClient) UpdateSpanConfigs(
return nil, fmt.Errorf("unsupported UpdateSpanConfigs call")
}

func (m *mockInternalClient) GetSystemSpanConfigs(
_ context.Context, _ *roachpb.GetSystemSpanConfigsRequest, _ ...grpc.CallOption,
) (*roachpb.GetSystemSpanConfigsResponse, error) {
return nil, fmt.Errorf("unsupported GetSpanConfigs call")
}

func (m *mockInternalClient) UpdateSystemSpanConfigs(
_ context.Context, _ *roachpb.UpdateSystemSpanConfigsRequest, _ ...grpc.CallOption,
) (*roachpb.UpdateSystemSpanConfigsResponse, error) {
return nil, fmt.Errorf("unsupported UpdateSpanConfigs call")
}

func (m *mockInternalClient) TenantSettings(
context.Context, *roachpb.TenantSettingsRequest, ...grpc.CallOption,
) (roachpb.Internal_TenantSettingsClient, error) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/roachpb/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2826,6 +2826,12 @@ service Internal {
// keyspans.
rpc UpdateSpanConfigs (UpdateSpanConfigsRequest) returns (UpdateSpanConfigsResponse) { }

// GetSystemSpanConfigs is used to fetch system span configurations.
rpc GetSystemSpanConfigs(GetSystemSpanConfigsRequest) returns (GetSystemSpanConfigsResponse) { }

// UpdateSystemSpanConfigs is used to update system span configurations.
rpc UpdateSystemSpanConfigs (UpdateSystemSpanConfigsRequest) returns (UpdateSystemSpanConfigsResponse) {}

// TenantSettings is used by tenants to obtain and stay up to date with tenant
// setting overrides.
rpc TenantSettings (TenantSettingsRequest) returns (stream TenantSettingsEvent) { }
Expand Down
40 changes: 40 additions & 0 deletions pkg/roachpb/mocks_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 29 additions & 3 deletions pkg/roachpb/span_config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ message SystemSpanConfigTarget {
// TenantID indicates the tenant ID of the logical cluster being targeted.
// For secondary tenants this field is left unset. For the host we can use
// this field to protect a specific secondary tenant.
//
// TODO(arul): Ensure that secondary tenants don't populate this field when
// we make use of these in the RPC.
roachpb.TenantID tenant_id = 1 [(gogoproto.customname) = "TenantID", (gogoproto.nullable) = true];
}

Expand Down Expand Up @@ -219,6 +216,18 @@ message GetSpanConfigsResponse {
repeated SpanConfigEntry span_config_entries = 1 [(gogoproto.nullable) = false];
};

// GetSystemSpanConfigsRequest is used to fetch all system span configurations
// installed by the requesting tenant.
message GetSystemSpanConfigsRequest {};

// GetSystemSpanConfigsResponse lists out all system span configurations that
// are installed by the requesting tenant.
message GetSystemSpanConfigsResponse {
// SystemSpanConfigEntries captures the system span configurations that have
// been set by the tenant.
repeated SystemSpanConfigEntry system_span_config_entries = 1 [(gogoproto.nullable) = false];
};

// UpdateSpanConfigsRequest is used to update the span configurations over the
// given spans.
//
Expand All @@ -245,3 +254,20 @@ message UpdateSpanConfigsRequest {

message UpdateSpanConfigsResponse { };

// UpdateSystemSpanConfigsRequest is used to update system span configurations.

// System span config targets being deleted are expected to have been present.
// Targets are not allowed to be duplicated in the same list or across lists;
// existing span configs should be updated by including in the upsert list
// without deleting their targets first.
message UpdateSystemSpanConfigsRequest {
// SystemSpanConfigsToDelete captures the targets of the system span
// configurations to delete.
repeated SystemSpanConfigTarget to_delete = 1 [(gogoproto.nullable) = false];

// SystemSpanConfigsToUpsert captures the system span configurations we want
// to upsert with.
repeated SystemSpanConfigEntry to_upsert = 2 [(gogoproto.nullable) = false];
};

message UpdateSystemSpanConfigsResponse {};
40 changes: 40 additions & 0 deletions pkg/rpc/auth_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ func (a tenantAuthorizer) authorize(
case "/cockroach.roachpb.Internal/UpdateSpanConfigs":
return a.authUpdateSpanConfigs(tenID, req.(*roachpb.UpdateSpanConfigsRequest))

case "/cockroach.roachpb.Internal/UpdateSystemSpanConfigs":
return a.authUpdateSystemSpanConfigs(tenID, req.(*roachpb.UpdateSystemSpanConfigsRequest))

case "/cockroach.roachpb.Internal/GetSystemSpanConfigs":
return a.authTenant(tenID)

default:
return authErrorf("unknown method %q", fullMethod)
}
Expand Down Expand Up @@ -296,6 +302,40 @@ func (a tenantAuthorizer) authUpdateSpanConfigs(
return nil
}

func (a tenantAuthorizer) authUpdateSystemSpanConfigs(
tenID roachpb.TenantID, args *roachpb.UpdateSystemSpanConfigsRequest,
) error {
if err := a.authTenant(tenID); err != nil {
return err
}

// The host tenant is allowed to target other secondary tenants, so we can
// skip validation checks below.
if tenID == roachpb.SystemTenantID {
return nil
}

// Ensure a secondary tenant isn't being targeted.
validate := func(target roachpb.SystemSpanConfigTarget) error {
if target.TenantID != nil {
return authError("secondary tenants cannot target tenants for system span configurations")
}
return nil
}

for _, target := range args.ToDelete {
if err := validate(target); err != nil {
return err
}
}
for _, entry := range args.ToUpsert {
if err := validate(entry.SystemSpanConfigTarget); err != nil {
return err
}
}
return nil
}

func contextWithTenant(ctx context.Context, tenID roachpb.TenantID) context.Context {
ctx = roachpb.NewContextForTenant(ctx, tenID)
ctx = logtags.AddTag(ctx, "tenant", tenID.String())
Expand Down
14 changes: 14 additions & 0 deletions pkg/rpc/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,20 @@ func (a internalClientAdapter) UpdateSpanConfigs(
return a.server.UpdateSpanConfigs(ctx, req)
}

// GetSystemSpanConfigs is part of the roachpb.InternalClient interface.
func (a internalClientAdapter) GetSystemSpanConfigs(
ctx context.Context, req *roachpb.GetSystemSpanConfigsRequest, _ ...grpc.CallOption,
) (*roachpb.GetSystemSpanConfigsResponse, error) {
return a.server.GetSystemSpanConfigs(ctx, req)
}

// UpdateSystemSpanConfigs is part of the roachpb.InternalClient interface.
func (a internalClientAdapter) UpdateSystemSpanConfigs(
ctx context.Context, req *roachpb.UpdateSystemSpanConfigsRequest, _ ...grpc.CallOption,
) (*roachpb.UpdateSystemSpanConfigsResponse, error) {
return a.server.UpdateSystemSpanConfigs(ctx, req)
}

type respStreamClientAdapter struct {
ctx context.Context
respC chan interface{}
Expand Down
12 changes: 12 additions & 0 deletions pkg/rpc/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,18 @@ func (*internalServer) GetSpanConfigs(
panic("unimplemented")
}

func (*internalServer) UpdateSystemSpanConfigs(
context.Context, *roachpb.UpdateSystemSpanConfigsRequest,
) (*roachpb.UpdateSystemSpanConfigsResponse, error) {
panic("unimplemented")
}

func (*internalServer) GetSystemSpanConfigs(
context.Context, *roachpb.GetSystemSpanConfigsRequest,
) (*roachpb.GetSystemSpanConfigsResponse, error) {
panic("unimplemented")
}

func (*internalServer) UpdateSpanConfigs(
context.Context, *roachpb.UpdateSpanConfigsRequest,
) (*roachpb.UpdateSpanConfigsResponse, error) {
Expand Down
12 changes: 12 additions & 0 deletions pkg/rpc/nodedialer/nodedialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,18 @@ func (*internalServer) UpdateSpanConfigs(
panic("unimplemented")
}

func (*internalServer) GetSystemSpanConfigs(
context.Context, *roachpb.GetSystemSpanConfigsRequest,
) (*roachpb.GetSystemSpanConfigsResponse, error) {
panic("unimplemented")
}

func (*internalServer) UpdateSystemSpanConfigs(
context.Context, *roachpb.UpdateSystemSpanConfigsRequest,
) (*roachpb.UpdateSystemSpanConfigsResponse, error) {
panic("unimplemented")
}

func (*internalServer) TenantSettings(
*roachpb.TenantSettingsRequest, roachpb.Internal_TenantSettingsServer,
) error {
Expand Down
23 changes: 23 additions & 0 deletions pkg/server/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1518,3 +1518,26 @@ func (n *Node) UpdateSpanConfigs(
}
return &roachpb.UpdateSpanConfigsResponse{}, nil
}

// GetSystemSpanConfigs implements the roachpb.InternalServer interface.
func (n *Node) GetSystemSpanConfigs(
ctx context.Context, _ *roachpb.GetSystemSpanConfigsRequest,
) (*roachpb.GetSystemSpanConfigsResponse, error) {
entries, err := n.spanConfigAccessor.GetSystemSpanConfigEntries(ctx)
if err != nil {
return nil, err
}

return &roachpb.GetSystemSpanConfigsResponse{SystemSpanConfigEntries: entries}, nil
}

// UpdateSystemSpanConfigs implements the roachpb.InternalServer interface.
func (n *Node) UpdateSystemSpanConfigs(
ctx context.Context, req *roachpb.UpdateSystemSpanConfigsRequest,
) (*roachpb.UpdateSystemSpanConfigsResponse, error) {
err := n.spanConfigAccessor.UpdateSystemSpanConfigEntries(ctx, req.ToDelete, req.ToUpsert)
if err != nil {
return nil, err
}
return &roachpb.UpdateSystemSpanConfigsResponse{}, nil
}
14 changes: 14 additions & 0 deletions pkg/spanconfig/spanconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ type KVAccessor interface {
toUpsert []roachpb.SpanConfigEntry,
) error

// GetSystemSpanConfigEntries returns the system span config entries that
// have been installed by the tenant.
GetSystemSpanConfigEntries(ctx context.Context) ([]roachpb.SystemSpanConfigEntry, error)

// UpdateSystemSpanConfigEntries updates system span configurations for the
// given targets. Targets for span config entries being deleted are expected
// to have been present; targets must be distinct within and across the two
// lists.
UpdateSystemSpanConfigEntries(
ctx context.Context,
toDelete []roachpb.SystemSpanConfigTarget,
toUpsert []roachpb.SystemSpanConfigEntry,
) error

// WithTxn returns a KVAccessor that runs using the given transaction (with
// its operations discarded if aborted, valid only if committed). If nil, a
// transaction is created internally for every operation.
Expand Down
14 changes: 14 additions & 0 deletions pkg/spanconfig/spanconfigkvaccessor/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ func (k dummyKVAccessor) UpdateSpanConfigEntries(
return k.error
}

// GetSystemSpanConfigEntries is part of the spanconfig.KVAccessor interface.
func (k dummyKVAccessor) GetSystemSpanConfigEntries(
context.Context,
) ([]roachpb.SystemSpanConfigEntry, error) {
return nil, k.error
}

// UpdateSystemSpanConfigEntries is part of the spanconfig.KVAccessor interface.
func (k dummyKVAccessor) UpdateSystemSpanConfigEntries(
context.Context, []roachpb.SystemSpanConfigTarget, []roachpb.SystemSpanConfigEntry,
) error {
return k.error
}

func (k dummyKVAccessor) WithTxn(context.Context, *kv.Txn) spanconfig.KVAccessor {
return k
}
Loading

0 comments on commit 5177417

Please sign in to comment.