config.go 4.8 KB

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