doc.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Package raft provides an implementation of the raft consensus algorithm.
  16. Raft is a protocol with which a cluster of nodes can maintain a replicated state machine.
  17. The state machine is kept in sync through the use of a replicated log.
  18. For more details on Raft, see "In Search of an Understandable Consensus Algorithm"
  19. (https://ramcloud.stanford.edu/raft.pdf) by Diego Ongaro and John Ousterhout.
  20. A simple example application, _raftexample_, is also available to help illustrate
  21. how to use this package in practice:
  22. https://github.com/coreos/etcd/tree/master/contrib/raftexample
  23. Usage
  24. The primary object in raft is a Node. You either start a Node from scratch
  25. using raft.StartNode or start a Node from some initial state using raft.RestartNode.
  26. To start a node from scratch:
  27. storage := raft.NewMemoryStorage()
  28. c := &Config{
  29. ID: 0x01,
  30. ElectionTick: 10,
  31. HeartbeatTick: 1,
  32. Storage: storage,
  33. MaxSizePerMsg: 4096,
  34. MaxInflightMsgs: 256,
  35. }
  36. n := raft.StartNode(c, []raft.Peer{{ID: 0x02}, {ID: 0x03}})
  37. To restart a node from previous state:
  38. storage := raft.NewMemoryStorage()
  39. // recover the in-memory storage from persistent
  40. // snapshot, state and entries.
  41. storage.ApplySnapshot(snapshot)
  42. storage.SetHardState(state)
  43. storage.Append(entries)
  44. c := &Config{
  45. ID: 0x01,
  46. ElectionTick: 10,
  47. HeartbeatTick: 1,
  48. Storage: storage,
  49. MaxSizePerMsg: 4096,
  50. MaxInflightMsgs: 256,
  51. }
  52. // restart raft without peer information.
  53. // peer information is already included in the storage.
  54. n := raft.RestartNode(c)
  55. Now that you are holding onto a Node you have a few responsibilities:
  56. First, you must read from the Node.Ready() channel and process the updates
  57. it contains. These steps may be performed in parallel, except as noted in step
  58. 2.
  59. 1. Write HardState, Entries, and Snapshot to persistent storage if they are
  60. not empty. Note that when writing an Entry with Index i, any
  61. previously-persisted entries with Index >= i must be discarded.
  62. 2. Send all Messages to the nodes named in the To field. It is important that
  63. no messages be sent until after the latest HardState has been persisted to disk,
  64. and all Entries written by any previous Ready batch (Messages may be sent while
  65. entries from the same batch are being persisted). If any Message has type MsgSnap,
  66. call Node.ReportSnapshot() after it has been sent (these messages may be large).
  67. 3. Apply Snapshot (if any) and CommittedEntries to the state machine.
  68. If any committed Entry has Type EntryConfChange, call Node.ApplyConfChange()
  69. to apply it to the node. The configuration change may be cancelled at this point
  70. by setting the NodeID field to zero before calling ApplyConfChange
  71. (but ApplyConfChange must be called one way or the other, and the decision to cancel
  72. must be based solely on the state machine and not external information such as
  73. the observed health of the node).
  74. 4. Call Node.Advance() to signal readiness for the next batch of updates.
  75. This may be done at any time after step 1, although all updates must be processed
  76. in the order they were returned by Ready.
  77. Second, all persisted log entries must be made available via an
  78. implementation of the Storage interface. The provided MemoryStorage
  79. type can be used for this (if you repopulate its state upon a
  80. restart), or you can supply your own disk-backed implementation.
  81. Third, when you receive a message from another node, pass it to Node.Step:
  82. func recvRaftRPC(ctx context.Context, m raftpb.Message) {
  83. n.Step(ctx, m)
  84. }
  85. Finally, you need to call Node.Tick() at regular intervals (probably
  86. via a time.Ticker). Raft has two important timeouts: heartbeat and the
  87. election timeout. However, internally to the raft package time is
  88. represented by an abstract "tick".
  89. The total state machine handling loop will look something like this:
  90. for {
  91. select {
  92. case <-s.Ticker:
  93. n.Tick()
  94. case rd := <-s.Node.Ready():
  95. saveToStorage(rd.State, rd.Entries, rd.Snapshot)
  96. send(rd.Messages)
  97. if !raft.IsEmptySnap(rd.Snapshot) {
  98. processSnapshot(rd.Snapshot)
  99. }
  100. for _, entry := range rd.CommittedEntries {
  101. process(entry)
  102. if entry.Type == raftpb.EntryConfChange {
  103. var cc raftpb.ConfChange
  104. cc.Unmarshal(entry.Data)
  105. s.Node.ApplyConfChange(cc)
  106. }
  107. s.Node.Advance()
  108. case <-s.done:
  109. return
  110. }
  111. }
  112. To propose changes to the state machine from your node take your application
  113. data, serialize it into a byte slice and call:
  114. n.Propose(ctx, data)
  115. If the proposal is committed, data will appear in committed entries with type
  116. raftpb.EntryNormal. There is no guarantee that a proposed command will be
  117. committed; you may have to re-propose after a timeout.
  118. To add or remove node in a cluster, build ConfChange struct 'cc' and call:
  119. n.ProposeConfChange(ctx, cc)
  120. After config change is committed, some committed entry with type
  121. raftpb.EntryConfChange will be returned. You must apply it to node through:
  122. var cc raftpb.ConfChange
  123. cc.Unmarshal(data)
  124. n.ApplyConfChange(cc)
  125. Note: An ID represents a unique node in a cluster for all time. A
  126. given ID MUST be used only once even if the old node has been removed.
  127. This means that for example IP addresses make poor node IDs since they
  128. may be reused. Node IDs must be non-zero.
  129. Implementation notes
  130. This implementation is up to date with the final Raft thesis
  131. (https://ramcloud.stanford.edu/~ongaro/thesis.pdf), although our
  132. implementation of the membership change protocol differs somewhat from
  133. that described in chapter 4. The key invariant that membership changes
  134. happen one node at a time is preserved, but in our implementation the
  135. membership change takes effect when its entry is applied, not when it
  136. is added to the log (so the entry is committed under the old
  137. membership instead of the new). This is equivalent in terms of safety,
  138. since the old and new configurations are guaranteed to overlap.
  139. To ensure that we do not attempt to commit two membership changes at
  140. once by matching log positions (which would be unsafe since they
  141. should have different quorum requirements), we simply disallow any
  142. proposed membership change while any uncommitted change appears in
  143. the leader's log.
  144. This approach introduces a problem when you try to remove a member
  145. from a two-member cluster: If one of the members dies before the
  146. other one receives the commit of the confchange entry, then the member
  147. cannot be removed any more since the cluster cannot make progress.
  148. For this reason it is highly recommended to use three or more nodes in
  149. every cluster.
  150. */
  151. package raft