config.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. "context"
  17. "fmt"
  18. "path/filepath"
  19. "sort"
  20. "strings"
  21. "time"
  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 time.Duration
  49. AutoCompactionMode string
  50. QuotaBackendBytes int64
  51. MaxTxnOps uint
  52. // MaxRequestBytes is the maximum request size to send over raft.
  53. MaxRequestBytes uint
  54. StrictReconfigCheck bool
  55. // ClientCertAuthEnabled is true when cert has been signed by the client CA.
  56. ClientCertAuthEnabled bool
  57. AuthToken string
  58. CorruptCheckTime time.Duration
  59. }
  60. // VerifyBootstrap sanity-checks the initial config for bootstrap case
  61. // and returns an error for things that should never happen.
  62. func (c *ServerConfig) VerifyBootstrap() error {
  63. if err := c.hasLocalMember(); err != nil {
  64. return err
  65. }
  66. if err := c.advertiseMatchesCluster(); err != nil {
  67. return err
  68. }
  69. if checkDuplicateURL(c.InitialPeerURLsMap) {
  70. return fmt.Errorf("initial cluster %s has duplicate url", c.InitialPeerURLsMap)
  71. }
  72. if c.InitialPeerURLsMap.String() == "" && c.DiscoveryURL == "" {
  73. return fmt.Errorf("initial cluster unset and no discovery URL found")
  74. }
  75. return nil
  76. }
  77. // VerifyJoinExisting sanity-checks the initial config for join existing cluster
  78. // case and returns an error for things that should never happen.
  79. func (c *ServerConfig) VerifyJoinExisting() error {
  80. // The member has announced its peer urls to the cluster before starting; no need to
  81. // set the configuration again.
  82. if err := c.hasLocalMember(); err != nil {
  83. return err
  84. }
  85. if checkDuplicateURL(c.InitialPeerURLsMap) {
  86. return fmt.Errorf("initial cluster %s has duplicate url", c.InitialPeerURLsMap)
  87. }
  88. if c.DiscoveryURL != "" {
  89. return fmt.Errorf("discovery URL should not be set when joining existing initial cluster")
  90. }
  91. return nil
  92. }
  93. // hasLocalMember checks that the cluster at least contains the local server.
  94. func (c *ServerConfig) hasLocalMember() error {
  95. if urls := c.InitialPeerURLsMap[c.Name]; urls == nil {
  96. return fmt.Errorf("couldn't find local name %q in the initial cluster configuration", c.Name)
  97. }
  98. return nil
  99. }
  100. // advertiseMatchesCluster confirms peer URLs match those in the cluster peer list.
  101. func (c *ServerConfig) advertiseMatchesCluster() error {
  102. urls, apurls := c.InitialPeerURLsMap[c.Name], c.PeerURLs.StringSlice()
  103. urls.Sort()
  104. sort.Strings(apurls)
  105. ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
  106. defer cancel()
  107. if netutil.URLStringsEqual(ctx, apurls, urls.StringSlice()) {
  108. return nil
  109. }
  110. initMap, apMap := make(map[string]struct{}), make(map[string]struct{})
  111. for _, url := range c.PeerURLs {
  112. apMap[url.String()] = struct{}{}
  113. }
  114. for _, url := range c.InitialPeerURLsMap[c.Name] {
  115. initMap[url.String()] = struct{}{}
  116. }
  117. missing := []string{}
  118. for url := range initMap {
  119. if _, ok := apMap[url]; !ok {
  120. missing = append(missing, url)
  121. }
  122. }
  123. if len(missing) > 0 {
  124. for i := range missing {
  125. missing[i] = c.Name + "=" + missing[i]
  126. }
  127. mstr := strings.Join(missing, ",")
  128. apStr := strings.Join(apurls, ",")
  129. return fmt.Errorf("--initial-cluster has %s but missing from --initial-advertise-peer-urls=%s ", mstr, apStr)
  130. }
  131. for url := range apMap {
  132. if _, ok := initMap[url]; !ok {
  133. missing = append(missing, url)
  134. }
  135. }
  136. mstr := strings.Join(missing, ",")
  137. umap := types.URLsMap(map[string]types.URLs{c.Name: c.PeerURLs})
  138. return fmt.Errorf("--initial-advertise-peer-urls has %s but missing from --initial-cluster=%s", mstr, umap.String())
  139. }
  140. func (c *ServerConfig) MemberDir() string { return filepath.Join(c.DataDir, "member") }
  141. func (c *ServerConfig) WALDir() string {
  142. if c.DedicatedWALDir != "" {
  143. return c.DedicatedWALDir
  144. }
  145. return filepath.Join(c.MemberDir(), "wal")
  146. }
  147. func (c *ServerConfig) SnapDir() string { return filepath.Join(c.MemberDir(), "snap") }
  148. func (c *ServerConfig) ShouldDiscover() bool { return c.DiscoveryURL != "" }
  149. // ReqTimeout returns timeout for request to finish.
  150. func (c *ServerConfig) ReqTimeout() time.Duration {
  151. // 5s for queue waiting, computation and disk IO delay
  152. // + 2 * election timeout for possible leader election
  153. return 5*time.Second + 2*time.Duration(c.ElectionTicks*int(c.TickMs))*time.Millisecond
  154. }
  155. func (c *ServerConfig) electionTimeout() time.Duration {
  156. return time.Duration(c.ElectionTicks*int(c.TickMs)) * time.Millisecond
  157. }
  158. func (c *ServerConfig) peerDialTimeout() time.Duration {
  159. // 1s for queue wait and election timeout
  160. return time.Second + time.Duration(c.ElectionTicks*int(c.TickMs))*time.Millisecond
  161. }
  162. func (c *ServerConfig) PrintWithInitial() { c.print(true) }
  163. func (c *ServerConfig) Print() { c.print(false) }
  164. func (c *ServerConfig) print(initial bool) {
  165. plog.Infof("name = %s", c.Name)
  166. if c.ForceNewCluster {
  167. plog.Infof("force new cluster")
  168. }
  169. plog.Infof("data dir = %s", c.DataDir)
  170. plog.Infof("member dir = %s", c.MemberDir())
  171. if c.DedicatedWALDir != "" {
  172. plog.Infof("dedicated WAL dir = %s", c.DedicatedWALDir)
  173. }
  174. plog.Infof("heartbeat = %dms", c.TickMs)
  175. plog.Infof("election = %dms", c.ElectionTicks*int(c.TickMs))
  176. plog.Infof("snapshot count = %d", c.SnapCount)
  177. if len(c.DiscoveryURL) != 0 {
  178. plog.Infof("discovery URL= %s", c.DiscoveryURL)
  179. if len(c.DiscoveryProxy) != 0 {
  180. plog.Infof("discovery proxy = %s", c.DiscoveryProxy)
  181. }
  182. }
  183. plog.Infof("advertise client URLs = %s", c.ClientURLs)
  184. if initial {
  185. plog.Infof("initial advertise peer URLs = %s", c.PeerURLs)
  186. plog.Infof("initial cluster = %s", c.InitialPeerURLsMap)
  187. }
  188. }
  189. func checkDuplicateURL(urlsmap types.URLsMap) bool {
  190. um := make(map[string]bool)
  191. for _, urls := range urlsmap {
  192. for _, url := range urls {
  193. u := url.String()
  194. if um[u] {
  195. return true
  196. }
  197. um[u] = true
  198. }
  199. }
  200. return false
  201. }
  202. func (c *ServerConfig) bootstrapTimeout() time.Duration {
  203. if c.BootstrapTimeout != 0 {
  204. return c.BootstrapTimeout
  205. }
  206. return time.Second
  207. }
  208. func (c *ServerConfig) backendPath() string { return filepath.Join(c.SnapDir(), "db") }