forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: add TokenBucket connector API
This change adds the TokenBucket API proposed in the RFC (cockroachdb#66436), a stub implementation and client for it, and the corresponding KV connector interface. The client and server-side code lives in ccl/multitenantccl/tenantcostclient and tenantcostserver. Release note: None
- Loading branch information
1 parent
639cb02
commit 304a9e6
Showing
34 changed files
with
1,763 additions
and
546 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
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
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,12 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "multitenantccl", | ||
srcs = ["doc.go"], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/ccl/multitenantccl", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/ccl/multitenantccl/tenantcostclient", | ||
"//pkg/ccl/multitenantccl/tenantcostserver", | ||
], | ||
) |
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,15 @@ | ||
// Copyright 2021 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 multitenantccl | ||
|
||
import ( | ||
// Imports for the CCL init hooks. | ||
_ "github.com/cockroachdb/cockroach/pkg/ccl/multitenantccl/tenantcostclient" | ||
_ "github.com/cockroachdb/cockroach/pkg/ccl/multitenantccl/tenantcostserver" | ||
) |
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,17 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "tenantcostclient", | ||
srcs = ["tenant_side.go"], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/ccl/multitenantccl/tenantcostclient", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/kv/kvclient/kvtenant", | ||
"//pkg/multitenant", | ||
"//pkg/roachpb:with-mocks", | ||
"//pkg/server", | ||
"//pkg/util/log", | ||
"//pkg/util/stop", | ||
"@com_github_cockroachdb_errors//:errors", | ||
], | ||
) |
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,80 @@ | ||
// Copyright 2021 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 tenantcostclient | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvtenant" | ||
"github.com/cockroachdb/cockroach/pkg/multitenant" | ||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/server" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/cockroachdb/cockroach/pkg/util/stop" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
// NewTenantSideCostController creates an object which implements the | ||
// server.TenantSideCostController interface. | ||
func NewTenantSideCostController( | ||
ctx context.Context, tenantID roachpb.TenantID, provider kvtenant.TokenBucketProvider, | ||
) (multitenant.TenantSideCostController, error) { | ||
if tenantID == roachpb.SystemTenantID { | ||
return nil, errors.AssertionFailedf("cost controller can't be used for system tenant") | ||
} | ||
return &tenantSideCostController{ | ||
tenantID: tenantID, | ||
provider: provider, | ||
}, nil | ||
} | ||
|
||
func init() { | ||
server.NewTenantSideCostController = NewTenantSideCostController | ||
} | ||
|
||
type tenantSideCostController struct { | ||
tenantID roachpb.TenantID | ||
provider kvtenant.TokenBucketProvider | ||
} | ||
|
||
var _ multitenant.TenantSideCostController = (*tenantSideCostController)(nil) | ||
|
||
// Start is part of multitenant.TenantSideCostController. | ||
func (c *tenantSideCostController) Start(ctx context.Context, stopper *stop.Stopper) error { | ||
return stopper.RunAsyncTask(ctx, "cost-controller", func(ctx context.Context) { | ||
c.mainLoop(ctx, stopper) | ||
}) | ||
} | ||
|
||
func (c *tenantSideCostController) mainLoop(ctx context.Context, stopper *stop.Stopper) { | ||
ticker := time.NewTicker(10 * time.Second) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ticker.C: | ||
req := roachpb.TokenBucketRequest{ | ||
ConsumptionSinceLastRequest: roachpb.TokenBucketRequest_Consumption{ | ||
// Report a dummy 1 RU consumption each time. | ||
RU: 1, | ||
SQLPodCPUSeconds: 1, | ||
}, | ||
} | ||
_, err := c.provider.TokenBucket(ctx, &req) | ||
if err != nil { | ||
log.Warningf(ctx, "TokenBucket error: %v", err) | ||
} | ||
|
||
case <-stopper.ShouldQuiesce(): | ||
// TODO(radu): send one last request to update consumption. | ||
return | ||
} | ||
} | ||
} |
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,21 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "tenantcostserver", | ||
srcs = [ | ||
"server.go", | ||
"token_bucket.go", | ||
], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/ccl/multitenantccl/tenantcostserver", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/ccl/multitenantccl/tenantcostserver/tenanttokenbucket", | ||
"//pkg/kv", | ||
"//pkg/multitenant", | ||
"//pkg/roachpb:with-mocks", | ||
"//pkg/server", | ||
"//pkg/sql", | ||
"//pkg/util/timeutil", | ||
"@com_github_cockroachdb_errors//:errors", | ||
], | ||
) |
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,38 @@ | ||
// Copyright 2021 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 tenantcostserver | ||
|
||
import ( | ||
"github.com/cockroachdb/cockroach/pkg/kv" | ||
"github.com/cockroachdb/cockroach/pkg/multitenant" | ||
"github.com/cockroachdb/cockroach/pkg/server" | ||
"github.com/cockroachdb/cockroach/pkg/sql" | ||
) | ||
|
||
type instance struct { | ||
db *kv.DB | ||
executor *sql.InternalExecutor | ||
} | ||
|
||
func newInstance(db *kv.DB, executor *sql.InternalExecutor) *instance { | ||
return &instance{ | ||
db: db, | ||
executor: executor, | ||
} | ||
} | ||
|
||
var _ multitenant.TenantUsageServer = (*instance)(nil) | ||
|
||
func init() { | ||
server.NewTenantUsageServer = func( | ||
db *kv.DB, executor *sql.InternalExecutor, | ||
) multitenant.TenantUsageServer { | ||
return newInstance(db, executor) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
pkg/ccl/multitenantccl/tenantcostserver/tenanttokenbucket/BUILD.bazel
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,9 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "tenanttokenbucket", | ||
srcs = ["tenant_token_bucket.go"], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/ccl/multitenantccl/tenantcostserver/tenanttokenbucket", | ||
visibility = ["//visibility:public"], | ||
deps = ["//pkg/roachpb:with-mocks"], | ||
) |
45 changes: 45 additions & 0 deletions
45
pkg/ccl/multitenantccl/tenantcostserver/tenanttokenbucket/tenant_token_bucket.go
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,45 @@ | ||
// Copyright 2021 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 tenanttokenbucket implements the tenant token bucket server-side | ||
// algorithm described in the distributed token bucket RFC. It has minimal | ||
// dependencies and is meant to be testable on its own. | ||
package tenanttokenbucket | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
) | ||
|
||
// State of the distributed token bucket. | ||
type State struct { | ||
// RUBurstLimit is the burst limit in RUs. | ||
RUBurstLimit float64 | ||
// RURefillRate is the refill rate in RUs/second. | ||
RURefillRate float64 | ||
|
||
// RUCurrent is the available (burst) RUs. | ||
RUCurrent float64 | ||
|
||
// CurrentShareSum is the sum of the last reported share value for | ||
// each active SQL pod for the tenant. | ||
CurrentShareSum float64 | ||
} | ||
|
||
// Request processes a request for more tokens and updates the State | ||
// accordingly. | ||
func (s *State) Request( | ||
req *roachpb.TokenBucketRequest, now time.Time, | ||
) roachpb.TokenBucketResponse { | ||
var res roachpb.TokenBucketResponse | ||
// TODO(radu): fill in response. | ||
return res | ||
} | ||
|
||
// TODO(radu): add Reconfigure API. |
Oops, something went wrong.