config.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 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(); err != nil {
  47. return err
  48. }
  49. if err := c.Cluster.Validate(); err != nil {
  50. return err
  51. }
  52. if c.Cluster.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. if err := c.verifyLocalMember(); err != nil {
  61. return err
  62. }
  63. if err := c.Cluster.Validate(); err != nil {
  64. return err
  65. }
  66. if c.DiscoveryURL != "" {
  67. return fmt.Errorf("discovery URL should not be set when joining existing initial cluster")
  68. }
  69. return nil
  70. }
  71. // verifyLocalMember verifies that the local member is valid and is listed
  72. // in the cluster correctly.
  73. func (c *ServerConfig) verifyLocalMember() error {
  74. m := c.Cluster.MemberByName(c.Name)
  75. // Make sure the cluster at least contains the local server.
  76. if m == nil {
  77. return fmt.Errorf("couldn't find local name %q in the initial cluster configuration", c.Name)
  78. }
  79. if uint64(m.ID) == raft.None {
  80. return fmt.Errorf("cannot use %x as member id", raft.None)
  81. }
  82. // Advertised peer URLs must match those in the cluster peer list
  83. // TODO: Remove URLStringsEqual after improvement of using hostnames #2150 #2123
  84. apurls := c.PeerURLs.StringSlice()
  85. sort.Strings(apurls)
  86. if !netutil.URLStringsEqual(apurls, m.PeerURLs) {
  87. return fmt.Errorf("%s has different advertised URLs in the cluster and advertised peer URLs list", c.Name)
  88. }
  89. return nil
  90. }
  91. func (c *ServerConfig) MemberDir() string { return path.Join(c.DataDir, "member") }
  92. func (c *ServerConfig) WALDir() string { return path.Join(c.MemberDir(), "wal") }
  93. func (c *ServerConfig) SnapDir() string { return path.Join(c.MemberDir(), "snap") }
  94. func (c *ServerConfig) ShouldDiscover() bool { return c.DiscoveryURL != "" }
  95. func (c *ServerConfig) PrintWithInitial() { c.print(true) }
  96. func (c *ServerConfig) Print() { c.print(false) }
  97. func (c *ServerConfig) print(initial bool) {
  98. log.Printf("etcdserver: name = %s", c.Name)
  99. if c.ForceNewCluster {
  100. log.Println("etcdserver: force new cluster")
  101. }
  102. log.Printf("etcdserver: data dir = %s", c.DataDir)
  103. log.Printf("etcdserver: member dir = %s", c.MemberDir())
  104. log.Printf("etcdserver: heartbeat = %dms", c.TickMs)
  105. log.Printf("etcdserver: election = %dms", c.ElectionTicks*int(c.TickMs))
  106. log.Printf("etcdserver: snapshot count = %d", c.SnapCount)
  107. if len(c.DiscoveryURL) != 0 {
  108. log.Printf("etcdserver: discovery URL= %s", c.DiscoveryURL)
  109. if len(c.DiscoveryProxy) != 0 {
  110. log.Printf("etcdserver: discovery proxy = %s", c.DiscoveryProxy)
  111. }
  112. }
  113. log.Printf("etcdserver: advertise client URLs = %s", c.ClientURLs)
  114. if initial {
  115. log.Printf("etcdserver: initial advertise peer URLs = %s", c.PeerURLs)
  116. log.Printf("etcdserver: initial cluster = %s", c.Cluster)
  117. }
  118. }