server.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package etcdserver
  2. import (
  3. "errors"
  4. "time"
  5. "code.google.com/p/go.net/context"
  6. pb "github.com/coreos/etcd/etcdserver2/etcdserverpb"
  7. "github.com/coreos/etcd/raft"
  8. "github.com/coreos/etcd/raft/raftpb"
  9. "github.com/coreos/etcd/store"
  10. "github.com/coreos/etcd/wait"
  11. )
  12. var (
  13. ErrUnknownMethod = errors.New("etcdserver: unknown method")
  14. ErrStopped = errors.New("etcdserver: server stopped")
  15. )
  16. type SendFunc func(m []raftpb.Message)
  17. type Response struct {
  18. Event *store.Event
  19. Watcher *store.Watcher
  20. err error
  21. }
  22. type Server struct {
  23. w *wait.List
  24. done chan struct{}
  25. Node raft.Node
  26. Store store.Store
  27. // Send specifies the send function for sending msgs to peers. Send
  28. // MUST NOT block. It is okay to drop messages, since clients should
  29. // timeout and reissue their messages. If Send is nil, Server will
  30. // panic.
  31. Send SendFunc
  32. // Save specifies the save function for saving ents to stable storage.
  33. // Save MUST block until st and ents are on stable storage. If Send is
  34. // nil, Server will panic.
  35. Save func(st raftpb.State, ents []raftpb.Entry)
  36. Ticker <-chan time.Time
  37. }
  38. // Start prepares and starts server in a new goroutine. It is no longer safe to
  39. // modify a Servers fields after it has been sent to Start.
  40. func Start(s *Server) {
  41. s.w = wait.New()
  42. s.done = make(chan struct{})
  43. go s.run()
  44. }
  45. func (s *Server) run() {
  46. for {
  47. select {
  48. case <-s.Ticker:
  49. s.Node.Tick()
  50. case rd := <-s.Node.Ready():
  51. s.Save(rd.State, rd.Entries)
  52. s.Send(rd.Messages)
  53. // TODO(bmizerany): do this in the background, but take
  54. // care to apply entries in a single goroutine, and not
  55. // race them.
  56. for _, e := range rd.CommittedEntries {
  57. var r pb.Request
  58. if err := r.Unmarshal(e.Data); err != nil {
  59. panic("TODO: this is bad, what do we do about it?")
  60. }
  61. s.w.Trigger(r.Id, s.apply(r))
  62. }
  63. case <-s.done:
  64. return
  65. }
  66. }
  67. }
  68. // Stop stops the server, and shutsdown the running goroutine. Stop should be
  69. // called after a Start(s), otherwise it will block forever.
  70. func (s *Server) Stop() {
  71. s.done <- struct{}{}
  72. }
  73. // Do interprets r and performs an operation on s.Store according to r.Method
  74. // and other fields. If r.Method is "POST", "PUT", "DELETE", or a "GET with
  75. // Quorum == true, r will be sent through consensus before performing its
  76. // respective operation. Do will block until an action is performed or there is
  77. // an error.
  78. func (s *Server) Do(ctx context.Context, r pb.Request) (Response, error) {
  79. if r.Id == 0 {
  80. panic("r.Id cannot be 0")
  81. }
  82. if r.Method == "GET" && r.Quorum {
  83. r.Method = "QGET"
  84. }
  85. switch r.Method {
  86. case "POST", "PUT", "DELETE", "QGET":
  87. data, err := r.Marshal()
  88. if err != nil {
  89. return Response{}, err
  90. }
  91. ch := s.w.Register(r.Id)
  92. s.Node.Propose(ctx, data)
  93. select {
  94. case x := <-ch:
  95. resp := x.(Response)
  96. return resp, resp.err
  97. case <-ctx.Done():
  98. s.w.Trigger(r.Id, nil) // GC wait
  99. return Response{}, ctx.Err()
  100. case <-s.done:
  101. return Response{}, ErrStopped
  102. }
  103. case "GET":
  104. switch {
  105. case r.Wait:
  106. wc, err := s.Store.Watch(r.Path, r.Recursive, false, r.Since)
  107. if err != nil {
  108. return Response{}, err
  109. }
  110. return Response{Watcher: wc}, nil
  111. default:
  112. ev, err := s.Store.Get(r.Path, r.Recursive, r.Sorted)
  113. if err != nil {
  114. return Response{}, err
  115. }
  116. return Response{Event: ev}, nil
  117. }
  118. default:
  119. return Response{}, ErrUnknownMethod
  120. }
  121. }
  122. // apply interprets r as a call to store.X and returns an Response interpreted from store.Event
  123. func (s *Server) apply(r pb.Request) Response {
  124. f := func(ev *store.Event, err error) Response {
  125. return Response{Event: ev, err: err}
  126. }
  127. expr := time.Unix(0, r.Expiration)
  128. switch r.Method {
  129. case "POST":
  130. return f(s.Store.Create(r.Path, r.Dir, r.Val, true, expr))
  131. case "PUT":
  132. exists, existsSet := getBool(r.PrevExists)
  133. switch {
  134. case existsSet:
  135. if exists {
  136. return f(s.Store.Update(r.Path, r.Val, expr))
  137. } else {
  138. return f(s.Store.Create(r.Path, r.Dir, r.Val, false, expr))
  139. }
  140. case r.PrevIndex > 0 || r.PrevValue != "":
  141. return f(s.Store.CompareAndSwap(r.Path, r.PrevValue, r.PrevIndex, r.Val, expr))
  142. default:
  143. return f(s.Store.Set(r.Path, r.Dir, r.Val, expr))
  144. }
  145. case "DELETE":
  146. switch {
  147. case r.PrevIndex > 0 || r.PrevValue != "":
  148. return f(s.Store.CompareAndDelete(r.Path, r.PrevValue, r.PrevIndex))
  149. default:
  150. return f(s.Store.Delete(r.Path, r.Recursive, r.Dir))
  151. }
  152. case "QGET":
  153. return f(s.Store.Get(r.Path, r.Recursive, r.Sorted))
  154. default:
  155. // This should never be reached, but just in case:
  156. return Response{err: ErrUnknownMethod}
  157. }
  158. }
  159. func getBool(v *bool) (vv bool, set bool) {
  160. if v == nil {
  161. return false, false
  162. }
  163. return *v, true
  164. }