14
14
15
15
package stream
16
16
17
- import (
18
- "github.com/pingcap/tidb/pkg/meta/model"
19
- )
20
-
21
17
// TableLocationInfo stores the table name, db id, and parent table id if is a partition
22
18
type TableLocationInfo struct {
23
19
DbID int64
24
20
TableName string
25
21
IsPartition bool
26
- ParentTableID int64 // only meaningful when IsPartition is true
22
+ ParentTableID int64 // only meaningful when IsPartition is true
23
+ Timestamp uint64 // timestamp when this location info was recorded
27
24
}
28
25
29
26
type LogBackupTableHistoryManager struct {
30
27
// maps table/partition ID to [original, current] location info
31
28
tableNameHistory map [int64 ][2 ]TableLocationInfo
32
29
dbIdToName map [int64 ]string
30
+ // maps db ID to timestamp when it was last updated
31
+ dbTimestamps map [int64 ]uint64
33
32
}
34
33
35
34
func NewTableHistoryManager () * LogBackupTableHistoryManager {
36
35
return & LogBackupTableHistoryManager {
37
36
tableNameHistory : make (map [int64 ][2 ]TableLocationInfo ),
38
37
dbIdToName : make (map [int64 ]string ),
38
+ dbTimestamps : make (map [int64 ]uint64 ),
39
39
}
40
40
}
41
41
42
42
// AddTableHistory adds or updates history for a regular table
43
- func (info * LogBackupTableHistoryManager ) AddTableHistory (tableId int64 , tableName string , dbID int64 ) {
43
+ func (info * LogBackupTableHistoryManager ) AddTableHistory (tableId int64 , tableName string , dbID int64 , ts uint64 ) {
44
44
locationInfo := TableLocationInfo {
45
45
DbID : dbID ,
46
46
TableName : tableName ,
47
47
IsPartition : false ,
48
48
ParentTableID : 0 ,
49
+ Timestamp : ts ,
49
50
}
50
51
info .addHistory (tableId , locationInfo )
51
52
}
52
53
53
54
// AddPartitionHistory adds or updates history for a partition
54
55
func (info * LogBackupTableHistoryManager ) AddPartitionHistory (partitionID int64 , tableName string ,
55
- dbID int64 , parentTableID int64 ) {
56
+ dbID int64 , parentTableID int64 , ts uint64 ) {
56
57
locationInfo := TableLocationInfo {
57
58
DbID : dbID ,
58
59
TableName : tableName ,
59
60
IsPartition : true ,
60
61
ParentTableID : parentTableID ,
62
+ Timestamp : ts ,
61
63
}
62
64
info .addHistory (partitionID , locationInfo )
63
65
}
@@ -69,12 +71,20 @@ func (info *LogBackupTableHistoryManager) addHistory(id int64, locationInfo Tabl
69
71
// first occurrence - store as both original and current
70
72
info .tableNameHistory [id ] = [2 ]TableLocationInfo {locationInfo , locationInfo }
71
73
} else {
72
- info .tableNameHistory [id ] = [2 ]TableLocationInfo {existing [0 ], locationInfo }
74
+ // only update if the new timestamp is newer than the current one
75
+ if locationInfo .Timestamp >= existing [1 ].Timestamp {
76
+ info .tableNameHistory [id ] = [2 ]TableLocationInfo {existing [0 ], locationInfo }
77
+ }
78
+ // if timestamp is older, don't update (keep the newer entry)
73
79
}
74
80
}
75
81
76
- func (info * LogBackupTableHistoryManager ) RecordDBIdToName (dbId int64 , dbName string ) {
77
- info .dbIdToName [dbId ] = dbName
82
+ func (info * LogBackupTableHistoryManager ) RecordDBIdToName (dbId int64 , dbName string , ts uint64 ) {
83
+ // only update if the new timestamp is newer than the existing one
84
+ if existingTs , exists := info .dbTimestamps [dbId ]; ! exists || ts >= existingTs {
85
+ info .dbIdToName [dbId ] = dbName
86
+ info .dbTimestamps [dbId ] = ts
87
+ }
78
88
}
79
89
80
90
// GetTableHistory returns information about all tables that have been renamed.
@@ -93,18 +103,17 @@ func (info *LogBackupTableHistoryManager) GetNewlyCreatedDBHistory() map[int64]s
93
103
}
94
104
95
105
// OnDatabaseInfo implements MetaInfoCollector.OnDatabaseInfo
96
- func (info * LogBackupTableHistoryManager ) OnDatabaseInfo (dbInfo * model. DBInfo ) {
97
- info .RecordDBIdToName (dbInfo . ID , dbInfo . Name . O )
106
+ func (info * LogBackupTableHistoryManager ) OnDatabaseInfo (dbId int64 , dbName string , ts uint64 ) {
107
+ info .RecordDBIdToName (dbId , dbName , ts )
98
108
}
99
109
100
110
// OnTableInfo implements MetaInfoCollector.OnTableInfo
101
- func (info * LogBackupTableHistoryManager ) OnTableInfo (dbID int64 , tableInfo * model.TableInfo ) {
102
- info .AddTableHistory (tableInfo .ID , tableInfo .Name .O , dbID )
111
+ func (info * LogBackupTableHistoryManager ) OnTableInfo (
112
+ dbID , tableId int64 , tableSimpleInfo * tableSimpleInfo , commitTs uint64 ) {
113
+ info .AddTableHistory (tableId , tableSimpleInfo .Name , dbID , commitTs )
103
114
104
115
// add history for all partitions if this is a partitioned table
105
- if tableInfo .Partition != nil && tableInfo .Partition .Definitions != nil {
106
- for _ , partition := range tableInfo .Partition .Definitions {
107
- info .AddPartitionHistory (partition .ID , tableInfo .Name .O , dbID , tableInfo .ID )
108
- }
116
+ for _ , partitionId := range tableSimpleInfo .PartitionIds {
117
+ info .AddPartitionHistory (partitionId , tableSimpleInfo .Name , dbID , tableId , commitTs )
109
118
}
110
119
}
0 commit comments