Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kv: all leases start as expiration based #101765

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 0 additions & 74 deletions pkg/kv/kvserver/client_lease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/keys"
Expand Down Expand Up @@ -1499,76 +1498,3 @@ func TestLeaseTransfersUseExpirationLeasesAndBumpToEpochBasedOnes(t *testing.T)
defer mu.Unlock()
require.Equal(t, roachpb.LeaseExpiration, mu.lease.Type())
}

// TestLeaseUpgradeVersionGate tests the version gating for the lease-upgrade
// process.
//
// TODO(irfansharif): Delete this in 23.1 (or whenever we get rid of the
// clusterversion.EnableLeaseUpgrade).
func TestLeaseUpgradeVersionGate(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

ctx := context.Background()
st := cluster.MakeTestingClusterSettingsWithVersions(
clusterversion.TestingBinaryVersion,
clusterversion.ByKey(clusterversion.TODODelete_V22_2EnableLeaseUpgrade-1),
false, /* initializeVersion */
)
kvserver.ExpirationLeasesOnly.Override(ctx, &st.SV, false) // override metamorphism

tci := serverutils.StartNewTestCluster(t, 2, base.TestClusterArgs{
ReplicationMode: base.ReplicationManual,
ServerArgs: base.TestServerArgs{
Settings: st,
Knobs: base.TestingKnobs{
Server: &server.TestingKnobs{
DisableAutomaticVersionUpgrade: make(chan struct{}),
BinaryVersionOverride: clusterversion.ByKey(clusterversion.TODODelete_V22_2EnableLeaseUpgrade - 1),
},
},
},
})
tc := tci.(*testcluster.TestCluster)
defer tc.Stopper().Stop(ctx)

scratchKey := tc.ScratchRange(t)
n1, n1Target := tc.Server(0), tc.Target(0)
n2, n2Target := tc.Server(1), tc.Target(1)

// Add a replica; we're going to move the lease to and from it below.
desc := tc.AddVotersOrFatal(t, scratchKey, n2Target)

// Transfer the lease from n1 to n2. It should be transferred as an
// epoch-based one since we've not upgraded past
// clusterversion.EnableLeaseUpgrade yet.
tc.TransferRangeLeaseOrFatal(t, desc, n2Target)
testutils.SucceedsSoon(t, func() error {
li, _, err := tc.FindRangeLeaseEx(ctx, desc, nil)
require.NoError(t, err)
if !li.Current().OwnedBy(n2.GetFirstStoreID()) {
return errors.New("lease still owned by n1")
}
require.Equal(t, roachpb.LeaseEpoch, li.Current().Type())
return nil
})

// Enable the version gate.
_, err := tc.Conns[0].ExecContext(ctx, `SET CLUSTER SETTING version = $1`,
clusterversion.ByKey(clusterversion.TODODelete_V22_2EnableLeaseUpgrade).String())
require.NoError(t, err)

// Transfer the lease back from n2 to n1. It should be transferred as an
// expiration-based lease that's later upgraded to an epoch based one now
// that we're past the version gate.
tc.TransferRangeLeaseOrFatal(t, desc, n1Target)
testutils.SucceedsSoon(t, func() error {
li, _, err := tc.FindRangeLeaseEx(ctx, desc, nil)
require.NoError(t, err)
if !li.Current().OwnedBy(n1.GetFirstStoreID()) {
return errors.New("lease still owned by n2")
}
return nil
})
tc.WaitForLeaseUpgrade(ctx, t, desc)
}
71 changes: 37 additions & 34 deletions pkg/kv/kvserver/client_replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,9 @@ func TestLeaseMetricsOnSplitAndTransfer(t *testing.T) {
},
})
defer tc.Stopper().Stop(ctx)
// Ignore the initial counts since many ranges are created.
expiration0, epoch0 := getLeaseMetrics(tc, t)

// Up-replicate to two replicas.
expirationKey := tc.ScratchRangeWithExpirationLease(t)
expirationDesc := tc.LookupRangeOrFatal(t, expirationKey)
Expand Down Expand Up @@ -2114,41 +2117,41 @@ func TestLeaseMetricsOnSplitAndTransfer(t *testing.T) {
require.Error(t, err)

metrics := tc.GetFirstStoreFromServer(t, 0).Metrics()
if a, e := metrics.LeaseTransferSuccessCount.Count(), int64(1); a != e {
t.Errorf("expected %d lease transfer successes; got %d", e, a)
}
if a, e := metrics.LeaseTransferErrorCount.Count(), int64(1); a != e {
t.Errorf("expected %d lease transfer errors; got %d", e, a)
}

// Expire current leases and put a key to the epoch based scratch range to
// get a lease.
testutils.SucceedsSoon(t, func() error {
manualClock.Increment(tc.GetFirstStoreFromServer(t, 0).GetStoreConfig().LeaseExpiration())
if err := tc.GetFirstStoreFromServer(t, 0).DB().Put(context.Background(), epochKey, "foo"); err != nil {
return err
}
require.Equal(t, int64(1), metrics.LeaseTransferSuccessCount.Count())
require.Equal(t, int64(1), metrics.LeaseTransferErrorCount.Count())

require.Equal(t, roachpb.LeaseExpiration, tc.GetFirstStoreFromServer(t, 0).LookupReplica(roachpb.RKey(expirationKey)).CurrentLeaseStatus(ctx).Lease.Type())
require.Equal(t, roachpb.LeaseEpoch, tc.GetFirstStoreFromServer(t, 0).LookupReplica(roachpb.RKey(epochKey)).CurrentLeaseStatus(ctx).Lease.Type())
expiration1, epoch1 := getLeaseMetrics(tc, t)
require.Equal(t, int64(1), expiration1-expiration0)
require.Equal(t, int64(2), epoch1-epoch0)

// Expire current leases and put a key to the epoch0 based scratch range to
// convert to Epoch
manualClock.Increment(tc.GetFirstStoreFromServer(t, 0).GetStoreConfig().LeaseExpiration())
require.NoError(t, tc.GetFirstStoreFromServer(t, 0).DB().Put(context.Background(), epochKey, "foo"))
require.Equal(t, roachpb.LeaseExpiration, tc.GetFirstStoreFromServer(t, 0).LookupReplica(roachpb.RKey(expirationKey)).CurrentLeaseStatus(ctx).Lease.Type())
require.Equal(t, roachpb.LeaseEpoch, tc.GetFirstStoreFromServer(t, 0).LookupReplica(roachpb.RKey(epochKey)).CurrentLeaseStatus(ctx).Lease.Type())

// The one expired lease should be recreated, not sure where all epoch leases
// come from. I think it is internal retries due to the error.
expiration2, epoch2 := getLeaseMetrics(tc, t)
require.Equal(t, int64(3), expiration2)
require.GreaterOrEqual(t, epoch2-epoch1, int64(1))
}

// Update replication gauges for all stores and verify we have 1 each of
// expiration and epoch leases.
var expirationLeases int64
var epochLeases int64
for i := range tc.Servers {
if err := tc.GetFirstStoreFromServer(t, i).ComputeMetrics(context.Background()); err != nil {
return err
}
metrics = tc.GetFirstStoreFromServer(t, i).Metrics()
expirationLeases += metrics.LeaseExpirationCount.Value()
epochLeases += metrics.LeaseEpochCount.Value()
}
if a, e := expirationLeases, int64(1); a != e {
return errors.Errorf("expected %d expiration lease count; got %d", e, a)
}
if a, e := epochLeases, int64(1); a < e {
return errors.Errorf("expected greater than %d epoch lease count; got %d", e, a)
}
return nil
})
func getLeaseMetrics(tc *testcluster.TestCluster, t *testing.T) (int64, int64) {
// Update replication gauges for all stores and verify we have 1 each of
// expiration and epoch leases.
var expirationLeases int64
var epochLeases int64
for i := range tc.Servers {
require.NoError(t, tc.GetFirstStoreFromServer(t, i).ComputeMetrics(context.Background()))
metrics := tc.GetFirstStoreFromServer(t, i).Metrics()
expirationLeases += metrics.LeaseExpirationCount.Value()
epochLeases += metrics.LeaseEpochCount.Value()
}
return expirationLeases, epochLeases
}

// Test that leases held before a restart are not used after the restart.
Expand Down
Loading