etcd.go 14 KB

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