config.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etcdserver
  15. import (
  16. "context"
  17. "fmt"
  18. "path/filepath"
  19. "sort"
  20. "strings"
  21. "time"
  22. "go.etcd.io/etcd/pkg/netutil"
  23. "go.etcd.io/etcd/pkg/transport"
  24. "go.etcd.io/etcd/pkg/types"
  25. "go.uber.org/zap"
  26. "go.uber.org/zap/zapcore"
  27. )
  28. // ServerConfig holds the configuration of etcd as taken from the command line or discovery.
  29. type ServerConfig struct {
  30. Name string
  31. DiscoveryURL string
  32. DiscoveryProxy string
  33. ClientURLs types.URLs
  34. PeerURLs types.URLs
  35. DataDir string
  36. // DedicatedWALDir config will make the etcd to write the WAL to the WALDir
  37. // rather than the dataDir/member/wal.
  38. DedicatedWALDir string
  39. SnapshotCount uint64
  40. // SnapshotCatchUpEntries is the number of entries for a slow follower
  41. // to catch-up after compacting the raft storage entries.
  42. // We expect the follower has a millisecond level latency with the leader.
  43. // The max throughput is around 10K. Keep a 5K entries is enough for helping
  44. // follower to catch up.
  45. // WARNING: only change this for tests. Always use "DefaultSnapshotCatchUpEntries"
  46. SnapshotCatchUpEntries uint64
  47. MaxSnapFiles uint
  48. MaxWALFiles uint
  49. InitialPeerURLsMap types.URLsMap
  50. InitialClusterToken string
  51. NewCluster bool
  52. PeerTLSInfo transport.TLSInfo
  53. CORS map[string]struct{}
  54. // HostWhitelist lists acceptable hostnames from client requests.
  55. // If server is insecure (no TLS), server only accepts requests
  56. // whose Host header value exists in this white list.
  57. HostWhitelist map[string]struct{}
  58. TickMs uint
  59. ElectionTicks int
  60. // InitialElectionTickAdvance is true, then local member fast-forwards
  61. // election ticks to speed up "initial" leader election trigger. This
  62. // benefits the case of larger election ticks. For instance, cross
  63. // datacenter deployment may require longer election timeout of 10-second.
  64. // If true, local node does not need wait up to 10-second. Instead,
  65. // forwards its election ticks to 8-second, and have only 2-second left
  66. // before leader election.
  67. //
  68. // Major assumptions are that:
  69. // - cluster has no active leader thus advancing ticks enables faster
  70. // leader election, or
  71. // - cluster already has an established leader, and rejoining follower
  72. // is likely to receive heartbeats from the leader after tick advance
  73. // and before election timeout.
  74. //
  75. // However, when network from leader to rejoining follower is congested,
  76. // and the follower does not receive leader heartbeat within left election
  77. // ticks, disruptive election has to happen thus affecting cluster
  78. // availabilities.
  79. //
  80. // Disabling this would slow down initial bootstrap process for cross
  81. // datacenter deployments. Make your own tradeoffs by configuring
  82. // --initial-election-tick-advance at the cost of slow initial bootstrap.
  83. //
  84. // If single-node, it advances ticks regardless.
  85. //
  86. // See https://go.etcd.io/etcd/issues/9333 for more detail.
  87. InitialElectionTickAdvance bool
  88. BootstrapTimeout time.Duration
  89. AutoCompactionRetention time.Duration
  90. AutoCompactionMode string
  91. QuotaBackendBytes int64
  92. MaxTxnOps uint
  93. // MaxRequestBytes is the maximum request size to send over raft.
  94. MaxRequestBytes uint
  95. StrictReconfigCheck bool
  96. // ClientCertAuthEnabled is true when cert has been signed by the client CA.
  97. ClientCertAuthEnabled bool
  98. AuthToken string
  99. BcryptCost uint
  100. // InitialCorruptCheck is true to check data corruption on boot
  101. // before serving any peer/client traffic.
  102. InitialCorruptCheck bool
  103. CorruptCheckTime time.Duration
  104. // PreVote is true to enable Raft Pre-Vote.
  105. PreVote bool
  106. // Logger logs server-side operations.
  107. // If not nil, it disables "capnslog" and uses the given logger.
  108. Logger *zap.Logger
  109. // LoggerConfig is server logger configuration for Raft logger.
  110. // Must be either: "LoggerConfig != nil" or "LoggerCore != nil && LoggerWriteSyncer != nil".
  111. LoggerConfig *zap.Config
  112. // LoggerCore is "zapcore.Core" for raft logger.
  113. // Must be either: "LoggerConfig != nil" or "LoggerCore != nil && LoggerWriteSyncer != nil".
  114. LoggerCore zapcore.Core
  115. LoggerWriteSyncer zapcore.WriteSyncer
  116. Debug bool
  117. ForceNewCluster bool
  118. // LeaseCheckpointInterval time.Duration is the wait duration between lease checkpoints.
  119. LeaseCheckpointInterval time.Duration
  120. }
  121. // VerifyBootstrap sanity-checks the initial config for bootstrap case
  122. // and returns an error for things that should never happen.
  123. func (c *ServerConfig) VerifyBootstrap() error {
  124. if err := c.hasLocalMember(); err != nil {
  125. return err
  126. }
  127. if err := c.advertiseMatchesCluster(); err != nil {
  128. return err
  129. }
  130. if checkDuplicateURL(c.InitialPeerURLsMap) {
  131. return fmt.Errorf("initial cluster %s has duplicate url", c.InitialPeerURLsMap)
  132. }
  133. if c.InitialPeerURLsMap.String() == "" && c.DiscoveryURL == "" {
  134. return fmt.Errorf("initial cluster unset and no discovery URL found")
  135. }
  136. return nil
  137. }
  138. // VerifyJoinExisting sanity-checks the initial config for join existing cluster
  139. // case and returns an error for things that should never happen.
  140. func (c *ServerConfig) VerifyJoinExisting() error {
  141. // The member has announced its peer urls to the cluster before starting; no need to
  142. // set the configuration again.
  143. if err := c.hasLocalMember(); err != nil {
  144. return err
  145. }
  146. if checkDuplicateURL(c.InitialPeerURLsMap) {
  147. return fmt.Errorf("initial cluster %s has duplicate url", c.InitialPeerURLsMap)
  148. }
  149. if c.DiscoveryURL != "" {
  150. return fmt.Errorf("discovery URL should not be set when joining existing initial cluster")
  151. }
  152. return nil
  153. }
  154. // hasLocalMember checks that the cluster at least contains the local server.
  155. func (c *ServerConfig) hasLocalMember() error {
  156. if urls := c.InitialPeerURLsMap[c.Name]; urls == nil {
  157. return fmt.Errorf("couldn't find local name %q in the initial cluster configuration", c.Name)
  158. }
  159. return nil
  160. }
  161. // advertiseMatchesCluster confirms peer URLs match those in the cluster peer list.
  162. func (c *ServerConfig) advertiseMatchesCluster() error {
  163. urls, apurls := c.InitialPeerURLsMap[c.Name], c.PeerURLs.StringSlice()
  164. urls.Sort()
  165. sort.Strings(apurls)
  166. ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
  167. defer cancel()
  168. ok, err := netutil.URLStringsEqual(ctx, c.Logger, apurls, urls.StringSlice())
  169. if ok {
  170. return nil
  171. }
  172. initMap, apMap := make(map[string]struct{}), make(map[string]struct{})
  173. for _, url := range c.PeerURLs {
  174. apMap[url.String()] = struct{}{}
  175. }
  176. for _, url := range c.InitialPeerURLsMap[c.Name] {
  177. initMap[url.String()] = struct{}{}
  178. }
  179. missing := []string{}
  180. for url := range initMap {
  181. if _, ok := apMap[url]; !ok {
  182. missing = append(missing, url)
  183. }
  184. }
  185. if len(missing) > 0 {
  186. for i := range missing {
  187. missing[i] = c.Name + "=" + missing[i]
  188. }
  189. mstr := strings.Join(missing, ",")
  190. apStr := strings.Join(apurls, ",")
  191. return fmt.Errorf("--initial-cluster has %s but missing from --initial-advertise-peer-urls=%s (%v)", mstr, apStr, err)
  192. }
  193. for url := range apMap {
  194. if _, ok := initMap[url]; !ok {
  195. missing = append(missing, url)
  196. }
  197. }
  198. if len(missing) > 0 {
  199. mstr := strings.Join(missing, ",")
  200. umap := types.URLsMap(map[string]types.URLs{c.Name: c.PeerURLs})
  201. return fmt.Errorf("--initial-advertise-peer-urls has %s but missing from --initial-cluster=%s", mstr, umap.String())
  202. }
  203. // resolved URLs from "--initial-advertise-peer-urls" and "--initial-cluster" did not match or failed
  204. apStr := strings.Join(apurls, ",")
  205. umap := types.URLsMap(map[string]types.URLs{c.Name: c.PeerURLs})
  206. return fmt.Errorf("failed to resolve %s to match --initial-cluster=%s (%v)", apStr, umap.String(), err)
  207. }
  208. func (c *ServerConfig) MemberDir() string { return filepath.Join(c.DataDir, "member") }
  209. func (c *ServerConfig) WALDir() string {
  210. if c.DedicatedWALDir != "" {
  211. return c.DedicatedWALDir
  212. }
  213. return filepath.Join(c.MemberDir(), "wal")
  214. }
  215. func (c *ServerConfig) SnapDir() string { return filepath.Join(c.MemberDir(), "snap") }
  216. func (c *ServerConfig) ShouldDiscover() bool { return c.DiscoveryURL != "" }
  217. // ReqTimeout returns timeout for request to finish.
  218. func (c *ServerConfig) ReqTimeout() time.Duration {
  219. // 5s for queue waiting, computation and disk IO delay
  220. // + 2 * election timeout for possible leader election
  221. return 5*time.Second + 2*time.Duration(c.ElectionTicks*int(c.TickMs))*time.Millisecond
  222. }
  223. func (c *ServerConfig) electionTimeout() time.Duration {
  224. return time.Duration(c.ElectionTicks*int(c.TickMs)) * time.Millisecond
  225. }
  226. func (c *ServerConfig) peerDialTimeout() time.Duration {
  227. // 1s for queue wait and election timeout
  228. return time.Second + time.Duration(c.ElectionTicks*int(c.TickMs))*time.Millisecond
  229. }
  230. func checkDuplicateURL(urlsmap types.URLsMap) bool {
  231. um := make(map[string]bool)
  232. for _, urls := range urlsmap {
  233. for _, url := range urls {
  234. u := url.String()
  235. if um[u] {
  236. return true
  237. }
  238. um[u] = true
  239. }
  240. }
  241. return false
  242. }
  243. func (c *ServerConfig) bootstrapTimeout() time.Duration {
  244. if c.BootstrapTimeout != 0 {
  245. return c.BootstrapTimeout
  246. }
  247. return time.Second
  248. }
  249. func (c *ServerConfig) backendPath() string { return filepath.Join(c.SnapDir(), "db") }