-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathddl_event.go
359 lines (312 loc) · 10.7 KB
/
ddl_event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2025 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package event
import (
"encoding/binary"
"encoding/json"
"github.com/pingcap/log"
"github.com/pingcap/ticdc/pkg/apperror"
"github.com/pingcap/ticdc/pkg/common"
"github.com/pingcap/tidb/pkg/meta/model"
"go.uber.org/zap"
)
const (
DDLEventVersion = 0
)
type DDLEvent struct {
// Version is the version of the DDLEvent struct.
Version byte `json:"version"`
DispatcherID common.DispatcherID `json:"-"`
Type byte `json:"type"`
// SchemaID is from upstream job.SchemaID
SchemaID int64 `json:"schema_id"`
// TableID is from upstream job.TableID
// TableID means different for different job types:
// - for most ddl types which just involve a single table id, it is the table id of the table
// - for ExchangeTablePartition, it is the table id of the normal table before exchange
// and it is one of of the partition ids after exchange
// - for TruncateTable, it the table ID of the old table
TableID int64 `json:"table_id"`
SchemaName string `json:"schema_name"`
TableName string `json:"table_name"`
// the following two fields are just used for RenameTable,
// they are the old schema/table name of the table
ExtraSchemaName string `json:"extra_schema_name"`
ExtraTableName string `json:"extra_table_name"`
Query string `json:"query"`
TableInfo *common.TableInfo `json:"-"`
FinishedTs uint64 `json:"finished_ts"`
// The seq of the event. It is set by event service.
Seq uint64 `json:"seq"`
// State is the state of sender when sending this event.
State EventSenderState `json:"state"`
MultipleTableInfos []*common.TableInfo `json:"-"`
BlockedTables *InfluencedTables `json:"blocked_tables"`
NeedDroppedTables *InfluencedTables `json:"need_dropped_tables"`
NeedAddedTables []Table `json:"need_added_tables"`
// Only set when tables moves between databases
UpdatedSchemas []SchemaIDChange `json:"updated_schemas"`
// DDLs which may change table name:
// Create Table
// Create Tables
// Drop Table
// Rename Table
// Rename Tables
// Drop Schema
// Recover Table
TableNameChange *TableNameChange `json:"table_name_change"`
// the table name for the ddl job in the information_schema.ddl_jobs table(just ddl job.TableName)
TableNameInDDLJob string `msg:"table_name_in_ddl_job"`
// the database name for the ddl job in the information_schema.ddl_jobs table(just ddl job.dbName)
DBNameInDDLJob string `msg:"db_name_in_ddl_job"`
TiDBOnly bool `json:"tidb_only"`
BDRMode string `json:"bdr_mode"`
// Call when event flush is completed
PostTxnFlushed []func() `json:"-"`
// eventSize is the size of the event in bytes. It is set when it's unmarshaled.
eventSize int64 `json:"-"`
Err error `json:"-"`
// for simple protocol
IsBootstrap bool `msg:"-"`
}
func (d *DDLEvent) GetType() int {
return TypeDDLEvent
}
func (d *DDLEvent) GetDispatcherID() common.DispatcherID {
return d.DispatcherID
}
func (d *DDLEvent) GetStartTs() common.Ts {
return 0
}
func (d *DDLEvent) GetError() error {
return d.Err
}
func (d *DDLEvent) GetCommitTs() common.Ts {
return d.FinishedTs
}
func (d *DDLEvent) PostFlush() {
for _, f := range d.PostTxnFlushed {
f()
}
}
func (d *DDLEvent) GetSchemaName() string {
return d.SchemaName
}
func (d *DDLEvent) GetTableName() string {
return d.TableName
}
func (d *DDLEvent) GetExtraSchemaName() string {
return d.ExtraSchemaName
}
func (d *DDLEvent) GetExtraTableName() string {
return d.ExtraTableName
}
func (d *DDLEvent) GetTableNameInDDLJob() string {
return d.TableNameInDDLJob
}
func (d *DDLEvent) GetDBNameInDDLJob() string {
return d.DBNameInDDLJob
}
func (d *DDLEvent) GetEvents() []*DDLEvent {
// Some ddl event may be multi-events, we need to split it into multiple messages.
// Such as rename table test.table1 to test.table10, test.table2 to test.table20
switch model.ActionType(d.Type) {
case model.ActionCreateTables, model.ActionRenameTables:
events := make([]*DDLEvent, 0, len(d.MultipleTableInfos))
queries, err := SplitQueries(d.Query)
if err != nil {
log.Panic("split queries failed", zap.Error(err))
}
if len(queries) != len(d.MultipleTableInfos) {
log.Panic("queries length should be equal to multipleTableInfos length", zap.String("query", d.Query), zap.Any("multipleTableInfos", d.MultipleTableInfos))
}
if len(d.TableNameChange.DropName) != len(d.MultipleTableInfos) {
log.Panic("drop name length should be equal to multipleTableInfos length", zap.Any("query", d.TableNameChange.DropName), zap.Any("multipleTableInfos", d.MultipleTableInfos))
}
for i, info := range d.MultipleTableInfos {
events = append(events, &DDLEvent{
Version: d.Version,
Type: d.Type,
SchemaName: info.GetSchemaName(),
TableName: info.GetTableName(),
ExtraSchemaName: d.TableNameChange.DropName[i].SchemaName,
ExtraTableName: d.TableNameChange.DropName[i].TableName,
TableInfo: info,
Query: queries[i],
FinishedTs: d.FinishedTs,
})
}
return events
default:
}
return []*DDLEvent{d}
}
func (d *DDLEvent) GetSeq() uint64 {
return d.Seq
}
func (d *DDLEvent) ClearPostFlushFunc() {
d.PostTxnFlushed = d.PostTxnFlushed[:0]
}
func (d *DDLEvent) AddPostFlushFunc(f func()) {
d.PostTxnFlushed = append(d.PostTxnFlushed, f)
}
func (d *DDLEvent) PushFrontFlushFunc(f func()) {
d.PostTxnFlushed = append([]func(){f}, d.PostTxnFlushed...)
}
func (e *DDLEvent) GetBlockedTables() *InfluencedTables {
return e.BlockedTables
}
func (e *DDLEvent) GetNeedDroppedTables() *InfluencedTables {
return e.NeedDroppedTables
}
func (e *DDLEvent) GetNeedAddedTables() []Table {
return e.NeedAddedTables
}
func (e *DDLEvent) GetUpdatedSchemas() []SchemaIDChange {
return e.UpdatedSchemas
}
func (e *DDLEvent) GetDDLQuery() string {
if e == nil {
log.Error("DDLEvent is nil, should not happened in production env", zap.Any("event", e))
return ""
}
return e.Query
}
func (e *DDLEvent) GetDDLSchemaName() string {
if e == nil {
return "" // 要报错的
}
return e.SchemaName
}
func (e *DDLEvent) GetDDLType() model.ActionType {
return model.ActionType(e.Type)
}
func (t DDLEvent) Marshal() ([]byte, error) {
// restData | dispatcherIDData | dispatcherIDDataSize | tableInfoData | tableInfoDataSize | multipleTableInfos | multipletableInfosDataSize |errorData | errorDataSize
data, err := json.Marshal(t)
if err != nil {
return nil, err
}
dispatcherIDData := t.DispatcherID.Marshal()
dispatcherIDDataSize := make([]byte, 8)
binary.BigEndian.PutUint64(dispatcherIDDataSize, uint64(len(dispatcherIDData)))
data = append(data, dispatcherIDData...)
data = append(data, dispatcherIDDataSize...)
if t.TableInfo != nil {
tableInfoData, err := t.TableInfo.Marshal()
if err != nil {
return nil, err
}
tableInfoDataSize := make([]byte, 8)
binary.BigEndian.PutUint64(tableInfoDataSize, uint64(len(tableInfoData)))
data = append(data, tableInfoData...)
data = append(data, tableInfoDataSize...)
} else {
tableInfoDataSize := make([]byte, 8)
binary.BigEndian.PutUint64(tableInfoDataSize, 0)
data = append(data, tableInfoDataSize...)
}
for _, info := range t.MultipleTableInfos {
tableInfoData, err := info.Marshal()
if err != nil {
return nil, err
}
tableInfoDataSize := make([]byte, 8)
binary.BigEndian.PutUint64(tableInfoDataSize, uint64(len(tableInfoData)))
data = append(data, tableInfoData...)
data = append(data, tableInfoDataSize...)
}
multipletableInfosDataSize := make([]byte, 8)
binary.BigEndian.PutUint64(multipletableInfosDataSize, uint64(len(t.MultipleTableInfos)))
data = append(data, multipletableInfosDataSize...)
if t.Err != nil {
errData := []byte(t.Err.Error())
errDataSize := make([]byte, 8)
binary.BigEndian.PutUint64(errDataSize, uint64(len(errData)))
data = append(data, errData...)
data = append(data, errDataSize...)
} else {
errDataSize := make([]byte, 8)
binary.BigEndian.PutUint64(errDataSize, 0)
data = append(data, errDataSize...)
}
return data, nil
}
func (t *DDLEvent) Unmarshal(data []byte) error {
// restData | dispatcherIDData | dispatcherIDDataSize | tableInfoData | tableInfoDataSize | multipleTableInfos | multipletableInfosDataSize | errorData | errorDataSize
t.eventSize = int64(len(data))
errorDataSize := binary.BigEndian.Uint64(data[len(data)-8:])
if errorDataSize > 0 {
errorData := data[len(data)-8-int(errorDataSize) : len(data)-8]
log.Info("errorData", zap.String("errorData", string(errorData)))
t.Err = apperror.ErrDDLEventError.FastGen(string(errorData))
}
end := len(data) - 8 - int(errorDataSize)
multipletableInfosDataSize := binary.BigEndian.Uint64(data[end-8 : end])
for i := 0; i < int(multipletableInfosDataSize); i++ {
tableInfoDataSize := binary.BigEndian.Uint64(data[end-8 : end])
tableInfoData := data[end-8-int(tableInfoDataSize) : end-8]
info, err := common.UnmarshalJSONToTableInfo(tableInfoData)
if err != nil {
return err
}
t.MultipleTableInfos = append(t.MultipleTableInfos, info)
end -= 8 + int(tableInfoDataSize)
}
end -= 8 + int(multipletableInfosDataSize)
tableInfoDataSize := binary.BigEndian.Uint64(data[end-8 : end])
var err error
if tableInfoDataSize > 0 {
tableInfoData := data[end-8-int(tableInfoDataSize) : end-8]
t.TableInfo, err = common.UnmarshalJSONToTableInfo(tableInfoData)
if err != nil {
return err
}
}
end -= 8 + int(tableInfoDataSize)
dispatcherIDDatSize := binary.BigEndian.Uint64(data[end-8 : end])
dispatcherIDData := data[end-8-int(dispatcherIDDatSize) : end-8]
err = t.DispatcherID.Unmarshal(dispatcherIDData)
if err != nil {
return err
}
err = json.Unmarshal(data[:end-8-int(dispatcherIDDatSize)], t)
if err != nil {
return err
}
return nil
}
func (t *DDLEvent) GetSize() int64 {
return t.eventSize
}
func (t *DDLEvent) IsPaused() bool {
return t.State.IsPaused()
}
func (t *DDLEvent) Len() int32 {
return 1
}
type SchemaTableName struct {
SchemaName string
TableName string
}
type DB struct {
SchemaID int64
SchemaName string
}
// TableNameChange will record each ddl change of the table name.
// Each TableNameChange is related to a ddl event
type TableNameChange struct {
AddName []SchemaTableName
DropName []SchemaTableName
DropDatabaseName string
}