config.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2015 The etcd Authors
  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. "path"
  18. "sort"
  19. "strings"
  20. "time"
  21. "github.com/coreos/etcd/pkg/netutil"
  22. "github.com/coreos/etcd/pkg/transport"
  23. "github.com/coreos/etcd/pkg/types"
  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. // DedicatedWALDir config will make the etcd to write the WAL to the WALDir
  34. // rather than the dataDir/member/wal.
  35. DedicatedWALDir string
  36. SnapCount uint64
  37. MaxSnapFiles uint
  38. MaxWALFiles uint
  39. InitialPeerURLsMap types.URLsMap
  40. InitialClusterToken string
  41. NewCluster bool
  42. ForceNewCluster bool
  43. PeerTLSInfo transport.TLSInfo
  44. TickMs uint
  45. ElectionTicks int
  46. BootstrapTimeout time.Duration
  47. AutoCompactionRetention int
  48. QuotaBackendBytes int64
  49. StrictReconfigCheck bool
  50. EnablePprof bool
  51. // ClientCertAuthEnabled is true when cert has been signed by the client CA.
  52. ClientCertAuthEnabled bool
  53. }
  54. // VerifyBootstrap sanity-checks the initial config for bootstrap case
  55. // and returns an error for things that should never happen.
  56. func (c *ServerConfig) VerifyBootstrap() error {
  57. if err := c.verifyLocalMember(true); err != nil {
  58. return err
  59. }
  60. if checkDuplicateURL(c.InitialPeerURLsMap) {
  61. return fmt.Errorf("initial cluster %s has duplicate url", c.InitialPeerURLsMap)
  62. }
  63. if c.InitialPeerURLsMap.String() == "" && c.DiscoveryURL == "" {
  64. return fmt.Errorf("initial cluster unset and no discovery URL found")
  65. }
  66. return nil
  67. }
  68. // VerifyJoinExisting sanity-checks the initial config for join existing cluster
  69. // case and returns an error for things that should never happen.
  70. func (c *ServerConfig) VerifyJoinExisting() error {
  71. // no need for strict checking since the member have announced its
  72. // peer urls to the cluster before starting and do not have to set
  73. // it in the configuration again.
  74. if err := c.verifyLocalMember(false); err != nil {
  75. return err
  76. }
  77. if checkDuplicateURL(c.InitialPeerURLsMap) {
  78. return fmt.Errorf("initial cluster %s has duplicate url", c.InitialPeerURLsMap)
  79. }
  80. if c.DiscoveryURL != "" {
  81. return fmt.Errorf("discovery URL should not be set when joining existing initial cluster")
  82. }
  83. return nil
  84. }
  85. // verifyLocalMember verifies the configured member is in configured
  86. // cluster. If strict is set, it also verifies the configured member
  87. // has the same peer urls as configured advertised peer urls.
  88. func (c *ServerConfig) verifyLocalMember(strict bool) error {
  89. urls := c.InitialPeerURLsMap[c.Name]
  90. // Make sure the cluster at least contains the local server.
  91. if urls == nil {
  92. return fmt.Errorf("couldn't find local name %q in the initial cluster configuration", c.Name)
  93. }
  94. // Advertised peer URLs must match those in the cluster peer list
  95. apurls := c.PeerURLs.StringSlice()
  96. sort.Strings(apurls)
  97. urls.Sort()
  98. if strict {
  99. if !netutil.URLStringsEqual(apurls, urls.StringSlice()) {
  100. umap := map[string]types.URLs{c.Name: c.PeerURLs}
  101. return fmt.Errorf("--initial-cluster must include %s given --initial-advertise-peer-urls=%s", types.URLsMap(umap).String(), strings.Join(apurls, ","))
  102. }
  103. }
  104. return nil
  105. }
  106. func (c *ServerConfig) MemberDir() string { return path.Join(c.DataDir, "member") }
  107. func (c *ServerConfig) WALDir() string {
  108. if c.DedicatedWALDir != "" {
  109. return c.DedicatedWALDir
  110. }
  111. return path.Join(c.MemberDir(), "wal")
  112. }
  113. func (c *ServerConfig) SnapDir() string { return path.Join(c.MemberDir(), "snap") }
  114. func (c *ServerConfig) ShouldDiscover() bool { return c.DiscoveryURL != "" }
  115. // ReqTimeout returns timeout for request to finish.
  116. func (c *ServerConfig) ReqTimeout() time.Duration {
  117. // 5s for queue waiting, computation and disk IO delay
  118. // + 2 * election timeout for possible leader election
  119. return 5*time.Second + 2*time.Duration(c.ElectionTicks)*time.Duration(c.TickMs)*time.Millisecond
  120. }
  121. func (c *ServerConfig) electionTimeout() time.Duration {
  122. return time.Duration(c.ElectionTicks) * time.Duration(c.TickMs) * time.Millisecond
  123. }
  124. func (c *ServerConfig) peerDialTimeout() time.Duration {
  125. // 1s for queue wait and system delay
  126. // + one RTT, which is smaller than 1/5 election timeout
  127. return time.Second + time.Duration(c.ElectionTicks)*time.Duration(c.TickMs)*time.Millisecond/5
  128. }
  129. func (c *ServerConfig) PrintWithInitial() { c.print(true) }
  130. func (c *ServerConfig) Print() { c.print(false) }
  131. func (c *ServerConfig) print(initial bool) {
  132. plog.Infof("name = %s", c.Name)
  133. if c.ForceNewCluster {
  134. plog.Infof("force new cluster")
  135. }
  136. plog.Infof("data dir = %s", c.DataDir)
  137. plog.Infof("member dir = %s", c.MemberDir())
  138. if c.DedicatedWALDir != "" {
  139. plog.Infof("dedicated WAL dir = %s", c.DedicatedWALDir)
  140. }
  141. plog.Infof("heartbeat = %dms", c.TickMs)
  142. plog.Infof("election = %dms", c.ElectionTicks*int(c.TickMs))
  143. plog.Infof("snapshot count = %d", c.SnapCount)
  144. if len(c.DiscoveryURL) != 0 {
  145. plog.Infof("discovery URL= %s", c.DiscoveryURL)
  146. if len(c.DiscoveryProxy) != 0 {
  147. plog.Infof("discovery proxy = %s", c.DiscoveryProxy)
  148. }
  149. }
  150. plog.Infof("advertise client URLs = %s", c.ClientURLs)
  151. if initial {
  152. plog.Infof("initial advertise peer URLs = %s", c.PeerURLs)
  153. plog.Infof("initial cluster = %s", c.InitialPeerURLsMap)
  154. }
  155. }
  156. func checkDuplicateURL(urlsmap types.URLsMap) bool {
  157. um := make(map[string]bool)
  158. for _, urls := range urlsmap {
  159. for _, url := range urls {
  160. u := url.String()
  161. if um[u] {
  162. return true
  163. }
  164. um[u] = true
  165. }
  166. }
  167. return false
  168. }
  169. func (c *ServerConfig) bootstrapTimeout() time.Duration {
  170. if c.BootstrapTimeout != 0 {
  171. return c.BootstrapTimeout
  172. }
  173. return time.Second
  174. }