raft.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 etcdserver
  15. import (
  16. "encoding/json"
  17. "expvar"
  18. "sort"
  19. "sync"
  20. "sync/atomic"
  21. "time"
  22. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  23. "github.com/coreos/etcd/etcdserver/membership"
  24. "github.com/coreos/etcd/pkg/contention"
  25. "github.com/coreos/etcd/pkg/pbutil"
  26. "github.com/coreos/etcd/pkg/types"
  27. "github.com/coreos/etcd/raft"
  28. "github.com/coreos/etcd/raft/raftpb"
  29. "github.com/coreos/etcd/rafthttp"
  30. "github.com/coreos/etcd/wal"
  31. "github.com/coreos/etcd/wal/walpb"
  32. "github.com/coreos/pkg/capnslog"
  33. )
  34. const (
  35. // Number of entries for slow follower to catch-up after compacting
  36. // the raft storage entries.
  37. // We expect the follower has a millisecond level latency with the leader.
  38. // The max throughput is around 10K. Keep a 5K entries is enough for helping
  39. // follower to catch up.
  40. numberOfCatchUpEntries = 5000
  41. // The max throughput of etcd will not exceed 100MB/s (100K * 1KB value).
  42. // Assuming the RTT is around 10ms, 1MB max size is large enough.
  43. maxSizePerMsg = 1 * 1024 * 1024
  44. // Never overflow the rafthttp buffer, which is 4096.
  45. // TODO: a better const?
  46. maxInflightMsgs = 4096 / 8
  47. )
  48. var (
  49. // protects raftStatus
  50. raftStatusMu sync.Mutex
  51. // indirection for expvar func interface
  52. // expvar panics when publishing duplicate name
  53. // expvar does not support remove a registered name
  54. // so only register a func that calls raftStatus
  55. // and change raftStatus as we need.
  56. raftStatus func() raft.Status
  57. )
  58. func init() {
  59. raft.SetLogger(capnslog.NewPackageLogger("github.com/coreos/etcd", "raft"))
  60. expvar.Publish("raft.status", expvar.Func(func() interface{} {
  61. raftStatusMu.Lock()
  62. defer raftStatusMu.Unlock()
  63. return raftStatus()
  64. }))
  65. }
  66. type RaftTimer interface {
  67. Index() uint64
  68. Term() uint64
  69. }
  70. // apply contains entries, snapshot to be applied. Once
  71. // an apply is consumed, the entries will be persisted to
  72. // to raft storage concurrently; the application must read
  73. // raftDone before assuming the raft messages are stable.
  74. type apply struct {
  75. entries []raftpb.Entry
  76. snapshot raftpb.Snapshot
  77. raftDone <-chan struct{} // rx {} after raft has persisted messages
  78. }
  79. type raftNode struct {
  80. // Cache of the latest raft index and raft term the server has seen.
  81. // These three unit64 fields must be the first elements to keep 64-bit
  82. // alignment for atomic access to the fields.
  83. index uint64
  84. term uint64
  85. lead uint64
  86. raftNodeConfig
  87. // a chan to send/receive snapshot
  88. msgSnapC chan raftpb.Message
  89. // a chan to send out apply
  90. applyc chan apply
  91. // a chan to send out readState
  92. readStateC chan raft.ReadState
  93. // utility
  94. ticker *time.Ticker
  95. // contention detectors for raft heartbeat message
  96. td *contention.TimeoutDetector
  97. stopped chan struct{}
  98. done chan struct{}
  99. }
  100. type raftNodeConfig struct {
  101. // to check if msg receiver is removed from cluster
  102. isIDRemoved func(id uint64) bool
  103. raft.Node
  104. raftStorage *raft.MemoryStorage
  105. storage Storage
  106. heartbeat time.Duration // for logging
  107. // transport specifies the transport to send and receive msgs to members.
  108. // Sending messages MUST NOT block. It is okay to drop messages, since
  109. // clients should timeout and reissue their messages.
  110. // If transport is nil, server will panic.
  111. transport rafthttp.Transporter
  112. }
  113. func newRaftNode(cfg raftNodeConfig) *raftNode {
  114. r := &raftNode{
  115. raftNodeConfig: cfg,
  116. // set up contention detectors for raft heartbeat message.
  117. // expect to send a heartbeat within 2 heartbeat intervals.
  118. td: contention.NewTimeoutDetector(2 * cfg.heartbeat),
  119. readStateC: make(chan raft.ReadState, 1),
  120. msgSnapC: make(chan raftpb.Message, maxInFlightMsgSnap),
  121. applyc: make(chan apply),
  122. stopped: make(chan struct{}),
  123. done: make(chan struct{}),
  124. }
  125. if r.heartbeat == 0 {
  126. r.ticker = &time.Ticker{}
  127. } else {
  128. r.ticker = time.NewTicker(r.heartbeat)
  129. }
  130. return r
  131. }
  132. // start prepares and starts raftNode in a new goroutine. It is no longer safe
  133. // to modify the fields after it has been started.
  134. func (r *raftNode) start(rh *raftReadyHandler) {
  135. internalTimeout := time.Second
  136. go func() {
  137. defer r.onStop()
  138. islead := false
  139. for {
  140. select {
  141. case <-r.ticker.C:
  142. r.Tick()
  143. case rd := <-r.Ready():
  144. if rd.SoftState != nil {
  145. newLeader := rd.SoftState.Lead != raft.None && atomic.LoadUint64(&r.lead) != rd.SoftState.Lead
  146. if newLeader {
  147. leaderChanges.Inc()
  148. }
  149. if rd.SoftState.Lead == raft.None {
  150. hasLeader.Set(0)
  151. } else {
  152. hasLeader.Set(1)
  153. }
  154. atomic.StoreUint64(&r.lead, rd.SoftState.Lead)
  155. islead = rd.RaftState == raft.StateLeader
  156. rh.updateLeadership(newLeader)
  157. r.td.Reset()
  158. }
  159. if len(rd.ReadStates) != 0 {
  160. select {
  161. case r.readStateC <- rd.ReadStates[len(rd.ReadStates)-1]:
  162. case <-time.After(internalTimeout):
  163. plog.Warningf("timed out sending read state")
  164. case <-r.stopped:
  165. return
  166. }
  167. }
  168. raftDone := make(chan struct{}, 1)
  169. ap := apply{
  170. entries: rd.CommittedEntries,
  171. snapshot: rd.Snapshot,
  172. raftDone: raftDone,
  173. }
  174. updateCommittedIndex(&ap, rh)
  175. select {
  176. case r.applyc <- ap:
  177. case <-r.stopped:
  178. return
  179. }
  180. // the leader can write to its disk in parallel with replicating to the followers and them
  181. // writing to their disks.
  182. // For more details, check raft thesis 10.2.1
  183. if islead {
  184. // gofail: var raftBeforeLeaderSend struct{}
  185. r.transport.Send(r.processMessages(rd.Messages))
  186. }
  187. // gofail: var raftBeforeSave struct{}
  188. if err := r.storage.Save(rd.HardState, rd.Entries); err != nil {
  189. plog.Fatalf("raft save state and entries error: %v", err)
  190. }
  191. if !raft.IsEmptyHardState(rd.HardState) {
  192. proposalsCommitted.Set(float64(rd.HardState.Commit))
  193. }
  194. // gofail: var raftAfterSave struct{}
  195. if !raft.IsEmptySnap(rd.Snapshot) {
  196. // gofail: var raftBeforeSaveSnap struct{}
  197. if err := r.storage.SaveSnap(rd.Snapshot); err != nil {
  198. plog.Fatalf("raft save snapshot error: %v", err)
  199. }
  200. // gofail: var raftAfterSaveSnap struct{}
  201. r.raftStorage.ApplySnapshot(rd.Snapshot)
  202. plog.Infof("raft applied incoming snapshot at index %d", rd.Snapshot.Metadata.Index)
  203. // gofail: var raftAfterApplySnap struct{}
  204. }
  205. r.raftStorage.Append(rd.Entries)
  206. if !islead {
  207. // finish processing incoming messages before we signal raftdone chan
  208. msgs := r.processMessages(rd.Messages)
  209. // now unblocks 'applyAll' that waits on Raft log disk writes before triggering snapshots
  210. raftDone <- struct{}{}
  211. // Candidate or follower needs to wait for all pending configuration
  212. // changes to be applied before sending messages.
  213. // Otherwise we might incorrectly count votes (e.g. votes from removed members).
  214. // Also slow machine's follower raft-layer could proceed to become the leader
  215. // on its own single-node cluster, before apply-layer applies the config change.
  216. // We simply wait for ALL pending entries to be applied for now.
  217. // We might improve this later on if it causes unnecessary long blocking issues.
  218. waitApply := false
  219. for _, ent := range rd.CommittedEntries {
  220. if ent.Type == raftpb.EntryConfChange {
  221. waitApply = true
  222. break
  223. }
  224. }
  225. if waitApply {
  226. rh.waitForApply()
  227. }
  228. // gofail: var raftBeforeFollowerSend struct{}
  229. r.transport.Send(msgs)
  230. } else {
  231. // leader already processed 'MsgSnap' and signaled
  232. raftDone <- struct{}{}
  233. }
  234. r.Advance()
  235. case <-r.stopped:
  236. return
  237. }
  238. }
  239. }()
  240. }
  241. func updateCommittedIndex(ap *apply, rh *raftReadyHandler) {
  242. var ci uint64
  243. if len(ap.entries) != 0 {
  244. ci = ap.entries[len(ap.entries)-1].Index
  245. }
  246. if ap.snapshot.Metadata.Index > ci {
  247. ci = ap.snapshot.Metadata.Index
  248. }
  249. if ci != 0 {
  250. rh.updateCommittedIndex(ci)
  251. }
  252. }
  253. func (r *raftNode) processMessages(ms []raftpb.Message) []raftpb.Message {
  254. sentAppResp := false
  255. for i := len(ms) - 1; i >= 0; i-- {
  256. if r.isIDRemoved(ms[i].To) {
  257. ms[i].To = 0
  258. }
  259. if ms[i].Type == raftpb.MsgAppResp {
  260. if sentAppResp {
  261. ms[i].To = 0
  262. } else {
  263. sentAppResp = true
  264. }
  265. }
  266. if ms[i].Type == raftpb.MsgSnap {
  267. // There are two separate data store: the store for v2, and the KV for v3.
  268. // The msgSnap only contains the most recent snapshot of store without KV.
  269. // So we need to redirect the msgSnap to etcd server main loop for merging in the
  270. // current store snapshot and KV snapshot.
  271. select {
  272. case r.msgSnapC <- ms[i]:
  273. default:
  274. // drop msgSnap if the inflight chan if full.
  275. }
  276. ms[i].To = 0
  277. }
  278. if ms[i].Type == raftpb.MsgHeartbeat {
  279. ok, exceed := r.td.Observe(ms[i].To)
  280. if !ok {
  281. // TODO: limit request rate.
  282. plog.Warningf("failed to send out heartbeat on time (exceeded the %v timeout for %v)", r.heartbeat, exceed)
  283. plog.Warningf("server is likely overloaded")
  284. }
  285. }
  286. }
  287. return ms
  288. }
  289. func (r *raftNode) apply() chan apply {
  290. return r.applyc
  291. }
  292. func (r *raftNode) stop() {
  293. r.stopped <- struct{}{}
  294. <-r.done
  295. }
  296. func (r *raftNode) onStop() {
  297. r.Stop()
  298. r.ticker.Stop()
  299. r.transport.Stop()
  300. if err := r.storage.Close(); err != nil {
  301. plog.Panicf("raft close storage error: %v", err)
  302. }
  303. close(r.done)
  304. }
  305. // for testing
  306. func (r *raftNode) pauseSending() {
  307. p := r.transport.(rafthttp.Pausable)
  308. p.Pause()
  309. }
  310. func (r *raftNode) resumeSending() {
  311. p := r.transport.(rafthttp.Pausable)
  312. p.Resume()
  313. }
  314. // advanceTicksForElection advances ticks to the node for fast election.
  315. // This reduces the time to wait for first leader election if bootstrapping the whole
  316. // cluster, while leaving at least 1 heartbeat for possible existing leader
  317. // to contact it.
  318. func advanceTicksForElection(n raft.Node, electionTicks int) {
  319. for i := 0; i < electionTicks-1; i++ {
  320. n.Tick()
  321. }
  322. }
  323. func startNode(cfg *ServerConfig, cl *membership.RaftCluster, ids []types.ID) (id types.ID, n raft.Node, s *raft.MemoryStorage, w *wal.WAL) {
  324. var err error
  325. member := cl.MemberByName(cfg.Name)
  326. metadata := pbutil.MustMarshal(
  327. &pb.Metadata{
  328. NodeID: uint64(member.ID),
  329. ClusterID: uint64(cl.ID()),
  330. },
  331. )
  332. if w, err = wal.Create(cfg.WALDir(), metadata); err != nil {
  333. plog.Fatalf("create wal error: %v", err)
  334. }
  335. peers := make([]raft.Peer, len(ids))
  336. for i, id := range ids {
  337. ctx, err := json.Marshal((*cl).Member(id))
  338. if err != nil {
  339. plog.Panicf("marshal member should never fail: %v", err)
  340. }
  341. peers[i] = raft.Peer{ID: uint64(id), Context: ctx}
  342. }
  343. id = member.ID
  344. plog.Infof("starting member %s in cluster %s", id, cl.ID())
  345. s = raft.NewMemoryStorage()
  346. c := &raft.Config{
  347. ID: uint64(id),
  348. ElectionTick: cfg.ElectionTicks,
  349. HeartbeatTick: 1,
  350. Storage: s,
  351. MaxSizePerMsg: maxSizePerMsg,
  352. MaxInflightMsgs: maxInflightMsgs,
  353. CheckQuorum: true,
  354. }
  355. n = raft.StartNode(c, peers)
  356. raftStatusMu.Lock()
  357. raftStatus = n.Status
  358. raftStatusMu.Unlock()
  359. advanceTicksForElection(n, c.ElectionTick)
  360. return
  361. }
  362. func restartNode(cfg *ServerConfig, snapshot *raftpb.Snapshot) (types.ID, *membership.RaftCluster, raft.Node, *raft.MemoryStorage, *wal.WAL) {
  363. var walsnap walpb.Snapshot
  364. if snapshot != nil {
  365. walsnap.Index, walsnap.Term = snapshot.Metadata.Index, snapshot.Metadata.Term
  366. }
  367. w, id, cid, st, ents := readWAL(cfg.WALDir(), walsnap)
  368. plog.Infof("restarting member %s in cluster %s at commit index %d", id, cid, st.Commit)
  369. cl := membership.NewCluster("")
  370. cl.SetID(cid)
  371. s := raft.NewMemoryStorage()
  372. if snapshot != nil {
  373. s.ApplySnapshot(*snapshot)
  374. }
  375. s.SetHardState(st)
  376. s.Append(ents)
  377. c := &raft.Config{
  378. ID: uint64(id),
  379. ElectionTick: cfg.ElectionTicks,
  380. HeartbeatTick: 1,
  381. Storage: s,
  382. MaxSizePerMsg: maxSizePerMsg,
  383. MaxInflightMsgs: maxInflightMsgs,
  384. CheckQuorum: true,
  385. }
  386. n := raft.RestartNode(c)
  387. raftStatusMu.Lock()
  388. raftStatus = n.Status
  389. raftStatusMu.Unlock()
  390. advanceTicksForElection(n, c.ElectionTick)
  391. return id, cl, n, s, w
  392. }
  393. func restartAsStandaloneNode(cfg *ServerConfig, snapshot *raftpb.Snapshot) (types.ID, *membership.RaftCluster, raft.Node, *raft.MemoryStorage, *wal.WAL) {
  394. var walsnap walpb.Snapshot
  395. if snapshot != nil {
  396. walsnap.Index, walsnap.Term = snapshot.Metadata.Index, snapshot.Metadata.Term
  397. }
  398. w, id, cid, st, ents := readWAL(cfg.WALDir(), walsnap)
  399. // discard the previously uncommitted entries
  400. for i, ent := range ents {
  401. if ent.Index > st.Commit {
  402. plog.Infof("discarding %d uncommitted WAL entries ", len(ents)-i)
  403. ents = ents[:i]
  404. break
  405. }
  406. }
  407. // force append the configuration change entries
  408. toAppEnts := createConfigChangeEnts(getIDs(snapshot, ents), uint64(id), st.Term, st.Commit)
  409. ents = append(ents, toAppEnts...)
  410. // force commit newly appended entries
  411. err := w.Save(raftpb.HardState{}, toAppEnts)
  412. if err != nil {
  413. plog.Fatalf("%v", err)
  414. }
  415. if len(ents) != 0 {
  416. st.Commit = ents[len(ents)-1].Index
  417. }
  418. plog.Printf("forcing restart of member %s in cluster %s at commit index %d", id, cid, st.Commit)
  419. cl := membership.NewCluster("")
  420. cl.SetID(cid)
  421. s := raft.NewMemoryStorage()
  422. if snapshot != nil {
  423. s.ApplySnapshot(*snapshot)
  424. }
  425. s.SetHardState(st)
  426. s.Append(ents)
  427. c := &raft.Config{
  428. ID: uint64(id),
  429. ElectionTick: cfg.ElectionTicks,
  430. HeartbeatTick: 1,
  431. Storage: s,
  432. MaxSizePerMsg: maxSizePerMsg,
  433. MaxInflightMsgs: maxInflightMsgs,
  434. }
  435. n := raft.RestartNode(c)
  436. raftStatus = n.Status
  437. return id, cl, n, s, w
  438. }
  439. // getIDs returns an ordered set of IDs included in the given snapshot and
  440. // the entries. The given snapshot/entries can contain two kinds of
  441. // ID-related entry:
  442. // - ConfChangeAddNode, in which case the contained ID will be added into the set.
  443. // - ConfChangeRemoveNode, in which case the contained ID will be removed from the set.
  444. func getIDs(snap *raftpb.Snapshot, ents []raftpb.Entry) []uint64 {
  445. ids := make(map[uint64]bool)
  446. if snap != nil {
  447. for _, id := range snap.Metadata.ConfState.Nodes {
  448. ids[id] = true
  449. }
  450. }
  451. for _, e := range ents {
  452. if e.Type != raftpb.EntryConfChange {
  453. continue
  454. }
  455. var cc raftpb.ConfChange
  456. pbutil.MustUnmarshal(&cc, e.Data)
  457. switch cc.Type {
  458. case raftpb.ConfChangeAddNode:
  459. ids[cc.NodeID] = true
  460. case raftpb.ConfChangeRemoveNode:
  461. delete(ids, cc.NodeID)
  462. case raftpb.ConfChangeUpdateNode:
  463. // do nothing
  464. default:
  465. plog.Panicf("ConfChange Type should be either ConfChangeAddNode or ConfChangeRemoveNode!")
  466. }
  467. }
  468. sids := make(types.Uint64Slice, 0, len(ids))
  469. for id := range ids {
  470. sids = append(sids, id)
  471. }
  472. sort.Sort(sids)
  473. return []uint64(sids)
  474. }
  475. // createConfigChangeEnts creates a series of Raft entries (i.e.
  476. // EntryConfChange) to remove the set of given IDs from the cluster. The ID
  477. // `self` is _not_ removed, even if present in the set.
  478. // If `self` is not inside the given ids, it creates a Raft entry to add a
  479. // default member with the given `self`.
  480. func createConfigChangeEnts(ids []uint64, self uint64, term, index uint64) []raftpb.Entry {
  481. ents := make([]raftpb.Entry, 0)
  482. next := index + 1
  483. found := false
  484. for _, id := range ids {
  485. if id == self {
  486. found = true
  487. continue
  488. }
  489. cc := &raftpb.ConfChange{
  490. Type: raftpb.ConfChangeRemoveNode,
  491. NodeID: id,
  492. }
  493. e := raftpb.Entry{
  494. Type: raftpb.EntryConfChange,
  495. Data: pbutil.MustMarshal(cc),
  496. Term: term,
  497. Index: next,
  498. }
  499. ents = append(ents, e)
  500. next++
  501. }
  502. if !found {
  503. m := membership.Member{
  504. ID: types.ID(self),
  505. RaftAttributes: membership.RaftAttributes{PeerURLs: []string{"http://localhost:2380"}},
  506. }
  507. ctx, err := json.Marshal(m)
  508. if err != nil {
  509. plog.Panicf("marshal member should never fail: %v", err)
  510. }
  511. cc := &raftpb.ConfChange{
  512. Type: raftpb.ConfChangeAddNode,
  513. NodeID: self,
  514. Context: ctx,
  515. }
  516. e := raftpb.Entry{
  517. Type: raftpb.EntryConfChange,
  518. Data: pbutil.MustMarshal(cc),
  519. Term: term,
  520. Index: next,
  521. }
  522. ents = append(ents, e)
  523. }
  524. return ents
  525. }