15
15
package ddl
16
16
17
17
import (
18
- "context"
19
-
20
- "github.com/pingcap/errors"
21
18
"github.com/pingcap/tidb/pkg/ddl/notifier"
22
- "github.com/pingcap/tidb/pkg/infoschema"
23
- "github.com/pingcap/tidb/pkg/meta/model"
24
19
"github.com/pingcap/tidb/pkg/sessionctx"
25
- "github.com/pingcap/tidb/pkg/statistics/handle/logutil"
26
- "github.com/pingcap/tidb/pkg/statistics/handle/storage"
27
20
"github.com/pingcap/tidb/pkg/statistics/handle/util"
28
- "go.uber.org/zap"
29
21
)
30
22
31
23
func (h * ddlHandlerImpl ) onExchangeAPartition (t * notifier.SchemaChangeEvent ) error {
@@ -42,126 +34,3 @@ func (h *ddlHandlerImpl) onExchangeAPartition(t *notifier.SchemaChangeEvent) err
42
34
)
43
35
}, util .FlagWrapTxn )
44
36
}
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