rawnode.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Copyright 2015 The etcd Authors
  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. package raft
  15. import (
  16. "errors"
  17. pb "github.com/coreos/etcd/raft/raftpb"
  18. )
  19. // ErrStepLocalMsg is returned when try to step a local raft message
  20. var ErrStepLocalMsg = errors.New("raft: cannot step raft local message")
  21. // ErrStepPeerNotFound is returned when try to step a response message
  22. // but there is no peer found in raft.prs for that node.
  23. var ErrStepPeerNotFound = errors.New("raft: cannot step as peer not found")
  24. // RawNode is a thread-unsafe Node.
  25. // The methods of this struct correspond to the methods of Node and are described
  26. // more fully there.
  27. type RawNode struct {
  28. raft *raft
  29. prevSoftSt *SoftState
  30. prevHardSt pb.HardState
  31. }
  32. func (rn *RawNode) newReady() Ready {
  33. return newReady(rn.raft, rn.prevSoftSt, rn.prevHardSt)
  34. }
  35. func (rn *RawNode) commitReady(rd Ready) {
  36. if rd.SoftState != nil {
  37. rn.prevSoftSt = rd.SoftState
  38. }
  39. if !IsEmptyHardState(rd.HardState) {
  40. rn.prevHardSt = rd.HardState
  41. }
  42. if rn.prevHardSt.Commit != 0 {
  43. // In most cases, prevHardSt and rd.HardState will be the same
  44. // because when there are new entries to apply we just sent a
  45. // HardState with an updated Commit value. However, on initial
  46. // startup the two are different because we don't send a HardState
  47. // until something changes, but we do send any un-applied but
  48. // committed entries (and previously-committed entries may be
  49. // incorporated into the snapshot, even if rd.CommittedEntries is
  50. // empty). Therefore we mark all committed entries as applied
  51. // whether they were included in rd.HardState or not.
  52. rn.raft.raftLog.appliedTo(rn.prevHardSt.Commit)
  53. }
  54. if len(rd.Entries) > 0 {
  55. e := rd.Entries[len(rd.Entries)-1]
  56. rn.raft.raftLog.stableTo(e.Index, e.Term)
  57. }
  58. if !IsEmptySnap(rd.Snapshot) {
  59. rn.raft.raftLog.stableSnapTo(rd.Snapshot.Metadata.Index)
  60. }
  61. if len(rd.ReadStates) != 0 {
  62. rn.raft.readStates = nil
  63. }
  64. }
  65. // NewRawNode returns a new RawNode given configuration and a list of raft peers.
  66. func NewRawNode(config *Config, peers []Peer) (*RawNode, error) {
  67. if config.ID == 0 {
  68. panic("config.ID must not be zero")
  69. }
  70. r := newRaft(config)
  71. rn := &RawNode{
  72. raft: r,
  73. }
  74. lastIndex, err := config.Storage.LastIndex()
  75. if err != nil {
  76. panic(err) // TODO(bdarnell)
  77. }
  78. // If the log is empty, this is a new RawNode (like StartNode); otherwise it's
  79. // restoring an existing RawNode (like RestartNode).
  80. // TODO(bdarnell): rethink RawNode initialization and whether the application needs
  81. // to be able to tell us when it expects the RawNode to exist.
  82. if lastIndex == 0 {
  83. r.becomeFollower(1, None)
  84. ents := make([]pb.Entry, len(peers))
  85. for i, peer := range peers {
  86. cc := pb.ConfChange{Type: pb.ConfChangeAddNode, NodeID: peer.ID, Context: peer.Context}
  87. data, err := cc.Marshal()
  88. if err != nil {
  89. panic("unexpected marshal error")
  90. }
  91. ents[i] = pb.Entry{Type: pb.EntryConfChange, Term: 1, Index: uint64(i + 1), Data: data}
  92. }
  93. r.raftLog.append(ents...)
  94. r.raftLog.committed = uint64(len(ents))
  95. for _, peer := range peers {
  96. r.addNode(peer.ID)
  97. }
  98. }
  99. // Set the initial hard and soft states after performing all initialization.
  100. rn.prevSoftSt = r.softState()
  101. if lastIndex == 0 {
  102. rn.prevHardSt = emptyState
  103. } else {
  104. rn.prevHardSt = r.hardState()
  105. }
  106. return rn, nil
  107. }
  108. // Tick advances the internal logical clock by a single tick.
  109. func (rn *RawNode) Tick() {
  110. rn.raft.tick()
  111. }
  112. // Campaign causes this RawNode to transition to candidate state.
  113. func (rn *RawNode) Campaign() error {
  114. return rn.raft.Step(pb.Message{
  115. Type: pb.MsgHup,
  116. })
  117. }
  118. // Propose proposes data be appended to the raft log.
  119. func (rn *RawNode) Propose(data []byte) error {
  120. return rn.raft.Step(pb.Message{
  121. Type: pb.MsgProp,
  122. From: rn.raft.id,
  123. Entries: []pb.Entry{
  124. {Data: data},
  125. }})
  126. }
  127. // ProposeConfChange proposes a config change.
  128. func (rn *RawNode) ProposeConfChange(cc pb.ConfChange) error {
  129. data, err := cc.Marshal()
  130. if err != nil {
  131. return err
  132. }
  133. return rn.raft.Step(pb.Message{
  134. Type: pb.MsgProp,
  135. Entries: []pb.Entry{
  136. {Type: pb.EntryConfChange, Data: data},
  137. },
  138. })
  139. }
  140. // ApplyConfChange applies a config change to the local node.
  141. func (rn *RawNode) ApplyConfChange(cc pb.ConfChange) *pb.ConfState {
  142. if cc.NodeID == None {
  143. rn.raft.resetPendingConf()
  144. return &pb.ConfState{Nodes: rn.raft.nodes()}
  145. }
  146. switch cc.Type {
  147. case pb.ConfChangeAddNode:
  148. rn.raft.addNode(cc.NodeID)
  149. case pb.ConfChangeRemoveNode:
  150. rn.raft.removeNode(cc.NodeID)
  151. case pb.ConfChangeUpdateNode:
  152. rn.raft.resetPendingConf()
  153. default:
  154. panic("unexpected conf type")
  155. }
  156. return &pb.ConfState{Nodes: rn.raft.nodes()}
  157. }
  158. // Step advances the state machine using the given message.
  159. func (rn *RawNode) Step(m pb.Message) error {
  160. // ignore unexpected local messages receiving over network
  161. if IsLocalMsg(m.Type) {
  162. return ErrStepLocalMsg
  163. }
  164. if _, ok := rn.raft.prs[m.From]; ok || !IsResponseMsg(m.Type) {
  165. return rn.raft.Step(m)
  166. }
  167. return ErrStepPeerNotFound
  168. }
  169. // Ready returns the current point-in-time state of this RawNode.
  170. func (rn *RawNode) Ready() Ready {
  171. rd := rn.newReady()
  172. rn.raft.msgs = nil
  173. return rd
  174. }
  175. // HasReady called when RawNode user need to check if any Ready pending.
  176. // Checking logic in this method should be consistent with Ready.containsUpdates().
  177. func (rn *RawNode) HasReady() bool {
  178. r := rn.raft
  179. if !r.softState().equal(rn.prevSoftSt) {
  180. return true
  181. }
  182. if hardSt := r.hardState(); !IsEmptyHardState(hardSt) && !isHardStateEqual(hardSt, rn.prevHardSt) {
  183. return true
  184. }
  185. if r.raftLog.unstable.snapshot != nil && !IsEmptySnap(*r.raftLog.unstable.snapshot) {
  186. return true
  187. }
  188. if len(r.msgs) > 0 || len(r.raftLog.unstableEntries()) > 0 || r.raftLog.hasNextEnts() {
  189. return true
  190. }
  191. if len(r.readStates) != 0 {
  192. return true
  193. }
  194. return false
  195. }
  196. // Advance notifies the RawNode that the application has applied and saved progress in the
  197. // last Ready results.
  198. func (rn *RawNode) Advance(rd Ready) {
  199. rn.commitReady(rd)
  200. }
  201. // Status returns the current status of the given group.
  202. func (rn *RawNode) Status() *Status {
  203. status := getStatus(rn.raft)
  204. return &status
  205. }
  206. // ReportUnreachable reports the given node is not reachable for the last send.
  207. func (rn *RawNode) ReportUnreachable(id uint64) {
  208. _ = rn.raft.Step(pb.Message{Type: pb.MsgUnreachable, From: id})
  209. }
  210. // ReportSnapshot reports the status of the sent snapshot.
  211. func (rn *RawNode) ReportSnapshot(id uint64, status SnapshotStatus) {
  212. rej := status == SnapshotFailure
  213. _ = rn.raft.Step(pb.Message{Type: pb.MsgSnapStatus, From: id, Reject: rej})
  214. }
  215. // TransferLeader tries to transfer leadership to the given transferee.
  216. func (rn *RawNode) TransferLeader(transferee uint64) {
  217. _ = rn.raft.Step(pb.Message{Type: pb.MsgTransferLeader, From: transferee})
  218. }
  219. // ReadIndex requests a read state. The read state will be set in ready.
  220. // Read State has a read index. Once the application advances further than the read
  221. // index, any linearizable read requests issued before the read request can be
  222. // processed safely. The read state will have the same rctx attached.
  223. func (rn *RawNode) ReadIndex(rctx []byte) {
  224. _ = rn.raft.Step(pb.Message{Type: pb.MsgReadIndex, Entries: []pb.Entry{{Data: rctx}}})
  225. }