-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stats: implement stats handler for DDL notifier part 1 #56419
Merged
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ddl | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pingcap/errors" | ||
"github.com/pingcap/tidb/pkg/ddl/notifier" | ||
"github.com/pingcap/tidb/pkg/meta/model" | ||
"github.com/pingcap/tidb/pkg/sessionctx" | ||
"github.com/pingcap/tidb/pkg/sessionctx/variable" | ||
"github.com/pingcap/tidb/pkg/statistics/handle/history" | ||
"github.com/pingcap/tidb/pkg/statistics/handle/logutil" | ||
"github.com/pingcap/tidb/pkg/statistics/handle/storage" | ||
"github.com/pingcap/tidb/pkg/statistics/handle/types" | ||
"github.com/pingcap/tidb/pkg/statistics/handle/util" | ||
"github.com/pingcap/tidb/pkg/util/intest" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type handler struct { | ||
statsCache types.StatsCache | ||
} | ||
|
||
// NewHandlerAndRegister creates a new handler and registers it to the DDL | ||
// notifier. | ||
func NewHandlerAndRegister(statsCache types.StatsCache) { | ||
h := handler{statsCache: statsCache} | ||
notifier.RegisterHandler(notifier.StatsHandlerID, h.handle) | ||
} | ||
|
||
func (h handler) handle( | ||
ctx context.Context, | ||
sctx sessionctx.Context, | ||
change *notifier.SchemaChangeEvent, | ||
) error { | ||
switch change.GetType() { | ||
case model.ActionCreateTable: | ||
info := change.GetCreateTableInfo() | ||
ids, err := getPhysicalIDs(sctx, info) | ||
if err != nil { | ||
return err | ||
} | ||
for _, id := range ids { | ||
err = h.insertStats4PhysicalID(ctx, sctx, info, id) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
} | ||
case model.ActionTruncateTable: | ||
newTableInfo, droppedTableInfo := change.GetTruncateTableInfo() | ||
ids, err := getPhysicalIDs(sctx, newTableInfo) | ||
if err != nil { | ||
return err | ||
} | ||
for _, id := range ids { | ||
err = h.insertStats4PhysicalID(ctx, sctx, newTableInfo, id) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
} | ||
|
||
// Remove the old table stats. | ||
droppedIDs, err2 := getPhysicalIDs(sctx, droppedTableInfo) | ||
if err2 != nil { | ||
return err2 | ||
} | ||
for _, id := range droppedIDs { | ||
err2 = h.delayedDeleteStats4PhysicalID(ctx, sctx, id) | ||
if err2 != nil { | ||
return errors.Trace(err2) | ||
} | ||
} | ||
case model.ActionDropTable: | ||
droppedTableInfo := change.GetDropTableInfo() | ||
ids, err := getPhysicalIDs(sctx, droppedTableInfo) | ||
if err != nil { | ||
return err | ||
} | ||
for _, id := range ids { | ||
err = h.delayedDeleteStats4PhysicalID(ctx, sctx, id) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
} | ||
case model.ActionAddColumn: | ||
// TODO: implement me | ||
case model.ActionModifyColumn: | ||
// TODO: implement me | ||
case model.ActionAddTablePartition: | ||
// TODO: implement me | ||
case model.ActionTruncateTablePartition: | ||
// TODO: implement me | ||
case model.ActionDropTablePartition: | ||
// TODO: implement me | ||
case model.ActionExchangeTablePartition: | ||
// TODO: implement me | ||
case model.ActionReorganizePartition: | ||
// TODO: implement me | ||
case model.ActionAlterTablePartitioning: | ||
// TODO: implement me | ||
case model.ActionRemovePartitioning: | ||
// TODO: implement me | ||
case model.ActionFlashbackCluster: | ||
// TODO: implement me | ||
default: | ||
intest.Assert(false) | ||
logutil.StatsLogger().Error("Unhandled schema change event", | ||
zap.Stringer("type", change)) | ||
} | ||
return nil | ||
} | ||
|
||
func (h handler) insertStats4PhysicalID( | ||
ctx context.Context, | ||
sctx sessionctx.Context, | ||
info *model.TableInfo, | ||
id int64, | ||
) error { | ||
startTS, err := storage.InsertTableStats2KV(ctx, sctx, info, id) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
return errors.Trace(h.recordHistoricalStatsMeta(ctx, sctx, id, startTS)) | ||
} | ||
|
||
func (h handler) recordHistoricalStatsMeta( | ||
ctx context.Context, | ||
sctx sessionctx.Context, | ||
id int64, | ||
startTS uint64, | ||
) error { | ||
if startTS == 0 { | ||
return nil | ||
} | ||
enableHistoricalStats, err2 := getEnableHistoricalStats(sctx) | ||
if err2 != nil { | ||
return err2 | ||
} | ||
if !enableHistoricalStats { | ||
return nil | ||
} | ||
|
||
tbl, ok := h.statsCache.Get(id) | ||
if !ok || !tbl.IsInitialized() { | ||
return nil | ||
} | ||
|
||
return history.RecordHistoricalStatsMeta( | ||
ctx, | ||
sctx, | ||
id, | ||
startTS, | ||
util.StatsMetaHistorySourceSchemaChange, | ||
) | ||
} | ||
|
||
func (h handler) delayedDeleteStats4PhysicalID( | ||
ctx context.Context, | ||
sctx sessionctx.Context, | ||
id int64, | ||
) error { | ||
startTS, err2 := storage.UpdateStatsMetaVersionForGC(ctx, sctx, id) | ||
if err2 != nil { | ||
return errors.Trace(err2) | ||
} | ||
return errors.Trace(h.recordHistoricalStatsMeta(ctx, sctx, id, startTS)) | ||
} | ||
|
||
func getPhysicalIDs( | ||
sctx sessionctx.Context, | ||
tblInfo *model.TableInfo, | ||
) (ids []int64, err error) { | ||
pi := tblInfo.GetPartitionInfo() | ||
if pi == nil { | ||
return []int64{tblInfo.ID}, nil | ||
} | ||
ids = make([]int64, 0, len(pi.Definitions)+1) | ||
for _, def := range pi.Definitions { | ||
ids = append(ids, def.ID) | ||
} | ||
pruneMode, err := getCurrentPruneMode(sctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if pruneMode == variable.Dynamic { | ||
ids = append(ids, tblInfo.ID) | ||
} | ||
return ids, nil | ||
} | ||
|
||
func getCurrentPruneMode( | ||
sctx sessionctx.Context, | ||
) (variable.PartitionPruneMode, error) { | ||
pruneMode, err := sctx.GetSessionVars(). | ||
GlobalVarsAccessor. | ||
GetGlobalSysVar(variable.TiDBPartitionPruneMode) | ||
return variable.PartitionPruneMode(pruneMode), errors.Trace(err) | ||
} | ||
|
||
func getEnableHistoricalStats( | ||
sctx sessionctx.Context, | ||
) (bool, error) { | ||
val, err := sctx.GetSessionVars(). | ||
GlobalVarsAccessor. | ||
GetGlobalSysVar(variable.TiDBEnableHistoricalStats) | ||
return variable.TiDBOptOn(val), errors.Trace(err) | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.