config.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. // BackendBatchInterval is the maximum time before commit the backend transaction.
  50. BackendBatchInterval time.Duration
  51. // BackendBatchLimit is the maximum operations before commit the backend transaction.
  52. BackendBatchLimit int
  53. InitialPeerURLsMap types.URLsMap
  54. InitialClusterToken string
  55. NewCluster bool
  56. PeerTLSInfo transport.TLSInfo
  57. CORS map[string]struct{}
  58. // HostWhitelist lists acceptable hostnames from client requests.
  59. // If server is insecure (no TLS), server only accepts requests
  60. // whose Host header value exists in this white list.
  61. HostWhitelist map[string]struct{}
  62. TickMs uint
  63. ElectionTicks int
  64. // InitialElectionTickAdvance is true, then local member fast-forwards
  65. // election ticks to speed up "initial" leader election trigger. This
  66. // benefits the case of larger election ticks. For instance, cross
  67. // datacenter deployment may require longer election timeout of 10-second.
  68. // If true, local node does not need wait up to 10-second. Instead,
  69. // forwards its election ticks to 8-second, and have only 2-second left
  70. // before leader election.
  71. //
  72. // Major assumptions are that:
  73. // - cluster has no active leader thus advancing ticks enables faster
  74. // leader election, or
  75. // - cluster already has an established leader, and rejoining follower
  76. // is likely to receive heartbeats from the leader after tick advance
  77. // and before election timeout.
  78. //
  79. // However, when network from leader to rejoining follower is congested,
  80. // and the follower does not receive leader heartbeat within left election
  81. // ticks, disruptive election has to happen thus affecting cluster
  82. // availabilities.
  83. //
  84. // Disabling this would slow down initial bootstrap process for cross
  85. // datacenter deployments. Make your own tradeoffs by configuring
  86. // --initial-election-tick-advance at the cost of slow initial bootstrap.
  87. //
  88. // If single-node, it advances ticks regardless.
  89. //
  90. // See https://github.com/etcd-io/etcd/issues/9333 for more detail.
  91. InitialElectionTickAdvance bool
  92. BootstrapTimeout time.Duration
  93. AutoCompactionRetention time.Duration
  94. AutoCompactionMode string
  95. QuotaBackendBytes int64
  96. MaxTxnOps uint
  97. // MaxRequestBytes is the maximum request size to send over raft.
  98. MaxRequestBytes uint
  99. StrictReconfigCheck bool
  100. // ClientCertAuthEnabled is true when cert has been signed by the client CA.
  101. ClientCertAuthEnabled bool
  102. AuthToken string
  103. BcryptCost uint
  104. // InitialCorruptCheck is true to check data corruption on boot
  105. // before serving any peer/client traffic.
  106. InitialCorruptCheck bool
  107. CorruptCheckTime time.Duration
  108. // PreVote is true to enable Raft Pre-Vote.
  109. PreVote bool
  110. // Logger logs server-side operations.
  111. // If not nil, it disables "capnslog" and uses the given logger.
  112. Logger *zap.Logger
  113. // LoggerConfig is server logger configuration for Raft logger.
  114. // Must be either: "LoggerConfig != nil" or "LoggerCore != nil && LoggerWriteSyncer != nil".
  115. LoggerConfig *zap.Config
  116. // LoggerCore is "zapcore.Core" for raft logger.
  117. // Must be either: "LoggerConfig != nil" or "LoggerCore != nil && LoggerWriteSyncer != nil".
  118. LoggerCore zapcore.Core
  119. LoggerWriteSyncer zapcore.WriteSyncer
  120. Debug bool
  121. ForceNewCluster bool
  122. // LeaseCheckpointInterval time.Duration is the wait duration between lease checkpoints.
  123. LeaseCheckpointInterval time.Duration
  124. }
  125. // VerifyBootstrap sanity-checks the initial config for bootstrap case
  126. // and returns an error for things that should never happen.
  127. func (c *ServerConfig) VerifyBootstrap() error {
  128. if err := c.hasLocalMember(); err != nil {
  129. return err
  130. }
  131. if err := c.advertiseMatchesCluster(); err != nil {
  132. return err
  133. }
  134. if checkDuplicateURL(c.InitialPeerURLsMap) {
  135. return fmt.Errorf("initial cluster %s has duplicate url", c.InitialPeerURLsMap)
  136. }
  137. if c.InitialPeerURLsMap.String() == "" && c.DiscoveryURL == "" {
  138. return fmt.Errorf("initial cluster unset and no discovery URL found")
  139. }
  140. return nil
  141. }
  142. // VerifyJoinExisting sanity-checks the initial config for join existing cluster
  143. // case and returns an error for things that should never happen.
  144. func (c *ServerConfig) VerifyJoinExisting() error {
  145. // The member has announced its peer urls to the cluster before starting; no need to
  146. // set the configuration again.
  147. if err := c.hasLocalMember(); err != nil {
  148. return err
  149. }
  150. if checkDuplicateURL(c.InitialPeerURLsMap) {
  151. return fmt.Errorf("initial cluster %s has duplicate url", c.InitialPeerURLsMap)
  152. }
  153. if c.DiscoveryURL != "" {
  154. return fmt.Errorf("discovery URL should not be set when joining existing initial cluster")
  155. }
  156. return nil
  157. }
  158. // hasLocalMember checks that the cluster at least contains the local server.
  159. func (c *ServerConfig) hasLocalMember() error {
  160. if urls := c.InitialPeerURLsMap[c.Name]; urls == nil {
  161. return fmt.Errorf("couldn't find local name %q in the initial cluster configuration", c.Name)
  162. }
  163. return nil
  164. }
  165. // advertiseMatchesCluster confirms peer URLs match those in the cluster peer list.
  166. func (c *ServerConfig) advertiseMatchesCluster() error {
  167. urls, apurls := c.InitialPeerURLsMap[c.Name], c.PeerURLs.StringSlice()
  168. urls.Sort()
  169. sort.Strings(apurls)
  170. ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
  171. defer cancel()
  172. ok, err := netutil.URLStringsEqual(ctx, c.Logger, apurls, urls.StringSlice())
  173. if ok {
  174. return nil
  175. }
  176. initMap, apMap := make(map[string]struct{}), make(map[string]struct{})
  177. for _, url := range c.PeerURLs {
  178. apMap[url.String()] = struct{}{}
  179. }
  180. for _, url := range c.InitialPeerURLsMap[c.Name] {
  181. initMap[url.String()] = struct{}{}
  182. }
  183. missing := []string{}
  184. for url := range initMap {
  185. if _, ok := apMap[url]; !ok {
  186. missing = append(missing, url)
  187. }
  188. }
  189. if len(missing) > 0 {
  190. for i := range missing {
  191. missing[i] = c.Name + "=" + missing[i]
  192. }
  193. mstr := strings.Join(missing, ",")
  194. apStr := strings.Join(apurls, ",")
  195. return fmt.Errorf("--initial-cluster has %s but missing from --initial-advertise-peer-urls=%s (%v)", mstr, apStr, err)
  196. }
  197. for url := range apMap {
  198. if _, ok := initMap[url]; !ok {
  199. missing = append(missing, url)
  200. }
  201. }
  202. if len(missing) > 0 {
  203. mstr := strings.Join(missing, ",")
  204. umap := types.URLsMap(map[string]types.URLs{c.Name: c.PeerURLs})
  205. return fmt.Errorf("--initial-advertise-peer-urls has %s but missing from --initial-cluster=%s", mstr, umap.String())
  206. }
  207. // resolved URLs from "--initial-advertise-peer-urls" and "--initial-cluster" did not match or failed
  208. apStr := strings.Join(apurls, ",")
  209. umap := types.URLsMap(map[string]types.URLs{c.Name: c.PeerURLs})
  210. return fmt.Errorf("failed to resolve %s to match --initial-cluster=%s (%v)", apStr, umap.String(), err)
  211. }
  212. func (c *ServerConfig) MemberDir() string { return filepath.Join(c.DataDir, "member") }
  213. func (c *ServerConfig) WALDir() string {
  214. if c.DedicatedWALDir != "" {
  215. return c.DedicatedWALDir
  216. }
  217. return filepath.Join(c.MemberDir(), "wal")
  218. }
  219. func (c *ServerConfig) SnapDir() string { return filepath.Join(c.MemberDir(), "snap") }
  220. func (c *ServerConfig) ShouldDiscover() bool { return c.DiscoveryURL != "" }
  221. // ReqTimeout returns timeout for request to finish.
  222. func (c *ServerConfig) ReqTimeout() time.Duration {
  223. // 5s for queue waiting, computation and disk IO delay
  224. // + 2 * election timeout for possible leader election
  225. return 5*time.Second + 2*time.Duration(c.ElectionTicks*int(c.TickMs))*time.Millisecond
  226. }
  227. func (c *ServerConfig) electionTimeout() time.Duration {
  228. return time.Duration(c.ElectionTicks*int(c.TickMs)) * time.Millisecond
  229. }
  230. func (c *ServerConfig) peerDialTimeout() time.Duration {
  231. // 1s for queue wait and election timeout
  232. return time.Second + time.Duration(c.ElectionTicks*int(c.TickMs))*time.Millisecond
  233. }
  234. func checkDuplicateURL(urlsmap types.URLsMap) bool {
  235. um := make(map[string]bool)
  236. for _, urls := range urlsmap {
  237. for _, url := range urls {
  238. u := url.String()
  239. if um[u] {
  240. return true
  241. }
  242. um[u] = true
  243. }
  244. }
  245. return false
  246. }
  247. func (c *ServerConfig) bootstrapTimeout() time.Duration {
  248. if c.BootstrapTimeout != 0 {
  249. return c.BootstrapTimeout
  250. }
  251. return time.Second
  252. }
  253. func (c *ServerConfig) backendPath() string { return filepath.Join(c.SnapDir(), "db") }