Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
111427: catalog/lease: add new cluster setting for session based leasing r=fqazi a=fqazi

To help support session-based leasing, we will add a new internal cluster setting for tracking the leasing mode. As well as interfaces to query this setting to help add support for future session-based leasing. This setting will be used in future PRs.

Epic: CC-24173
Release note: None

Co-authored-by: Faizan Qazi <[email protected]>
  • Loading branch information
craig[bot] and fqazi committed Oct 21, 2023
2 parents 73fe6f1 + c19c44d commit 0791553
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/gen/stringer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ STRINGER_SRCS = [
"//pkg/sql/catalog/catalogkeys:commenttype_string.go",
"//pkg/sql/catalog/catpb:privilegedescversion_string.go",
"//pkg/sql/catalog/descpb:formatversion_string.go",
"//pkg/sql/catalog/lease:sessionbasedleasingmode_string.go",
"//pkg/sql/colfetcher:fetcherstate_string.go",
"//pkg/sql/execinfra:consumerstatus_string.go",
"//pkg/sql/execinfra:procstate_string.go",
Expand Down
7 changes: 7 additions & 0 deletions pkg/sql/catalog/lease/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//build:STRINGER.bzl", "stringer")

go_library(
name = "lease",
Expand Down Expand Up @@ -62,6 +63,12 @@ go_library(
],
)

stringer(
name = "gen-lease-mode-stringer",
src = "lease.go",
typ = "SessionBasedLeasingMode",
)

go_test(
name = "lease_test",
size = "large",
Expand Down
47 changes: 46 additions & 1 deletion pkg/sql/catalog/lease/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,51 @@ var LeaseJitterFraction = settings.RegisterFloatSetting(
base.DefaultDescriptorLeaseJitterFraction,
settings.Fraction)

//go:generate stringer -type=SessionBasedLeasingMode

type SessionBasedLeasingMode int64

const (
// SessionBasedLeasingOff expiry based leasing is being used.
SessionBasedLeasingOff SessionBasedLeasingMode = iota
// SessionBasedDualWrite expiry based and session based leasing are
// active concurrently, and both tables must be consulted schema changes.
SessionBasedDualWrite
// SessionBasedDrain expiry based leases will not be granted or renewed.
// Valid pre-existing leases that are expiry based will still be respected.
SessionBasedDrain
// SessionBasedOnly session based leases are only active, and schema
// changes only need to consult this table.
SessionBasedOnly
)

// LeaseEnableSessionBasedLeasing used to enable / disable support for
// session based leasing.
var LeaseEnableSessionBasedLeasing = settings.RegisterEnumSetting(
settings.ApplicationLevel,
"sql.catalog.experimental_use_session_based_leasing",
"enables session based leasing for internal testing.",
"off",
map[int64]string{
int64(SessionBasedLeasingOff): "off",
int64(SessionBasedDualWrite): "dual_write",
int64(SessionBasedDrain): "drain",
int64(SessionBasedOnly): "session",
},
settings.WithReportable(false),
)

// sessionBasedLeasingModeActive determines if the current mode at least meets
// the required minimum.
func (m *Manager) isSessionBasedLeasingModeActive(minimumMode SessionBasedLeasingMode) bool {
return m.getSessionBasedLeasingMode() >= minimumMode
}

// getSessionBasedLeasingMode returns the current session based leasing mode.
func (m *Manager) getSessionBasedLeasingMode() SessionBasedLeasingMode {
return SessionBasedLeasingMode(LeaseEnableSessionBasedLeasing.Get(&m.settings.SV))
}

// WaitForNoVersion returns once there are no unexpired leases left
// for any version of the descriptor.
func (m *Manager) WaitForNoVersion(
Expand Down Expand Up @@ -756,10 +801,10 @@ func NewLeaseManager(
}
lm.storage.regionPrefix = &atomic.Value{}
lm.storage.regionPrefix.Store(enum.One)
lm.storage.sessionBasedLeasingMode = lm
lm.stopper.AddCloser(lm.sem.Closer("stopper"))
lm.mu.descriptors = make(map[descpb.ID]*descriptorState)
lm.mu.updatesResolvedTimestamp = clock.Now()

lm.draining.Store(false)
return lm
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/sql/catalog/lease/lease_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1568,3 +1568,34 @@ func TestGetDescriptorsFromStoreForIntervalCPULimiterPagination(t *testing.T) {
require.Len(t, descs, 3)
require.Equal(t, numRequests, 1)
}

// TestSessionLeasingClusterSetting sanity testing for the new
// experimental_use_session_based_leasing cluster setting and interfaces used
// to consume it.
func TestSessionLeasingClusterSetting(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

ctx := context.Background()
srv, sqlDB, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer srv.Stopper().Stop(ctx)

// Validate all settings can be set and the provider works correctly.
for idx, setting := range []string{"off", "dual_write", "drain", "session"} {
_, err := sqlDB.Exec("SET CLUSTER SETTING sql.catalog.experimental_use_session_based_leasing=$1::STRING", setting)
require.NoError(t, err)
lm := srv.LeaseManager().(*Manager)

// Validate that the mode we just set is active and the provider handles
// it properly.
require.True(t, lm.isSessionBasedLeasingModeActive(SessionBasedLeasingMode(idx)))
require.Equal(t, lm.getSessionBasedLeasingMode(), SessionBasedLeasingMode(idx))
// Validate that the previous minimums are active and forwards ones are not.
for mode := SessionBasedLeasingOff; mode <= SessionBasedLeasingMode(idx); mode++ {
require.True(t, lm.isSessionBasedLeasingModeActive(mode))
}
for mode := SessionBasedLeasingMode(idx) + 1; mode <= SessionBasedOnly; mode++ {
require.False(t, lm.isSessionBasedLeasingModeActive(mode))
}
}
}
30 changes: 30 additions & 0 deletions pkg/sql/catalog/lease/sessionbasedleasingmode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions pkg/sql/catalog/lease/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ import (
// the manager. Some of these fields belong on the manager, in any case, since
// they're only used by the manager and not by the store itself.
type storage struct {
nodeIDContainer *base.SQLIDContainer
db isql.DB
clock *hlc.Clock
settings *cluster.Settings
codec keys.SQLCodec
regionPrefix *atomic.Value
sysDBCache *catkv.SystemDatabaseCache
nodeIDContainer *base.SQLIDContainer
db isql.DB
clock *hlc.Clock
settings *cluster.Settings
codec keys.SQLCodec
regionPrefix *atomic.Value
sessionBasedLeasingMode sessionBasedLeasingModeReader
sysDBCache *catkv.SystemDatabaseCache

// group is used for all calls made to acquireNodeLease to prevent
// concurrent lease acquisitions from the store.
Expand All @@ -78,6 +79,11 @@ type writer interface {
insertLease(context.Context, *kv.Txn, leaseFields) error
}

type sessionBasedLeasingModeReader interface {
isSessionBasedLeasingModeActive(minimumMode SessionBasedLeasingMode) bool
getSessionBasedLeasingMode() SessionBasedLeasingMode
}

// LeaseRenewalDuration controls the default time before a lease expires when
// acquisition to renew the lease begins.
var LeaseRenewalDuration = settings.RegisterDurationSetting(
Expand Down

0 comments on commit 0791553

Please sign in to comment.