@@ -1985,6 +1985,7 @@ func (cf *changeFrontier) managePerTableProtectedTimestamps(
1985
1985
1986
1986
pts := cf .FlowCtx .Cfg .ProtectedTimestampProvider .WithTxn (txn )
1987
1987
tableIDsToRelease := make ([]descpb.ID , 0 )
1988
+ tableIDsToCreate := make (map [descpb.ID ]hlc.Timestamp )
1988
1989
for tableID , frontier := range cf .frontier .Frontiers () {
1989
1990
tableHighWater := func () hlc.Timestamp {
1990
1991
// If this table has not yet finished its initial scan, we use the highwater
@@ -2016,10 +2017,7 @@ func (cf *changeFrontier) managePerTableProtectedTimestamps(
2016
2017
}
2017
2018
} else {
2018
2019
// TODO(#152448): Do not include system table protections in these records.
2019
- if err := cf .createPerTableProtectedTimestampRecord (ctx , txn , ptsEntries , tableID , tableHighWater , pts ); err != nil {
2020
- return hlc.Timestamp {}, false , err
2021
- }
2022
- updatedPerTablePTS = true
2020
+ tableIDsToCreate [tableID ] = tableHighWater
2023
2021
}
2024
2022
}
2025
2023
@@ -2030,6 +2028,13 @@ func (cf *changeFrontier) managePerTableProtectedTimestamps(
2030
2028
updatedPerTablePTS = true
2031
2029
}
2032
2030
2031
+ if len (tableIDsToCreate ) > 0 {
2032
+ if err := cf .createPerTableProtectedTimestampRecords (ctx , txn , ptsEntries , tableIDsToCreate , pts ); err != nil {
2033
+ return hlc.Timestamp {}, false , err
2034
+ }
2035
+ updatedPerTablePTS = true
2036
+ }
2037
+
2033
2038
return newPTS , updatedPerTablePTS , nil
2034
2039
}
2035
2040
@@ -2072,40 +2077,51 @@ func (cf *changeFrontier) advancePerTableProtectedTimestampRecord(
2072
2077
return true , nil
2073
2078
}
2074
2079
2075
- func (cf * changeFrontier ) createPerTableProtectedTimestampRecord (
2080
+ func (cf * changeFrontier ) createPerTableProtectedTimestampRecords (
2076
2081
ctx context.Context ,
2077
2082
txn isql.Txn ,
2078
2083
ptsEntries * cdcprogresspb.ProtectedTimestampRecords ,
2079
- tableID descpb.ID ,
2080
- tableHighWater hlc.Timestamp ,
2084
+ tableIDsToCreate map [descpb.ID ]hlc.Timestamp ,
2081
2085
pts protectedts.Storage ,
2082
2086
) error {
2083
- // If the table is lagging and doesn't have a per table PTS record,
2084
- // we create a new one.
2085
- targets := changefeedbase.Targets {}
2086
- if cf .targets .Size > 0 {
2087
- err := cf .targets .EachTarget (func (target changefeedbase.Target ) error {
2088
- if target .DescID == tableID {
2089
- targets .Add (target )
2090
- }
2091
- return nil
2092
- })
2087
+ if ptsEntries .ProtectedTimestampRecords == nil {
2088
+ ptsEntries .ProtectedTimestampRecords = make (map [descpb.ID ]* uuid.UUID )
2089
+ }
2090
+ for tableID , tableHighWater := range tableIDsToCreate {
2091
+ targets , err := cf .createPerTablePTSTarget (tableID )
2093
2092
if err != nil {
2094
2093
return err
2095
2094
}
2095
+ ptr := createProtectedTimestampRecord (
2096
+ ctx , cf .FlowCtx .Codec (), cf .spec .JobID , targets , tableHighWater ,
2097
+ )
2098
+ uuid := ptr .ID .GetUUID ()
2099
+ ptsEntries .ProtectedTimestampRecords [tableID ] = & uuid
2100
+ if err := pts .Protect (ctx , ptr ); err != nil {
2101
+ return err
2102
+ }
2096
2103
}
2097
- ptr := createProtectedTimestampRecord (
2098
- ctx , cf .FlowCtx .Codec (), cf .spec .JobID , targets , tableHighWater ,
2099
- )
2100
- if ptsEntries .ProtectedTimestampRecords == nil {
2101
- ptsEntries .ProtectedTimestampRecords = make (map [descpb.ID ]* uuid.UUID )
2104
+ return writeChangefeedJobInfo (ctx , perTableProtectedTimestampsFilename , ptsEntries , txn , cf .spec .JobID )
2105
+ }
2106
+
2107
+ func (cf * changeFrontier ) createPerTablePTSTarget (
2108
+ tableID descpb.ID ,
2109
+ ) (changefeedbase.Targets , error ) {
2110
+ targets := changefeedbase.Targets {}
2111
+ if cf .targets .Size > 0 {
2112
+ if found , err := cf .targets .EachHavingTableID (tableID , func (target changefeedbase.Target ) error {
2113
+ targets .Add (target )
2114
+ return nil
2115
+ }); err != nil {
2116
+ return changefeedbase.Targets {}, err
2117
+ } else if ! found {
2118
+ return changefeedbase.Targets {}, errors .AssertionFailedf ("attempted to create a per-table PTS record for table %d, but no target was found" , tableID )
2119
+ }
2102
2120
}
2103
- uuid := ptr .ID .GetUUID ()
2104
- ptsEntries .ProtectedTimestampRecords [tableID ] = & uuid
2105
- if err := pts .Protect (ctx , ptr ); err != nil {
2106
- return err
2121
+ if targets .Size != 1 {
2122
+ return changefeedbase.Targets {}, errors .AssertionFailedf ("expected 1 target, got %d" , targets .Size )
2107
2123
}
2108
- return writeChangefeedJobInfo ( ctx , perTableProtectedTimestampsFilename , ptsEntries , txn , cf . spec . JobID )
2124
+ return targets , nil
2109
2125
}
2110
2126
2111
2127
func (cf * changeFrontier ) advanceProtectedTimestamp (
0 commit comments