etcd.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "log"
  17. "net/http"
  18. "sync"
  19. "time"
  20. "github.com/coreos/etcd/config"
  21. )
  22. const (
  23. participantMode int64 = iota
  24. standbyMode
  25. stopMode
  26. )
  27. type Server struct {
  28. config *config.Config
  29. id int64
  30. pubAddr string
  31. raftPubAddr string
  32. tickDuration time.Duration
  33. mode atomicInt
  34. p *participant
  35. s *standby
  36. client *v2client
  37. peerHub *peerHub
  38. stopped bool
  39. mu sync.Mutex
  40. stopc chan struct{}
  41. }
  42. func New(c *config.Config) *Server {
  43. if err := c.Sanitize(); err != nil {
  44. log.Fatalf("failed sanitizing configuration: %v", err)
  45. }
  46. tc := &tls.Config{
  47. InsecureSkipVerify: true,
  48. }
  49. var err error
  50. if c.PeerTLSInfo().Scheme() == "https" {
  51. tc, err = c.PeerTLSInfo().ClientConfig()
  52. if err != nil {
  53. log.Fatal("failed to create raft transporter tls:", err)
  54. }
  55. }
  56. tr := new(http.Transport)
  57. tr.TLSClientConfig = tc
  58. client := &http.Client{Transport: tr}
  59. s := &Server{
  60. config: c,
  61. id: genId(),
  62. pubAddr: c.Addr,
  63. raftPubAddr: c.Peer.Addr,
  64. tickDuration: defaultTickDuration,
  65. mode: atomicInt(stopMode),
  66. client: newClient(tc),
  67. peerHub: newPeerHub(c.Peers, client),
  68. stopc: make(chan struct{}),
  69. }
  70. return s
  71. }
  72. func (s *Server) SetTick(tick time.Duration) {
  73. s.tickDuration = tick
  74. }
  75. // Stop stops the server elegently.
  76. func (s *Server) Stop() {
  77. if s.mode.Get() == stopMode {
  78. return
  79. }
  80. s.mu.Lock()
  81. s.stopped = true
  82. switch s.mode.Get() {
  83. case participantMode:
  84. s.p.stop()
  85. case standbyMode:
  86. s.s.stop()
  87. }
  88. s.mu.Unlock()
  89. <-s.stopc
  90. s.client.CloseConnections()
  91. s.peerHub.stop()
  92. }
  93. func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  94. switch s.mode.Get() {
  95. case participantMode:
  96. s.p.ServeHTTP(w, r)
  97. case standbyMode:
  98. s.s.ServeHTTP(w, r)
  99. default:
  100. http.NotFound(w, r)
  101. }
  102. }
  103. func (s *Server) RaftHandler() http.Handler {
  104. return http.HandlerFunc(s.ServeRaftHTTP)
  105. }
  106. func (s *Server) ServeRaftHTTP(w http.ResponseWriter, r *http.Request) {
  107. switch s.mode.Get() {
  108. case participantMode:
  109. s.p.raftHandler().ServeHTTP(w, r)
  110. default:
  111. http.NotFound(w, r)
  112. }
  113. }
  114. func (s *Server) Run() {
  115. next := participantMode
  116. for {
  117. s.mu.Lock()
  118. if s.stopped {
  119. next = stopMode
  120. }
  121. switch next {
  122. case participantMode:
  123. s.p = newParticipant(s.id, s.pubAddr, s.raftPubAddr, s.client, s.peerHub, s.tickDuration)
  124. s.mode.Set(participantMode)
  125. s.mu.Unlock()
  126. next = s.p.run()
  127. case standbyMode:
  128. s.s = newStandby(s.client, s.peerHub)
  129. s.mode.Set(standbyMode)
  130. s.mu.Unlock()
  131. next = s.s.run()
  132. case stopMode:
  133. s.mode.Set(stopMode)
  134. s.mu.Unlock()
  135. s.stopc <- struct{}{}
  136. return
  137. default:
  138. panic("unsupport mode")
  139. }
  140. s.id = genId()
  141. }
  142. }
  143. // setId sets the id for the participant. This should only be used for testing.
  144. func (s *Server) setId(id int64) {
  145. s.id = id
  146. }