config.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2015 CoreOS, Inc.
  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. "fmt"
  17. "net/http"
  18. "path"
  19. "sort"
  20. "time"
  21. "github.com/coreos/etcd/pkg/netutil"
  22. "github.com/coreos/etcd/pkg/types"
  23. )
  24. // ServerConfig holds the configuration of etcd as taken from the command line or discovery.
  25. type ServerConfig struct {
  26. Name string
  27. DiscoveryURL string
  28. DiscoveryProxy string
  29. ClientURLs types.URLs
  30. PeerURLs types.URLs
  31. DataDir string
  32. // DedicatedWALDir config will make the etcd to write the WAL to the WALDir
  33. // rather than the dataDir/member/wal.
  34. DedicatedWALDir string
  35. SnapCount uint64
  36. MaxSnapFiles uint
  37. MaxWALFiles uint
  38. InitialPeerURLsMap types.URLsMap
  39. InitialClusterToken string
  40. NewCluster bool
  41. ForceNewCluster bool
  42. Transport *http.Transport
  43. TickMs uint
  44. ElectionTicks int
  45. V3demo bool
  46. StrictReconfigCheck bool
  47. }
  48. // VerifyBootstrapConfig sanity-checks the initial config for bootstrap case
  49. // and returns an error for things that should never happen.
  50. func (c *ServerConfig) VerifyBootstrap() error {
  51. if err := c.verifyLocalMember(true); err != nil {
  52. return err
  53. }
  54. if checkDuplicateURL(c.InitialPeerURLsMap) {
  55. return fmt.Errorf("initial cluster %s has duplicate url", c.InitialPeerURLsMap)
  56. }
  57. if c.InitialPeerURLsMap.String() == "" && c.DiscoveryURL == "" {
  58. return fmt.Errorf("initial cluster unset and no discovery URL found")
  59. }
  60. return nil
  61. }
  62. // VerifyJoinExisting sanity-checks the initial config for join existing cluster
  63. // case and returns an error for things that should never happen.
  64. func (c *ServerConfig) VerifyJoinExisting() error {
  65. // no need for strict checking since the member have announced its
  66. // peer urls to the cluster before starting and do not have to set
  67. // it in the configuration again.
  68. if err := c.verifyLocalMember(false); err != nil {
  69. return err
  70. }
  71. if checkDuplicateURL(c.InitialPeerURLsMap) {
  72. return fmt.Errorf("initial cluster %s has duplicate url", c.InitialPeerURLsMap)
  73. }
  74. if c.DiscoveryURL != "" {
  75. return fmt.Errorf("discovery URL should not be set when joining existing initial cluster")
  76. }
  77. return nil
  78. }
  79. // verifyLocalMember verifies the configured member is in configured
  80. // cluster. If strict is set, it also verifies the configured member
  81. // has the same peer urls as configured advertised peer urls.
  82. func (c *ServerConfig) verifyLocalMember(strict bool) error {
  83. urls := c.InitialPeerURLsMap[c.Name]
  84. // Make sure the cluster at least contains the local server.
  85. if urls == nil {
  86. return fmt.Errorf("couldn't find local name %q in the initial cluster configuration", c.Name)
  87. }
  88. // Advertised peer URLs must match those in the cluster peer list
  89. apurls := c.PeerURLs.StringSlice()
  90. sort.Strings(apurls)
  91. urls.Sort()
  92. if strict {
  93. if !netutil.URLStringsEqual(apurls, urls.StringSlice()) {
  94. return fmt.Errorf("--initial-cluster must include %s=%s given --initial-advertise-peer-urls=%s", c.Name, apurls, apurls)
  95. }
  96. }
  97. return nil
  98. }
  99. func (c *ServerConfig) MemberDir() string { return path.Join(c.DataDir, "member") }
  100. func (c *ServerConfig) WALDir() string {
  101. if c.DedicatedWALDir != "" {
  102. return c.DedicatedWALDir
  103. }
  104. return path.Join(c.MemberDir(), "wal")
  105. }
  106. func (c *ServerConfig) SnapDir() string { return path.Join(c.MemberDir(), "snap") }
  107. func (c *ServerConfig) StorageDir() string { return path.Join(c.MemberDir(), "storage") }
  108. func (c *ServerConfig) ShouldDiscover() bool { return c.DiscoveryURL != "" }
  109. // ReqTimeout returns timeout for request to finish.
  110. func (c *ServerConfig) ReqTimeout() time.Duration {
  111. // 5s for queue waiting, computation and disk IO delay
  112. // + 2 * election timeout for possible leader election
  113. return 5*time.Second + 2*time.Duration(c.ElectionTicks)*time.Duration(c.TickMs)*time.Millisecond
  114. }
  115. func (c *ServerConfig) PrintWithInitial() { c.print(true) }
  116. func (c *ServerConfig) Print() { c.print(false) }
  117. func (c *ServerConfig) print(initial bool) {
  118. plog.Infof("name = %s", c.Name)
  119. if c.ForceNewCluster {
  120. plog.Infof("force new cluster")
  121. }
  122. plog.Infof("data dir = %s", c.DataDir)
  123. plog.Infof("member dir = %s", c.MemberDir())
  124. if c.DedicatedWALDir != "" {
  125. plog.Infof("dedicated WAL dir = %s", c.DedicatedWALDir)
  126. }
  127. plog.Infof("heartbeat = %dms", c.TickMs)
  128. plog.Infof("election = %dms", c.ElectionTicks*int(c.TickMs))
  129. plog.Infof("snapshot count = %d", c.SnapCount)
  130. if len(c.DiscoveryURL) != 0 {
  131. plog.Infof("discovery URL= %s", c.DiscoveryURL)
  132. if len(c.DiscoveryProxy) != 0 {
  133. plog.Infof("discovery proxy = %s", c.DiscoveryProxy)
  134. }
  135. }
  136. plog.Infof("advertise client URLs = %s", c.ClientURLs)
  137. if initial {
  138. plog.Infof("initial advertise peer URLs = %s", c.PeerURLs)
  139. plog.Infof("initial cluster = %s", c.InitialPeerURLsMap)
  140. }
  141. }
  142. func checkDuplicateURL(urlsmap types.URLsMap) bool {
  143. um := make(map[string]bool)
  144. for _, urls := range urlsmap {
  145. for _, url := range urls {
  146. u := url.String()
  147. if um[u] {
  148. return true
  149. }
  150. um[u] = true
  151. }
  152. }
  153. return false
  154. }