rawnode.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. // TickQuiesced advances the internal logical clock by a single tick without
  113. // performing any other state machine processing. It allows the caller to avoid
  114. // periodic heartbeats and elections when all of the peers in a Raft group are
  115. // known to be at the same state. Expected usage is to periodically invoke Tick
  116. // or TickQuiesced depending on whether the group is "active" or "quiesced".
  117. //
  118. // WARNING: Be very careful about using this method as it subverts the Raft
  119. // state machine. You should probably be using Tick instead.
  120. func (rn *RawNode) TickQuiesced() {
  121. rn.raft.electionElapsed++
  122. }
  123. // Campaign causes this RawNode to transition to candidate state.
  124. func (rn *RawNode) Campaign() error {
  125. return rn.raft.Step(pb.Message{
  126. Type: pb.MsgHup,
  127. })
  128. }
  129. // Propose proposes data be appended to the raft log.
  130. func (rn *RawNode) Propose(data []byte) error {
  131. return rn.raft.Step(pb.Message{
  132. Type: pb.MsgProp,
  133. From: rn.raft.id,
  134. Entries: []pb.Entry{
  135. {Data: data},
  136. }})
  137. }
  138. // ProposeConfChange proposes a config change.
  139. func (rn *RawNode) ProposeConfChange(cc pb.ConfChange) error {
  140. data, err := cc.Marshal()
  141. if err != nil {
  142. return err
  143. }
  144. return rn.raft.Step(pb.Message{
  145. Type: pb.MsgProp,
  146. Entries: []pb.Entry{
  147. {Type: pb.EntryConfChange, Data: data},
  148. },
  149. })
  150. }
  151. // ApplyConfChange applies a config change to the local node.
  152. func (rn *RawNode) ApplyConfChange(cc pb.ConfChange) *pb.ConfState {
  153. if cc.NodeID == None {
  154. rn.raft.resetPendingConf()
  155. return &pb.ConfState{Nodes: rn.raft.nodes()}
  156. }
  157. switch cc.Type {
  158. case pb.ConfChangeAddNode:
  159. rn.raft.addNode(cc.NodeID)
  160. case pb.ConfChangeAddLearnerNode:
  161. rn.raft.addLearner(cc.NodeID)
  162. case pb.ConfChangeRemoveNode:
  163. rn.raft.removeNode(cc.NodeID)
  164. case pb.ConfChangeUpdateNode:
  165. rn.raft.resetPendingConf()
  166. default:
  167. panic("unexpected conf type")
  168. }
  169. return &pb.ConfState{Nodes: rn.raft.nodes()}
  170. }
  171. // Step advances the state machine using the given message.
  172. func (rn *RawNode) Step(m pb.Message) error {
  173. // ignore unexpected local messages receiving over network
  174. if IsLocalMsg(m.Type) {
  175. return ErrStepLocalMsg
  176. }
  177. if pr := rn.raft.getProgress(m.From); pr != nil || !IsResponseMsg(m.Type) {
  178. return rn.raft.Step(m)
  179. }
  180. return ErrStepPeerNotFound
  181. }
  182. // Ready returns the current point-in-time state of this RawNode.
  183. func (rn *RawNode) Ready() Ready {
  184. rd := rn.newReady()
  185. rn.raft.msgs = nil
  186. return rd
  187. }
  188. // HasReady called when RawNode user need to check if any Ready pending.
  189. // Checking logic in this method should be consistent with Ready.containsUpdates().
  190. func (rn *RawNode) HasReady() bool {
  191. r := rn.raft
  192. if !r.softState().equal(rn.prevSoftSt) {
  193. return true
  194. }
  195. if hardSt := r.hardState(); !IsEmptyHardState(hardSt) && !isHardStateEqual(hardSt, rn.prevHardSt) {
  196. return true
  197. }
  198. if r.raftLog.unstable.snapshot != nil && !IsEmptySnap(*r.raftLog.unstable.snapshot) {
  199. return true
  200. }
  201. if len(r.msgs) > 0 || len(r.raftLog.unstableEntries()) > 0 || r.raftLog.hasNextEnts() {
  202. return true
  203. }
  204. if len(r.readStates) != 0 {
  205. return true
  206. }
  207. return false
  208. }
  209. // Advance notifies the RawNode that the application has applied and saved progress in the
  210. // last Ready results.
  211. func (rn *RawNode) Advance(rd Ready) {
  212. rn.commitReady(rd)
  213. }
  214. // Status returns the current status of the given group.
  215. func (rn *RawNode) Status() *Status {
  216. status := getStatus(rn.raft)
  217. return &status
  218. }
  219. // ReportUnreachable reports the given node is not reachable for the last send.
  220. func (rn *RawNode) ReportUnreachable(id uint64) {
  221. _ = rn.raft.Step(pb.Message{Type: pb.MsgUnreachable, From: id})
  222. }
  223. // ReportSnapshot reports the status of the sent snapshot.
  224. func (rn *RawNode) ReportSnapshot(id uint64, status SnapshotStatus) {
  225. rej := status == SnapshotFailure
  226. _ = rn.raft.Step(pb.Message{Type: pb.MsgSnapStatus, From: id, Reject: rej})
  227. }
  228. // TransferLeader tries to transfer leadership to the given transferee.
  229. func (rn *RawNode) TransferLeader(transferee uint64) {
  230. _ = rn.raft.Step(pb.Message{Type: pb.MsgTransferLeader, From: transferee})
  231. }
  232. // ReadIndex requests a read state. The read state will be set in ready.
  233. // Read State has a read index. Once the application advances further than the read
  234. // index, any linearizable read requests issued before the read request can be
  235. // processed safely. The read state will have the same rctx attached.
  236. func (rn *RawNode) ReadIndex(rctx []byte) {
  237. _ = rn.raft.Step(pb.Message{Type: pb.MsgReadIndex, Entries: []pb.Entry{{Data: rctx}}})
  238. }