config.go 5.3 KB

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