@@ -19,22 +19,18 @@ import (
19
19
20
20
"github.com/pingcap/errors"
21
21
"github.com/pingcap/tidb/pkg/ddl/notifier"
22
- "github.com/pingcap/tidb/pkg/meta/model"
23
22
"github.com/pingcap/tidb/pkg/sessionctx"
24
- "github.com/pingcap/tidb/pkg/sessionctx/variable"
25
23
"github.com/pingcap/tidb/pkg/statistics/handle/lockstats"
26
- "github.com/pingcap/tidb/pkg/statistics/handle/logutil"
27
24
"github.com/pingcap/tidb/pkg/statistics/handle/storage"
28
25
"github.com/pingcap/tidb/pkg/statistics/handle/types"
29
26
"github.com/pingcap/tidb/pkg/statistics/handle/util"
30
- "github.com/pingcap/tidb/pkg/util/intest"
31
- "go.uber.org/zap"
32
27
)
33
28
34
29
type ddlHandlerImpl struct {
35
30
ddlEventCh chan * notifier.SchemaChangeEvent
36
31
statsWriter types.StatsReadWriter
37
32
statsHandler types.StatsHandle
33
+ sub * subscriber
38
34
}
39
35
40
36
// NewDDLHandler creates a new ddl handler.
@@ -46,147 +42,13 @@ func NewDDLHandler(
46
42
ddlEventCh : make (chan * notifier.SchemaChangeEvent , 1000 ),
47
43
statsWriter : statsWriter ,
48
44
statsHandler : statsHandler ,
45
+ sub : NewSubscriber (statsHandler ),
49
46
}
50
47
}
51
48
52
49
// HandleDDLEvent begins to process a ddl task.
53
- func (h * ddlHandlerImpl ) HandleDDLEvent (s * notifier.SchemaChangeEvent ) error {
54
- switch s .GetType () {
55
- case model .ActionCreateTable :
56
- newTableInfo := s .GetCreateTableInfo ()
57
- ids , err := h .getTableIDs (newTableInfo )
58
- if err != nil {
59
- return err
60
- }
61
- for _ , id := range ids {
62
- if err := h .statsWriter .InsertTableStats2KV (newTableInfo , id ); err != nil {
63
- return err
64
- }
65
- }
66
- case model .ActionTruncateTable :
67
- newTableInfo , droppedTableInfo := s .GetTruncateTableInfo ()
68
- ids , err := h .getTableIDs (newTableInfo )
69
- if err != nil {
70
- return err
71
- }
72
- for _ , id := range ids {
73
- if err := h .statsWriter .InsertTableStats2KV (newTableInfo , id ); err != nil {
74
- return err
75
- }
76
- }
77
-
78
- // Remove the old table stats.
79
- droppedIDs , err := h .getTableIDs (droppedTableInfo )
80
- if err != nil {
81
- return err
82
- }
83
- for _ , id := range droppedIDs {
84
- if err := h .statsWriter .UpdateStatsMetaVersionForGC (id ); err != nil {
85
- return err
86
- }
87
- }
88
- case model .ActionDropTable :
89
- droppedTableInfo := s .GetDropTableInfo ()
90
- ids , err := h .getTableIDs (droppedTableInfo )
91
- if err != nil {
92
- return err
93
- }
94
- for _ , id := range ids {
95
- if err := h .statsWriter .UpdateStatsMetaVersionForGC (id ); err != nil {
96
- return err
97
- }
98
- }
99
- case model .ActionAddColumn :
100
- newTableInfo , newColumnInfo := s .GetAddColumnInfo ()
101
- ids , err := h .getTableIDs (newTableInfo )
102
- if err != nil {
103
- return err
104
- }
105
- for _ , id := range ids {
106
- if err := h .statsWriter .InsertColStats2KV (id , newColumnInfo ); err != nil {
107
- return err
108
- }
109
- }
110
- case model .ActionModifyColumn :
111
- newTableInfo , modifiedColumnInfo := s .GetModifyColumnInfo ()
112
- ids , err := h .getTableIDs (newTableInfo )
113
- if err != nil {
114
- return err
115
- }
116
- for _ , id := range ids {
117
- if err := h .statsWriter .InsertColStats2KV (id , modifiedColumnInfo ); err != nil {
118
- return err
119
- }
120
- }
121
- case model .ActionAddTablePartition :
122
- globalTableInfo , addedPartitionInfo := s .GetAddPartitionInfo ()
123
- for _ , def := range addedPartitionInfo .Definitions {
124
- if err := h .statsWriter .InsertTableStats2KV (globalTableInfo , def .ID ); err != nil {
125
- return err
126
- }
127
- }
128
- case model .ActionTruncateTablePartition :
129
- if err := h .onTruncatePartitions (s ); err != nil {
130
- return err
131
- }
132
- case model .ActionDropTablePartition :
133
- if err := h .onDropPartitions (s ); err != nil {
134
- return err
135
- }
136
- // EXCHANGE PARTITION EVENT NOTES:
137
- // 1. When a partition is exchanged with a system table, we need to adjust the global statistics
138
- // based on the count delta and modify count delta. However, due to the involvement of the system table,
139
- // a complete update of the global statistics is not feasible. Therefore, we bypass the statistics update
140
- // for the table in this scenario. Despite this, the table id still changes, so the statistics for the
141
- // system table will still be visible.
142
- // 2. If the system table is a partitioned table, we will update the global statistics for the partitioned table.
143
- // It is rare to exchange a partition from a system table, so we can ignore this case. In this case,
144
- // the system table will have statistics, but this is not a significant issue.
145
- // So we decided to completely ignore the system table event.
146
- case model .ActionExchangeTablePartition :
147
- if err := h .onExchangeAPartition (s ); err != nil {
148
- return err
149
- }
150
- case model .ActionReorganizePartition :
151
- if err := h .onReorganizePartitions (s ); err != nil {
152
- return err
153
- }
154
- case model .ActionAlterTablePartitioning :
155
- oldSingleTableID , globalTableInfo , addedPartInfo := s .GetAddPartitioningInfo ()
156
- // Add new partition stats.
157
- for _ , def := range addedPartInfo .Definitions {
158
- if err := h .statsWriter .InsertTableStats2KV (globalTableInfo , def .ID ); err != nil {
159
- return err
160
- }
161
- }
162
- // Change id for global stats, since the data has not changed!
163
- // Note: This operation will update all tables related to statistics with the new ID.
164
- return h .statsWriter .ChangeGlobalStatsID (oldSingleTableID , globalTableInfo .ID )
165
- case model .ActionRemovePartitioning :
166
- // Change id for global stats, since the data has not changed!
167
- // Note: This operation will update all tables related to statistics with the new ID.
168
- oldTblID , newSingleTableInfo , droppedPartInfo := s .GetRemovePartitioningInfo ()
169
- if err := h .statsWriter .ChangeGlobalStatsID (oldTblID , newSingleTableInfo .ID ); err != nil {
170
- return err
171
- }
172
-
173
- // Remove partition stats.
174
- for _ , def := range droppedPartInfo .Definitions {
175
- if err := h .statsWriter .UpdateStatsMetaVersionForGC (def .ID ); err != nil {
176
- return err
177
- }
178
- }
179
- case model .ActionFlashbackCluster :
180
- return h .statsWriter .UpdateStatsVersion ()
181
- case model .ActionAddIndex :
182
- // No need to update the stats meta for the adding index event.
183
- case model .ActionDropSchema :
184
- // TODO: handle the drop schema event.
185
- default :
186
- intest .Assert (false )
187
- logutil .StatsLogger ().Error ("Unhandled schema change event" , zap .Stringer ("type" , s ))
188
- }
189
- return nil
50
+ func (h * ddlHandlerImpl ) HandleDDLEvent (ctx context.Context , sctx sessionctx.Context , s * notifier.SchemaChangeEvent ) error {
51
+ return h .sub .handle (ctx , sctx , s )
190
52
}
191
53
192
54
// UpdateStatsWithCountDeltaAndModifyCountDeltaForTest updates the global stats with the given count delta and modify count delta.
@@ -282,25 +144,6 @@ func updateStatsWithCountDeltaAndModifyCountDelta(
282
144
return err
283
145
}
284
146
285
- func (h * ddlHandlerImpl ) getTableIDs (tblInfo * model.TableInfo ) (ids []int64 , err error ) {
286
- pi := tblInfo .GetPartitionInfo ()
287
- if pi == nil {
288
- return []int64 {tblInfo .ID }, nil
289
- }
290
- ids = make ([]int64 , 0 , len (pi .Definitions )+ 1 )
291
- for _ , def := range pi .Definitions {
292
- ids = append (ids , def .ID )
293
- }
294
- pruneMode , err := util .GetCurrentPruneMode (h .statsHandler .SPool ())
295
- if err != nil {
296
- return nil , err
297
- }
298
- if variable .PartitionPruneMode (pruneMode ) == variable .Dynamic {
299
- ids = append (ids , tblInfo .ID )
300
- }
301
- return ids , nil
302
- }
303
-
304
147
// DDLEventCh returns ddl events channel in handle.
305
148
func (h * ddlHandlerImpl ) DDLEventCh () chan * notifier.SchemaChangeEvent {
306
149
return h .ddlEventCh
0 commit comments