config.go 12 KB

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