config.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcdserver
  14. import (
  15. "fmt"
  16. "log"
  17. "net/http"
  18. "path"
  19. "reflect"
  20. "sort"
  21. "github.com/coreos/etcd/pkg/types"
  22. "github.com/coreos/etcd/raft"
  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. Cluster *Cluster
  36. NewCluster bool
  37. ForceNewCluster bool
  38. Transport *http.Transport
  39. TickMs uint
  40. ElectionTicks int
  41. }
  42. // VerifyBootstrapConfig sanity-checks the initial config and returns an error
  43. // for things that should never happen.
  44. func (c *ServerConfig) VerifyBootstrapConfig() error {
  45. m := c.Cluster.MemberByName(c.Name)
  46. // Make sure the cluster at least contains the local server.
  47. if m == nil {
  48. return fmt.Errorf("couldn't find local name %q in the initial cluster configuration", c.Name)
  49. }
  50. if uint64(m.ID) == raft.None {
  51. return fmt.Errorf("cannot use %x as member id", raft.None)
  52. }
  53. if c.DiscoveryURL == "" && !c.NewCluster {
  54. return fmt.Errorf("initial cluster state unset and no wal or discovery URL found")
  55. }
  56. // No identical IPs in the cluster peer list
  57. urlMap := make(map[string]bool)
  58. for _, m := range c.Cluster.Members() {
  59. for _, url := range m.PeerURLs {
  60. if urlMap[url] {
  61. return fmt.Errorf("duplicate url %v in cluster config", url)
  62. }
  63. urlMap[url] = true
  64. }
  65. }
  66. // Advertised peer URLs must match those in the cluster peer list
  67. apurls := c.PeerURLs.StringSlice()
  68. sort.Strings(apurls)
  69. if !reflect.DeepEqual(apurls, m.PeerURLs) {
  70. return fmt.Errorf("%s has different advertised URLs in the cluster and advertised peer URLs list", c.Name)
  71. }
  72. return nil
  73. }
  74. func (c *ServerConfig) WALDir() string { return path.Join(c.DataDir, "wal") }
  75. func (c *ServerConfig) SnapDir() string { return path.Join(c.DataDir, "snap") }
  76. func (c *ServerConfig) ShouldDiscover() bool { return c.DiscoveryURL != "" }
  77. func (c *ServerConfig) PrintWithInitial() { c.print(true) }
  78. func (c *ServerConfig) Print() { c.print(false) }
  79. func (c *ServerConfig) print(initial bool) {
  80. log.Printf("etcdserver: name = %s", c.Name)
  81. if c.ForceNewCluster {
  82. log.Println("etcdserver: force new cluster")
  83. }
  84. log.Printf("etcdserver: data dir = %s", c.DataDir)
  85. log.Printf("etcdserver: heartbeat = %dms", c.TickMs)
  86. log.Printf("etcdserver: election = %dms", c.ElectionTicks*int(c.TickMs))
  87. log.Printf("etcdserver: snapshot count = %d", c.SnapCount)
  88. if len(c.DiscoveryURL) != 0 {
  89. log.Printf("etcdserver: discovery URL= %s", c.DiscoveryURL)
  90. if len(c.DiscoveryProxy) != 0 {
  91. log.Printf("etcdserver: discovery proxy = %s", c.DiscoveryProxy)
  92. }
  93. }
  94. log.Printf("etcdserver: advertise client URLs = %s", c.ClientURLs)
  95. if initial {
  96. log.Printf("etcdserver: initial advertise peer URLs = %s", c.PeerURLs)
  97. log.Printf("etcdserver: initial cluster = %s", c.Cluster)
  98. }
  99. }