config.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. // Every change should be reflected on help.go as well.
  15. package etcdmain
  16. import (
  17. "flag"
  18. "fmt"
  19. "io/ioutil"
  20. "net/url"
  21. "os"
  22. "runtime"
  23. "strings"
  24. "github.com/coreos/etcd/etcdserver"
  25. "github.com/coreos/etcd/pkg/cors"
  26. "github.com/coreos/etcd/pkg/flags"
  27. "github.com/coreos/etcd/pkg/transport"
  28. "github.com/coreos/etcd/pkg/types"
  29. "github.com/coreos/etcd/version"
  30. "github.com/ghodss/yaml"
  31. )
  32. const (
  33. proxyFlagOff = "off"
  34. proxyFlagReadonly = "readonly"
  35. proxyFlagOn = "on"
  36. fallbackFlagExit = "exit"
  37. fallbackFlagProxy = "proxy"
  38. clusterStateFlagNew = "new"
  39. clusterStateFlagExisting = "existing"
  40. defaultName = "default"
  41. defaultInitialAdvertisePeerURLs = "http://localhost:2380"
  42. defaultAdvertiseClientURLs = "http://localhost:2379"
  43. defaultListenPeerURLs = "http://localhost:2380"
  44. defaultListenClientURLs = "http://localhost:2379"
  45. // maxElectionMs specifies the maximum value of election timeout.
  46. // More details are listed in ../Documentation/tuning.md#time-parameters.
  47. maxElectionMs = 50000
  48. )
  49. var (
  50. ignored = []string{
  51. "cluster-active-size",
  52. "cluster-remove-delay",
  53. "cluster-sync-interval",
  54. "config",
  55. "force",
  56. "max-result-buffer",
  57. "max-retry-attempts",
  58. "peer-heartbeat-interval",
  59. "peer-election-timeout",
  60. "retry-interval",
  61. "snapshot",
  62. "v",
  63. "vv",
  64. }
  65. ErrConflictBootstrapFlags = fmt.Errorf("multiple discovery or bootstrap flags are set. " +
  66. "Choose one of \"initial-cluster\", \"discovery\" or \"discovery-srv\"")
  67. errUnsetAdvertiseClientURLsFlag = fmt.Errorf("--advertise-client-urls is required when --listen-client-urls is set explicitly")
  68. )
  69. type config struct {
  70. *flag.FlagSet
  71. // member
  72. corsInfo *cors.CORSInfo
  73. lpurls, lcurls []url.URL
  74. Dir string `json:"data-dir"`
  75. WalDir string `json:"wal-dir"`
  76. MaxSnapFiles uint `json:"max-snapshots"`
  77. MaxWalFiles uint `json:"max-wals"`
  78. Name string `json:"name"`
  79. SnapCount uint64 `json:"snapshot-count"`
  80. LPUrlsCfgFile string `json:"listen-peer-urls"`
  81. LCUrlsCfgFile string `json:"listen-client-urls"`
  82. CorsCfgFile string `json:"cors"`
  83. // TickMs is the number of milliseconds between heartbeat ticks.
  84. // TODO: decouple tickMs and heartbeat tick (current heartbeat tick = 1).
  85. // make ticks a cluster wide configuration.
  86. TickMs uint `json:"heartbeat-interval"`
  87. ElectionMs uint `json:"election-timeout"`
  88. QuotaBackendBytes int64 `json:"quota-backend-bytes"`
  89. // clustering
  90. apurls, acurls []url.URL
  91. clusterState *flags.StringsFlag
  92. DnsCluster string `json:"discovery-srv"`
  93. Dproxy string `json:"discovery-proxy"`
  94. Durl string `json:"discovery"`
  95. fallback *flags.StringsFlag
  96. InitialCluster string `json:"initial-cluster"`
  97. InitialClusterToken string `json:"initial-cluster-token"`
  98. StrictReconfigCheck bool `json:"strict-reconfig-check"`
  99. ApurlsCfgFile string `json:"initial-advertise-peer-urls"`
  100. AcurlsCfgFile string `json:"advertise-client-urls"`
  101. ClusterStateCfgFile string `json:"initial-cluster-state"`
  102. FallbackCfgFile string `json:"discovery-fallback"`
  103. // proxy
  104. proxy *flags.StringsFlag
  105. ProxyFailureWaitMs uint `json:"proxy-failure-wait"`
  106. ProxyRefreshIntervalMs uint `json:"proxy-refresh-interval"`
  107. ProxyDialTimeoutMs uint `json:"proxy-dial-timeout"`
  108. ProxyWriteTimeoutMs uint `json:"proxy-write-timeout"`
  109. ProxyReadTimeoutMs uint `json:"proxy-read-timeout"`
  110. ProxyCfgFile string `json:"proxy"`
  111. // security
  112. clientTLSInfo, peerTLSInfo transport.TLSInfo
  113. ClientAutoTLS bool
  114. PeerAutoTLS bool
  115. ClientSecurityCfgFile securityConfig `json:"client-transport-security"`
  116. PeerSecurityCfgFile securityConfig `json:"peer-transport-security"`
  117. // Debug logging
  118. Debug bool `json:"debug"`
  119. LogPkgLevels string `json:"log-package-levels"`
  120. // ForceNewCluster is unsafe
  121. ForceNewCluster bool `json:"force-new-cluster"`
  122. printVersion bool
  123. autoCompactionRetention int
  124. enablePprof bool
  125. configFile string
  126. ignored []string
  127. }
  128. type securityConfig struct {
  129. CAFile string `json:"ca-file"`
  130. CertFile string `json:"cert-file"`
  131. KeyFile string `json:"key-file"`
  132. CertAuth bool `json:"client-cert-auth"`
  133. TrustedCAFile string `json:"trusted-ca-file"`
  134. AutoTLS bool `json:"auto-tls"`
  135. }
  136. func NewConfig() *config {
  137. cfg := &config{
  138. corsInfo: &cors.CORSInfo{},
  139. clusterState: flags.NewStringsFlag(
  140. clusterStateFlagNew,
  141. clusterStateFlagExisting,
  142. ),
  143. fallback: flags.NewStringsFlag(
  144. fallbackFlagExit,
  145. fallbackFlagProxy,
  146. ),
  147. ignored: ignored,
  148. proxy: flags.NewStringsFlag(
  149. proxyFlagOff,
  150. proxyFlagReadonly,
  151. proxyFlagOn,
  152. ),
  153. }
  154. cfg.FlagSet = flag.NewFlagSet("etcd", flag.ContinueOnError)
  155. fs := cfg.FlagSet
  156. fs.Usage = func() {
  157. fmt.Println(usageline)
  158. }
  159. fs.StringVar(&cfg.configFile, "config-file", "", "Path to the server configuration file")
  160. // member
  161. fs.Var(cfg.corsInfo, "cors", "Comma-separated white list of origins for CORS (cross-origin resource sharing).")
  162. fs.StringVar(&cfg.Dir, "data-dir", "", "Path to the data directory.")
  163. fs.StringVar(&cfg.WalDir, "wal-dir", "", "Path to the dedicated wal directory.")
  164. fs.Var(flags.NewURLsValue(defaultListenPeerURLs), "listen-peer-urls", "List of URLs to listen on for peer traffic.")
  165. fs.Var(flags.NewURLsValue(defaultListenClientURLs), "listen-client-urls", "List of URLs to listen on for client traffic.")
  166. fs.UintVar(&cfg.MaxSnapFiles, "max-snapshots", defaultMaxSnapshots, "Maximum number of snapshot files to retain (0 is unlimited).")
  167. fs.UintVar(&cfg.MaxWalFiles, "max-wals", defaultMaxWALs, "Maximum number of wal files to retain (0 is unlimited).")
  168. fs.StringVar(&cfg.Name, "name", defaultName, "Human-readable name for this member.")
  169. fs.Uint64Var(&cfg.SnapCount, "snapshot-count", etcdserver.DefaultSnapCount, "Number of committed transactions to trigger a snapshot to disk.")
  170. fs.UintVar(&cfg.TickMs, "heartbeat-interval", 100, "Time (in milliseconds) of a heartbeat interval.")
  171. fs.UintVar(&cfg.ElectionMs, "election-timeout", 1000, "Time (in milliseconds) for an election to timeout.")
  172. fs.Int64Var(&cfg.QuotaBackendBytes, "quota-backend-bytes", 0, "Raise alarms when backend size exceeds the given quota. 0 means use the default quota.")
  173. // clustering
  174. fs.Var(flags.NewURLsValue(defaultInitialAdvertisePeerURLs), "initial-advertise-peer-urls", "List of this member's peer URLs to advertise to the rest of the cluster.")
  175. fs.Var(flags.NewURLsValue(defaultAdvertiseClientURLs), "advertise-client-urls", "List of this member's client URLs to advertise to the public.")
  176. fs.StringVar(&cfg.Durl, "discovery", "", "Discovery URL used to bootstrap the cluster.")
  177. fs.Var(cfg.fallback, "discovery-fallback", fmt.Sprintf("Valid values include %s", strings.Join(cfg.fallback.Values, ", ")))
  178. if err := cfg.fallback.Set(fallbackFlagProxy); err != nil {
  179. // Should never happen.
  180. plog.Panicf("unexpected error setting up discovery-fallback flag: %v", err)
  181. }
  182. fs.StringVar(&cfg.Dproxy, "discovery-proxy", "", "HTTP proxy to use for traffic to discovery service.")
  183. fs.StringVar(&cfg.DnsCluster, "discovery-srv", "", "DNS domain used to bootstrap initial cluster.")
  184. fs.StringVar(&cfg.InitialCluster, "initial-cluster", initialClusterFromName(defaultName), "Initial cluster configuration for bootstrapping.")
  185. fs.StringVar(&cfg.InitialClusterToken, "initial-cluster-token", "etcd-cluster", "Initial cluster token for the etcd cluster during bootstrap.")
  186. fs.Var(cfg.clusterState, "initial-cluster-state", "Initial cluster state ('new' or 'existing').")
  187. if err := cfg.clusterState.Set(clusterStateFlagNew); err != nil {
  188. // Should never happen.
  189. plog.Panicf("unexpected error setting up clusterStateFlag: %v", err)
  190. }
  191. fs.BoolVar(&cfg.StrictReconfigCheck, "strict-reconfig-check", false, "Reject reconfiguration requests that would cause quorum loss.")
  192. // proxy
  193. fs.Var(cfg.proxy, "proxy", fmt.Sprintf("Valid values include %s", strings.Join(cfg.proxy.Values, ", ")))
  194. if err := cfg.proxy.Set(proxyFlagOff); err != nil {
  195. // Should never happen.
  196. plog.Panicf("unexpected error setting up proxyFlag: %v", err)
  197. }
  198. fs.UintVar(&cfg.ProxyFailureWaitMs, "proxy-failure-wait", 5000, "Time (in milliseconds) an endpoint will be held in a failed state.")
  199. fs.UintVar(&cfg.ProxyRefreshIntervalMs, "proxy-refresh-interval", 30000, "Time (in milliseconds) of the endpoints refresh interval.")
  200. fs.UintVar(&cfg.ProxyDialTimeoutMs, "proxy-dial-timeout", 1000, "Time (in milliseconds) for a dial to timeout.")
  201. fs.UintVar(&cfg.ProxyWriteTimeoutMs, "proxy-write-timeout", 5000, "Time (in milliseconds) for a write to timeout.")
  202. fs.UintVar(&cfg.ProxyReadTimeoutMs, "proxy-read-timeout", 0, "Time (in milliseconds) for a read to timeout.")
  203. // security
  204. fs.StringVar(&cfg.clientTLSInfo.CAFile, "ca-file", "", "DEPRECATED: Path to the client server TLS CA file.")
  205. fs.StringVar(&cfg.clientTLSInfo.CertFile, "cert-file", "", "Path to the client server TLS cert file.")
  206. fs.StringVar(&cfg.clientTLSInfo.KeyFile, "key-file", "", "Path to the client server TLS key file.")
  207. fs.BoolVar(&cfg.clientTLSInfo.ClientCertAuth, "client-cert-auth", false, "Enable client cert authentication.")
  208. fs.StringVar(&cfg.clientTLSInfo.TrustedCAFile, "trusted-ca-file", "", "Path to the client server TLS trusted CA key file.")
  209. fs.BoolVar(&cfg.ClientAutoTLS, "auto-tls", false, "Client TLS using generated certificates")
  210. fs.StringVar(&cfg.peerTLSInfo.CAFile, "peer-ca-file", "", "DEPRECATED: Path to the peer server TLS CA file.")
  211. fs.StringVar(&cfg.peerTLSInfo.CertFile, "peer-cert-file", "", "Path to the peer server TLS cert file.")
  212. fs.StringVar(&cfg.peerTLSInfo.KeyFile, "peer-key-file", "", "Path to the peer server TLS key file.")
  213. fs.BoolVar(&cfg.peerTLSInfo.ClientCertAuth, "peer-client-cert-auth", false, "Enable peer client cert authentication.")
  214. fs.StringVar(&cfg.peerTLSInfo.TrustedCAFile, "peer-trusted-ca-file", "", "Path to the peer server TLS trusted CA file.")
  215. fs.BoolVar(&cfg.PeerAutoTLS, "peer-auto-tls", false, "Peer TLS using generated certificates")
  216. // logging
  217. fs.BoolVar(&cfg.Debug, "debug", false, "Enable debug-level logging for etcd.")
  218. fs.StringVar(&cfg.LogPkgLevels, "log-package-levels", "", "Specify a particular log level for each etcd package (eg: 'etcdmain=CRITICAL,etcdserver=DEBUG').")
  219. // unsafe
  220. fs.BoolVar(&cfg.ForceNewCluster, "force-new-cluster", false, "Force to create a new one member cluster.")
  221. // version
  222. fs.BoolVar(&cfg.printVersion, "version", false, "Print the version and exit.")
  223. // demo flag
  224. fs.IntVar(&cfg.autoCompactionRetention, "experimental-auto-compaction-retention", 0, "Auto compaction retention in hour. 0 means disable auto compaction.")
  225. // backwards-compatibility with v0.4.6
  226. fs.Var(&flags.IPAddressPort{}, "addr", "DEPRECATED: Use --advertise-client-urls instead.")
  227. fs.Var(&flags.IPAddressPort{}, "bind-addr", "DEPRECATED: Use --listen-client-urls instead.")
  228. fs.Var(&flags.IPAddressPort{}, "peer-addr", "DEPRECATED: Use --initial-advertise-peer-urls instead.")
  229. fs.Var(&flags.IPAddressPort{}, "peer-bind-addr", "DEPRECATED: Use --listen-peer-urls instead.")
  230. fs.Var(&flags.DeprecatedFlag{Name: "peers"}, "peers", "DEPRECATED: Use --initial-cluster instead.")
  231. fs.Var(&flags.DeprecatedFlag{Name: "peers-file"}, "peers-file", "DEPRECATED: Use --initial-cluster instead.")
  232. // pprof profiler via HTTP
  233. fs.BoolVar(&cfg.enablePprof, "enable-pprof", false, "Enable runtime profiling data via HTTP server. Address is at client URL + \"/debug/pprof\"")
  234. // ignored
  235. for _, f := range cfg.ignored {
  236. fs.Var(&flags.IgnoredFlag{Name: f}, f, "")
  237. }
  238. return cfg
  239. }
  240. func (cfg *config) Parse(arguments []string) error {
  241. perr := cfg.FlagSet.Parse(arguments)
  242. switch perr {
  243. case nil:
  244. case flag.ErrHelp:
  245. fmt.Println(flagsline)
  246. os.Exit(0)
  247. default:
  248. os.Exit(2)
  249. }
  250. if len(cfg.FlagSet.Args()) != 0 {
  251. return fmt.Errorf("'%s' is not a valid flag", cfg.FlagSet.Arg(0))
  252. }
  253. if cfg.printVersion {
  254. fmt.Printf("etcd Version: %s\n", version.Version)
  255. fmt.Printf("Git SHA: %s\n", version.GitSHA)
  256. fmt.Printf("Go Version: %s\n", runtime.Version())
  257. fmt.Printf("Go OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
  258. os.Exit(0)
  259. }
  260. var err error
  261. if cfg.configFile != "" {
  262. plog.Infof("Loading server configuration from %q", cfg.configFile)
  263. err = cfg.configFromFile()
  264. } else {
  265. err = cfg.configFromCmdLine()
  266. }
  267. return err
  268. }
  269. func (cfg *config) configFromCmdLine() error {
  270. err := flags.SetFlagsFromEnv("ETCD", cfg.FlagSet)
  271. if err != nil {
  272. plog.Fatalf("%v", err)
  273. }
  274. flags.SetBindAddrFromAddr(cfg.FlagSet, "peer-bind-addr", "peer-addr")
  275. flags.SetBindAddrFromAddr(cfg.FlagSet, "bind-addr", "addr")
  276. cfg.lpurls, err = flags.URLsFromFlags(cfg.FlagSet, "listen-peer-urls", "peer-bind-addr", cfg.peerTLSInfo)
  277. if err != nil {
  278. return err
  279. }
  280. cfg.apurls, err = flags.URLsFromFlags(cfg.FlagSet, "initial-advertise-peer-urls", "peer-addr", cfg.peerTLSInfo)
  281. if err != nil {
  282. return err
  283. }
  284. cfg.lcurls, err = flags.URLsFromFlags(cfg.FlagSet, "listen-client-urls", "bind-addr", cfg.clientTLSInfo)
  285. if err != nil {
  286. return err
  287. }
  288. cfg.acurls, err = flags.URLsFromFlags(cfg.FlagSet, "advertise-client-urls", "addr", cfg.clientTLSInfo)
  289. if err != nil {
  290. return err
  291. }
  292. return cfg.validateConfig(func(field string) bool {
  293. return flags.IsSet(cfg.FlagSet, field)
  294. })
  295. }
  296. func (cfg *config) configFromFile() error {
  297. b, err := ioutil.ReadFile(cfg.configFile)
  298. if err != nil {
  299. return err
  300. }
  301. err = yaml.Unmarshal(b, cfg)
  302. if err != nil {
  303. return err
  304. }
  305. if cfg.LPUrlsCfgFile != "" {
  306. u, err := types.NewURLs(strings.Split(cfg.LPUrlsCfgFile, ","))
  307. if err != nil {
  308. plog.Fatalf("unexpected error setting up listen-peer-urls: %v", err)
  309. }
  310. cfg.lpurls = []url.URL(u)
  311. }
  312. if cfg.LCUrlsCfgFile != "" {
  313. u, err := types.NewURLs(strings.Split(cfg.LCUrlsCfgFile, ","))
  314. if err != nil {
  315. plog.Fatalf("unexpected error setting up listen-client-urls: %v", err)
  316. }
  317. cfg.lcurls = []url.URL(u)
  318. }
  319. if cfg.CorsCfgFile != "" {
  320. if err := cfg.corsInfo.Set(cfg.CorsCfgFile); err != nil {
  321. plog.Panicf("unexpected error setting up cors: %v", err)
  322. }
  323. }
  324. if cfg.ApurlsCfgFile != "" {
  325. u, err := types.NewURLs(strings.Split(cfg.ApurlsCfgFile, ","))
  326. if err != nil {
  327. plog.Fatalf("unexpected error setting up initial-advertise-peer-urls: %v", err)
  328. }
  329. cfg.apurls = []url.URL(u)
  330. }
  331. if cfg.AcurlsCfgFile != "" {
  332. u, err := types.NewURLs(strings.Split(cfg.AcurlsCfgFile, ","))
  333. if err != nil {
  334. plog.Fatalf("unexpected error setting up advertise-peer-urls: %v", err)
  335. }
  336. cfg.acurls = []url.URL(u)
  337. }
  338. if cfg.ClusterStateCfgFile != "" {
  339. if err := cfg.clusterState.Set(cfg.ClusterStateCfgFile); err != nil {
  340. plog.Panicf("unexpected error setting up clusterStateFlag: %v", err)
  341. }
  342. }
  343. if cfg.FallbackCfgFile != "" {
  344. if err := cfg.fallback.Set(cfg.FallbackCfgFile); err != nil {
  345. plog.Panicf("unexpected error setting up discovery-fallback flag: %v", err)
  346. }
  347. }
  348. if cfg.ProxyCfgFile != "" {
  349. if err := cfg.proxy.Set(cfg.ProxyCfgFile); err != nil {
  350. plog.Panicf("unexpected error setting up proxyFlag: %v", err)
  351. }
  352. }
  353. copySecurityDetails := func(tls *transport.TLSInfo, ysc *securityConfig) {
  354. tls.CAFile = ysc.CAFile
  355. tls.CertFile = ysc.CertFile
  356. tls.KeyFile = ysc.KeyFile
  357. tls.ClientCertAuth = ysc.CertAuth
  358. tls.TrustedCAFile = ysc.TrustedCAFile
  359. }
  360. copySecurityDetails(&cfg.clientTLSInfo, &cfg.ClientSecurityCfgFile)
  361. copySecurityDetails(&cfg.peerTLSInfo, &cfg.PeerSecurityCfgFile)
  362. cfg.ClientAutoTLS = cfg.ClientSecurityCfgFile.AutoTLS
  363. cfg.PeerAutoTLS = cfg.PeerSecurityCfgFile.AutoTLS
  364. fieldsToBeChecked := map[string]bool{
  365. "discovery": (cfg.Durl != ""),
  366. "listen-client-urls": (cfg.LCUrlsCfgFile != ""),
  367. "advertise-client-urls": (cfg.AcurlsCfgFile != ""),
  368. "initial-cluster": (cfg.InitialCluster != ""),
  369. "discovery-srv": (cfg.DnsCluster != ""),
  370. }
  371. return cfg.validateConfig(func(field string) bool {
  372. return fieldsToBeChecked[field]
  373. })
  374. }
  375. func (cfg *config) validateConfig(isSet func(field string) bool) error {
  376. // when etcd runs in member mode user needs to set --advertise-client-urls if --listen-client-urls is set.
  377. // TODO(yichengq): check this for joining through discovery service case
  378. mayFallbackToProxy := isSet("discovery") && cfg.fallback.String() == fallbackFlagProxy
  379. mayBeProxy := cfg.proxy.String() != proxyFlagOff || mayFallbackToProxy
  380. if !mayBeProxy {
  381. if isSet("listen-client-urls") && !isSet("advertise-client-urls") {
  382. return errUnsetAdvertiseClientURLsFlag
  383. }
  384. }
  385. // Check if conflicting flags are passed.
  386. nSet := 0
  387. for _, v := range []bool{isSet("discovery"), isSet("initial-cluster"), isSet("discovery-srv")} {
  388. if v {
  389. nSet += 1
  390. }
  391. }
  392. if nSet > 1 {
  393. return ErrConflictBootstrapFlags
  394. }
  395. if 5*cfg.TickMs > cfg.ElectionMs {
  396. return fmt.Errorf("--election-timeout[%vms] should be at least as 5 times as --heartbeat-interval[%vms]", cfg.ElectionMs, cfg.TickMs)
  397. }
  398. if cfg.ElectionMs > maxElectionMs {
  399. return fmt.Errorf("--election-timeout[%vms] is too long, and should be set less than %vms", cfg.ElectionMs, maxElectionMs)
  400. }
  401. return nil
  402. }
  403. func initialClusterFromName(name string) string {
  404. n := name
  405. if name == "" {
  406. n = defaultName
  407. }
  408. return fmt.Sprintf("%s=http://localhost:2380", n)
  409. }
  410. func (cfg config) isNewCluster() bool { return cfg.clusterState.String() == clusterStateFlagNew }
  411. func (cfg config) isProxy() bool { return cfg.proxy.String() != proxyFlagOff }
  412. func (cfg config) isReadonlyProxy() bool { return cfg.proxy.String() == proxyFlagReadonly }
  413. func (cfg config) shouldFallbackToProxy() bool { return cfg.fallback.String() == fallbackFlagProxy }
  414. func (cfg config) electionTicks() int { return int(cfg.ElectionMs / cfg.TickMs) }