Skip to content

Commit 09a462e

Browse files
craig[bot]miraradeva
andcommitted
Merge #144485
144485: kvserver: shorten leader-lease min-expiration grace period r=arulajmani a=miraradeva Previously, when constructing a new leader lease, the min-expiration of the new lease was set to the maximum of the previous lease expiration and now + 6s (where the 6s corresponded to the lease duration). While the former is needed for correctness (to ensure the lease disjointness property), the latter is just a grace period for the new leader to finish fortifying. When now + 6s was introduced, the duration of store liveness support was also 6s, so this matched the duration of a typical leader lease. However, since then the store liveness support duration was decreased to 3s, and now the 6s seems excessive for a grace period. This became evident from the last remaining case (failover/non-system/blackhole-recv) where the failover duration of expiration leases was a little better than leader leases (see #133612). In this scenario, the old leader was not able to receive messages but was successfully proposing new leader leases with min-expirations of 6s into the future. When a new leader was eventually elected, it had to wait out that min-expiration before starting a new valid lease. This commit introduces a new config setting for the fortification grace period and sets it to a default of 3s. Informs: #133612 Release note: None Co-authored-by: Mira Radeva <[email protected]>
2 parents 5094a32 + e00a5ad commit 09a462e

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

pkg/base/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ var (
235235
defaultStoreLivenessSupportDuration = envutil.EnvOrDefaultDuration(
236236
"COCKROACH_STORE_LIVENESS_SUPPORT_DURATION", 3*time.Second)
237237

238+
// defaultFortificationGracePeriod is the default value for
239+
// FortificationGracePeriod.
240+
defaultFortificationGracePeriod = envutil.EnvOrDefaultDuration(
241+
"COCKROACH_RAFT_FORTIFICATION_GRACE_PERIOD", 3*time.Second)
242+
238243
// defaultRaftTickInterval is the default resolution of the Raft timer.
239244
defaultRaftTickInterval = envutil.EnvOrDefaultDuration(
240245
"COCKROACH_RAFT_TICK_INTERVAL", 500*time.Millisecond)
@@ -590,6 +595,10 @@ type RaftConfig struct {
590595
// stores request and extend.
591596
StoreLivenessSupportDuration time.Duration
592597

598+
// FortificationGracePeriod is the minimum validity of a new leader lease to
599+
// allow for the new leader to fortify.
600+
FortificationGracePeriod time.Duration
601+
593602
// RangeLeaseRaftElectionTimeoutMultiplier specifies the range lease duration.
594603
RangeLeaseDuration time.Duration
595604
// RangeLeaseRenewalFraction specifies what fraction the range lease renewal
@@ -706,6 +715,9 @@ func (cfg *RaftConfig) SetDefaults() {
706715
if cfg.StoreLivenessSupportDuration == 0 {
707716
cfg.StoreLivenessSupportDuration = defaultStoreLivenessSupportDuration
708717
}
718+
if cfg.FortificationGracePeriod == 0 {
719+
cfg.FortificationGracePeriod = defaultFortificationGracePeriod
720+
}
709721
if cfg.RangeLeaseDuration == 0 {
710722
cfg.RangeLeaseDuration = defaultRangeLeaseDuration
711723
}

pkg/base/testdata/raft_config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ echo
99
RaftHeartbeatIntervalTicks: (int64) 2,
1010
StoreLivenessHeartbeatInterval: (time.Duration) 1s,
1111
StoreLivenessSupportDuration: (time.Duration) 3s,
12+
FortificationGracePeriod: (time.Duration) 3s,
1213
RangeLeaseDuration: (time.Duration) 6s,
1314
RangeLeaseRenewalFraction: (float64) 0.5,
1415
RaftEnableCheckQuorum: (bool) true,

pkg/kv/kvserver/leases/build.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ type Settings struct {
5757
// minimum expiration field in the lease object. It is used for mixed-version
5858
// compatibility.
5959
MinExpirationSupported bool
60-
// RangeLeaseDuration specifies the range lease duration.
60+
// RangeLeaseDuration specifies the range lease duration. It's used for
61+
// extending expiration leases.
6162
RangeLeaseDuration time.Duration
63+
// FortificationGracePeriod specifies the leader lease duration.
64+
FortificationGracePeriod time.Duration
6265
}
6366

6467
// PrevLeaseManipulation contains a set of instructions for manipulating the
@@ -537,7 +540,7 @@ func leaseMinTimestamp(st Settings, i BuildInput, nextType roachpb.LeaseType) hl
537540
// there's no chance of an expiration regression. Still, it is still useful
538541
// to set a minimum expiration time so that the new lease is guaranteed to
539542
// have some validity period, even if the raft leader is unable to fortify.
540-
minExp := i.Now.ToTimestamp().Add(int64(st.RangeLeaseDuration), 0)
543+
minExp := i.Now.ToTimestamp().Add(int64(st.FortificationGracePeriod), 0)
541544
minExp.Forward(i.PrevLeaseExpiration())
542545
return minExp
543546
default:

pkg/kv/kvserver/leases/build_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ var (
2626
cts10 = hlc.ClockTimestamp{WallTime: 10}
2727
cts20 = hlc.ClockTimestamp{WallTime: 20}
2828
cts30 = hlc.ClockTimestamp{WallTime: 30}
29+
cts35 = hlc.ClockTimestamp{WallTime: 35}
2930
cts40 = hlc.ClockTimestamp{WallTime: 40}
3031
cts50 = hlc.ClockTimestamp{WallTime: 50}
3132
ts10 = cts10.ToTimestamp()
3233
ts30 = cts30.ToTimestamp()
34+
ts35 = cts35.ToTimestamp()
3335
ts40 = cts40.ToTimestamp()
3436
ts50 = cts50.ToTimestamp()
3537
)
@@ -203,6 +205,7 @@ func defaultSettings() Settings {
203205
ExpToEpochEquiv: true,
204206
MinExpirationSupported: true,
205207
RangeLeaseDuration: 20,
208+
FortificationGracePeriod: 15,
206209
}
207210
}
208211

@@ -443,7 +446,7 @@ func TestBuild(t *testing.T) {
443446
Term: 5,
444447
Sequence: 8,
445448
AcquisitionType: roachpb.LeaseAcquisitionType_Request,
446-
MinExpiration: ts40,
449+
MinExpiration: ts35,
447450
},
448451
},
449452
},
@@ -460,7 +463,7 @@ func TestBuild(t *testing.T) {
460463
Term: 5,
461464
Sequence: 8,
462465
AcquisitionType: roachpb.LeaseAcquisitionType_Request,
463-
MinExpiration: ts40,
466+
MinExpiration: ts35,
464467
},
465468
},
466469
},
@@ -723,7 +726,7 @@ func TestBuild(t *testing.T) {
723726
Term: 5,
724727
Sequence: 7, // sequence not changed
725728
AcquisitionType: roachpb.LeaseAcquisitionType_Request,
726-
MinExpiration: ts40,
729+
MinExpiration: ts35,
727730
},
728731
},
729732
},
@@ -819,7 +822,7 @@ func TestBuild(t *testing.T) {
819822
Term: 5,
820823
Sequence: 7, // sequence not changed
821824
AcquisitionType: roachpb.LeaseAcquisitionType_Request,
822-
MinExpiration: ts40,
825+
MinExpiration: ts35,
823826
},
824827
},
825828
},
@@ -927,7 +930,7 @@ func TestBuild(t *testing.T) {
927930
Term: 5,
928931
Sequence: 8, // sequence changed
929932
AcquisitionType: roachpb.LeaseAcquisitionType_Request,
930-
MinExpiration: ts40,
933+
MinExpiration: ts35,
931934
},
932935
PrevLeaseManipulation: PrevLeaseManipulation{
933936
RevokeAndForwardNextExpiration: true,

pkg/kv/kvserver/replica_range_lease.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,9 @@ func (r *Replica) leaseSettings(ctx context.Context) leases.Settings {
796796
// TODO(arul): remove this field entirely.
797797
ExpToEpochEquiv: true,
798798
// TODO(radu): remove this field entirely.
799-
MinExpirationSupported: true,
800-
RangeLeaseDuration: r.store.cfg.RangeLeaseDuration,
799+
MinExpirationSupported: true,
800+
RangeLeaseDuration: r.store.cfg.RangeLeaseDuration,
801+
FortificationGracePeriod: r.store.cfg.FortificationGracePeriod,
801802
}
802803
}
803804

0 commit comments

Comments
 (0)