rawnode.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. }
  62. // NewRawNode returns a new RawNode given configuration and a list of raft peers.
  63. func NewRawNode(config *Config, peers []Peer) (*RawNode, error) {
  64. if config.ID == 0 {
  65. panic("config.ID must not be zero")
  66. }
  67. r := newRaft(config)
  68. rn := &RawNode{
  69. raft: r,
  70. }
  71. lastIndex, err := config.Storage.LastIndex()
  72. if err != nil {
  73. panic(err) // TODO(bdarnell)
  74. }
  75. // If the log is empty, this is a new RawNode (like StartNode); otherwise it's
  76. // restoring an existing RawNode (like RestartNode).
  77. // TODO(bdarnell): rethink RawNode initialization and whether the application needs
  78. // to be able to tell us when it expects the RawNode to exist.
  79. if lastIndex == 0 {
  80. r.becomeFollower(1, None)
  81. ents := make([]pb.Entry, len(peers))
  82. for i, peer := range peers {
  83. cc := pb.ConfChange{Type: pb.ConfChangeAddNode, NodeID: peer.ID, Context: peer.Context}
  84. data, err := cc.Marshal()
  85. if err != nil {
  86. panic("unexpected marshal error")
  87. }
  88. ents[i] = pb.Entry{Type: pb.EntryConfChange, Term: 1, Index: uint64(i + 1), Data: data}
  89. }
  90. r.raftLog.append(ents...)
  91. r.raftLog.committed = uint64(len(ents))
  92. for _, peer := range peers {
  93. r.addNode(peer.ID)
  94. }
  95. }
  96. // Set the initial hard and soft states after performing all initialization.
  97. rn.prevSoftSt = r.softState()
  98. rn.prevHardSt = r.hardState()
  99. return rn, nil
  100. }
  101. // Tick advances the internal logical clock by a single tick.
  102. func (rn *RawNode) Tick() {
  103. rn.raft.tick()
  104. }
  105. // Campaign causes this RawNode to transition to candidate state.
  106. func (rn *RawNode) Campaign() error {
  107. return rn.raft.Step(pb.Message{
  108. Type: pb.MsgHup,
  109. })
  110. }
  111. // Propose proposes data be appended to the raft log.
  112. func (rn *RawNode) Propose(data []byte) error {
  113. return rn.raft.Step(pb.Message{
  114. Type: pb.MsgProp,
  115. From: rn.raft.id,
  116. Entries: []pb.Entry{
  117. {Data: data},
  118. }})
  119. }
  120. // ProposeConfChange proposes a config change.
  121. func (rn *RawNode) ProposeConfChange(cc pb.ConfChange) error {
  122. data, err := cc.Marshal()
  123. if err != nil {
  124. return err
  125. }
  126. return rn.raft.Step(pb.Message{
  127. Type: pb.MsgProp,
  128. Entries: []pb.Entry{
  129. {Type: pb.EntryConfChange, Data: data},
  130. },
  131. })
  132. }
  133. // ApplyConfChange applies a config change to the local node.
  134. func (rn *RawNode) ApplyConfChange(cc pb.ConfChange) *pb.ConfState {
  135. if cc.NodeID == None {
  136. rn.raft.resetPendingConf()
  137. return &pb.ConfState{Nodes: rn.raft.nodes()}
  138. }
  139. switch cc.Type {
  140. case pb.ConfChangeAddNode:
  141. rn.raft.addNode(cc.NodeID)
  142. case pb.ConfChangeRemoveNode:
  143. rn.raft.removeNode(cc.NodeID)
  144. case pb.ConfChangeUpdateNode:
  145. rn.raft.resetPendingConf()
  146. default:
  147. panic("unexpected conf type")
  148. }
  149. return &pb.ConfState{Nodes: rn.raft.nodes()}
  150. }
  151. // Step advances the state machine using the given message.
  152. func (rn *RawNode) Step(m pb.Message) error {
  153. // ignore unexpected local messages receiving over network
  154. if IsLocalMsg(m.Type) {
  155. return ErrStepLocalMsg
  156. }
  157. if _, ok := rn.raft.prs[m.From]; ok || !IsResponseMsg(m.Type) {
  158. return rn.raft.Step(m)
  159. }
  160. return ErrStepPeerNotFound
  161. }
  162. // Ready returns the current point-in-time state of this RawNode.
  163. func (rn *RawNode) Ready() Ready {
  164. rd := rn.newReady()
  165. rn.raft.msgs = nil
  166. return rd
  167. }
  168. // HasReady called when RawNode user need to check if any Ready pending.
  169. // Checking logic in this method should be consistent with Ready.containsUpdates().
  170. func (rn *RawNode) HasReady() bool {
  171. r := rn.raft
  172. if !r.softState().equal(rn.prevSoftSt) {
  173. return true
  174. }
  175. if hardSt := r.hardState(); !IsEmptyHardState(hardSt) && !isHardStateEqual(hardSt, rn.prevHardSt) {
  176. return true
  177. }
  178. if r.raftLog.unstable.snapshot != nil && !IsEmptySnap(*r.raftLog.unstable.snapshot) {
  179. return true
  180. }
  181. if len(r.msgs) > 0 || len(r.raftLog.unstableEntries()) > 0 || r.raftLog.hasNextEnts() {
  182. return true
  183. }
  184. return false
  185. }
  186. // Advance notifies the RawNode that the application has applied and saved progress in the
  187. // last Ready results.
  188. func (rn *RawNode) Advance(rd Ready) {
  189. rn.commitReady(rd)
  190. }
  191. // Status returns the current status of the given group.
  192. func (rn *RawNode) Status() *Status {
  193. status := getStatus(rn.raft)
  194. return &status
  195. }
  196. // ReportUnreachable reports the given node is not reachable for the last send.
  197. func (rn *RawNode) ReportUnreachable(id uint64) {
  198. _ = rn.raft.Step(pb.Message{Type: pb.MsgUnreachable, From: id})
  199. }
  200. // ReportSnapshot reports the status of the sent snapshot.
  201. func (rn *RawNode) ReportSnapshot(id uint64, status SnapshotStatus) {
  202. rej := status == SnapshotFailure
  203. _ = rn.raft.Step(pb.Message{Type: pb.MsgSnapStatus, From: id, Reject: rej})
  204. }
  205. // TransferLeader tries to transfer leadership to the given transferee.
  206. func (rn *RawNode) TransferLeader(transferee uint64) {
  207. _ = rn.raft.Step(pb.Message{Type: pb.MsgTransferLeader, From: transferee})
  208. }