config.go 9.0 KB

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