config.go 4.7 KB

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