etcd.go 15 KB

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