etcd.go 15 KB

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