From 91f58d337f35e2008c58049e357f39c08a77c2ad Mon Sep 17 00:00:00 2001 From: highpon Date: Sat, 30 Sep 2023 17:48:08 +0900 Subject: [PATCH] feat: Add lease read option --- server/config/config.go | 3 +++ server/embed/config.go | 4 ++++ server/embed/etcd.go | 7 +++++++ server/etcdmain/help.go | 2 ++ server/etcdserver/bootstrap.go | 1 + 5 files changed, 17 insertions(+) diff --git a/server/config/config.go b/server/config/config.go index 057f78b6f9f4..06cc2359158d 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -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. @@ -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. diff --git a/server/embed/config.go b/server/embed/config.go index 9107b77e016c..9b750bec4561 100644 --- a/server/embed/config.go +++ b/server/embed/config.go @@ -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 @@ -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.") diff --git a/server/embed/etcd.go b/server/embed/etcd.go index 188d73f4496c..7bcf9452994b 100644 --- a/server/embed/etcd.go +++ b/server/embed/etcd.go @@ -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 ( @@ -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, @@ -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, diff --git a/server/etcdmain/help.go b/server/etcdmain/help.go index 1f139680d3d7..94e684afc7d2 100644 --- a/server/etcdmain/help.go +++ b/server/etcdmain/help.go @@ -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: diff --git a/server/etcdserver/bootstrap.go b/server/etcdserver/bootstrap.go index e416fd079c25..26a2c6bdb1b2 100644 --- a/server/etcdserver/bootstrap.go +++ b/server/etcdserver/bootstrap.go @@ -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, } }