|
| 1 | +// Copyright 2024 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package workloadrepo |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/pingcap/tidb/pkg/infoschema" |
| 24 | + "github.com/pingcap/tidb/pkg/parser/model" |
| 25 | + "github.com/pingcap/tidb/pkg/sessionctx" |
| 26 | + "github.com/pingcap/tidb/pkg/sessiontxn" |
| 27 | + "github.com/pingcap/tidb/pkg/util/logutil" |
| 28 | + "github.com/pingcap/tidb/pkg/util/sqlescape" |
| 29 | + "go.uber.org/zap" |
| 30 | +) |
| 31 | + |
| 32 | +func calcNextTick(now time.Time) time.Duration { |
| 33 | + // only activated at 2am |
| 34 | + next := time.Date(now.Year(), now.Month(), now.Day(), 2, 0, 0, 0, time.Local) |
| 35 | + if !next.After(now) { |
| 36 | + next = next.AddDate(0, 0, 1) |
| 37 | + } |
| 38 | + return next.Sub(now) |
| 39 | +} |
| 40 | + |
| 41 | +func createAllPartitions(ctx context.Context, sess sessionctx.Context, is infoschema.InfoSchema) error { |
| 42 | + sb := &strings.Builder{} |
| 43 | + for _, tbl := range workloadTables { |
| 44 | + tbSchema, err := is.TableByName(ctx, workloadSchemaCIStr, model.NewCIStr(tbl.destTable)) |
| 45 | + if err != nil { |
| 46 | + logutil.BgLogger().Info("workload repository cannot get table", zap.String("tbl", tbl.destTable), zap.NamedError("err", err)) |
| 47 | + return err |
| 48 | + } |
| 49 | + tbInfo := tbSchema.Meta() |
| 50 | + |
| 51 | + sb.Reset() |
| 52 | + sqlescape.MustFormatSQL(sb, "ALTER TABLE %n.%n ADD PARTITION (", WorkloadSchema, tbl.destTable) |
| 53 | + if !generatePartitionRanges(sb, tbInfo) { |
| 54 | + fmt.Fprintf(sb, ")") |
| 55 | + _, err = execRetry(ctx, sess, sb.String()) |
| 56 | + if err != nil { |
| 57 | + logutil.BgLogger().Info("workload repository cannot add partitions", zap.String("parts", sb.String()), zap.NamedError("err", err)) |
| 58 | + return err |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func (w *worker) dropOldPartitions(ctx context.Context, sess sessionctx.Context, is infoschema.InfoSchema, now time.Time) error { |
| 66 | + w.Lock() |
| 67 | + retention := int(w.retentionDays) |
| 68 | + w.Unlock() |
| 69 | + |
| 70 | + if retention == 0 { |
| 71 | + // disabled housekeeping |
| 72 | + return nil |
| 73 | + } |
| 74 | + |
| 75 | + sb := &strings.Builder{} |
| 76 | + for _, tbl := range workloadTables { |
| 77 | + tbSchema, err := is.TableByName(ctx, workloadSchemaCIStr, model.NewCIStr(tbl.destTable)) |
| 78 | + if err != nil { |
| 79 | + logutil.BgLogger().Info("workload repository cannot get table", zap.String("tbl", tbl.destTable), zap.NamedError("err", err)) |
| 80 | + continue |
| 81 | + } |
| 82 | + tbInfo := tbSchema.Meta() |
| 83 | + for _, pt := range tbInfo.GetPartitionInfo().Definitions { |
| 84 | + ot, err := time.Parse("p20060102", pt.Name.L) |
| 85 | + if err != nil { |
| 86 | + logutil.BgLogger().Info("workload repository cannot parse partition name", zap.String("part", pt.Name.L), zap.NamedError("err", err)) |
| 87 | + break |
| 88 | + } |
| 89 | + if int(now.Sub(ot).Hours()/24) < retention { |
| 90 | + continue |
| 91 | + } |
| 92 | + sb.Reset() |
| 93 | + sqlescape.MustFormatSQL(sb, "ALTER TABLE %s.%s DROP PARTITION %s", |
| 94 | + WorkloadSchema, tbl.destTable, pt.Name.L) |
| 95 | + _, err = execRetry(ctx, sess, sb.String()) |
| 96 | + if err != nil { |
| 97 | + logutil.BgLogger().Info("workload repository cannot drop partition", zap.String("part", pt.Name.L), zap.NamedError("err", err)) |
| 98 | + break |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func (w *worker) startHouseKeeper(ctx context.Context) func() { |
| 106 | + return func() { |
| 107 | + now := time.Now() |
| 108 | + timer := time.NewTimer(calcNextTick(now)) |
| 109 | + defer timer.Stop() |
| 110 | + |
| 111 | + _sessctx := w.getSessionWithRetry() |
| 112 | + defer w.sesspool.Put(_sessctx) |
| 113 | + sess := _sessctx.(sessionctx.Context) |
| 114 | + for { |
| 115 | + select { |
| 116 | + case <-ctx.Done(): |
| 117 | + return |
| 118 | + case now := <-timer.C: |
| 119 | + // Owner only |
| 120 | + if !w.owner.IsOwner() { |
| 121 | + continue |
| 122 | + } |
| 123 | + |
| 124 | + // get the latest infoschema |
| 125 | + is := sessiontxn.GetTxnManager(sess).GetTxnInfoSchema() |
| 126 | + |
| 127 | + // create new partitions |
| 128 | + if err := createAllPartitions(ctx, sess, is); err != nil { |
| 129 | + continue |
| 130 | + } |
| 131 | + |
| 132 | + // drop old partitions |
| 133 | + if err := w.dropOldPartitions(ctx, sess, is, now); err != nil { |
| 134 | + continue |
| 135 | + } |
| 136 | + |
| 137 | + // reschedule, drain channel first |
| 138 | + if !timer.Stop() { |
| 139 | + <-timer.C |
| 140 | + } |
| 141 | + timer.Reset(calcNextTick(now)) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments