Skip to content

Commit cb3ab8b

Browse files
committed
statistics: refactor common functions into subscriber (pingcap#58127)
ref pingcap#57871
1 parent 3d92909 commit cb3ab8b

File tree

4 files changed

+284
-307
lines changed

4 files changed

+284
-307
lines changed

pkg/statistics/handle/ddl/drop_partition.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,8 @@
1515
package ddl
1616

1717
import (
18-
"context"
19-
20-
"github.com/pingcap/errors"
2118
"github.com/pingcap/tidb/pkg/ddl/notifier"
22-
"github.com/pingcap/tidb/pkg/meta/model"
2319
"github.com/pingcap/tidb/pkg/sessionctx"
24-
"github.com/pingcap/tidb/pkg/sessionctx/variable"
25-
"github.com/pingcap/tidb/pkg/statistics/handle/lockstats"
26-
"github.com/pingcap/tidb/pkg/statistics/handle/storage"
2720
"github.com/pingcap/tidb/pkg/statistics/handle/util"
2821
)
2922

@@ -46,47 +39,3 @@ func (h *ddlHandlerImpl) onDropPartitions(t *notifier.SchemaChangeEvent) error {
4639

4740
return nil
4841
}
49-
50-
func updateGlobalTableStats4DropPartition(
51-
ctx context.Context,
52-
sctx sessionctx.Context,
53-
globalTableInfo *model.TableInfo,
54-
droppedPartitionInfo *model.PartitionInfo,
55-
) error {
56-
count := int64(0)
57-
for _, def := range droppedPartitionInfo.Definitions {
58-
// Get the count and modify count of the partition.
59-
tableCount, _, _, err := storage.StatsMetaCountAndModifyCount(ctx, sctx, def.ID)
60-
if err != nil {
61-
return err
62-
}
63-
count += tableCount
64-
}
65-
if count == 0 {
66-
return nil
67-
}
68-
69-
lockedTables, err := lockstats.QueryLockedTables(ctx, sctx)
70-
if err != nil {
71-
return errors.Trace(err)
72-
}
73-
isLocked := false
74-
if _, ok := lockedTables[globalTableInfo.ID]; ok {
75-
isLocked = true
76-
}
77-
startTS, err := util.GetStartTS(sctx)
78-
if err != nil {
79-
return errors.Trace(err)
80-
}
81-
82-
// Because we drop the partition, we should subtract the count from the global stats.
83-
delta := -count
84-
return errors.Trace(storage.UpdateStatsMeta(
85-
ctx,
86-
sctx,
87-
startTS,
88-
variable.TableDelta{Count: count, Delta: delta},
89-
globalTableInfo.ID,
90-
isLocked,
91-
))
92-
}

pkg/statistics/handle/ddl/exchange_partition.go

Lines changed: 0 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,9 @@
1515
package ddl
1616

1717
import (
18-
"context"
19-
20-
"github.com/pingcap/errors"
2118
"github.com/pingcap/tidb/pkg/ddl/notifier"
22-
"github.com/pingcap/tidb/pkg/infoschema"
23-
"github.com/pingcap/tidb/pkg/meta/model"
2419
"github.com/pingcap/tidb/pkg/sessionctx"
25-
"github.com/pingcap/tidb/pkg/statistics/handle/logutil"
26-
"github.com/pingcap/tidb/pkg/statistics/handle/storage"
2720
"github.com/pingcap/tidb/pkg/statistics/handle/util"
28-
"go.uber.org/zap"
2921
)
3022

3123
func (h *ddlHandlerImpl) onExchangeAPartition(t *notifier.SchemaChangeEvent) error {
@@ -42,126 +34,3 @@ func (h *ddlHandlerImpl) onExchangeAPartition(t *notifier.SchemaChangeEvent) err
4234
)
4335
}, util.FlagWrapTxn)
4436
}
45-
46-
func updateGlobalTableStats4ExchangePartition(
47-
ctx context.Context,
48-
sctx sessionctx.Context,
49-
globalTableInfo *model.TableInfo,
50-
originalPartInfo *model.PartitionInfo,
51-
originalTableInfo *model.TableInfo,
52-
) error {
53-
partCount, partModifyCount, tableCount, tableModifyCount, err := getCountsAndModifyCounts(
54-
ctx,
55-
sctx,
56-
originalPartInfo.Definitions[0].ID,
57-
originalTableInfo.ID,
58-
)
59-
if err != nil {
60-
return errors.Trace(err)
61-
}
62-
63-
// The count of the partition should be added to the table.
64-
// The formula is: total_count = original_table_count - original_partition_count + new_table_count.
65-
// So the delta is : new_table_count - original_partition_count.
66-
countDelta := tableCount - partCount
67-
// Initially, the sum of tableCount and partCount represents
68-
// the operation of deleting the partition and adding the table.
69-
// Therefore, they are considered as modifyCountDelta.
70-
// Next, since the old partition no longer belongs to the table,
71-
// the modify count of the partition should be subtracted.
72-
// The modify count of the table should be added as we are adding the table as a partition.
73-
modifyCountDelta := (tableCount + partCount) - partModifyCount + tableModifyCount
74-
75-
if modifyCountDelta == 0 && countDelta == 0 {
76-
return nil
77-
}
78-
79-
// Update the global stats.
80-
is := sctx.GetDomainInfoSchema().(infoschema.InfoSchema)
81-
globalTableSchema, ok := infoschema.SchemaByTable(is, globalTableInfo)
82-
if !ok {
83-
return errors.Errorf("schema not found for table %s", globalTableInfo.Name.O)
84-
}
85-
if err = updateStatsWithCountDeltaAndModifyCountDelta(
86-
ctx,
87-
sctx,
88-
globalTableInfo.ID, countDelta, modifyCountDelta,
89-
); err != nil {
90-
fields := exchangePartitionLogFields(
91-
globalTableSchema.Name.O,
92-
globalTableInfo,
93-
originalPartInfo.Definitions[0],
94-
originalTableInfo,
95-
countDelta, modifyCountDelta,
96-
partCount,
97-
partModifyCount,
98-
tableCount,
99-
tableModifyCount,
100-
)
101-
fields = append(fields, zap.Error(err))
102-
logutil.StatsLogger().Error(
103-
"Update global stats after exchange partition failed",
104-
fields...,
105-
)
106-
return errors.Trace(err)
107-
}
108-
logutil.StatsLogger().Info(
109-
"Update global stats after exchange partition",
110-
exchangePartitionLogFields(
111-
globalTableSchema.Name.O,
112-
globalTableInfo,
113-
originalPartInfo.Definitions[0],
114-
originalTableInfo,
115-
countDelta, modifyCountDelta,
116-
partCount,
117-
partModifyCount,
118-
tableCount,
119-
tableModifyCount,
120-
)...,
121-
)
122-
return nil
123-
}
124-
125-
func getCountsAndModifyCounts(
126-
ctx context.Context,
127-
sctx sessionctx.Context,
128-
partitionID, tableID int64,
129-
) (partCount, partModifyCount, tableCount, tableModifyCount int64, err error) {
130-
partCount, partModifyCount, _, err = storage.StatsMetaCountAndModifyCount(ctx, sctx, partitionID)
131-
if err != nil {
132-
return
133-
}
134-
135-
tableCount, tableModifyCount, _, err = storage.StatsMetaCountAndModifyCount(ctx, sctx, tableID)
136-
if err != nil {
137-
return
138-
}
139-
140-
return
141-
}
142-
143-
func exchangePartitionLogFields(
144-
globalTableSchemaName string,
145-
globalTableInfo *model.TableInfo,
146-
originalPartDef model.PartitionDefinition,
147-
originalTableInfo *model.TableInfo,
148-
countDelta, modifyCountDelta,
149-
partCount, partModifyCount,
150-
tableCount, tableModifyCount int64,
151-
) []zap.Field {
152-
return []zap.Field{
153-
zap.String("globalTableSchema", globalTableSchemaName),
154-
zap.Int64("globalTableID", globalTableInfo.ID),
155-
zap.String("globalTableName", globalTableInfo.Name.O),
156-
zap.Int64("countDelta", countDelta),
157-
zap.Int64("modifyCountDelta", modifyCountDelta),
158-
zap.Int64("partitionID", originalPartDef.ID),
159-
zap.String("partitionName", originalPartDef.Name.O),
160-
zap.Int64("partitionCount", partCount),
161-
zap.Int64("partitionModifyCount", partModifyCount),
162-
zap.Int64("tableID", originalTableInfo.ID),
163-
zap.String("tableName", originalTableInfo.Name.O),
164-
zap.Int64("tableCount", tableCount),
165-
zap.Int64("tableModifyCount", tableModifyCount),
166-
}
167-
}

0 commit comments

Comments
 (0)