@@ -102,6 +102,12 @@ var WaitForInitialVersion = settings.RegisterBoolSetting(settings.ApplicationLev
102
102
"enables waiting for the initial version of a descriptor" ,
103
103
true )
104
104
105
+ var LockedLeaseTimestamp = settings .RegisterBoolSetting (settings .ApplicationLevel ,
106
+ "sql.catalog.descriptor_lease.use_locked_timestamps.enabled" ,
107
+ "guarantees transactional version consistency for descriptors used by the lease manager," +
108
+ "descriptors used can be intentionally older to support this" ,
109
+ false )
110
+
105
111
// WaitForNoVersion returns once there are no unexpired leases left
106
112
// for any version of the descriptor.
107
113
func (m * Manager ) WaitForNoVersion (
@@ -1265,7 +1271,7 @@ func (m *Manager) purgeOldVersions(
1265
1271
// Acquire a refcount on the descriptor on the latest version to maintain an
1266
1272
// active lease, so that it doesn't get released when removeInactives()
1267
1273
// is called below. Release this lease after calling removeInactives().
1268
- desc , _ , err = t .findForTimestamp (ctx , m .storage .clock .Now ())
1274
+ desc , _ , err = t .findForTimestamp (ctx , TimestampToReadTimestamp ( m .storage .clock .Now () ))
1269
1275
if err == nil || ! errors .Is (err , errRenewLease ) {
1270
1276
break
1271
1277
}
@@ -1600,7 +1606,7 @@ func (m *Manager) SetRegionPrefix(val []byte) {
1600
1606
// id and fails because the id has been dropped by the TRUNCATE.
1601
1607
func (m * Manager ) AcquireByName (
1602
1608
ctx context.Context ,
1603
- timestamp hlc. Timestamp ,
1609
+ timestamp ReadTimestamp ,
1604
1610
parentID descpb.ID ,
1605
1611
parentSchemaID descpb.ID ,
1606
1612
name string ,
@@ -1622,9 +1628,9 @@ func (m *Manager) AcquireByName(
1622
1628
return desc , nil
1623
1629
}
1624
1630
// Check if we have cached an ID for this name.
1625
- descVersion , _ := m .names .get (ctx , parentID , parentSchemaID , name , timestamp )
1631
+ descVersion , _ := m .names .get (ctx , parentID , parentSchemaID , name , timestamp . GetTimestamp () )
1626
1632
if descVersion != nil {
1627
- if descVersion .GetModificationTime ().LessEq (timestamp ) {
1633
+ if descVersion .GetModificationTime ().LessEq (timestamp . GetTimestamp () ) {
1628
1634
return validateDescriptorForReturn (descVersion )
1629
1635
}
1630
1636
// m.names.get() incremented the refcount, we decrement it to get a new
@@ -1643,7 +1649,7 @@ func (m *Manager) AcquireByName(
1643
1649
// lease with at least a bit of lifetime left in it. So, we do it the hard
1644
1650
// way: look in the database to resolve the name, then acquire a new lease.
1645
1651
var err error
1646
- id , err := m .resolveName (ctx , timestamp , parentID , parentSchemaID , name )
1652
+ id , err := m .resolveName (ctx , timestamp . GetTimestamp () , parentID , parentSchemaID , name )
1647
1653
if err != nil {
1648
1654
return nil , err
1649
1655
}
@@ -1777,7 +1783,7 @@ type LeasedDescriptor interface {
1777
1783
// can only return an older version of a descriptor if the latest version
1778
1784
// can be leased; as it stands a dropped descriptor cannot be leased.
1779
1785
func (m * Manager ) Acquire (
1780
- ctx context.Context , timestamp hlc. Timestamp , id descpb.ID ,
1786
+ ctx context.Context , timestamp ReadTimestamp , id descpb.ID ,
1781
1787
) (LeasedDescriptor , error ) {
1782
1788
for {
1783
1789
if m .IsDraining () {
@@ -1802,7 +1808,7 @@ func (m *Manager) Acquire(
1802
1808
1803
1809
case errors .Is (err , errReadOlderVersion ):
1804
1810
// Read old versions from the store. This can block while reading.
1805
- versions , errRead := m .readOlderVersionForTimestamp (ctx , id , timestamp )
1811
+ versions , errRead := m .readOlderVersionForTimestamp (ctx , id , timestamp . GetTimestamp () )
1806
1812
if errRead != nil {
1807
1813
return nil , errRead
1808
1814
}
@@ -2135,6 +2141,9 @@ func (m *Manager) watchForUpdates(ctx context.Context) {
2135
2141
}
2136
2142
2137
2143
handleCheckpoint := func (ctx context.Context , checkpoint * kvpb.RangeFeedCheckpoint ) {
2144
+ if m .testingKnobs .TestingOnRangeFeedCheckPoint != nil {
2145
+ m .testingKnobs .TestingOnRangeFeedCheckPoint ()
2146
+ }
2138
2147
// Track checkpoints that occur from the rangefeed to make sure progress
2139
2148
// is always made.
2140
2149
m .mu .Lock ()
@@ -2908,6 +2917,23 @@ func (m *Manager) deleteOrphanedLeasesWithSameInstanceID(
2908
2917
instanceID , releasedCount .Load (), totalLeases )
2909
2918
}
2910
2919
2920
+ // GetReadTimestamp returns a locked timestamp to use for lease management.
2921
+ func (m * Manager ) GetReadTimestamp (timestamp hlc.Timestamp ) ReadTimestamp {
2922
+ if LockedLeaseTimestamp .Get (& m .settings .SV ) {
2923
+ replicationTS := m .GetSafeReplicationTS ()
2924
+ if ! replicationTS .IsEmpty () && replicationTS .Less (timestamp ) {
2925
+ return LeaseTimestamp {
2926
+ ReadTimestamp : timestamp ,
2927
+ LeaseTimestamp : replicationTS ,
2928
+ }
2929
+ }
2930
+ }
2931
+ // Fallback to existing behavior with timestamps.
2932
+ return LeaseTimestamp {
2933
+ ReadTimestamp : timestamp ,
2934
+ }
2935
+ }
2936
+
2911
2937
// TestingGetBoundAccount returns the bound account used by the lease manager.
2912
2938
func (m * Manager ) TestingGetBoundAccount () * mon.ConcurrentBoundAccount {
2913
2939
return m .boundAccount
0 commit comments