-
Notifications
You must be signed in to change notification settings - Fork 6.1k
workload repo: Merge Workload Repository into master #57148
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
Merged
+1,382
−3
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
20c31f4
*: workload repository init code (#1098)
xhebox 18444f0
repository: create persistent tables table (#1220)
xhebox 85fdc6d
repository: housekeeper thread (#1245)
xhebox def1c11
repository: add table create and insert statement generation support.…
wddevries b7a3832
repository: sample and snapshot threads (#1272)
xhebox 585caca
repository: Add variables to control the sampling and snapshot interv…
wddevries 6601452
repository: expose variables for users (#1353)
xhebox 5c34c72
Add code to start owner compaign.
wddevries 50e5681
repository: Separate the repository code from the domain. (#1342)
wddevries c440359
Fix bazel scripts.
wddevries d77e8fd
Use any instead of interface{} in worker.go.
wddevries 7783930
Fix owner campaign patch.
wddevries 3499a36
Remove unused error return value from Stop()
wddevries 920ed62
Use tagged switch on dst.
wddevries 91fd463
Check error result from res.Close() in worker.runQuery().
wddevries 2ed51fa
Rename unused parameters to _.
wddevries 1074f4c
Remove unused method receiver 'w' from several functions.
wddevries b280791
Fix type in description of comment for WR_VER field of HIST_SNAPSHOT.
wddevries 0150dc3
Cancel() was removed from owner.Manger. Use Close() instead.
wddevries 4a3a4a9
Remove unneeded return statement and unused method receivers.
wddevries 94a5619
repository: unit test example (#1379)
xhebox 28e5f18
Fix example tests.
wddevries 0027670
Rework worker.stop() to fix race.
wddevries c0d62d7
Set owner.ManagerSessionTTL instead of starting a campaign in the uni…
wddevries 4719db2
Move repository to pkg/util and rename it to workloadrepo. (#1418)
wddevries 5368a64
Rewrite some loops to use range.
wddevries 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,56 @@ | ||
| load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
|
||
| go_library( | ||
| name = "workloadrepo", | ||
| srcs = [ | ||
| "const.go", | ||
| "housekeeper.go", | ||
| "sampling.go", | ||
| "snapshot.go", | ||
| "table.go", | ||
| "utils.go", | ||
| "worker.go", | ||
| ], | ||
| importpath = "github.com/pingcap/tidb/pkg/util/workloadrepo", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//pkg/domain", | ||
| "//pkg/domain/infosync", | ||
| "//pkg/infoschema", | ||
| "//pkg/kv", | ||
| "//pkg/meta/model", | ||
| "//pkg/owner", | ||
| "//pkg/parser/model", | ||
| "//pkg/sessionctx", | ||
| "//pkg/sessionctx/variable", | ||
| "//pkg/sessiontxn", | ||
| "//pkg/util", | ||
| "//pkg/util/chunk", | ||
| "//pkg/util/logutil", | ||
| "//pkg/util/slice", | ||
| "//pkg/util/sqlescape", | ||
| "//pkg/util/sqlexec", | ||
| "@com_github_ngaut_pools//:pools", | ||
| "@com_github_pingcap_errors//:errors", | ||
| "@io_etcd_go_etcd_client_v3//:client", | ||
| "@org_uber_go_zap//:zap", | ||
| ], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "workloadrepo_test", | ||
| timeout = "short", | ||
| srcs = ["worker_test.go"], | ||
| embed = [":workloadrepo"], | ||
| flaky = True, | ||
| deps = [ | ||
| "//pkg/domain", | ||
| "//pkg/kv", | ||
| "//pkg/owner", | ||
| "//pkg/parser/model", | ||
| "//pkg/testkit", | ||
| "@com_github_stretchr_testify//require", | ||
| "@io_etcd_go_etcd_client_v3//:client", | ||
| "@io_etcd_go_etcd_server_v3//embed", | ||
| ], | ||
| ) |
This file contains hidden or 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,42 @@ | ||
| // 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 workloadrepo | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/pingcap/tidb/pkg/parser/model" | ||
| ) | ||
|
|
||
| const ( | ||
| ownerKey = "/tidb/workloadrepo/owner" | ||
| promptKey = "workloadrepo" | ||
| snapIDKey = "/tidb/workloadrepo/snap_id" | ||
|
|
||
| etcdOpTimeout = 5 * time.Second | ||
|
|
||
| defSamplingInterval = 5 | ||
| defSnapshotInterval = 3600 | ||
| defRententionDays = 7 | ||
|
|
||
| // WorkloadSchema is the name of database for workloadrepo worker. | ||
| WorkloadSchema = "WORKLOAD_SCHEMA" | ||
| histSnapshotsTable = "HIST_SNAPSHOTS" | ||
| ) | ||
|
|
||
| var ( | ||
| workloadSchemaCIStr = model.NewCIStr(WorkloadSchema) | ||
| zeroTime = time.Time{} | ||
| ) |
This file contains hidden or 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,145 @@ | ||
| // 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 workloadrepo | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/pingcap/tidb/pkg/infoschema" | ||
| "github.com/pingcap/tidb/pkg/parser/model" | ||
| "github.com/pingcap/tidb/pkg/sessionctx" | ||
| "github.com/pingcap/tidb/pkg/sessiontxn" | ||
| "github.com/pingcap/tidb/pkg/util/logutil" | ||
| "github.com/pingcap/tidb/pkg/util/sqlescape" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| func calcNextTick(now time.Time) time.Duration { | ||
| // only activated at 2am | ||
| next := time.Date(now.Year(), now.Month(), now.Day(), 2, 0, 0, 0, time.Local) | ||
| if !next.After(now) { | ||
| next = next.AddDate(0, 0, 1) | ||
| } | ||
| return next.Sub(now) | ||
| } | ||
|
|
||
| func createAllPartitions(ctx context.Context, sess sessionctx.Context, is infoschema.InfoSchema) error { | ||
| sb := &strings.Builder{} | ||
| for _, tbl := range workloadTables { | ||
| tbSchema, err := is.TableByName(ctx, workloadSchemaCIStr, model.NewCIStr(tbl.destTable)) | ||
| if err != nil { | ||
| logutil.BgLogger().Info("workload repository cannot get table", zap.String("tbl", tbl.destTable), zap.NamedError("err", err)) | ||
| return err | ||
| } | ||
| tbInfo := tbSchema.Meta() | ||
|
|
||
| sb.Reset() | ||
| sqlescape.MustFormatSQL(sb, "ALTER TABLE %n.%n ADD PARTITION (", WorkloadSchema, tbl.destTable) | ||
| if !generatePartitionRanges(sb, tbInfo) { | ||
| fmt.Fprintf(sb, ")") | ||
| _, err = execRetry(ctx, sess, sb.String()) | ||
| if err != nil { | ||
| logutil.BgLogger().Info("workload repository cannot add partitions", zap.String("parts", sb.String()), zap.NamedError("err", err)) | ||
| return err | ||
| } | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (w *worker) dropOldPartitions(ctx context.Context, sess sessionctx.Context, is infoschema.InfoSchema, now time.Time) error { | ||
| w.Lock() | ||
| retention := int(w.retentionDays) | ||
| w.Unlock() | ||
|
|
||
| if retention == 0 { | ||
| // disabled housekeeping | ||
| return nil | ||
| } | ||
|
|
||
| sb := &strings.Builder{} | ||
| for _, tbl := range workloadTables { | ||
| tbSchema, err := is.TableByName(ctx, workloadSchemaCIStr, model.NewCIStr(tbl.destTable)) | ||
| if err != nil { | ||
| logutil.BgLogger().Info("workload repository cannot get table", zap.String("tbl", tbl.destTable), zap.NamedError("err", err)) | ||
| continue | ||
| } | ||
| tbInfo := tbSchema.Meta() | ||
| for _, pt := range tbInfo.GetPartitionInfo().Definitions { | ||
| ot, err := time.Parse("p20060102", pt.Name.L) | ||
| if err != nil { | ||
| logutil.BgLogger().Info("workload repository cannot parse partition name", zap.String("part", pt.Name.L), zap.NamedError("err", err)) | ||
| break | ||
| } | ||
| if int(now.Sub(ot).Hours()/24) < retention { | ||
| continue | ||
| } | ||
| sb.Reset() | ||
| sqlescape.MustFormatSQL(sb, "ALTER TABLE %s.%s DROP PARTITION %s", | ||
| WorkloadSchema, tbl.destTable, pt.Name.L) | ||
| _, err = execRetry(ctx, sess, sb.String()) | ||
| if err != nil { | ||
| logutil.BgLogger().Info("workload repository cannot drop partition", zap.String("part", pt.Name.L), zap.NamedError("err", err)) | ||
| break | ||
| } | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (w *worker) startHouseKeeper(ctx context.Context) func() { | ||
| return func() { | ||
| now := time.Now() | ||
| timer := time.NewTimer(calcNextTick(now)) | ||
| defer timer.Stop() | ||
|
|
||
| _sessctx := w.getSessionWithRetry() | ||
| defer w.sesspool.Put(_sessctx) | ||
| sess := _sessctx.(sessionctx.Context) | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case now := <-timer.C: | ||
| // Owner only | ||
| if !w.owner.IsOwner() { | ||
| continue | ||
| } | ||
|
|
||
| // get the latest infoschema | ||
| is := sessiontxn.GetTxnManager(sess).GetTxnInfoSchema() | ||
|
|
||
| // create new partitions | ||
| if err := createAllPartitions(ctx, sess, is); err != nil { | ||
| continue | ||
| } | ||
|
|
||
| // drop old partitions | ||
| if err := w.dropOldPartitions(ctx, sess, is, now); err != nil { | ||
| continue | ||
| } | ||
|
|
||
| // reschedule, drain channel first | ||
| if !timer.Stop() { | ||
| <-timer.C | ||
| } | ||
| timer.Reset(calcNextTick(now)) | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
Maybe we should use const in
repositorypackage. Removing depedency is great, but it will also be convenient when we want to know all occurences of schema table name by searching workloadSchemaCIStr.