config.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "sort"
  21. "github.com/coreos/etcd/pkg/netutil"
  22. "github.com/coreos/etcd/pkg/types"
  23. "github.com/coreos/etcd/raft"
  24. )
  25. // ServerConfig holds the configuration of etcd as taken from the command line or discovery.
  26. type ServerConfig struct {
  27. Name string
  28. DiscoveryURL string
  29. DiscoveryProxy string
  30. ClientURLs types.URLs
  31. PeerURLs types.URLs
  32. DataDir string
  33. SnapCount uint64
  34. MaxSnapFiles uint
  35. MaxWALFiles uint
  36. Cluster *Cluster
  37. NewCluster bool
  38. ForceNewCluster bool
  39. Transport *http.Transport
  40. TickMs uint
  41. ElectionTicks int
  42. }
  43. // VerifyBootstrapConfig sanity-checks the initial config and returns an error
  44. // for things that should never happen.
  45. func (c *ServerConfig) VerifyBootstrapConfig() error {
  46. m := c.Cluster.MemberByName(c.Name)
  47. // Make sure the cluster at least contains the local server.
  48. if m == nil {
  49. return fmt.Errorf("couldn't find local name %q in the initial cluster configuration", c.Name)
  50. }
  51. if uint64(m.ID) == raft.None {
  52. return fmt.Errorf("cannot use %x as member id", raft.None)
  53. }
  54. if c.DiscoveryURL == "" && !c.NewCluster {
  55. return fmt.Errorf("initial cluster state unset and no wal or discovery URL found")
  56. }
  57. // No identical IPs in the cluster peer list
  58. urlMap := make(map[string]bool)
  59. for _, m := range c.Cluster.Members() {
  60. for _, url := range m.PeerURLs {
  61. if urlMap[url] {
  62. return fmt.Errorf("duplicate url %v in cluster config", url)
  63. }
  64. urlMap[url] = true
  65. }
  66. }
  67. // Advertised peer URLs must match those in the cluster peer list
  68. // TODO: Remove URLStringsEqual after improvement of using hostnames #2150 #2123
  69. apurls := c.PeerURLs.StringSlice()
  70. sort.Strings(apurls)
  71. if !netutil.URLStringsEqual(apurls, m.PeerURLs) {
  72. return fmt.Errorf("%s has different advertised URLs in the cluster and advertised peer URLs list", c.Name)
  73. }
  74. return nil
  75. }
  76. func (c *ServerConfig) MemberDir() string { return path.Join(c.DataDir, "member") }
  77. func (c *ServerConfig) WALDir() string { return path.Join(c.MemberDir(), "wal") }
  78. func (c *ServerConfig) SnapDir() string { return path.Join(c.MemberDir(), "snap") }
  79. func (c *ServerConfig) ShouldDiscover() bool { return c.DiscoveryURL != "" }
  80. func (c *ServerConfig) PrintWithInitial() { c.print(true) }
  81. func (c *ServerConfig) Print() { c.print(false) }
  82. func (c *ServerConfig) print(initial bool) {
  83. log.Printf("etcdserver: name = %s", c.Name)
  84. if c.ForceNewCluster {
  85. log.Println("etcdserver: force new cluster")
  86. }
  87. log.Printf("etcdserver: data dir = %s", c.DataDir)
  88. log.Printf("etcdserver: member dir = %s", c.MemberDir())
  89. log.Printf("etcdserver: heartbeat = %dms", c.TickMs)
  90. log.Printf("etcdserver: election = %dms", c.ElectionTicks*int(c.TickMs))
  91. log.Printf("etcdserver: snapshot count = %d", c.SnapCount)
  92. if len(c.DiscoveryURL) != 0 {
  93. log.Printf("etcdserver: discovery URL= %s", c.DiscoveryURL)
  94. if len(c.DiscoveryProxy) != 0 {
  95. log.Printf("etcdserver: discovery proxy = %s", c.DiscoveryProxy)
  96. }
  97. }
  98. log.Printf("etcdserver: advertise client URLs = %s", c.ClientURLs)
  99. if initial {
  100. log.Printf("etcdserver: initial advertise peer URLs = %s", c.PeerURLs)
  101. log.Printf("etcdserver: initial cluster = %s", c.Cluster)
  102. }
  103. }