Skip to content

Commit

Permalink
feat: Add lease read option
Browse files Browse the repository at this point in the history
  • Loading branch information
highpon committed Oct 3, 2023
1 parent 88f714b commit 91f58d3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"go.etcd.io/etcd/pkg/v3/netutil"
"go.etcd.io/etcd/server/v3/etcdserver/api/v3discovery"
"go.etcd.io/etcd/server/v3/storage/datadir"
"go.etcd.io/raft/v3"
)

// ServerConfig holds the configuration of etcd as taken from the command line or discovery.
Expand Down Expand Up @@ -162,6 +163,8 @@ type ServerConfig struct {

ForceNewCluster bool

ReadOnlyOption raft.ReadOnlyOption `json:"read-only-option"`

// EnableLeaseCheckpoint enables leader to send regular checkpoints to other members to prevent reset of remaining TTL on leader change.
EnableLeaseCheckpoint bool
// LeaseCheckpointInterval time.Duration is the wait duration between lease checkpoints.
Expand Down
4 changes: 4 additions & 0 deletions server/embed/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ type Config struct {
// ForceNewCluster starts a new cluster even if previously started; unsafe.
ForceNewCluster bool `json:"force-new-cluster"`

// Set to true to enable lease-based read.
LeaseRead bool `json:"lease-read"`

EnablePprof bool `json:"enable-pprof"`
Metrics string `json:"metrics"`
ListenMetricsUrls []url.URL
Expand Down Expand Up @@ -642,6 +645,7 @@ func (cfg *Config) AddFlags(fs *flag.FlagSet) {
fs.BoolVar(&cfg.StrictReconfigCheck, "strict-reconfig-check", cfg.StrictReconfigCheck, "Reject reconfiguration requests that would cause quorum loss.")

fs.BoolVar(&cfg.PreVote, "pre-vote", cfg.PreVote, "Enable to run an additional Raft election phase.")
fs.BoolVar(&cfg.LeaseRead, "lease-read", false, "Set to true to enable lease-based read.")

// security
fs.StringVar(&cfg.ClientTLSInfo.CertFile, "cert-file", "", "Path to the client server TLS cert file.")
Expand Down
7 changes: 7 additions & 0 deletions server/embed/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"go.etcd.io/etcd/server/v3/etcdserver/api/rafthttp"
"go.etcd.io/etcd/server/v3/storage"
"go.etcd.io/etcd/server/v3/verify"
"go.etcd.io/raft/v3"
)

const (
Expand Down Expand Up @@ -166,6 +167,11 @@ func StartEtcd(inCfg *Config) (e *Etcd, err error) {

backendFreelistType := parseBackendFreelistType(cfg.BackendFreelistType)

readOnlyOption := raft.ReadOnlySafe
if cfg.LeaseRead {
readOnlyOption = raft.ReadOnlyLeaseBased
}

srvcfg := config.ServerConfig{
Name: cfg.Name,
ClientURLs: cfg.AdvertiseClientUrls,
Expand Down Expand Up @@ -211,6 +217,7 @@ func StartEtcd(inCfg *Config) (e *Etcd, err error) {
PreVote: cfg.PreVote,
Logger: cfg.logger,
ForceNewCluster: cfg.ForceNewCluster,
ReadOnlyOption: readOnlyOption,
EnableGRPCGateway: cfg.EnableGRPCGateway,
ExperimentalEnableDistributedTracing: cfg.ExperimentalEnableDistributedTracing,
UnsafeNoFsync: cfg.UnsafeNoFsync,
Expand Down
2 changes: 2 additions & 0 deletions server/etcdmain/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ Clustering:
Auto compaction retention length. 0 means disable auto compaction.
--auto-compaction-mode 'periodic'
Interpret 'auto-compaction-retention' one of: periodic|revision. 'periodic' for duration based retention, defaulting to hours if no time unit is provided (e.g. '5m'). 'revision' for revision number based retention.
--lease-read 'false'
Set to true to enable lease-based read.
--v2-deprecation '` + string(cconfig.V2_DEPR_DEFAULT) + `'
Phase of v2store deprecation. Allows to opt-in for higher compatibility mode.
Supported values:
Expand Down
1 change: 1 addition & 0 deletions server/etcdserver/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ func raftConfig(cfg config.ServerConfig, id uint64, s *raft.MemoryStorage) *raft
CheckQuorum: true,
PreVote: cfg.PreVote,
Logger: NewRaftLoggerZap(cfg.Logger.Named("raft")),
ReadOnlyOption: cfg.ReadOnlyOption,
}
}

Expand Down

0 comments on commit 91f58d3

Please sign in to comment.