server.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. }
  37. // Start prepares and starts server in a new goroutine. It is no longer safe to
  38. // modify a Servers fields after it has been sent to Start.
  39. func Start(s *Server) {
  40. s.w = wait.New()
  41. s.done = make(chan struct{})
  42. go s.run()
  43. }
  44. func (s *Server) run() {
  45. for {
  46. select {
  47. case rd := <-s.Node.Ready():
  48. s.Save(rd.State, rd.Entries)
  49. s.Send(rd.Messages)
  50. // TODO(bmizerany): do this in the background, but take
  51. // care to apply entries in a single goroutine, and not
  52. // race them.
  53. for _, e := range rd.CommittedEntries {
  54. var r pb.Request
  55. if err := r.Unmarshal(e.Data); err != nil {
  56. panic("TODO: this is bad, what do we do about it?")
  57. }
  58. s.w.Trigger(r.Id, s.apply(r))
  59. }
  60. case <-s.done:
  61. return
  62. }
  63. }
  64. }
  65. // Stop stops the server, and shutsdown the running goroutine. Stop should be
  66. // called after a Start(s), otherwise it will block forever.
  67. func (s *Server) Stop() {
  68. s.done <- struct{}{}
  69. }
  70. // Do interprets r and performs an operation on s.Store according to r.Method
  71. // and other fields. If r.Method is "POST", "PUT", "DELETE", or a "GET with
  72. // Quorum == true, r will be sent through consensus before performing its
  73. // respective operation. Do will block until an action is performed or there is
  74. // an error.
  75. func (s *Server) Do(ctx context.Context, r pb.Request) (Response, error) {
  76. if r.Id == 0 {
  77. panic("r.Id cannot be 0")
  78. }
  79. if r.Method == "GET" && r.Quorum {
  80. r.Method = "QGET"
  81. }
  82. switch r.Method {
  83. case "POST", "PUT", "DELETE", "QGET":
  84. data, err := r.Marshal()
  85. if err != nil {
  86. return Response{}, err
  87. }
  88. ch := s.w.Register(r.Id)
  89. s.Node.Propose(ctx, data)
  90. select {
  91. case x := <-ch:
  92. resp := x.(Response)
  93. return resp, resp.err
  94. case <-ctx.Done():
  95. s.w.Trigger(r.Id, nil) // GC wait
  96. return Response{}, ctx.Err()
  97. case <-s.done:
  98. return Response{}, ErrStopped
  99. }
  100. case "GET":
  101. switch {
  102. case r.Wait:
  103. wc, err := s.Store.Watch(r.Path, r.Recursive, false, r.Since)
  104. if err != nil {
  105. return Response{}, err
  106. }
  107. return Response{Watcher: wc}, nil
  108. default:
  109. ev, err := s.Store.Get(r.Path, r.Recursive, r.Sorted)
  110. if err != nil {
  111. return Response{}, err
  112. }
  113. return Response{Event: ev}, nil
  114. }
  115. default:
  116. return Response{}, ErrUnknownMethod
  117. }
  118. }
  119. // apply interprets r as a call to store.X and returns an Response interpreted from store.Event
  120. func (s *Server) apply(r pb.Request) Response {
  121. f := func(ev *store.Event, err error) Response {
  122. return Response{Event: ev, err: err}
  123. }
  124. expr := time.Unix(0, r.Expiration)
  125. switch r.Method {
  126. case "POST":
  127. return f(s.Store.Create(r.Path, r.Dir, r.Val, true, expr))
  128. case "PUT":
  129. exists, existsSet := getBool(r.PrevExists)
  130. switch {
  131. case existsSet:
  132. if exists {
  133. return f(s.Store.Update(r.Path, r.Val, expr))
  134. } else {
  135. return f(s.Store.Create(r.Path, r.Dir, r.Val, false, expr))
  136. }
  137. case r.PrevIndex > 0 || r.PrevValue != "":
  138. return f(s.Store.CompareAndSwap(r.Path, r.PrevValue, r.PrevIndex, r.Val, expr))
  139. default:
  140. return f(s.Store.Set(r.Path, r.Dir, r.Val, expr))
  141. }
  142. case "DELETE":
  143. switch {
  144. case r.PrevIndex > 0 || r.PrevValue != "":
  145. return f(s.Store.CompareAndDelete(r.Path, r.PrevValue, r.PrevIndex))
  146. default:
  147. return f(s.Store.Delete(r.Path, r.Recursive, r.Dir))
  148. }
  149. case "QGET":
  150. return f(s.Store.Get(r.Path, r.Recursive, r.Sorted))
  151. default:
  152. // This should never be reached, but just in case:
  153. return Response{err: ErrUnknownMethod}
  154. }
  155. }
  156. func getBool(v *bool) (vv bool, set bool) {
  157. if v == nil {
  158. return false, false
  159. }
  160. return *v, true
  161. }