etcd.go 14 KB

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