etcd.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. }
  44. func New(c *config.Config) *Server {
  45. if err := c.Sanitize(); err != nil {
  46. log.Fatalf("failed sanitizing configuration: %v", err)
  47. }
  48. tc := &tls.Config{
  49. InsecureSkipVerify: true,
  50. }
  51. var err error
  52. if c.PeerTLSInfo().Scheme() == "https" {
  53. tc, err = c.PeerTLSInfo().ClientConfig()
  54. if err != nil {
  55. log.Fatal("failed to create raft transporter tls:", err)
  56. }
  57. }
  58. tr := new(http.Transport)
  59. tr.TLSClientConfig = tc
  60. client := &http.Client{Transport: tr}
  61. s := &Server{
  62. config: c,
  63. id: genId(),
  64. pubAddr: c.Addr,
  65. raftPubAddr: c.Peer.Addr,
  66. tickDuration: defaultTickDuration,
  67. mode: atomicInt(stopMode),
  68. client: newClient(tc),
  69. peerHub: newPeerHub(client),
  70. stopc: make(chan struct{}),
  71. }
  72. return s
  73. }
  74. func (s *Server) SetTick(tick time.Duration) {
  75. s.tickDuration = tick
  76. }
  77. // Stop stops the server elegently.
  78. func (s *Server) Stop() {
  79. if s.mode.Get() == stopMode {
  80. return
  81. }
  82. s.mu.Lock()
  83. s.stopped = true
  84. switch s.mode.Get() {
  85. case participantMode:
  86. s.p.stop()
  87. case standbyMode:
  88. s.s.stop()
  89. }
  90. s.mu.Unlock()
  91. <-s.stopc
  92. s.client.CloseConnections()
  93. s.peerHub.stop()
  94. }
  95. func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  96. switch s.mode.Get() {
  97. case participantMode:
  98. s.p.ServeHTTP(w, r)
  99. case standbyMode:
  100. s.s.ServeHTTP(w, r)
  101. default:
  102. http.NotFound(w, r)
  103. }
  104. }
  105. func (s *Server) RaftHandler() http.Handler {
  106. return http.HandlerFunc(s.ServeRaftHTTP)
  107. }
  108. func (s *Server) ServeRaftHTTP(w http.ResponseWriter, r *http.Request) {
  109. switch s.mode.Get() {
  110. case participantMode:
  111. s.p.raftHandler().ServeHTTP(w, r)
  112. default:
  113. http.NotFound(w, r)
  114. }
  115. }
  116. func (s *Server) Run() error {
  117. var d *discoverer
  118. var seeds []string
  119. durl := s.config.Discovery
  120. if durl != "" {
  121. u, err := url.Parse(durl)
  122. if err != nil {
  123. return fmt.Errorf("bad discovery URL error: %v", err)
  124. }
  125. d = newDiscoverer(u, fmt.Sprint(s.id), s.raftPubAddr)
  126. if seeds, err = d.discover(); err != nil {
  127. return err
  128. }
  129. } else {
  130. seeds = s.config.Peers
  131. }
  132. s.peerHub.setSeeds(seeds)
  133. next := participantMode
  134. for {
  135. s.mu.Lock()
  136. if s.stopped {
  137. next = stopMode
  138. }
  139. switch next {
  140. case participantMode:
  141. s.p = newParticipant(s.id, s.pubAddr, s.raftPubAddr, s.client, s.peerHub, s.tickDuration)
  142. dStopc := make(chan struct{})
  143. if d != nil {
  144. go d.heartbeat(dStopc)
  145. }
  146. s.mode.Set(participantMode)
  147. s.mu.Unlock()
  148. next = s.p.run()
  149. if d != nil {
  150. close(dStopc)
  151. }
  152. case standbyMode:
  153. s.s = newStandby(s.client, s.peerHub)
  154. s.mode.Set(standbyMode)
  155. s.mu.Unlock()
  156. next = s.s.run()
  157. case stopMode:
  158. s.mode.Set(stopMode)
  159. s.mu.Unlock()
  160. s.stopc <- struct{}{}
  161. return nil
  162. default:
  163. panic("unsupport mode")
  164. }
  165. s.id = genId()
  166. }
  167. }
  168. // setId sets the id for the participant. This should only be used for testing.
  169. func (s *Server) setId(id int64) {
  170. s.id = id
  171. }