config.go 7.9 KB

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