-
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
repository: Merge Workload Repository into master #57148
Open
wddevries
wants to merge
11
commits into
pingcap:master
Choose a base branch
from
wddevries:rebase_to_master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,215
−3
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
47c6736
*: workload repository init code (#1098)
xhebox 40ddd61
repository: create persistent tables table (#1220)
xhebox 4a210e2
repository: housekeeper thread (#1245)
xhebox b8dbf20
repository: add table create and insert statement generation support.…
wddevries 868d162
repository: sample and snapshot threads (#1272)
xhebox 0d657b1
repository: Add variables to control the sampling and snapshot interv…
wddevries 6cfe0e8
repository: expose variables for users (#1353)
xhebox fe34bb3
Add code to start owner compaign.
wddevries 5cc8b4f
repository: Separate the repository code from the domain. (#1342)
wddevries 6d24e57
Fix bazel scripts.
wddevries 202a37b
Use any instead of interface{} in worker.go.
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 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,39 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "repository", | ||
srcs = [ | ||
"const.go", | ||
"housekeeper.go", | ||
"sampling.go", | ||
"snapshot.go", | ||
"table.go", | ||
"utils.go", | ||
"worker.go", | ||
], | ||
importpath = "github.com/pingcap/tidb/pkg/domain/repository", | ||
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_atomic//:atomic", | ||
"@org_uber_go_zap//:zap", | ||
], | ||
) |
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 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 repository | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/pingcap/tidb/pkg/parser/model" | ||
) | ||
|
||
const ( | ||
ownerKey = "/tidb/repository/owner" | ||
promptKey = "repository" | ||
snapIDKey = "/tidb/repository/snap_id" | ||
|
||
etcdOpTimeout = 5 * time.Second | ||
|
||
// WorkloadSchema is the name of database for repository worker. | ||
WorkloadSchema = "WORKLOAD_SCHEMA" | ||
histSnapshotsTable = "HIST_SNAPSHOTS" | ||
) | ||
|
||
var ( | ||
workloadSchemaCIStr = model.NewCIStr(WorkloadSchema) | ||
zeroTime = time.Time{} | ||
) |
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,149 @@ | ||
// 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 repository | ||
|
||
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/atomic" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var ( | ||
retentionDays = atomic.NewInt32(-1) | ||
) | ||
|
||
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 (w *worker) 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("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 = w.execRetry(ctx, sess, sb.String()) | ||
if err != nil { | ||
logutil.BgLogger().Info("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 { | ||
retention := int(retentionDays.Load()) | ||
if retention == -1 { | ||
panic("Variable " + repositoryRetentionDays + " was not set before repository was started.") | ||
} else 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("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("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 = w.execRetry(ctx, sess, sb.String()) | ||
if err != nil { | ||
logutil.BgLogger().Info("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 := w.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.
Can you add some test cases for it?