server.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package etcdserver
  2. import (
  3. "errors"
  4. "log"
  5. "math/rand"
  6. "time"
  7. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  8. "github.com/coreos/etcd/raft"
  9. "github.com/coreos/etcd/raft/raftpb"
  10. "github.com/coreos/etcd/store"
  11. "github.com/coreos/etcd/third_party/code.google.com/p/go.net/context"
  12. "github.com/coreos/etcd/wait"
  13. )
  14. const (
  15. defaultSyncTimeout = time.Second
  16. DefaultSnapCount = 10000
  17. )
  18. var (
  19. ErrUnknownMethod = errors.New("etcdserver: unknown method")
  20. ErrStopped = errors.New("etcdserver: server stopped")
  21. )
  22. func init() {
  23. rand.Seed(time.Now().UnixNano())
  24. }
  25. type SendFunc func(m []raftpb.Message)
  26. type SaveFunc func(st raftpb.HardState, ents []raftpb.Entry)
  27. type Response struct {
  28. Event *store.Event
  29. Watcher store.Watcher
  30. err error
  31. }
  32. type Storage interface {
  33. // Save function saves ents and state to the underlying stable storage.
  34. // Save MUST block until st and ents are on stable storage.
  35. Save(st raftpb.HardState, ents []raftpb.Entry)
  36. // SaveSnap function saves snapshot to the underlying stable storage.
  37. SaveSnap(snap raftpb.Snapshot)
  38. // TODO: WAL should be able to control cut itself. After implement self-controled cut,
  39. // remove it in this interface.
  40. // Cut cuts out a new wal file for saving new state and entries.
  41. Cut() error
  42. }
  43. type Server interface {
  44. // Start performs any initialization of the Server necessary for it to
  45. // begin serving requests. It must be called before Do or Process.
  46. // Start must be non-blocking; any long-running server functionality
  47. // should be implemented in goroutines.
  48. Start()
  49. // Stop terminates the Server and performs any necessary finalization.
  50. // Do and Process cannot be called after Stop has been invoked.
  51. Stop()
  52. // Do takes a request and attempts to fulfil it, returning a Response.
  53. Do(ctx context.Context, r pb.Request) (Response, error)
  54. // Process takes a raft message and applies it to the server's raft state
  55. // machine, respecting any timeout of the given context.
  56. Process(ctx context.Context, m raftpb.Message) error
  57. }
  58. // EtcdServer is the production implementation of the Server interface
  59. type EtcdServer struct {
  60. w wait.Wait
  61. done chan struct{}
  62. Node raft.Node
  63. Store store.Store
  64. // Send specifies the send function for sending msgs to peers. Send
  65. // MUST NOT block. It is okay to drop messages, since clients should
  66. // timeout and reissue their messages. If Send is nil, server will
  67. // panic.
  68. Send SendFunc
  69. Storage Storage
  70. Ticker <-chan time.Time
  71. SyncTicker <-chan time.Time
  72. SnapCount int64 // number of entries to trigger a snapshot
  73. }
  74. // Start prepares and starts server in a new goroutine. It is no longer safe to
  75. // modify a server's fields after it has been sent to Start.
  76. func (s *EtcdServer) Start() {
  77. if s.SnapCount == 0 {
  78. log.Printf("etcdserver: set snapshot count to default %d", DefaultSnapCount)
  79. s.SnapCount = DefaultSnapCount
  80. }
  81. s.w = wait.New()
  82. s.done = make(chan struct{})
  83. go s.run()
  84. }
  85. func (s *EtcdServer) Process(ctx context.Context, m raftpb.Message) error {
  86. return s.Node.Step(ctx, m)
  87. }
  88. func (s *EtcdServer) run() {
  89. var syncC <-chan time.Time
  90. // snapi indicates the index of the last submitted snapshot request
  91. var snapi, appliedi int64
  92. for {
  93. select {
  94. case <-s.Ticker:
  95. s.Node.Tick()
  96. case rd := <-s.Node.Ready():
  97. s.Storage.Save(rd.HardState, rd.Entries)
  98. s.Storage.SaveSnap(rd.Snapshot)
  99. s.Send(rd.Messages)
  100. // TODO(bmizerany): do this in the background, but take
  101. // care to apply entries in a single goroutine, and not
  102. // race them.
  103. for _, e := range rd.CommittedEntries {
  104. var r pb.Request
  105. if err := r.Unmarshal(e.Data); err != nil {
  106. panic("TODO: this is bad, what do we do about it?")
  107. }
  108. s.w.Trigger(r.Id, s.apply(r))
  109. appliedi = e.Index
  110. }
  111. if rd.Snapshot.Index > snapi {
  112. snapi = rd.Snapshot.Index
  113. }
  114. // recover from snapshot if it is more updated than current applied
  115. if rd.Snapshot.Index > appliedi {
  116. if err := s.Store.Recovery(rd.Snapshot.Data); err != nil {
  117. panic("TODO: this is bad, what do we do about it?")
  118. }
  119. appliedi = rd.Snapshot.Index
  120. }
  121. if appliedi-snapi > s.SnapCount {
  122. s.snapshot()
  123. snapi = appliedi
  124. }
  125. if rd.SoftState != nil {
  126. if rd.RaftState == raft.StateLeader {
  127. syncC = s.SyncTicker
  128. } else {
  129. syncC = nil
  130. }
  131. }
  132. case <-syncC:
  133. s.sync(defaultSyncTimeout)
  134. case <-s.done:
  135. return
  136. }
  137. }
  138. }
  139. // Stop stops the server, and shuts down the running goroutine. Stop should be
  140. // called after a Start(s), otherwise it will block forever.
  141. func (s *EtcdServer) Stop() {
  142. s.Node.Stop()
  143. close(s.done)
  144. }
  145. // Do interprets r and performs an operation on s.Store according to r.Method
  146. // and other fields. If r.Method is "POST", "PUT", "DELETE", or a "GET" with
  147. // Quorum == true, r will be sent through consensus before performing its
  148. // respective operation. Do will block until an action is performed or there is
  149. // an error.
  150. func (s *EtcdServer) Do(ctx context.Context, r pb.Request) (Response, error) {
  151. if r.Id == 0 {
  152. panic("r.Id cannot be 0")
  153. }
  154. if r.Method == "GET" && r.Quorum {
  155. r.Method = "QGET"
  156. }
  157. switch r.Method {
  158. case "POST", "PUT", "DELETE", "QGET":
  159. data, err := r.Marshal()
  160. if err != nil {
  161. return Response{}, err
  162. }
  163. ch := s.w.Register(r.Id)
  164. s.Node.Propose(ctx, data)
  165. select {
  166. case x := <-ch:
  167. resp := x.(Response)
  168. return resp, resp.err
  169. case <-ctx.Done():
  170. s.w.Trigger(r.Id, nil) // GC wait
  171. return Response{}, ctx.Err()
  172. case <-s.done:
  173. return Response{}, ErrStopped
  174. }
  175. case "GET":
  176. switch {
  177. case r.Wait:
  178. wc, err := s.Store.Watch(r.Path, r.Recursive, false, r.Since)
  179. if err != nil {
  180. return Response{}, err
  181. }
  182. return Response{Watcher: wc}, nil
  183. default:
  184. ev, err := s.Store.Get(r.Path, r.Recursive, r.Sorted)
  185. if err != nil {
  186. return Response{}, err
  187. }
  188. return Response{Event: ev}, nil
  189. }
  190. default:
  191. return Response{}, ErrUnknownMethod
  192. }
  193. }
  194. // sync proposes a SYNC request and is non-blocking.
  195. // This makes no guarantee that the request will be proposed or performed.
  196. // The request will be cancelled after the given timeout.
  197. func (s *EtcdServer) sync(timeout time.Duration) {
  198. ctx, cancel := context.WithTimeout(context.Background(), timeout)
  199. req := pb.Request{
  200. Method: "SYNC",
  201. Id: GenID(),
  202. Time: time.Now().UnixNano(),
  203. }
  204. data, err := req.Marshal()
  205. if err != nil {
  206. log.Printf("marshal request %#v error: %v", req, err)
  207. return
  208. }
  209. // There is no promise that node has leader when do SYNC request,
  210. // so it uses goroutine to propose.
  211. go func() {
  212. s.Node.Propose(ctx, data)
  213. cancel()
  214. }()
  215. }
  216. // apply interprets r as a call to store.X and returns an Response interpreted from store.Event
  217. func (s *EtcdServer) apply(r pb.Request) Response {
  218. f := func(ev *store.Event, err error) Response {
  219. return Response{Event: ev, err: err}
  220. }
  221. expr := time.Unix(0, r.Expiration)
  222. switch r.Method {
  223. case "POST":
  224. return f(s.Store.Create(r.Path, r.Dir, r.Val, true, expr))
  225. case "PUT":
  226. exists, existsSet := getBool(r.PrevExist)
  227. switch {
  228. case existsSet:
  229. if exists {
  230. return f(s.Store.Update(r.Path, r.Val, expr))
  231. } else {
  232. return f(s.Store.Create(r.Path, r.Dir, r.Val, false, expr))
  233. }
  234. case r.PrevIndex > 0 || r.PrevValue != "":
  235. return f(s.Store.CompareAndSwap(r.Path, r.PrevValue, r.PrevIndex, r.Val, expr))
  236. default:
  237. return f(s.Store.Set(r.Path, r.Dir, r.Val, expr))
  238. }
  239. case "DELETE":
  240. switch {
  241. case r.PrevIndex > 0 || r.PrevValue != "":
  242. return f(s.Store.CompareAndDelete(r.Path, r.PrevValue, r.PrevIndex))
  243. default:
  244. return f(s.Store.Delete(r.Path, r.Dir, r.Recursive))
  245. }
  246. case "QGET":
  247. return f(s.Store.Get(r.Path, r.Recursive, r.Sorted))
  248. case "SYNC":
  249. s.Store.DeleteExpiredKeys(time.Unix(0, r.Time))
  250. return Response{}
  251. default:
  252. // This should never be reached, but just in case:
  253. return Response{err: ErrUnknownMethod}
  254. }
  255. }
  256. // TODO: non-blocking snapshot
  257. func (s *EtcdServer) snapshot() {
  258. d, err := s.Store.Save()
  259. // TODO: current store will never fail to do a snapshot
  260. // what should we do if the store might fail?
  261. if err != nil {
  262. panic("TODO: this is bad, what do we do about it?")
  263. }
  264. s.Node.Compact(d)
  265. s.Storage.Cut()
  266. }
  267. // TODO: move the function to /id pkg maybe?
  268. // GenID generates a random id that is not equal to 0.
  269. func GenID() (n int64) {
  270. for n == 0 {
  271. n = rand.Int63()
  272. }
  273. return
  274. }
  275. func getBool(v *bool) (vv bool, set bool) {
  276. if v == nil {
  277. return false, false
  278. }
  279. return *v, true
  280. }