Skip to content

Commit

Permalink
Adds a feature whitelist at planbuild stage (pingcap#304)
Browse files Browse the repository at this point in the history
* init

Signed-off-by: David <[email protected]>

* format errors

Signed-off-by: David <[email protected]>

* restrict information schema

Signed-off-by: David <[email protected]>

* fmt

Signed-off-by: David <[email protected]>

* bazel

Signed-off-by: David <[email protected]>

* fmt

Signed-off-by: David <[email protected]>

* disable datadir

Signed-off-by: David <[email protected]>

* add ttl check for create and alter table

Signed-off-by: David <[email protected]>

* create table ttl

Signed-off-by: David <[email protected]>

* address comments

Signed-off-by: David <[email protected]>

* update error doc

Signed-off-by: David <[email protected]>

* add the back to the error message

Signed-off-by: David <[email protected]>

* unblock 'SHOW CONFIG' for lightning

Signed-off-by: David <[email protected]>

---------

Signed-off-by: David <[email protected]>
Co-authored-by: zzm <[email protected]>
  • Loading branch information
AmoebaProtozoa and zeminzhou authored Mar 13, 2023
1 parent d05a1e2 commit ba65916
Show file tree
Hide file tree
Showing 11 changed files with 394 additions and 9 deletions.
1 change: 1 addition & 0 deletions ddl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ go_library(
"//util/ranger",
"//util/resourcegrouptag",
"//util/rowDecoder",
"//util/serverless",
"//util/set",
"//util/slice",
"//util/sqlexec",
Expand Down
5 changes: 5 additions & 0 deletions ddl/ttl.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/dbterror"
"github.com/pingcap/tidb/util/serverless"
)

func onTTLInfoRemove(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, err error) {
Expand Down Expand Up @@ -97,6 +98,10 @@ func onTTLInfoChange(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, err er
}

func checkTTLInfoValid(ctx sessionctx.Context, schema model.CIStr, tblInfo *model.TableInfo) error {
if err := serverless.VerifyTTLInfo(schema, tblInfo); err != nil {
return err
}

if err := checkTTLIntervalExpr(ctx, tblInfo.TTLInfo); err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions errno/errcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1112,4 +1112,7 @@ const (
ErrTiKVMaxTimestampNotSynced = 9011
ErrTiFlashServerTimeout = 9012
ErrTiFlashServerBusy = 9013

// Serverless tier errors.
ErrNotSupportedOnServerless = 20001
)
3 changes: 3 additions & 0 deletions errno/errname.go
Original file line number Diff line number Diff line change
Expand Up @@ -1119,4 +1119,7 @@ var MySQLErrName = map[uint16]*mysql.ErrMessage{
ErrPrometheusAddrIsNotSet: mysql.Message("Prometheus address is not set in PD and etcd", nil),
ErrTiKVStaleCommand: mysql.Message("TiKV server reports stale command", nil),
ErrTiKVMaxTimestampNotSynced: mysql.Message("TiKV max timestamp is not synced", nil),

// Serverless Tier errors.
ErrNotSupportedOnServerless: mysql.Message("'%s' is not supported on the Serverless Tier.", nil),
}
5 changes: 5 additions & 0 deletions errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,11 @@ error = '''
%s is not supported. Reason: %s. Try %s.
'''

["ddl:20001"]
error = '''
'%s' is not supported on the Serverless Tier.
'''

["ddl:3102"]
error = '''
Expression of generated column '%s' contains a disallowed function.
Expand Down
1 change: 1 addition & 0 deletions planner/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ go_library(
"//util/ranger",
"//util/rowcodec",
"//util/sem",
"//util/serverless",
"//util/set",
"//util/size",
"//util/sqlexec",
Expand Down
5 changes: 5 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import (
utilparser "github.com/pingcap/tidb/util/parser"
"github.com/pingcap/tidb/util/ranger"
"github.com/pingcap/tidb/util/sem"
"github.com/pingcap/tidb/util/serverless"
"github.com/pingcap/tidb/util/set"
"github.com/pingcap/tidb/util/sqlexec"
"github.com/pingcap/tidb/util/stmtsummary"
Expand Down Expand Up @@ -777,6 +778,10 @@ func (b *PlanBuilder) ResetForReuse() *PlanBuilder {
// Build builds the ast node to a Plan.
func (b *PlanBuilder) Build(ctx context.Context, node ast.Node) (Plan, error) {
b.optFlag |= flagPrunColumns

if err := serverless.VerifyStatement(node); err != nil {
return nil, err
}
switch x := node.(type) {
case *ast.AdminStmt:
return b.buildAdmin(ctx, x)
Expand Down
3 changes: 3 additions & 0 deletions util/dbterror/ddl_terror.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,7 @@ var (

// ErrNotSupportedYet returns when tidb does not support this feature.
ErrNotSupportedYet = ClassDDL.NewStd(mysql.ErrNotSupportedYet)

// ErrNotSupportedOnServerless returns when user requested a serverless unsupported feature.
ErrNotSupportedOnServerless = ClassDDL.NewStd(mysql.ErrNotSupportedOnServerless)
)
32 changes: 23 additions & 9 deletions util/sem/sem.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,21 @@ const (
restrictedPriv = "RESTRICTED_"
tidbAuditRetractLog = "tidb_audit_redact_log" // sysvar installed by a plugin

placementAdmin = "PLACEMENT_ADMIN"
placementAdmin = "PLACEMENT_ADMIN"
backupAdmin = "BACKUP_ADMIN"
restoreAdmin = "RESTORE_ADMIN"
resourceGroupAdmin = "RESOURCE_GROUP_ADMIN"

// Additional tables for serverless tier.
clusterInfo = "cluster_info"
tikvRegionStatus = "tikv_region_status"
tikvStoreStatus = "tikv_store_status"
tiflashSegments = "tiflash_segments"
tiflashTables = "tiflash_tables"
clusterInfo = "cluster_info"
tikvRegionStatus = "tikv_region_status"
tikvStoreStatus = "tikv_store_status"
tiflashSegments = "tiflash_segments"
tiflashTables = "tiflash_tables"
resourceGroups = "resource_groups"
tidbHotRegionsHistory = "tidb_hot_regions_history"
tidbServersInfo = "tidb_servers_info"

// Serverless tier slow query related tables.
slowQuery = "slow_query"
clusterSlowQuery = "cluster_slow_query"
Expand Down Expand Up @@ -132,7 +139,8 @@ func IsInvisibleTable(dbLowerName, tblLowerName string) bool {
inspectionRules, inspectionSummary, metricsSummary, metricsSummaryByLabel, metricsTables, tidbHotRegions,
clusterInfo, tikvRegionStatus, tikvStoreStatus, tiflashSegments, tiflashTables, clusterSlowQuery,
slowQuery, statementsSummary, statementsSummaryEvicted, statementsSummaryHistory, clusterStatementsSummary,
clusterStatementsSummaryEvicted, clusterStatementsSummaryHistory:
clusterStatementsSummaryEvicted, clusterStatementsSummaryHistory, resourceGroups, tidbHotRegionsHistory,
tidbServersInfo:
return true
}
case performanceSchema:
Expand Down Expand Up @@ -191,7 +199,8 @@ func IsInvisibleSysVar(varNameInLower string) bool {
variable.TiDBStmtSummaryFileMaxBackups,
variable.TiDBStmtSummaryFilename,
tidbAuditRetractLog,
variable.TiDBEnableAsyncCommit:
variable.TiDBEnableAsyncCommit,
variable.DataDir:
return true
}
return false
Expand All @@ -200,7 +209,12 @@ func IsInvisibleSysVar(varNameInLower string) bool {
// IsRestrictedPrivilege returns true if the privilege shuld not be satisfied by SUPER
// As most dynamic privileges are.
func IsRestrictedPrivilege(privNameInUpper string) bool {
if privNameInUpper == placementAdmin {
switch privNameInUpper {
case
placementAdmin,
backupAdmin,
restoreAdmin,
resourceGroupAdmin:
return true
}
if len(privNameInUpper) < 12 {
Expand Down
13 changes: 13 additions & 0 deletions util/serverless/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "serverless",
srcs = ["feature_control.go"],
importpath = "github.com/pingcap/tidb/util/serverless",
visibility = ["//visibility:public"],
deps = [
"//parser/ast",
"//parser/model",
"//util/dbterror",
],
)
Loading

0 comments on commit ba65916

Please sign in to comment.