config.go 6.3 KB

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