etcd.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. package main
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "encoding/json"
  7. "encoding/pem"
  8. "flag"
  9. "fmt"
  10. "github.com/coreos/etcd/store"
  11. "github.com/coreos/etcd/web"
  12. "github.com/coreos/go-raft"
  13. "io/ioutil"
  14. "net"
  15. "net/http"
  16. "net/url"
  17. "os"
  18. "os/signal"
  19. "path"
  20. "runtime/pprof"
  21. "strings"
  22. "time"
  23. )
  24. //------------------------------------------------------------------------------
  25. //
  26. // Initialization
  27. //
  28. //------------------------------------------------------------------------------
  29. var verbose bool
  30. var veryVerbose bool
  31. var machines string
  32. var machinesFile string
  33. var cluster []string
  34. var hostname string
  35. var clientPort int
  36. var raftPort int
  37. var webPort int
  38. var serverCertFile string
  39. var serverKeyFile string
  40. var serverCAFile string
  41. var clientCertFile string
  42. var clientKeyFile string
  43. var clientCAFile string
  44. var dirPath string
  45. var ignore bool
  46. var maxSize int
  47. var snapshot bool
  48. var retryTimes int
  49. var maxClusterSize int
  50. var cpuprofile string
  51. func init() {
  52. flag.BoolVar(&verbose, "v", false, "verbose logging")
  53. flag.BoolVar(&veryVerbose, "vv", false, "very verbose logging")
  54. flag.StringVar(&machines, "C", "", "the ip address and port of a existing machines in the cluster, sepearate by comma")
  55. flag.StringVar(&machinesFile, "CF", "", "the file contains a list of existing machines in the cluster, seperate by comma")
  56. flag.StringVar(&hostname, "h", "0.0.0.0", "the hostname of the local machine")
  57. flag.IntVar(&clientPort, "c", 4001, "the port to communicate with clients")
  58. flag.IntVar(&raftPort, "s", 7001, "the port to communicate with servers")
  59. flag.IntVar(&webPort, "w", -1, "the port of web interface (-1 means do not start web interface)")
  60. flag.StringVar(&serverCAFile, "serverCAFile", "", "the path of the CAFile")
  61. flag.StringVar(&serverCertFile, "serverCert", "", "the cert file of the server")
  62. flag.StringVar(&serverKeyFile, "serverKey", "", "the key file of the server")
  63. flag.StringVar(&clientCAFile, "clientCAFile", "", "the path of the client CAFile")
  64. flag.StringVar(&clientCertFile, "clientCert", "", "the cert file of the client")
  65. flag.StringVar(&clientKeyFile, "clientKey", "", "the key file of the client")
  66. flag.StringVar(&dirPath, "d", "/tmp/", "the directory to store log and snapshot")
  67. flag.BoolVar(&ignore, "i", false, "ignore the old configuration, create a new node")
  68. flag.BoolVar(&snapshot, "snapshot", false, "open or close snapshot")
  69. flag.IntVar(&maxSize, "m", 1024, "the max size of result buffer")
  70. flag.IntVar(&retryTimes, "r", 3, "the max retry attempts when trying to join a cluster")
  71. flag.IntVar(&maxClusterSize, "maxsize", 9, "the max size of the cluster")
  72. flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
  73. }
  74. // CONSTANTS
  75. const (
  76. HTTP = iota
  77. HTTPS
  78. HTTPSANDVERIFY
  79. )
  80. const (
  81. SERVER = iota
  82. CLIENT
  83. )
  84. const (
  85. ELECTIONTIMEOUT = 200 * time.Millisecond
  86. HEARTBEATTIMEOUT = 50 * time.Millisecond
  87. // Timeout for internal raft http connection
  88. // The original timeout for http is 45 seconds
  89. // which is too long for our usage.
  90. HTTPTIMEOUT = 10 * time.Second
  91. RETRYINTERVAL = 10
  92. )
  93. //------------------------------------------------------------------------------
  94. //
  95. // Typedefs
  96. //
  97. //------------------------------------------------------------------------------
  98. type Info struct {
  99. Hostname string `json:"hostname"`
  100. RaftPort int `json:"raftPort"`
  101. ClientPort int `json:"clientPort"`
  102. WebPort int `json:"webPort"`
  103. ServerCertFile string `json:"serverCertFile"`
  104. ServerKeyFile string `json:"serverKeyFile"`
  105. ServerCAFile string `json:"serverCAFile"`
  106. ClientCertFile string `json:"clientCertFile"`
  107. ClientKeyFile string `json:"clientKeyFile"`
  108. ClientCAFile string `json:"clientCAFile"`
  109. }
  110. //------------------------------------------------------------------------------
  111. //
  112. // Variables
  113. //
  114. //------------------------------------------------------------------------------
  115. var raftServer *raft.Server
  116. var raftTransporter transporter
  117. var etcdStore *store.Store
  118. var info *Info
  119. //------------------------------------------------------------------------------
  120. //
  121. // Functions
  122. //
  123. //------------------------------------------------------------------------------
  124. //--------------------------------------
  125. // Main
  126. //--------------------------------------
  127. func main() {
  128. flag.Parse()
  129. if cpuprofile != "" {
  130. f, err := os.Create(cpuprofile)
  131. if err != nil {
  132. fatal(err)
  133. }
  134. pprof.StartCPUProfile(f)
  135. defer pprof.StopCPUProfile()
  136. c := make(chan os.Signal, 1)
  137. signal.Notify(c, os.Interrupt)
  138. go func() {
  139. for sig := range c {
  140. fmt.Printf("captured %v, stopping profiler and exiting..", sig)
  141. pprof.StopCPUProfile()
  142. os.Exit(1)
  143. }
  144. }()
  145. }
  146. if veryVerbose {
  147. verbose = true
  148. raft.SetLogLevel(raft.Debug)
  149. }
  150. if machines != "" {
  151. cluster = strings.Split(machines, ",")
  152. } else if machinesFile != "" {
  153. b, err := ioutil.ReadFile(machinesFile)
  154. if err != nil {
  155. fatalf("Unable to read the given machines file: %s", err)
  156. }
  157. cluster = strings.Split(string(b), ",")
  158. }
  159. // Setup commands.
  160. registerCommands()
  161. // Read server info from file or grab it from user.
  162. if err := os.MkdirAll(dirPath, 0744); err != nil {
  163. fatalf("Unable to create path: %s", err)
  164. }
  165. info = getInfo(dirPath)
  166. // security type
  167. st := securityType(SERVER)
  168. clientSt := securityType(CLIENT)
  169. if st == -1 || clientSt == -1 {
  170. fatal("Please specify cert and key file or cert and key file and CAFile or none of the three")
  171. }
  172. // Create etcd key-value store
  173. etcdStore = store.CreateStore(maxSize)
  174. startRaft(st)
  175. if webPort != -1 {
  176. // start web
  177. etcdStore.SetMessager(&storeMsg)
  178. go webHelper()
  179. go web.Start(raftServer, webPort)
  180. }
  181. startClientTransport(info.ClientPort, clientSt)
  182. }
  183. // Start the raft server
  184. func startRaft(securityType int) {
  185. var err error
  186. raftName := fmt.Sprintf("%s:%d", info.Hostname, info.RaftPort)
  187. // Create transporter for raft
  188. raftTransporter = createTransporter(securityType)
  189. // Create raft server
  190. raftServer, err = raft.NewServer(raftName, dirPath, raftTransporter, etcdStore, nil)
  191. if err != nil {
  192. fatal(err)
  193. }
  194. // LoadSnapshot
  195. if snapshot {
  196. err = raftServer.LoadSnapshot()
  197. if err == nil {
  198. debugf("%s finished load snapshot", raftServer.Name())
  199. } else {
  200. debug(err)
  201. }
  202. }
  203. raftServer.SetElectionTimeout(ELECTIONTIMEOUT)
  204. raftServer.SetHeartbeatTimeout(HEARTBEATTIMEOUT)
  205. raftServer.Start()
  206. if raftServer.IsLogEmpty() {
  207. // start as a leader in a new cluster
  208. if len(cluster) == 0 {
  209. time.Sleep(time.Millisecond * 20)
  210. // leader need to join self as a peer
  211. for {
  212. command := &JoinCommand{
  213. Name: raftServer.Name(),
  214. Hostname: hostname,
  215. RaftPort: raftPort,
  216. ClientPort: clientPort,
  217. }
  218. _, err := raftServer.Do(command)
  219. if err == nil {
  220. break
  221. }
  222. }
  223. debugf("%s start as a leader", raftServer.Name())
  224. // start as a follower in a existing cluster
  225. } else {
  226. time.Sleep(time.Millisecond * 20)
  227. for i := 0; i < retryTimes; i++ {
  228. success := false
  229. for _, machine := range cluster {
  230. if len(machine) == 0 {
  231. continue
  232. }
  233. err = joinCluster(raftServer, machine)
  234. if err != nil {
  235. if err.Error() == errors[103] {
  236. fmt.Println(err)
  237. os.Exit(1)
  238. }
  239. debugf("cannot join to cluster via machine %s %s", machine, err)
  240. } else {
  241. success = true
  242. break
  243. }
  244. }
  245. if success {
  246. break
  247. }
  248. warnf("cannot join to cluster via given machines, retry in %d seconds", RETRYINTERVAL)
  249. time.Sleep(time.Second * RETRYINTERVAL)
  250. }
  251. if err != nil {
  252. fatalf("Cannot join the cluster via given machines after %x retries", retryTimes)
  253. }
  254. debugf("%s success join to the cluster", raftServer.Name())
  255. }
  256. } else {
  257. // rejoin the previous cluster
  258. debugf("%s restart as a follower", raftServer.Name())
  259. }
  260. // open the snapshot
  261. if snapshot {
  262. go raftServer.Snapshot()
  263. }
  264. // start to response to raft requests
  265. go startRaftTransport(info.RaftPort, securityType)
  266. }
  267. // Create transporter using by raft server
  268. // Create http or https transporter based on
  269. // whether the user give the server cert and key
  270. func createTransporter(st int) transporter {
  271. t := transporter{}
  272. switch st {
  273. case HTTP:
  274. t.scheme = "http://"
  275. tr := &http.Transport{
  276. Dial: dialTimeout,
  277. }
  278. t.client = &http.Client{
  279. Transport: tr,
  280. }
  281. case HTTPS:
  282. fallthrough
  283. case HTTPSANDVERIFY:
  284. t.scheme = "https://"
  285. tlsCert, err := tls.LoadX509KeyPair(serverCertFile, serverKeyFile)
  286. if err != nil {
  287. fatal(err)
  288. }
  289. tr := &http.Transport{
  290. TLSClientConfig: &tls.Config{
  291. Certificates: []tls.Certificate{tlsCert},
  292. InsecureSkipVerify: true,
  293. },
  294. Dial: dialTimeout,
  295. DisableCompression: true,
  296. }
  297. t.client = &http.Client{Transport: tr}
  298. }
  299. return t
  300. }
  301. // Dial with timeout
  302. func dialTimeout(network, addr string) (net.Conn, error) {
  303. return net.DialTimeout(network, addr, HTTPTIMEOUT)
  304. }
  305. // Start to listen and response raft command
  306. func startRaftTransport(port int, st int) {
  307. // internal commands
  308. http.HandleFunc("/join", JoinHttpHandler)
  309. http.HandleFunc("/vote", VoteHttpHandler)
  310. http.HandleFunc("/log", GetLogHttpHandler)
  311. http.HandleFunc("/log/append", AppendEntriesHttpHandler)
  312. http.HandleFunc("/snapshot", SnapshotHttpHandler)
  313. http.HandleFunc("/snapshotRecovery", SnapshotRecoveryHttpHandler)
  314. http.HandleFunc("/client", ClientHttpHandler)
  315. switch st {
  316. case HTTP:
  317. fmt.Printf("raft server [%s] listen on http port %v\n", hostname, port)
  318. fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
  319. case HTTPS:
  320. fmt.Printf("raft server [%s] listen on https port %v\n", hostname, port)
  321. fatal(http.ListenAndServeTLS(fmt.Sprintf(":%d", port), serverCertFile, serverKeyFile, nil))
  322. case HTTPSANDVERIFY:
  323. server := &http.Server{
  324. TLSConfig: &tls.Config{
  325. ClientAuth: tls.RequireAndVerifyClientCert,
  326. ClientCAs: createCertPool(serverCAFile),
  327. },
  328. Addr: fmt.Sprintf(":%d", port),
  329. }
  330. fmt.Printf("raft server [%s] listen on https port %v\n", hostname, port)
  331. fatal(server.ListenAndServeTLS(serverCertFile, serverKeyFile))
  332. }
  333. }
  334. // Start to listen and response client command
  335. func startClientTransport(port int, st int) {
  336. // external commands
  337. http.HandleFunc("/"+version+"/keys/", Multiplexer)
  338. http.HandleFunc("/"+version+"/watch/", WatchHttpHandler)
  339. http.HandleFunc("/leader", LeaderHttpHandler)
  340. http.HandleFunc("/machines", MachinesHttpHandler)
  341. http.HandleFunc("/", VersionHttpHandler)
  342. http.HandleFunc("/stats", StatsHttpHandler)
  343. http.HandleFunc("/test/", TestHttpHandler)
  344. switch st {
  345. case HTTP:
  346. fmt.Printf("etcd [%s] listen on http port %v\n", hostname, clientPort)
  347. fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
  348. case HTTPS:
  349. fmt.Printf("etcd [%s] listen on https port %v\n", hostname, clientPort)
  350. http.ListenAndServeTLS(fmt.Sprintf(":%d", port), clientCertFile, clientKeyFile, nil)
  351. case HTTPSANDVERIFY:
  352. server := &http.Server{
  353. TLSConfig: &tls.Config{
  354. ClientAuth: tls.RequireAndVerifyClientCert,
  355. ClientCAs: createCertPool(clientCAFile),
  356. },
  357. Addr: fmt.Sprintf(":%d", port),
  358. }
  359. fmt.Printf("etcd [%s] listen on https port %v\n", hostname, clientPort)
  360. fatal(server.ListenAndServeTLS(clientCertFile, clientKeyFile))
  361. }
  362. }
  363. //--------------------------------------
  364. // Config
  365. //--------------------------------------
  366. // Get the security type
  367. func securityType(source int) int {
  368. var keyFile, certFile, CAFile string
  369. switch source {
  370. case SERVER:
  371. keyFile = info.ServerKeyFile
  372. certFile = info.ServerCertFile
  373. CAFile = info.ServerCAFile
  374. case CLIENT:
  375. keyFile = info.ClientKeyFile
  376. certFile = info.ClientCertFile
  377. CAFile = info.ClientCAFile
  378. }
  379. // If the user do not specify key file, cert file and
  380. // CA file, the type will be HTTP
  381. if keyFile == "" && certFile == "" && CAFile == "" {
  382. return HTTP
  383. }
  384. if keyFile != "" && certFile != "" {
  385. if CAFile != "" {
  386. // If the user specify all the three file, the type
  387. // will be HTTPS with client cert auth
  388. return HTTPSANDVERIFY
  389. }
  390. // If the user specify key file and cert file but not
  391. // CA file, the type will be HTTPS without client cert
  392. // auth
  393. return HTTPS
  394. }
  395. // bad specification
  396. return -1
  397. }
  398. // Get the server info from previous conf file
  399. // or from the user
  400. func getInfo(path string) *Info {
  401. // Read in the server info if available.
  402. infoPath := fmt.Sprintf("%s/info", path)
  403. // Delete the old configuration if exist
  404. if ignore {
  405. logPath := fmt.Sprintf("%s/log", path)
  406. confPath := fmt.Sprintf("%s/conf", path)
  407. snapshotPath := fmt.Sprintf("%s/snapshot", path)
  408. os.Remove(infoPath)
  409. os.Remove(logPath)
  410. os.Remove(confPath)
  411. os.RemoveAll(snapshotPath)
  412. }
  413. if file, err := os.Open(infoPath); err == nil {
  414. info := &Info{}
  415. if content, err := ioutil.ReadAll(file); err != nil {
  416. fatalf("Unable to read info: %v", err)
  417. } else {
  418. if err = json.Unmarshal(content, &info); err != nil {
  419. fatalf("Unable to parse info: %v", err)
  420. }
  421. }
  422. file.Close()
  423. return info
  424. } else {
  425. // Otherwise ask user for info and write it to file.
  426. hostname = strings.TrimSpace(hostname)
  427. if hostname == "" {
  428. fatal("Please give the address of the local machine")
  429. }
  430. fmt.Println("address ", hostname)
  431. info := &Info{
  432. Hostname: hostname,
  433. RaftPort: raftPort,
  434. ClientPort: clientPort,
  435. WebPort: webPort,
  436. ClientCAFile: clientCAFile,
  437. ClientCertFile: clientCertFile,
  438. ClientKeyFile: clientKeyFile,
  439. ServerCAFile: serverCAFile,
  440. ServerKeyFile: serverKeyFile,
  441. ServerCertFile: serverCertFile,
  442. }
  443. // Write to file.
  444. content, _ := json.Marshal(info)
  445. content = []byte(string(content) + "\n")
  446. if err := ioutil.WriteFile(infoPath, content, 0644); err != nil {
  447. fatalf("Unable to write info to file: %v", err)
  448. }
  449. return info
  450. }
  451. }
  452. // Create client auth certpool
  453. func createCertPool(CAFile string) *x509.CertPool {
  454. pemByte, _ := ioutil.ReadFile(CAFile)
  455. block, pemByte := pem.Decode(pemByte)
  456. cert, err := x509.ParseCertificate(block.Bytes)
  457. if err != nil {
  458. fatal(err)
  459. }
  460. certPool := x509.NewCertPool()
  461. certPool.AddCert(cert)
  462. return certPool
  463. }
  464. // Send join requests to the leader.
  465. func joinCluster(s *raft.Server, serverName string) error {
  466. var b bytes.Buffer
  467. command := &JoinCommand{
  468. Name: s.Name(),
  469. Hostname: info.Hostname,
  470. RaftPort: info.RaftPort,
  471. ClientPort: info.ClientPort,
  472. }
  473. json.NewEncoder(&b).Encode(command)
  474. // t must be ok
  475. t, ok := raftServer.Transporter().(transporter)
  476. if !ok {
  477. panic("wrong type")
  478. }
  479. debugf("Send Join Request to %s", serverName)
  480. resp, err := t.Post(fmt.Sprintf("%s/join", serverName), &b)
  481. for {
  482. if err != nil {
  483. return fmt.Errorf("Unable to join: %v", err)
  484. }
  485. if resp != nil {
  486. defer resp.Body.Close()
  487. if resp.StatusCode == http.StatusOK {
  488. return nil
  489. }
  490. if resp.StatusCode == http.StatusTemporaryRedirect {
  491. address := resp.Header.Get("Location")
  492. debugf("Send Join Request to %s", address)
  493. u, err := url.Parse(address)
  494. if err != nil {
  495. return fmt.Errorf("Unable to join: %s", err.Error())
  496. }
  497. json.NewEncoder(&b).Encode(command)
  498. resp, err = t.Post(path.Join(u.Host, u.Path), &b)
  499. } else if resp.StatusCode == http.StatusBadRequest {
  500. debug("Reach max number machines in the cluster")
  501. return fmt.Errorf(errors[103])
  502. } else {
  503. return fmt.Errorf("Unable to join")
  504. }
  505. }
  506. }
  507. return fmt.Errorf("Unable to join: %v", err)
  508. }
  509. // Register commands to raft server
  510. func registerCommands() {
  511. raft.RegisterCommand(&JoinCommand{})
  512. raft.RegisterCommand(&SetCommand{})
  513. raft.RegisterCommand(&GetCommand{})
  514. raft.RegisterCommand(&DeleteCommand{})
  515. raft.RegisterCommand(&WatchCommand{})
  516. raft.RegisterCommand(&TestAndSetCommand{})
  517. }