etcd.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package main
  2. import (
  3. "crypto/tls"
  4. "flag"
  5. "github.com/coreos/etcd/store"
  6. "github.com/coreos/etcd/web"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "os"
  11. "os/signal"
  12. "runtime/pprof"
  13. "strings"
  14. "time"
  15. )
  16. //------------------------------------------------------------------------------
  17. //
  18. // Initialization
  19. //
  20. //------------------------------------------------------------------------------
  21. var verbose bool
  22. var veryVerbose bool
  23. var machines string
  24. var machinesFile string
  25. var cluster []string
  26. var argInfo Info
  27. var dirPath string
  28. var force bool
  29. var maxSize int
  30. var snapshot bool
  31. var retryTimes int
  32. var maxClusterSize int
  33. var cpuprofile string
  34. func init() {
  35. flag.BoolVar(&verbose, "v", false, "verbose logging")
  36. flag.BoolVar(&veryVerbose, "vv", false, "very verbose logging")
  37. flag.StringVar(&machines, "C", "", "the ip address and port of a existing machines in the cluster, sepearate by comma")
  38. flag.StringVar(&machinesFile, "CF", "", "the file contains a list of existing machines in the cluster, seperate by comma")
  39. flag.StringVar(&argInfo.Name, "n", "default-name", "the node name (required)")
  40. flag.StringVar(&argInfo.EtcdURL, "c", "127.0.0.1:4001", "the hostname:port for etcd client communication")
  41. flag.StringVar(&argInfo.RaftURL, "s", "127.0.0.1:7001", "the hostname:port for raft server communication")
  42. flag.StringVar(&argInfo.WebURL, "w", "", "the hostname:port of web interface")
  43. flag.StringVar(&argInfo.RaftTLS.CAFile, "serverCAFile", "", "the path of the CAFile")
  44. flag.StringVar(&argInfo.RaftTLS.CertFile, "serverCert", "", "the cert file of the server")
  45. flag.StringVar(&argInfo.RaftTLS.KeyFile, "serverKey", "", "the key file of the server")
  46. flag.StringVar(&argInfo.EtcdTLS.CAFile, "clientCAFile", "", "the path of the client CAFile")
  47. flag.StringVar(&argInfo.EtcdTLS.CertFile, "clientCert", "", "the cert file of the client")
  48. flag.StringVar(&argInfo.EtcdTLS.KeyFile, "clientKey", "", "the key file of the client")
  49. flag.StringVar(&dirPath, "d", ".", "the directory to store log and snapshot")
  50. flag.BoolVar(&force, "f", false, "force new node configuration if existing is found (WARNING: data loss!)")
  51. flag.BoolVar(&snapshot, "snapshot", false, "open or close snapshot")
  52. flag.IntVar(&maxSize, "m", 1024, "the max size of result buffer")
  53. flag.IntVar(&retryTimes, "r", 3, "the max retry attempts when trying to join a cluster")
  54. flag.IntVar(&maxClusterSize, "maxsize", 9, "the max size of the cluster")
  55. flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
  56. }
  57. const (
  58. ElectionTimeout = 200 * time.Millisecond
  59. HeartbeatTimeout = 50 * time.Millisecond
  60. // Timeout for internal raft http connection
  61. // The original timeout for http is 45 seconds
  62. // which is too long for our usage.
  63. HTTPTimeout = 10 * time.Second
  64. RetryInterval = 10
  65. )
  66. //------------------------------------------------------------------------------
  67. //
  68. // Typedefs
  69. //
  70. //------------------------------------------------------------------------------
  71. type TLSInfo struct {
  72. CertFile string `json:"CertFile"`
  73. KeyFile string `json:"KeyFile"`
  74. CAFile string `json:"CAFile"`
  75. }
  76. type Info struct {
  77. Name string `json:"name"`
  78. RaftURL string `json:"raftURL"`
  79. EtcdURL string `json:"etcdURL"`
  80. WebURL string `json:"webURL"`
  81. RaftTLS TLSInfo `json:"raftTLS"`
  82. EtcdTLS TLSInfo `json:"etcdTLS"`
  83. }
  84. type TLSConfig struct {
  85. Scheme string
  86. Server tls.Config
  87. Client tls.Config
  88. }
  89. //------------------------------------------------------------------------------
  90. //
  91. // Variables
  92. //
  93. //------------------------------------------------------------------------------
  94. var etcdStore *store.Store
  95. var info *Info
  96. //------------------------------------------------------------------------------
  97. //
  98. // Functions
  99. //
  100. //------------------------------------------------------------------------------
  101. //--------------------------------------
  102. // Main
  103. //--------------------------------------
  104. func main() {
  105. flag.Parse()
  106. if cpuprofile != "" {
  107. f, err := os.Create(cpuprofile)
  108. if err != nil {
  109. fatal(err)
  110. }
  111. pprof.StartCPUProfile(f)
  112. defer pprof.StopCPUProfile()
  113. c := make(chan os.Signal, 1)
  114. signal.Notify(c, os.Interrupt)
  115. go func() {
  116. for sig := range c {
  117. infof("captured %v, stopping profiler and exiting..", sig)
  118. pprof.StopCPUProfile()
  119. os.Exit(1)
  120. }
  121. }()
  122. }
  123. if veryVerbose {
  124. verbose = true
  125. }
  126. if machines != "" {
  127. cluster = strings.Split(machines, ",")
  128. } else if machinesFile != "" {
  129. b, err := ioutil.ReadFile(machinesFile)
  130. if err != nil {
  131. fatalf("Unable to read the given machines file: %s", err)
  132. }
  133. cluster = strings.Split(string(b), ",")
  134. }
  135. raftTLSConfig, ok := tlsConfigFromInfo(argInfo.RaftTLS)
  136. if !ok {
  137. fatal("Please specify cert and key file or cert and key file and CAFile or none of the three")
  138. }
  139. etcdTLSConfig, ok := tlsConfigFromInfo(argInfo.EtcdTLS)
  140. if !ok {
  141. fatal("Please specify cert and key file or cert and key file and CAFile or none of the three")
  142. }
  143. argInfo.Name = strings.TrimSpace(argInfo.Name)
  144. if argInfo.Name == "" {
  145. fatal("ERROR: server name required. e.g. '-n=server_name'")
  146. }
  147. argInfo.RaftURL = sanitizeURL(argInfo.RaftURL, raftTLSConfig.Scheme)
  148. argInfo.EtcdURL = sanitizeURL(argInfo.EtcdURL, etcdTLSConfig.Scheme)
  149. argInfo.WebURL = sanitizeURL(argInfo.WebURL, "http")
  150. // Setup commands.
  151. registerCommands()
  152. // Read server info from file or grab it from user.
  153. if err := os.MkdirAll(dirPath, 0744); err != nil {
  154. fatalf("Unable to create path: %s", err)
  155. }
  156. info = getInfo(dirPath)
  157. // Create etcd key-value store
  158. etcdStore = store.CreateStore(maxSize)
  159. snapConf = newSnapshotConf()
  160. startRaft(raftTLSConfig)
  161. if argInfo.WebURL != "" {
  162. // start web
  163. argInfo.WebURL = sanitizeURL(argInfo.WebURL, "http")
  164. go webHelper()
  165. go web.Start(raftServer, argInfo.WebURL)
  166. }
  167. startEtcdTransport(*info, etcdTLSConfig.Scheme, etcdTLSConfig.Server)
  168. }
  169. // Start to listen and response client command
  170. func startEtcdTransport(info Info, scheme string, tlsConf tls.Config) {
  171. u, err := url.Parse(info.EtcdURL)
  172. if err != nil {
  173. fatalf("invalid url '%s': %s", info.EtcdURL, err)
  174. }
  175. infof("etcd server [%s:%s]", info.Name, u)
  176. server := http.Server{
  177. Handler: NewEtcdMuxer(),
  178. TLSConfig: &tlsConf,
  179. Addr: u.Host,
  180. }
  181. if scheme == "http" {
  182. fatal(server.ListenAndServe())
  183. } else {
  184. fatal(server.ListenAndServeTLS(info.EtcdTLS.CertFile, info.EtcdTLS.KeyFile))
  185. }
  186. }