Skip to content

Commit

Permalink
Merge pull request #18989 from lego/joey/table-lease-singleflight
Browse files Browse the repository at this point in the history
sql: Refactor TableDescriptor lease acquisition to use singleflight
  • Loading branch information
lgo authored Oct 18, 2017
2 parents c86f16f + 899d08f commit f3c7781
Show file tree
Hide file tree
Showing 10 changed files with 399 additions and 85 deletions.
32 changes: 32 additions & 0 deletions pkg/base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ const (
// with a value of 0.2 and a liveness duration of 10 seconds, each node's
// liveness record would be eagerly renewed after 2 seconds.
livenessRenewalFraction = 0.5

// DefaultTableDescriptorLeaseDuration is the default mean duration a
// lease will be acquired for. The actual duration is jittered using
// the jitter fraction. Jittering is done to prevent multiple leases
// from being renewed simultaneously if they were all acquired
// simultaneously.
DefaultTableDescriptorLeaseDuration = 5 * time.Minute

// DefaultTableDescriptorLeaseJitterFraction is the default factor
// that we use to randomly jitter the lease duration when acquiring a
// new lease and the lease renewal timeout.
DefaultTableDescriptorLeaseJitterFraction = 0.25
)

var defaultRaftElectionTimeoutTicks = envutil.EnvOrDefaultInt(
Expand Down Expand Up @@ -466,3 +478,23 @@ func TempStorageConfigFromEnv(
Mon: &monitor,
}
}

// LeaseManagerConfig holds table lease manager parameters.
type LeaseManagerConfig struct {
// TableDescriptorLeaseDuration is the mean duration a lease will be
// acquired for.
TableDescriptorLeaseDuration time.Duration

// TableDescriptorLeaseJitterFraction is the factor that we use to
// randomly jitter the lease duration when acquiring a new lease and
// the lease renewal timeout.
TableDescriptorLeaseJitterFraction float64
}

// NewLeaseManagerConfig initializes a LeaseManagerConfig with default values.
func NewLeaseManagerConfig() *LeaseManagerConfig {
return &LeaseManagerConfig{
TableDescriptorLeaseDuration: DefaultTableDescriptorLeaseDuration,
TableDescriptorLeaseJitterFraction: DefaultTableDescriptorLeaseJitterFraction,
}
}
3 changes: 3 additions & 0 deletions pkg/base/test_server_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type TestServerArgs struct {
*cluster.Settings
RaftConfig

// LeaseManagerConfig holds configuration values specific to the LeaseManager.
LeaseManagerConfig *LeaseManagerConfig

// PartOfCluster must be set if the TestServer is joining others in a cluster.
// If not set (and hence the server is the only one in the cluster), the
// default zone config will be overridden to disable all replication - so that
Expand Down
4 changes: 4 additions & 0 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ type Config struct {

base.RaftConfig

// LeaseManagerConfig holds configuration values specific to the LeaseManager.
LeaseManagerConfig *base.LeaseManagerConfig

// Unix socket: for postgres only.
SocketFile string

Expand Down Expand Up @@ -369,6 +372,7 @@ func MakeConfig(ctx context.Context, st *cluster.Settings) Config {

cfg.Config.InitDefaults()
cfg.RaftConfig.SetDefaults()
cfg.LeaseManagerConfig = base.NewLeaseManagerConfig()
return cfg
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func NewServer(cfg Config, stopper *stop.Stopper) (*Server, error) {
lmKnobs = *leaseManagerTestingKnobs.(*sql.LeaseManagerTestingKnobs)
}
s.leaseMgr = sql.NewLeaseManager(&s.nodeIDContainer, *s.db, s.clock, lmKnobs,
s.stopper, &s.internalMemMetrics)
s.stopper, &s.internalMemMetrics, s.cfg.LeaseManagerConfig)
s.leaseMgr.RefreshLeases(s.stopper, s.db, s.gossip)

// We do not set memory monitors or a noteworthy limit because the children of
Expand Down
5 changes: 5 additions & 0 deletions pkg/server/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ func makeTestConfigFromParams(params base.TestServerArgs) Config {
cfg.TestingKnobs = params.Knobs
cfg.RaftConfig = params.RaftConfig
cfg.RaftConfig.SetDefaults()
if params.LeaseManagerConfig != nil {
cfg.LeaseManagerConfig = params.LeaseManagerConfig
} else {
cfg.LeaseManagerConfig = base.NewLeaseManagerConfig()
}
if params.JoinAddr != "" {
cfg.JoinList = []string{params.JoinAddr}
}
Expand Down
Loading

0 comments on commit f3c7781

Please sign in to comment.