doc.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. The primary object in raft is a Node. You either start a Node from scratch
  17. using raft.StartNode or start a Node from some initial state using raft.RestartNode.
  18. storage := raft.NewMemoryStorage()
  19. n := raft.StartNode(0x01, []raft.Peer{{ID: 0x02}, {ID: 0x03}}, 3, 1, storage)
  20. Now that you are holding onto a Node you have a few responsibilities:
  21. First, you need to push messages that you receive from other machines into the
  22. Node with n.Step().
  23. func recvRaftRPC(ctx context.Context, m raftpb.Message) {
  24. n.Step(ctx, m)
  25. }
  26. Second, you need to save log entries to storage, process committed log entries
  27. through your application and then send pending messages to peers by reading the
  28. channel returned by n.Ready(). It is important that the user persist any
  29. entries that require stable storage before sending messages to other peers to
  30. ensure fault-tolerance.
  31. An example MemoryStorage is provided in the raft package.
  32. And finally you need to service timeouts with Tick(). Raft has two important
  33. timeouts: heartbeat and the election timeout. However, internally to the raft
  34. package time is represented by an abstract "tick". The user is responsible for
  35. calling Tick() on their raft.Node on a regular interval in order to service
  36. these timeouts.
  37. The total state machine handling loop will look something like this:
  38. for {
  39. select {
  40. case <-s.Ticker:
  41. n.Tick()
  42. case rd := <-s.Node.Ready():
  43. saveToStorage(rd.State, rd.Entries)
  44. send(rd.Messages)
  45. process(rd.CommittedEntries)
  46. s.Node.Advance()
  47. case <-s.done:
  48. return
  49. }
  50. }
  51. To propose changes to the state machine from your node take your application
  52. data, serialize it into a byte slice and call:
  53. n.Propose(ctx, data)
  54. If the proposal is committed, data will appear in committed entries with type
  55. raftpb.EntryNormal.
  56. To add or remove node in a cluster, build ConfChange struct 'cc' and call:
  57. n.ProposeConfChange(ctx, cc)
  58. After config change is committed, some committed entry with type
  59. raftpb.EntryConfChange will be returned. You should apply it to node through:
  60. var cc raftpb.ConfChange
  61. cc.Unmarshal(data)
  62. n.ApplyConfChange(cc)
  63. Note: An ID represents a unique node in a cluster. A given ID MUST be used
  64. only once even if the old node has been removed.
  65. */
  66. package raft