rawnode.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 "go.etcd.io/etcd/v3/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 entries were applied (or a snapshot), update our cursor for
  43. // the next Ready. Note that if the current HardState contains a
  44. // new Commit index, this does not mean that we're also applying
  45. // all of the new entries due to commit pagination by size.
  46. if index := rd.appliedCursor(); index > 0 {
  47. rn.raft.raftLog.appliedTo(index)
  48. }
  49. if len(rd.Entries) > 0 {
  50. e := rd.Entries[len(rd.Entries)-1]
  51. rn.raft.raftLog.stableTo(e.Index, e.Term)
  52. }
  53. if !IsEmptySnap(rd.Snapshot) {
  54. rn.raft.raftLog.stableSnapTo(rd.Snapshot.Metadata.Index)
  55. }
  56. if len(rd.ReadStates) != 0 {
  57. rn.raft.readStates = nil
  58. }
  59. }
  60. // NewRawNode returns a new RawNode given configuration and a list of raft peers.
  61. func NewRawNode(config *Config, peers []Peer) (*RawNode, error) {
  62. if config.ID == 0 {
  63. panic("config.ID must not be zero")
  64. }
  65. r := newRaft(config)
  66. rn := &RawNode{
  67. raft: r,
  68. }
  69. lastIndex, err := config.Storage.LastIndex()
  70. if err != nil {
  71. panic(err) // TODO(bdarnell)
  72. }
  73. // If the log is empty, this is a new RawNode (like StartNode); otherwise it's
  74. // restoring an existing RawNode (like RestartNode).
  75. // TODO(bdarnell): rethink RawNode initialization and whether the application needs
  76. // to be able to tell us when it expects the RawNode to exist.
  77. if lastIndex == 0 {
  78. r.becomeFollower(1, None)
  79. ents := make([]pb.Entry, len(peers))
  80. for i, peer := range peers {
  81. cc := pb.ConfChange{Type: pb.ConfChangeAddNode, NodeID: peer.ID, Context: peer.Context}
  82. data, err := cc.Marshal()
  83. if err != nil {
  84. panic("unexpected marshal error")
  85. }
  86. ents[i] = pb.Entry{Type: pb.EntryConfChange, Term: 1, Index: uint64(i + 1), Data: data}
  87. }
  88. r.raftLog.append(ents...)
  89. r.raftLog.committed = uint64(len(ents))
  90. for _, peer := range peers {
  91. r.addNode(peer.ID)
  92. }
  93. }
  94. // Set the initial hard and soft states after performing all initialization.
  95. rn.prevSoftSt = r.softState()
  96. if lastIndex == 0 {
  97. rn.prevHardSt = emptyState
  98. } else {
  99. rn.prevHardSt = r.hardState()
  100. }
  101. return rn, nil
  102. }
  103. // Tick advances the internal logical clock by a single tick.
  104. func (rn *RawNode) Tick() {
  105. rn.raft.tick()
  106. }
  107. // TickQuiesced advances the internal logical clock by a single tick without
  108. // performing any other state machine processing. It allows the caller to avoid
  109. // periodic heartbeats and elections when all of the peers in a Raft group are
  110. // known to be at the same state. Expected usage is to periodically invoke Tick
  111. // or TickQuiesced depending on whether the group is "active" or "quiesced".
  112. //
  113. // WARNING: Be very careful about using this method as it subverts the Raft
  114. // state machine. You should probably be using Tick instead.
  115. func (rn *RawNode) TickQuiesced() {
  116. rn.raft.electionElapsed++
  117. }
  118. // Campaign causes this RawNode to transition to candidate state.
  119. func (rn *RawNode) Campaign() error {
  120. return rn.raft.Step(pb.Message{
  121. Type: pb.MsgHup,
  122. })
  123. }
  124. // Propose proposes data be appended to the raft log.
  125. func (rn *RawNode) Propose(data []byte) error {
  126. return rn.raft.Step(pb.Message{
  127. Type: pb.MsgProp,
  128. From: rn.raft.id,
  129. Entries: []pb.Entry{
  130. {Data: data},
  131. }})
  132. }
  133. // ProposeConfChange proposes a config change.
  134. func (rn *RawNode) ProposeConfChange(cc pb.ConfChange) error {
  135. data, err := cc.Marshal()
  136. if err != nil {
  137. return err
  138. }
  139. return rn.raft.Step(pb.Message{
  140. Type: pb.MsgProp,
  141. Entries: []pb.Entry{
  142. {Type: pb.EntryConfChange, Data: data},
  143. },
  144. })
  145. }
  146. // ApplyConfChange applies a config change to the local node.
  147. func (rn *RawNode) ApplyConfChange(cc pb.ConfChange) *pb.ConfState {
  148. if cc.NodeID == None {
  149. return &pb.ConfState{Nodes: rn.raft.prs.voterNodes(), Learners: rn.raft.prs.learnerNodes()}
  150. }
  151. switch cc.Type {
  152. case pb.ConfChangeAddNode:
  153. rn.raft.addNode(cc.NodeID)
  154. case pb.ConfChangeAddLearnerNode:
  155. rn.raft.addLearner(cc.NodeID)
  156. case pb.ConfChangeRemoveNode:
  157. rn.raft.removeNode(cc.NodeID)
  158. case pb.ConfChangeUpdateNode:
  159. default:
  160. panic("unexpected conf type")
  161. }
  162. return &pb.ConfState{Nodes: rn.raft.prs.voterNodes(), Learners: rn.raft.prs.learnerNodes()}
  163. }
  164. // Step advances the state machine using the given message.
  165. func (rn *RawNode) Step(m pb.Message) error {
  166. // ignore unexpected local messages receiving over network
  167. if IsLocalMsg(m.Type) {
  168. return ErrStepLocalMsg
  169. }
  170. if pr := rn.raft.prs.getProgress(m.From); pr != nil || !IsResponseMsg(m.Type) {
  171. return rn.raft.Step(m)
  172. }
  173. return ErrStepPeerNotFound
  174. }
  175. // Ready returns the current point-in-time state of this RawNode.
  176. func (rn *RawNode) Ready() Ready {
  177. rd := rn.newReady()
  178. rn.raft.msgs = nil
  179. rn.raft.reduceUncommittedSize(rd.CommittedEntries)
  180. return rd
  181. }
  182. // HasReady called when RawNode user need to check if any Ready pending.
  183. // Checking logic in this method should be consistent with Ready.containsUpdates().
  184. func (rn *RawNode) HasReady() bool {
  185. r := rn.raft
  186. if !r.softState().equal(rn.prevSoftSt) {
  187. return true
  188. }
  189. if hardSt := r.hardState(); !IsEmptyHardState(hardSt) && !isHardStateEqual(hardSt, rn.prevHardSt) {
  190. return true
  191. }
  192. if r.raftLog.unstable.snapshot != nil && !IsEmptySnap(*r.raftLog.unstable.snapshot) {
  193. return true
  194. }
  195. if len(r.msgs) > 0 || len(r.raftLog.unstableEntries()) > 0 || r.raftLog.hasNextEnts() {
  196. return true
  197. }
  198. if len(r.readStates) != 0 {
  199. return true
  200. }
  201. return false
  202. }
  203. // Advance notifies the RawNode that the application has applied and saved progress in the
  204. // last Ready results.
  205. func (rn *RawNode) Advance(rd Ready) {
  206. rn.commitReady(rd)
  207. }
  208. // Status returns the current status of the given group.
  209. func (rn *RawNode) Status() *Status {
  210. status := getStatus(rn.raft)
  211. return &status
  212. }
  213. // StatusWithoutProgress returns a Status without populating the Progress field
  214. // (and returns the Status as a value to avoid forcing it onto the heap). This
  215. // is more performant if the Progress is not required. See WithProgress for an
  216. // allocation-free way to introspect the Progress.
  217. func (rn *RawNode) StatusWithoutProgress() Status {
  218. return getStatusWithoutProgress(rn.raft)
  219. }
  220. // ProgressType indicates the type of replica a Progress corresponds to.
  221. type ProgressType byte
  222. const (
  223. // ProgressTypePeer accompanies a Progress for a regular peer replica.
  224. ProgressTypePeer ProgressType = iota
  225. // ProgressTypeLearner accompanies a Progress for a learner replica.
  226. ProgressTypeLearner
  227. )
  228. // WithProgress is a helper to introspect the Progress for this node and its
  229. // peers.
  230. func (rn *RawNode) WithProgress(visitor func(id uint64, typ ProgressType, pr Progress)) {
  231. for id, pr := range rn.raft.prs.nodes {
  232. pr := *pr
  233. pr.ins = nil
  234. visitor(id, ProgressTypePeer, pr)
  235. }
  236. for id, pr := range rn.raft.prs.learners {
  237. pr := *pr
  238. pr.ins = nil
  239. visitor(id, ProgressTypeLearner, pr)
  240. }
  241. }
  242. // ReportUnreachable reports the given node is not reachable for the last send.
  243. func (rn *RawNode) ReportUnreachable(id uint64) {
  244. _ = rn.raft.Step(pb.Message{Type: pb.MsgUnreachable, From: id})
  245. }
  246. // ReportSnapshot reports the status of the sent snapshot.
  247. func (rn *RawNode) ReportSnapshot(id uint64, status SnapshotStatus) {
  248. rej := status == SnapshotFailure
  249. _ = rn.raft.Step(pb.Message{Type: pb.MsgSnapStatus, From: id, Reject: rej})
  250. }
  251. // TransferLeader tries to transfer leadership to the given transferee.
  252. func (rn *RawNode) TransferLeader(transferee uint64) {
  253. _ = rn.raft.Step(pb.Message{Type: pb.MsgTransferLeader, From: transferee})
  254. }
  255. // ReadIndex requests a read state. The read state will be set in ready.
  256. // Read State has a read index. Once the application advances further than the read
  257. // index, any linearizable read requests issued before the read request can be
  258. // processed safely. The read state will have the same rctx attached.
  259. func (rn *RawNode) ReadIndex(rctx []byte) {
  260. _ = rn.raft.Step(pb.Message{Type: pb.MsgReadIndex, Entries: []pb.Entry{{Data: rctx}}})
  261. }