Skip to content

Commit

Permalink
distsql, executor: remove pctx from trectx (pingcap#52496)
Browse files Browse the repository at this point in the history
  • Loading branch information
YangKeao authored and 3AceShowHand committed Apr 16, 2024
1 parent cebb591 commit 8dce277
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 40 deletions.
2 changes: 1 addition & 1 deletion pkg/distsql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ go_library(
"//pkg/errctx",
"//pkg/errno",
"//pkg/expression",
"//pkg/infoschema",
"//pkg/infoschema/context",
"//pkg/kv",
"//pkg/metrics",
"//pkg/parser/model",
Expand Down
20 changes: 8 additions & 12 deletions pkg/distsql/request_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/pingcap/tidb/pkg/ddl/placement"
distsqlctx "github.com/pingcap/tidb/pkg/distsql/context"
"github.com/pingcap/tidb/pkg/errctx"
"github.com/pingcap/tidb/pkg/infoschema"
infoschema "github.com/pingcap/tidb/pkg/infoschema/context"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/parser/mysql"
Expand All @@ -45,7 +45,7 @@ import (
// It is called before we issue a kv request by "Select".
type RequestBuilder struct {
kv.Request
is infoschema.InfoSchema
is infoschema.MetaOnlyInfoSchema
err error
}

Expand Down Expand Up @@ -340,11 +340,7 @@ func (builder *RequestBuilder) SetTiDBServerID(serverID uint64) *RequestBuilder

// SetFromInfoSchema sets the following fields from infoSchema:
// "bundles"
func (builder *RequestBuilder) SetFromInfoSchema(pis any) *RequestBuilder {
is, ok := pis.(infoschema.InfoSchema)
if !ok {
return builder
}
func (builder *RequestBuilder) SetFromInfoSchema(is infoschema.MetaOnlyInfoSchema) *RequestBuilder {
builder.is = is
builder.Request.SchemaVar = is.SchemaMetaVersion()
return builder
Expand Down Expand Up @@ -387,13 +383,13 @@ func (builder *RequestBuilder) verifyTxnScope() error {
if !valid {
var tblName string
var partName string
tblInfo, _, partInfo := builder.is.FindTableByPartitionID(phyTableID)
tblInfo, _, partInfo := builder.is.FindTableInfoByPartitionID(phyTableID)
if tblInfo != nil && partInfo != nil {
tblName = tblInfo.Meta().Name.String()
tblName = tblInfo.Name.String()
partName = partInfo.Name.String()
} else {
tblInfo, _ = builder.is.TableByID(phyTableID)
tblName = tblInfo.Meta().Name.String()
tblInfo, _ = builder.is.TableInfoByID(phyTableID)
tblName = tblInfo.Name.String()
}
err := fmt.Errorf("table %v can not be read by %v txn_scope", tblName, txnScope)
if len(partName) > 0 {
Expand Down Expand Up @@ -703,7 +699,7 @@ func CommonHandleRangesToKVRanges(dctx *distsqlctx.DistSQLContext, tids []int64,
}

// VerifyTxnScope verify whether the txnScope and visited physical table break the leader rule's dcLocation.
func VerifyTxnScope(txnScope string, physicalTableID int64, is infoschema.InfoSchema) bool {
func VerifyTxnScope(txnScope string, physicalTableID int64, is infoschema.MetaOnlyInfoSchema) bool {
if txnScope == "" || txnScope == kv.GlobalTxnScope {
return true
}
Expand Down
1 change: 1 addition & 0 deletions pkg/executor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ go_library(
"//pkg/expression/aggregation",
"//pkg/expression/context",
"//pkg/infoschema",
"//pkg/infoschema/context",
"//pkg/keyspace",
"//pkg/kv",
"//pkg/lightning/log",
Expand Down
9 changes: 5 additions & 4 deletions pkg/executor/table_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/pingcap/tidb/pkg/expression"
exprctx "github.com/pingcap/tidb/pkg/expression/context"
"github.com/pingcap/tidb/pkg/infoschema"
isctx "github.com/pingcap/tidb/pkg/infoschema/context"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/parser/model"
planctx "github.com/pingcap/tidb/pkg/planner/context"
Expand Down Expand Up @@ -81,16 +82,16 @@ type tableReaderExecutorContext struct {
dctx *distsqlctx.DistSQLContext
rctx *rangerctx.RangerContext
buildPBCtx *planctx.BuildPBContext
pctx planctx.PlanContext
ectx exprctx.BuildContext

stmtMemTracker *memory.Tracker

infoSchema isctx.MetaOnlyInfoSchema
getDDLOwner func(context.Context) (*infosync.ServerInfo, error)
}

func (treCtx *tableReaderExecutorContext) GetInfoSchema() infoschema.InfoSchema {
return treCtx.pctx.GetInfoSchema().(infoschema.InfoSchema)
func (treCtx *tableReaderExecutorContext) GetInfoSchema() isctx.MetaOnlyInfoSchema {
return treCtx.infoSchema
}

func (treCtx *tableReaderExecutorContext) GetDDLOwner(ctx context.Context) (*infosync.ServerInfo, error) {
Expand Down Expand Up @@ -123,9 +124,9 @@ func newTableReaderExecutorContext(sctx sessionctx.Context) tableReaderExecutorC
dctx: sctx.GetDistSQLCtx(),
rctx: pctx.GetRangerCtx(),
buildPBCtx: pctx.GetBuildPBCtx(),
pctx: pctx,
ectx: sctx.GetExprCtx(),
stmtMemTracker: sctx.GetSessionVars().StmtCtx.MemTracker,
infoSchema: pctx.GetInfoSchema(),
getDDLOwner: getDDLOwner,
}
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/infoschema/context/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ go_library(
srcs = ["infoschema.go"],
importpath = "github.com/pingcap/tidb/pkg/infoschema/context",
visibility = ["//visibility:public"],
deps = ["//pkg/parser/model"],
deps = [
"//pkg/ddl/placement",
"//pkg/parser/model",
],
)
24 changes: 23 additions & 1 deletion pkg/infoschema/context/infoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package context

import "github.com/pingcap/tidb/pkg/parser/model"
import (
"github.com/pingcap/tidb/pkg/ddl/placement"
"github.com/pingcap/tidb/pkg/parser/model"
)

// MetaOnlyInfoSchema is a workaround.
// Due to circular dependency cannot return the complete interface.
Expand All @@ -31,4 +34,23 @@ type MetaOnlyInfoSchema interface {
AllSchemas() []*model.DBInfo
AllSchemaNames() []model.CIStr
SchemaTableInfos(schema model.CIStr) []*model.TableInfo
Misc
}

// Misc contains the methods that are not closely related to InfoSchema.
type Misc interface {
PolicyByName(name model.CIStr) (*model.PolicyInfo, bool)
ResourceGroupByName(name model.CIStr) (*model.ResourceGroupInfo, bool)
// PlacementBundleByPhysicalTableID is used to get a rule bundle.
PlacementBundleByPhysicalTableID(id int64) (*placement.Bundle, bool)
// AllPlacementBundles is used to get all placement bundles
AllPlacementBundles() []*placement.Bundle
// AllPlacementPolicies returns all placement policies
AllPlacementPolicies() []*model.PolicyInfo
// AllResourceGroups returns all resource groups
AllResourceGroups() []*model.ResourceGroupInfo
// HasTemporaryTable returns whether information schema has temporary table
HasTemporaryTable() bool
// GetTableReferredForeignKeys gets the table's ReferredFKInfo by lowercase schema and table name.
GetTableReferredForeignKeys(schema, table string) []*model.ReferredFKInfo
}
2 changes: 1 addition & 1 deletion pkg/infoschema/infoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/pingcap/tidb/pkg/util/mock"
)

var _ Misc = &infoSchemaMisc{}
var _ context.Misc = &infoSchemaMisc{}

type sortedTables []table.Table

Expand Down
20 changes: 0 additions & 20 deletions pkg/infoschema/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package infoschema

import (
"github.com/pingcap/tidb/pkg/ddl/placement"
"github.com/pingcap/tidb/pkg/infoschema/context"
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/table"
Expand All @@ -30,24 +29,5 @@ type InfoSchema interface {
TableByID(id int64) (table.Table, bool)
SchemaTables(schema model.CIStr) []table.Table
FindTableByPartitionID(partitionID int64) (table.Table, *model.DBInfo, *model.PartitionDefinition)
Misc
base() *infoSchema
}

// Misc contains the methods that are not closely related to InfoSchema.
type Misc interface {
PolicyByName(name model.CIStr) (*model.PolicyInfo, bool)
ResourceGroupByName(name model.CIStr) (*model.ResourceGroupInfo, bool)
// PlacementBundleByPhysicalTableID is used to get a rule bundle.
PlacementBundleByPhysicalTableID(id int64) (*placement.Bundle, bool)
// AllPlacementBundles is used to get all placement bundles
AllPlacementBundles() []*placement.Bundle
// AllPlacementPolicies returns all placement policies
AllPlacementPolicies() []*model.PolicyInfo
// AllResourceGroups returns all resource groups
AllResourceGroups() []*model.ResourceGroupInfo
// HasTemporaryTable returns whether information schema has temporary table
HasTemporaryTable() bool
// GetTableReferredForeignKeys gets the table's ReferredFKInfo by lowercase schema and table name.
GetTableReferredForeignKeys(schema, table string) []*model.ReferredFKInfo
}

0 comments on commit 8dce277

Please sign in to comment.