etcd.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. Copyright 2014 CoreOS Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcd
  14. import (
  15. "crypto/tls"
  16. "fmt"
  17. "log"
  18. "net/http"
  19. "net/url"
  20. "sync"
  21. "time"
  22. "github.com/coreos/etcd/config"
  23. )
  24. const (
  25. participantMode int64 = iota
  26. standbyMode
  27. stopMode
  28. )
  29. type Server struct {
  30. config *config.Config
  31. id int64
  32. pubAddr string
  33. raftPubAddr string
  34. tickDuration time.Duration
  35. mode atomicInt
  36. p *participant
  37. s *standby
  38. client *v2client
  39. peerHub *peerHub
  40. stopped bool
  41. mu sync.Mutex
  42. stopc chan struct{}
  43. log *log.Logger
  44. http.Handler
  45. }
  46. func New(c *config.Config) *Server {
  47. if err := c.Sanitize(); err != nil {
  48. log.Fatalf("server.new sanitizeErr=\"%v\"\n", err)
  49. }
  50. tc := &tls.Config{
  51. InsecureSkipVerify: true,
  52. }
  53. var err error
  54. if c.PeerTLSInfo().Scheme() == "https" {
  55. tc, err = c.PeerTLSInfo().ClientConfig()
  56. if err != nil {
  57. log.Fatalf("server.new ClientConfigErr=\"%v\"\n", err)
  58. }
  59. }
  60. tr := new(http.Transport)
  61. tr.TLSClientConfig = tc
  62. client := &http.Client{Transport: tr}
  63. s := &Server{
  64. config: c,
  65. id: genId(),
  66. pubAddr: c.Addr,
  67. raftPubAddr: c.Peer.Addr,
  68. tickDuration: defaultTickDuration,
  69. mode: atomicInt(stopMode),
  70. client: newClient(tc),
  71. peerHub: newPeerHub(client),
  72. stopc: make(chan struct{}),
  73. }
  74. m := http.NewServeMux()
  75. m.HandleFunc("/", s.requestHandler)
  76. m.HandleFunc("/version", versionHandler)
  77. s.Handler = m
  78. log.Printf("id=%x server.new raftPubAddr=%s\n", s.id, s.raftPubAddr)
  79. return s
  80. }
  81. func (s *Server) SetTick(tick time.Duration) {
  82. s.tickDuration = tick
  83. log.Printf("id=%x server.setTick tick=%q\n", s.id, s.tickDuration)
  84. }
  85. // Stop stops the server elegently.
  86. func (s *Server) Stop() {
  87. if s.mode.Get() == stopMode {
  88. return
  89. }
  90. s.mu.Lock()
  91. s.stopped = true
  92. switch s.mode.Get() {
  93. case participantMode:
  94. s.p.stop()
  95. case standbyMode:
  96. s.s.stop()
  97. }
  98. s.mu.Unlock()
  99. <-s.stopc
  100. s.client.CloseConnections()
  101. s.peerHub.stop()
  102. log.Printf("id=%x server.stop\n", s.id)
  103. }
  104. func (s *Server) requestHandler(w http.ResponseWriter, r *http.Request) {
  105. switch s.mode.Get() {
  106. case participantMode:
  107. s.p.ServeHTTP(w, r)
  108. case standbyMode:
  109. s.s.ServeHTTP(w, r)
  110. default:
  111. http.NotFound(w, r)
  112. }
  113. }
  114. func (s *Server) RaftHandler() http.Handler {
  115. return http.HandlerFunc(s.ServeRaftHTTP)
  116. }
  117. func (s *Server) ServeRaftHTTP(w http.ResponseWriter, r *http.Request) {
  118. switch s.mode.Get() {
  119. case participantMode:
  120. s.p.raftHandler().ServeHTTP(w, r)
  121. default:
  122. http.NotFound(w, r)
  123. }
  124. }
  125. func (s *Server) Run() error {
  126. var d *discoverer
  127. var seeds []string
  128. durl := s.config.Discovery
  129. if durl != "" {
  130. u, err := url.Parse(durl)
  131. if err != nil {
  132. return fmt.Errorf("bad discovery URL error: %v", err)
  133. }
  134. d = newDiscoverer(u, fmt.Sprint(s.id), s.raftPubAddr)
  135. if seeds, err = d.discover(); err != nil {
  136. return err
  137. }
  138. log.Printf("id=%x server.run source=-discovery seeds=\"%v\"\n", s.id, seeds)
  139. } else {
  140. seeds = s.config.Peers
  141. log.Printf("id=%x server.run source=-peers seeds=\"%v\"\n", s.id, seeds)
  142. }
  143. s.peerHub.setSeeds(seeds)
  144. next := participantMode
  145. for {
  146. s.mu.Lock()
  147. if s.stopped {
  148. next = stopMode
  149. }
  150. switch next {
  151. case participantMode:
  152. s.p = newParticipant(s.id, s.pubAddr, s.raftPubAddr, s.client, s.peerHub, s.tickDuration)
  153. dStopc := make(chan struct{})
  154. if d != nil {
  155. go d.heartbeat(dStopc)
  156. }
  157. s.mode.Set(participantMode)
  158. log.Printf("id=%x server.run mode=participantMode\n", s.id)
  159. s.mu.Unlock()
  160. next = s.p.run()
  161. if d != nil {
  162. close(dStopc)
  163. }
  164. case standbyMode:
  165. s.s = newStandby(s.client, s.peerHub)
  166. s.mode.Set(standbyMode)
  167. log.Printf("id=%x server.run mode=standbyMode\n", s.id)
  168. s.mu.Unlock()
  169. next = s.s.run()
  170. case stopMode:
  171. s.mode.Set(stopMode)
  172. log.Printf("id=%x server.run mode=stopMode\n", s.id)
  173. s.mu.Unlock()
  174. s.stopc <- struct{}{}
  175. return nil
  176. default:
  177. panic("unsupport mode")
  178. }
  179. s.id = genId()
  180. }
  181. }
  182. // setId sets the id for the participant. This should only be used for testing.
  183. func (s *Server) setId(id int64) {
  184. log.Printf("id=%x server.setId oldId=%x\n", id, s.id)
  185. s.id = id
  186. }