-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpersist_storage.go
1052 lines (957 loc) · 29.8 KB
/
persist_storage.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2024 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 schemastore
import (
"context"
"fmt"
"os"
"sort"
"sync"
"time"
"github.com/cockroachdb/pebble"
"github.com/flowbehappy/tigate/heartbeatpb"
"github.com/flowbehappy/tigate/pkg/common"
"github.com/flowbehappy/tigate/pkg/filter"
"github.com/pingcap/log"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/parser/model"
pd "github.com/tikv/pd/client"
"go.uber.org/zap"
)
// The parent folder to store schema data
const dataDir = "schema_store"
// persistentStorage stores the following kinds of data on disk:
// 1. table info and database info from upstream snapshot
// 2. incremental ddl jobs
// 3. metadata which describes the valid data range on disk
type persistentStorage struct {
pdCli pd.Client
kvStorage kv.Storage
db *pebble.DB
mu sync.RWMutex
// the current gcTs on disk
gcTs uint64
upperBound UpperBoundMeta
upperBoundChanged bool
tableMap map[int64]*BasicTableInfo
// schemaID -> database info
// it contains all databases and deleted databases
// will only be removed when its delete version is smaller than gc ts
databaseMap map[int64]*BasicDatabaseInfo
// table id -> a sorted list of finished ts for the table's ddl events
tablesDDLHistory map[int64][]uint64
// it has two use cases:
// 1. store the ddl events need to send to a table dispatcher
// Note: some ddl events in the history may never be send,
// for example the create table ddl, truncate table ddl(usually the first event)
// 2. build table info store for a table
tableTriggerDDLHistory []uint64
// tableID -> versioned store
// it just contains tables which is used by dispatchers
tableInfoStoreMap map[int64]*versionedTableInfoStore
// tableID -> total registered count
tableRegisteredCount map[int64]int
}
func newPersistentStorage(
ctx context.Context,
root string,
pdCli pd.Client,
storage kv.Storage,
) *persistentStorage {
gcSafePoint, err := pdCli.UpdateServiceGCSafePoint(ctx, "cdc-new-store", 0, 0)
if err != nil {
log.Panic("get ts failed", zap.Error(err))
}
dbPath := fmt.Sprintf("%s/%s", root, dataDir)
// FIXME: avoid remove
if err := os.RemoveAll(dbPath); err != nil {
log.Panic("fail to remove path")
}
// TODO: update pebble options
db, err := pebble.Open(dbPath, &pebble.Options{
DisableWAL: true,
})
if err != nil {
log.Fatal("open db failed", zap.Error(err))
}
// check whether the data on disk is reusable
isDataReusable := true
gcTs, err := readGcTs(db)
// TODO: distiguish non-exist key with other io errors
if err != nil {
isDataReusable = false
}
if gcSafePoint < gcTs {
log.Panic("gc safe point should never go back")
}
upperBound, err := readUpperBoundMeta(db)
if err != nil {
isDataReusable = false
}
if gcSafePoint >= upperBound.ResolvedTs {
isDataReusable = false
}
// initialize persistent storage
dataStorage := &persistentStorage{
pdCli: pdCli,
kvStorage: storage,
db: db,
gcTs: gcTs,
upperBound: upperBound,
tableMap: make(map[int64]*BasicTableInfo),
databaseMap: make(map[int64]*BasicDatabaseInfo),
tablesDDLHistory: make(map[int64][]uint64),
tableTriggerDDLHistory: make([]uint64, 0),
tableInfoStoreMap: make(map[int64]*versionedTableInfoStore),
tableRegisteredCount: make(map[int64]int),
}
if isDataReusable {
dataStorage.initializeFromDisk()
} else {
db.Close()
dataStorage.db = nil
dataStorage.initializeFromKVStorage(dbPath, storage, gcSafePoint)
}
go func() {
dataStorage.gc(ctx)
}()
go func() {
dataStorage.persistUpperBoundPeriodically(ctx)
}()
return dataStorage
}
func (p *persistentStorage) initializeFromKVStorage(dbPath string, storage kv.Storage, gcTs uint64) {
// TODO: avoid recreate db if the path is empty at start
if err := os.RemoveAll(dbPath); err != nil {
log.Panic("fail to remove path")
}
var err error
// TODO: update pebble options
if p.db, err = pebble.Open(dbPath, &pebble.Options{
DisableWAL: true,
}); err != nil {
log.Fatal("open db failed", zap.Error(err))
}
log.Info("schema store initialize from kv storage begin",
zap.Uint64("snapTs", gcTs))
if p.databaseMap, p.tableMap, err = writeSchemaSnapshotAndMeta(p.db, storage, gcTs, false); err != nil {
// TODO: retry
log.Fatal("fail to initialize from kv snapshot")
}
p.gcTs = gcTs
p.upperBound = UpperBoundMeta{
FinishedDDLTs: 0,
SchemaVersion: 0,
ResolvedTs: gcTs,
}
writeUpperBoundMeta(p.db, p.upperBound)
log.Info("schema store initialize from kv storage done",
zap.Int("databaseMapLen", len(p.databaseMap)),
zap.Int("tableMapLen", len(p.tableMap)))
}
func (p *persistentStorage) initializeFromDisk() {
cleanObseleteData(p.db, 0, p.gcTs)
storageSnap := p.db.NewSnapshot()
defer storageSnap.Close()
var err error
if p.databaseMap, err = loadDatabasesInKVSnap(storageSnap, p.gcTs); err != nil {
log.Fatal("load database info from disk failed")
}
if p.tableMap, err = loadTablesInKVSnap(storageSnap, p.gcTs, p.databaseMap); err != nil {
log.Fatal("load tables in kv snapshot failed")
}
if p.tablesDDLHistory, p.tableTriggerDDLHistory, err = loadAndApplyDDLHistory(
storageSnap,
p.gcTs,
p.upperBound.FinishedDDLTs,
p.databaseMap,
p.tableMap); err != nil {
log.Fatal("fail to initialize from disk")
}
}
// getAllPhysicalTables returns all physical tables in the snapshot
// caller must ensure current resolve ts is larger than snapTs
func (p *persistentStorage) getAllPhysicalTables(snapTs uint64, tableFilter filter.Filter) ([]common.Table, error) {
storageSnap := p.db.NewSnapshot()
defer storageSnap.Close()
p.mu.Lock()
if snapTs < p.gcTs {
return nil, fmt.Errorf("snapTs %d is smaller than gcTs %d", snapTs, p.gcTs)
}
gcTs := p.gcTs
p.mu.Unlock()
start := time.Now()
defer func() {
log.Info("getAllPhysicalTables finish",
zap.Uint64("snapTs", snapTs),
zap.Any("duration", time.Since(start).Seconds()))
}()
return loadAllPhysicalTablesAtTs(storageSnap, gcTs, snapTs, tableFilter)
}
// only return when table info is initialized
func (p *persistentStorage) registerTable(tableID int64, startTs uint64) error {
p.mu.Lock()
if startTs < p.gcTs {
p.mu.Unlock()
return fmt.Errorf("startTs %d is smaller than gcTs %d", startTs, p.gcTs)
}
p.tableRegisteredCount[tableID] += 1
store, ok := p.tableInfoStoreMap[tableID]
if !ok {
store = newEmptyVersionedTableInfoStore(tableID)
p.tableInfoStoreMap[tableID] = store
}
p.mu.Unlock()
if !ok {
return p.buildVersionedTableInfoStore(store)
}
store.waitTableInfoInitialized()
// Note: no need to check startTs < gcTs here again because if it is true, getTableInfo will failed later.
return nil
}
func (p *persistentStorage) unregisterTable(tableID int64) error {
p.mu.Lock()
defer p.mu.Unlock()
p.tableRegisteredCount[tableID] -= 1
if p.tableRegisteredCount[tableID] <= 0 {
if _, ok := p.tableInfoStoreMap[tableID]; !ok {
return fmt.Errorf(fmt.Sprintf("table %d not found", tableID))
}
delete(p.tableInfoStoreMap, tableID)
log.Info("unregister table",
zap.Int64("tableID", tableID))
}
return nil
}
func (p *persistentStorage) getTableInfo(tableID int64, ts uint64) (*common.TableInfo, error) {
p.mu.Lock()
store, ok := p.tableInfoStoreMap[tableID]
if !ok {
return nil, fmt.Errorf(fmt.Sprintf("table %d not found", tableID))
}
p.mu.Unlock()
return store.getTableInfo(ts)
}
// TODO: not all ddl in p.tablesDDLHistory should be sent to the dispatcher, verify dispatcher will set the right range
func (p *persistentStorage) fetchTableDDLEvents(tableID int64, tableFilter filter.Filter, start, end uint64) ([]common.DDLEvent, error) {
// TODO: check a dispatcher from created table start ts > finish ts of create table
// TODO: check a dispatcher from rename table start ts > finish ts of rename table(is it possible?)
p.mu.RLock()
if start < p.gcTs {
p.mu.Unlock()
return nil, fmt.Errorf("startTs %d is smaller than gcTs %d", start, p.gcTs)
}
// fast check
if len(p.tablesDDLHistory[tableID]) == 0 || start >= p.tablesDDLHistory[tableID][len(p.tablesDDLHistory[tableID])-1] {
p.mu.RUnlock()
return nil, nil
}
index := sort.Search(len(p.tablesDDLHistory[tableID]), func(i int) bool {
return p.tablesDDLHistory[tableID][i] > start
})
if index == len(p.tablesDDLHistory[tableID]) {
log.Panic("should not happen")
}
// copy all target ts to a new slice
allTargetTs := make([]uint64, 0)
for i := index; i < len(p.tablesDDLHistory[tableID]); i++ {
if p.tablesDDLHistory[tableID][i] <= end {
allTargetTs = append(allTargetTs, p.tablesDDLHistory[tableID][i])
}
}
storageSnap := p.db.NewSnapshot()
defer storageSnap.Close()
p.mu.RUnlock()
// TODO: if the first event is a create table ddl, return error?
events := make([]common.DDLEvent, 0, len(allTargetTs))
for _, ts := range allTargetTs {
rawEvent := readPersistedDDLEvent(storageSnap, ts)
if tableFilter != nil &&
tableFilter.ShouldDiscardDDL(model.ActionType(rawEvent.Type), rawEvent.SchemaName, rawEvent.TableName) &&
tableFilter.ShouldDiscardDDL(model.ActionType(rawEvent.Type), rawEvent.PrevSchemaName, rawEvent.PrevTableName) {
continue
}
events = append(events, buildDDLEvent(&rawEvent, tableFilter))
}
// log.Info("fetchTableDDLEvents",
// zap.Int64("tableID", tableID),
// zap.Uint64("start", start),
// zap.Uint64("end", end),
// zap.Any("history", history),
// zap.Any("allTargetTs", allTargetTs))
return events, nil
}
func (p *persistentStorage) fetchTableTriggerDDLEvents(tableFilter filter.Filter, start uint64, limit int) ([]common.DDLEvent, error) {
// get storage snap before check start < gcTs
storageSnap := p.db.NewSnapshot()
defer storageSnap.Close()
p.mu.Lock()
if start < p.gcTs {
p.mu.Unlock()
return nil, fmt.Errorf("startTs %d is smaller than gcTs %d", start, p.gcTs)
}
p.mu.Unlock()
// fast check
if len(p.tableTriggerDDLHistory) == 0 || start >= p.tableTriggerDDLHistory[len(p.tableTriggerDDLHistory)-1] {
return nil, nil
}
events := make([]common.DDLEvent, 0)
nextStartTs := start
for {
allTargetTs := make([]uint64, 0, limit)
p.mu.RLock()
// log.Info("fetchTableTriggerDDLEvents",
// zap.Any("start", start),
// zap.Int("limit", limit),
// zap.Any("tableTriggerDDLHistory", p.tableTriggerDDLHistory))
index := sort.Search(len(p.tableTriggerDDLHistory), func(i int) bool {
return p.tableTriggerDDLHistory[i] > nextStartTs
})
// no more events to read
if index == len(p.tableTriggerDDLHistory) {
p.mu.RUnlock()
return events, nil
}
for i := index; i < len(p.tableTriggerDDLHistory); i++ {
allTargetTs = append(allTargetTs, p.tableTriggerDDLHistory[i])
if len(allTargetTs) >= limit-len(events) {
break
}
}
p.mu.RUnlock()
if len(allTargetTs) == 0 {
return events, nil
}
for _, ts := range allTargetTs {
rawEvent := readPersistedDDLEvent(storageSnap, ts)
if tableFilter != nil &&
tableFilter.ShouldDiscardDDL(model.ActionType(rawEvent.Type), rawEvent.SchemaName, rawEvent.TableName) &&
tableFilter.ShouldDiscardDDL(model.ActionType(rawEvent.Type), rawEvent.PrevSchemaName, rawEvent.PrevTableName) {
continue
}
events = append(events, buildDDLEvent(&rawEvent, tableFilter))
}
if len(events) >= limit {
return events, nil
}
nextStartTs = allTargetTs[len(allTargetTs)-1]
}
}
func (p *persistentStorage) buildVersionedTableInfoStore(
store *versionedTableInfoStore,
) error {
tableID := store.getTableID()
// get snapshot from disk before get current gc ts to make sure data is not deleted by gc process
storageSnap := p.db.NewSnapshot()
defer storageSnap.Close()
p.mu.RLock()
kvSnapVersion := p.gcTs
tableBasicInfo, ok := p.tableMap[tableID]
if !ok {
log.Panic("table not found", zap.Int64("tableID", tableID))
}
inKVSnap := tableBasicInfo.InKVSnap
var allDDLFinishedTs []uint64
allDDLFinishedTs = append(allDDLFinishedTs, p.tablesDDLHistory[tableID]...)
p.mu.RUnlock()
if inKVSnap {
if err := addTableInfoFromKVSnap(store, kvSnapVersion, storageSnap); err != nil {
return err
}
}
for _, version := range allDDLFinishedTs {
ddlEvent := readPersistedDDLEvent(storageSnap, version)
store.applyDDLFromPersistStorage(ddlEvent)
}
store.setTableInfoInitialized()
return nil
}
func addTableInfoFromKVSnap(
store *versionedTableInfoStore,
kvSnapVersion uint64,
snap *pebble.Snapshot,
) error {
tableInfo := readTableInfoInKVSnap(snap, store.getTableID(), kvSnapVersion)
tableInfo.InitPreSQLs()
store.addInitialTableInfo(tableInfo)
return nil
}
func (p *persistentStorage) gc(ctx context.Context) error {
ticker := time.NewTicker(5 * time.Minute)
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
gcSafePoint, err := p.pdCli.UpdateServiceGCSafePoint(ctx, "cdc-new-store", 0, 0)
if err != nil {
log.Warn("get ts failed", zap.Error(err))
continue
}
p.doGc(gcSafePoint)
}
}
}
func (p *persistentStorage) doGc(gcTs uint64) error {
p.mu.Lock()
if gcTs > p.upperBound.ResolvedTs {
log.Panic("gc safe point is larger than resolvedTs",
zap.Uint64("gcTs", gcTs),
zap.Uint64("resolvedTs", p.upperBound.ResolvedTs))
}
if gcTs <= p.gcTs {
p.mu.Unlock()
return nil
}
oldGcTs := p.gcTs
p.mu.Unlock()
start := time.Now()
_, tablesInKVSnap, err := writeSchemaSnapshotAndMeta(p.db, p.kvStorage, gcTs, true)
if err != nil {
log.Warn("fail to write kv snapshot during gc",
zap.Uint64("gcTs", gcTs))
// TODO: return err and retry?
return nil
}
log.Info("persist storage: gc finish write schema snapshot",
zap.Uint64("gcTs", gcTs),
zap.Any("duration", time.Since(start).Seconds()))
// clean data in memeory before clean data on disk
p.cleanObseleteDataInMemory(gcTs, tablesInKVSnap)
log.Info("persist storage: gc finish clean in memory data",
zap.Uint64("gcTs", gcTs),
zap.Any("duration", time.Since(start).Seconds()))
cleanObseleteData(p.db, oldGcTs, gcTs)
log.Info("persist storage: gc finish",
zap.Uint64("gcTs", gcTs),
zap.Any("duration", time.Since(start).Seconds()))
return nil
}
func (p *persistentStorage) cleanObseleteDataInMemory(gcTs uint64, tablesInKVSnap map[int64]*BasicTableInfo) {
p.mu.Lock()
defer p.mu.Unlock()
p.gcTs = gcTs
for tableID := range tablesInKVSnap {
p.tableMap[tableID].InKVSnap = true
}
// clean tablesDDLHistory
for tableID := range p.tablesDDLHistory {
if _, ok := tablesInKVSnap[tableID]; !ok {
delete(p.tablesDDLHistory, tableID)
continue
}
i := sort.Search(len(p.tablesDDLHistory[tableID]), func(i int) bool {
return p.tablesDDLHistory[tableID][i] > gcTs
})
if i == len(p.tablesDDLHistory[tableID]) {
delete(p.tablesDDLHistory, tableID)
continue
}
p.tablesDDLHistory[tableID] = p.tablesDDLHistory[tableID][i:]
}
// clean tableTriggerDDLHistory
i := sort.Search(len(p.tableTriggerDDLHistory), func(i int) bool {
return p.tableTriggerDDLHistory[i] > gcTs
})
p.tableTriggerDDLHistory = p.tableTriggerDDLHistory[i:]
// clean tableInfoStoreMap
for tableID, store := range p.tableInfoStoreMap {
if _, ok := tablesInKVSnap[tableID]; !ok {
delete(p.tableInfoStoreMap, tableID)
continue
}
store.gc(gcTs)
}
}
func (p *persistentStorage) updateUpperBound(upperBound UpperBoundMeta) {
p.mu.Lock()
defer p.mu.Unlock()
p.upperBound = upperBound
p.upperBoundChanged = true
}
func (p *persistentStorage) getUpperBound() UpperBoundMeta {
p.mu.RLock()
defer p.mu.RUnlock()
return p.upperBound
}
func (p *persistentStorage) persistUpperBoundPeriodically(ctx context.Context) error {
ticker := time.NewTicker(5 * time.Second)
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
p.mu.Lock()
if !p.upperBoundChanged {
log.Warn("schema store upper bound not changed")
p.mu.Unlock()
continue
}
upperBound := p.upperBound
p.upperBoundChanged = false
p.mu.Unlock()
writeUpperBoundMeta(p.db, upperBound)
}
}
}
func (p *persistentStorage) handleSortedDDLEvents(ddlEvents ...PersistedDDLEvent) error {
// TODO: ignore some ddl event
// TODO: check ddl events are sorted
for i := range ddlEvents {
p.mu.Lock()
if shouldSkipDDL(&ddlEvents[i], p.databaseMap, p.tableMap) {
p.mu.Unlock()
continue
}
completePersistedDDLEvent(&ddlEvents[i], p.databaseMap, p.tableMap)
p.mu.Unlock()
writePersistedDDLEvent(p.db, &ddlEvents[i])
p.mu.Lock()
var err error
if p.tableTriggerDDLHistory, err = updateDDLHistory(
&ddlEvents[i],
p.databaseMap,
p.tableMap,
p.tablesDDLHistory,
p.tableTriggerDDLHistory); err != nil {
p.mu.Unlock()
return err
}
if err := updateDatabaseInfoAndTableInfo(&ddlEvents[i], p.databaseMap, p.tableMap); err != nil {
p.mu.Unlock()
return err
}
if err := updateRegisteredTableInfoStore(ddlEvents[i], p.tableInfoStoreMap); err != nil {
p.mu.Unlock()
return err
}
p.mu.Unlock()
}
return nil
}
func completePersistedDDLEvent(
event *PersistedDDLEvent,
databaseMap map[int64]*BasicDatabaseInfo,
tableMap map[int64]*BasicTableInfo,
) {
getSchemaName := func(schemaID int64) string {
databaseInfo, ok := databaseMap[schemaID]
if !ok {
log.Panic("database not found",
zap.Int64("schemaID", schemaID))
}
return databaseInfo.Name
}
getTableName := func(tableID int64) string {
tableInfo, ok := tableMap[tableID]
if !ok {
log.Panic("table not found",
zap.Int64("tableID", tableID))
}
return tableInfo.Name
}
getSchemaID := func(tableID int64) int64 {
tableInfo, ok := tableMap[tableID]
if !ok {
log.Panic("table not found",
zap.Int64("tableID", tableID))
}
return tableInfo.SchemaID
}
switch model.ActionType(event.Type) {
case model.ActionCreateSchema,
model.ActionDropSchema:
event.SchemaName = event.DBInfo.Name.O
case model.ActionCreateTable:
event.SchemaName = getSchemaName(event.SchemaID)
event.TableName = event.TableInfo.Name.O
case model.ActionDropTable,
model.ActionAddColumn,
model.ActionDropColumn,
model.ActionAddIndex,
model.ActionDropIndex,
model.ActionAddForeignKey,
model.ActionDropForeignKey,
model.ActionModifyColumn,
model.ActionRebaseAutoID,
model.ActionSetDefaultValue,
model.ActionShardRowID,
model.ActionModifyTableComment,
model.ActionRenameIndex:
event.SchemaName = getSchemaName(event.SchemaID)
event.TableName = getTableName(event.TableID)
case model.ActionTruncateTable:
event.SchemaName = getSchemaName(event.SchemaID)
event.TableName = getTableName(event.TableID)
// TODO: different with tidb, will it be confusing?
event.PrevTableID = event.TableID
event.TableID = event.TableInfo.ID
case model.ActionRenameTable:
event.PrevSchemaID = getSchemaID(event.TableID)
event.PrevSchemaName = getSchemaName(event.PrevSchemaID)
event.PrevTableName = getTableName(event.TableID)
// TODO: is the following SchemaName and TableName correct?
event.SchemaName = getSchemaName(event.SchemaID)
event.TableName = event.TableInfo.Name.O
case model.ActionCreateView, model.ActionCreateTables:
// ignore
default:
log.Panic("unknown ddl type",
zap.Any("ddlType", event.Type),
zap.String("DDL", event.Query))
}
}
// TODO: add some comment to explain why we should skip some ddl
func shouldSkipDDL(
event *PersistedDDLEvent,
databaseMap map[int64]*BasicDatabaseInfo,
tableMap map[int64]*BasicTableInfo,
) bool {
switch model.ActionType(event.Type) {
case model.ActionCreateSchema:
if _, ok := databaseMap[event.SchemaID]; ok {
log.Warn("database already exists. ignore DDL ",
zap.String("DDL", event.Query),
zap.Int64("jobID", event.ID),
zap.Int64("schemaID", event.SchemaID),
zap.Uint64("finishTs", event.FinishedTs),
zap.Int64("jobSchemaVersion", event.SchemaVersion))
return true
}
case model.ActionCreateTable:
if _, ok := tableMap[event.TableID]; ok {
log.Warn("table already exists. ignore DDL ",
zap.String("DDL", event.Query),
zap.Int64("jobID", event.ID),
zap.Int64("schemaID", event.SchemaID),
zap.Int64("tableID", event.TableID),
zap.Uint64("finishTs", event.FinishedTs),
zap.Int64("jobSchemaVersion", event.SchemaVersion))
return true
}
}
return false
}
func updateDDLHistory(
ddlEvent *PersistedDDLEvent,
databaseMap map[int64]*BasicDatabaseInfo,
tableMap map[int64]*BasicTableInfo,
tablesDDLHistory map[int64][]uint64,
tableTriggerDDLHistory []uint64,
) ([]uint64, error) {
addTableHistory := func(tableID int64) {
tablesDDLHistory[tableID] = append(tablesDDLHistory[tableID], ddlEvent.FinishedTs)
}
switch model.ActionType(ddlEvent.Type) {
case model.ActionCreateSchema,
model.ActionCreateView:
tableTriggerDDLHistory = append(tableTriggerDDLHistory, ddlEvent.FinishedTs)
for tableID := range tableMap {
addTableHistory(tableID)
}
case model.ActionDropSchema:
tableTriggerDDLHistory = append(tableTriggerDDLHistory, ddlEvent.FinishedTs)
for tableID := range databaseMap[ddlEvent.SchemaID].Tables {
addTableHistory(tableID)
}
case model.ActionCreateTable,
model.ActionDropTable:
tableTriggerDDLHistory = append(tableTriggerDDLHistory, ddlEvent.FinishedTs)
addTableHistory(ddlEvent.TableID)
case model.ActionAddColumn,
model.ActionDropColumn,
model.ActionAddIndex,
model.ActionDropIndex,
model.ActionAddForeignKey,
model.ActionDropForeignKey,
model.ActionModifyColumn,
model.ActionRebaseAutoID,
model.ActionSetDefaultValue,
model.ActionShardRowID,
model.ActionModifyTableComment,
model.ActionRenameIndex:
addTableHistory(ddlEvent.TableID)
case model.ActionTruncateTable:
addTableHistory(ddlEvent.TableID)
addTableHistory(ddlEvent.PrevTableID)
case model.ActionRenameTable:
tableTriggerDDLHistory = append(tableTriggerDDLHistory, ddlEvent.FinishedTs)
addTableHistory(ddlEvent.TableID)
default:
log.Panic("unknown ddl type",
zap.Any("ddlType", ddlEvent.Type),
zap.String("DDL", ddlEvent.Query))
}
return tableTriggerDDLHistory, nil
}
func updateDatabaseInfoAndTableInfo(
event *PersistedDDLEvent,
databaseMap map[int64]*BasicDatabaseInfo,
tableMap map[int64]*BasicTableInfo,
) error {
addTableToDB := func(schemaID int64, tableID int64) {
databaseInfo, ok := databaseMap[schemaID]
if !ok {
log.Panic("database not found.",
zap.String("DDL", event.Query),
zap.Int64("jobID", event.ID),
zap.Int64("schemaID", schemaID),
zap.Int64("tableID", tableID),
zap.Uint64("finishTs", event.FinishedTs),
zap.Int64("jobSchemaVersion", event.SchemaVersion))
}
databaseInfo.Tables[tableID] = true
}
removeTableFromDB := func(schemaID int64, tableID int64) {
databaseInfo, ok := databaseMap[schemaID]
if !ok {
log.Panic("database not found. ",
zap.String("DDL", event.Query),
zap.Int64("jobID", event.ID),
zap.Int64("schemaID", schemaID),
zap.Int64("tableID", tableID),
zap.Uint64("finishTs", event.FinishedTs),
zap.Int64("jobSchemaVersion", event.SchemaVersion))
}
delete(databaseInfo.Tables, tableID)
}
createTable := func(schemaID int64, tableID int64) {
addTableToDB(schemaID, tableID)
tableMap[tableID] = &BasicTableInfo{
SchemaID: schemaID,
Name: event.TableInfo.Name.O,
InKVSnap: false,
}
}
dropTable := func(schemaID int64, tableID int64) {
removeTableFromDB(schemaID, tableID)
delete(tableMap, tableID)
}
switch model.ActionType(event.Type) {
case model.ActionCreateSchema:
databaseMap[event.SchemaID] = &BasicDatabaseInfo{
Name: event.SchemaName,
Tables: make(map[int64]bool),
}
case model.ActionDropSchema:
for tableID := range databaseMap[event.SchemaID].Tables {
dropTable(event.SchemaID, tableID)
}
delete(databaseMap, event.SchemaID)
case model.ActionCreateTable:
createTable(event.SchemaID, event.TableID)
case model.ActionDropTable:
dropTable(event.SchemaID, event.TableID)
case model.ActionAddColumn,
model.ActionDropColumn,
model.ActionAddIndex,
model.ActionDropIndex,
model.ActionAddForeignKey,
model.ActionDropForeignKey,
model.ActionModifyColumn,
model.ActionRebaseAutoID:
// ignore
case model.ActionTruncateTable:
dropTable(event.SchemaID, event.PrevTableID)
createTable(event.SchemaID, event.TableID)
case model.ActionRenameTable:
oldSchemaID := tableMap[event.TableID].SchemaID
if oldSchemaID != event.SchemaID {
tableMap[event.TableID].SchemaID = event.SchemaID
removeTableFromDB(oldSchemaID, event.TableID)
addTableToDB(event.SchemaID, event.TableID)
}
tableMap[event.TableID].Name = event.TableInfo.Name.O
case model.ActionSetDefaultValue,
model.ActionShardRowID,
model.ActionModifyTableComment,
model.ActionRenameIndex,
model.ActionCreateView:
// TODO
// seems can be ignored
case model.ActionAddTablePartition:
// TODO
default:
log.Panic("unknown ddl type",
zap.Any("ddlType", event.Type),
zap.String("DDL", event.Query))
}
return nil
}
func updateRegisteredTableInfoStore(
event PersistedDDLEvent,
tableInfoStoreMap map[int64]*versionedTableInfoStore,
) error {
switch model.ActionType(event.Type) {
case model.ActionCreateSchema,
model.ActionDropSchema,
model.ActionCreateTable,
model.ActionAddIndex,
model.ActionDropIndex,
model.ActionAddForeignKey,
model.ActionDropForeignKey,
model.ActionRenameTable,
model.ActionCreateView:
// ignore
case model.ActionDropTable,
model.ActionAddColumn,
model.ActionDropColumn,
model.ActionTruncateTable,
model.ActionModifyColumn,
model.ActionRebaseAutoID,
model.ActionSetDefaultValue,
model.ActionShardRowID,
model.ActionModifyTableComment,
model.ActionRenameIndex:
store, ok := tableInfoStoreMap[event.TableID]
if ok {
store.applyDDL(event)
}
default:
log.Panic("unknown ddl type",
zap.Any("ddlType", event.Type),
zap.String("DDL", event.Query))
}
return nil
}
func buildDDLEvent(rawEvent *PersistedDDLEvent, tableFilter filter.Filter) common.DDLEvent {
ddlEvent := common.DDLEvent{
Type: rawEvent.Type,
SchemaID: rawEvent.SchemaID,
TableID: rawEvent.TableID,
SchemaName: rawEvent.SchemaName,
TableName: rawEvent.TableName,
Query: rawEvent.Query,
TableInfo: rawEvent.TableInfo,
FinishedTs: rawEvent.FinishedTs,
TiDBOnly: false,
}
// TODO: remove schema id when influcence type is normal
// TODO: respect filter for create table / drop table and more ddls
switch model.ActionType(rawEvent.Type) {
case model.ActionCreateSchema,
model.ActionAddColumn,
model.ActionDropColumn,
model.ActionAddIndex,
model.ActionDropIndex,
model.ActionAddForeignKey,
model.ActionDropForeignKey,
model.ActionModifyColumn,
model.ActionRebaseAutoID,
model.ActionSetDefaultValue,
model.ActionShardRowID,
model.ActionModifyTableComment,
model.ActionRenameIndex:
// ignore
case model.ActionDropSchema:
ddlEvent.NeedDroppedTables = &common.InfluencedTables{
InfluenceType: common.InfluenceTypeDB,
SchemaID: rawEvent.SchemaID,
}
ddlEvent.TableNameChange = &common.TableNameChange{
DropDatabaseName: rawEvent.SchemaName,
}
case model.ActionCreateTable:
// TODO: support create partition table
ddlEvent.NeedAddedTables = []common.Table{
{
SchemaID: rawEvent.SchemaID,
TableID: rawEvent.TableID,
},
}
ddlEvent.TableNameChange = &common.TableNameChange{
AddName: []common.SchemaTableName{
{
SchemaName: rawEvent.SchemaName,
TableName: rawEvent.TableName,
},
},
}
case model.ActionDropTable:
ddlEvent.BlockedTables = &common.InfluencedTables{
InfluenceType: common.InfluenceTypeNormal,
TableIDs: []int64{rawEvent.TableID, heartbeatpb.DDLSpan.TableID},
SchemaID: rawEvent.SchemaID,
}
ddlEvent.NeedDroppedTables = &common.InfluencedTables{
InfluenceType: common.InfluenceTypeNormal,
TableIDs: []int64{rawEvent.TableID},
SchemaID: rawEvent.SchemaID,
}
ddlEvent.TableNameChange = &common.TableNameChange{
DropName: []common.SchemaTableName{
{
SchemaName: rawEvent.SchemaName,
TableName: rawEvent.TableName,
},
},
}
case model.ActionTruncateTable:
ddlEvent.NeedDroppedTables = &common.InfluencedTables{
InfluenceType: common.InfluenceTypeNormal,
TableIDs: []int64{rawEvent.PrevTableID},
SchemaID: rawEvent.SchemaID,
}
ddlEvent.NeedAddedTables = []common.Table{
{