raft.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. mu sync.Mutex
  87. tickMu sync.Mutex
  88. // last lead elected time
  89. lt time.Time
  90. // to check if msg receiver is removed from cluster
  91. isIDRemoved func(id uint64) bool
  92. raft.Node
  93. // a chan to send/receive snapshot
  94. msgSnapC chan raftpb.Message
  95. // a chan to send out apply
  96. applyc chan apply
  97. // a chan to send out readState
  98. readStateC chan raft.ReadState
  99. // utility
  100. ticker <-chan time.Time
  101. // contention detectors for raft heartbeat message
  102. td *contention.TimeoutDetector
  103. heartbeat time.Duration // for logging
  104. raftStorage *raft.MemoryStorage
  105. storage Storage
  106. // transport specifies the transport to send and receive msgs to members.
  107. // Sending messages MUST NOT block. It is okay to drop messages, since
  108. // clients should timeout and reissue their messages.
  109. // If transport is nil, server will panic.
  110. transport rafthttp.Transporter
  111. stopped chan struct{}
  112. done chan struct{}
  113. }
  114. // raft.Node does not have locks in Raft package
  115. func (r *raftNode) tick() {
  116. r.tickMu.Lock()
  117. r.Tick()
  118. r.tickMu.Unlock()
  119. }
  120. // start prepares and starts raftNode in a new goroutine. It is no longer safe
  121. // to modify the fields after it has been started.
  122. func (r *raftNode) start(rh *raftReadyHandler) {
  123. r.applyc = make(chan apply)
  124. r.stopped = make(chan struct{})
  125. r.done = make(chan struct{})
  126. internalTimeout := time.Second
  127. go func() {
  128. defer r.onStop()
  129. islead := false
  130. for {
  131. select {
  132. case <-r.ticker:
  133. r.tick()
  134. case rd := <-r.Ready():
  135. if rd.SoftState != nil {
  136. if lead := atomic.LoadUint64(&r.lead); rd.SoftState.Lead != raft.None && lead != rd.SoftState.Lead {
  137. r.mu.Lock()
  138. r.lt = time.Now()
  139. r.mu.Unlock()
  140. leaderChanges.Inc()
  141. }
  142. if rd.SoftState.Lead == raft.None {
  143. hasLeader.Set(0)
  144. } else {
  145. hasLeader.Set(1)
  146. }
  147. atomic.StoreUint64(&r.lead, rd.SoftState.Lead)
  148. islead = rd.RaftState == raft.StateLeader
  149. if islead {
  150. isLeader.Set(1)
  151. } else {
  152. isLeader.Set(0)
  153. }
  154. rh.updateLeadership()
  155. }
  156. if len(rd.ReadStates) != 0 {
  157. select {
  158. case r.readStateC <- rd.ReadStates[len(rd.ReadStates)-1]:
  159. case <-time.After(internalTimeout):
  160. plog.Warningf("timed out sending read state")
  161. case <-r.stopped:
  162. return
  163. }
  164. }
  165. raftDone := make(chan struct{}, 1)
  166. ap := apply{
  167. entries: rd.CommittedEntries,
  168. snapshot: rd.Snapshot,
  169. raftDone: raftDone,
  170. }
  171. updateCommittedIndex(&ap, rh)
  172. select {
  173. case r.applyc <- ap:
  174. case <-r.stopped:
  175. return
  176. }
  177. // the leader can write to its disk in parallel with replicating to the followers and them
  178. // writing to their disks.
  179. // For more details, check raft thesis 10.2.1
  180. if islead {
  181. // gofail: var raftBeforeLeaderSend struct{}
  182. r.sendMessages(rd.Messages)
  183. }
  184. // gofail: var raftBeforeSave struct{}
  185. if err := r.storage.Save(rd.HardState, rd.Entries); err != nil {
  186. plog.Fatalf("raft save state and entries error: %v", err)
  187. }
  188. if !raft.IsEmptyHardState(rd.HardState) {
  189. proposalsCommitted.Set(float64(rd.HardState.Commit))
  190. }
  191. // gofail: var raftAfterSave struct{}
  192. if !raft.IsEmptySnap(rd.Snapshot) {
  193. // gofail: var raftBeforeSaveSnap struct{}
  194. if err := r.storage.SaveSnap(rd.Snapshot); err != nil {
  195. plog.Fatalf("raft save snapshot error: %v", err)
  196. }
  197. // gofail: var raftAfterSaveSnap struct{}
  198. r.raftStorage.ApplySnapshot(rd.Snapshot)
  199. plog.Infof("raft applied incoming snapshot at index %d", rd.Snapshot.Metadata.Index)
  200. // gofail: var raftAfterApplySnap struct{}
  201. }
  202. r.raftStorage.Append(rd.Entries)
  203. if !islead {
  204. // gofail: var raftBeforeFollowerSend struct{}
  205. r.sendMessages(rd.Messages)
  206. }
  207. raftDone <- struct{}{}
  208. r.Advance()
  209. case <-r.stopped:
  210. return
  211. }
  212. }
  213. }()
  214. }
  215. func updateCommittedIndex(ap *apply, rh *raftReadyHandler) {
  216. var ci uint64
  217. if len(ap.entries) != 0 {
  218. ci = ap.entries[len(ap.entries)-1].Index
  219. }
  220. if ap.snapshot.Metadata.Index > ci {
  221. ci = ap.snapshot.Metadata.Index
  222. }
  223. if ci != 0 {
  224. rh.updateCommittedIndex(ci)
  225. }
  226. }
  227. func (r *raftNode) sendMessages(ms []raftpb.Message) {
  228. sentAppResp := false
  229. for i := len(ms) - 1; i >= 0; i-- {
  230. if r.isIDRemoved(ms[i].To) {
  231. ms[i].To = 0
  232. }
  233. if ms[i].Type == raftpb.MsgAppResp {
  234. if sentAppResp {
  235. ms[i].To = 0
  236. } else {
  237. sentAppResp = true
  238. }
  239. }
  240. if ms[i].Type == raftpb.MsgSnap {
  241. // There are two separate data store: the store for v2, and the KV for v3.
  242. // The msgSnap only contains the most recent snapshot of store without KV.
  243. // So we need to redirect the msgSnap to etcd server main loop for merging in the
  244. // current store snapshot and KV snapshot.
  245. select {
  246. case r.msgSnapC <- ms[i]:
  247. default:
  248. // drop msgSnap if the inflight chan if full.
  249. }
  250. ms[i].To = 0
  251. }
  252. if ms[i].Type == raftpb.MsgHeartbeat {
  253. ok, exceed := r.td.Observe(ms[i].To)
  254. if !ok {
  255. // TODO: limit request rate.
  256. plog.Warningf("failed to send out heartbeat on time (exceeded the %v timeout for %v)", r.heartbeat, exceed)
  257. plog.Warningf("server is likely overloaded")
  258. heartbeatSendFailures.Inc()
  259. }
  260. }
  261. }
  262. r.transport.Send(ms)
  263. }
  264. func (r *raftNode) apply() chan apply {
  265. return r.applyc
  266. }
  267. func (r *raftNode) leadElectedTime() time.Time {
  268. r.mu.Lock()
  269. defer r.mu.Unlock()
  270. return r.lt
  271. }
  272. func (r *raftNode) stop() {
  273. r.stopped <- struct{}{}
  274. <-r.done
  275. }
  276. func (r *raftNode) onStop() {
  277. r.Stop()
  278. r.transport.Stop()
  279. if err := r.storage.Close(); err != nil {
  280. plog.Panicf("raft close storage error: %v", err)
  281. }
  282. close(r.done)
  283. }
  284. // for testing
  285. func (r *raftNode) pauseSending() {
  286. p := r.transport.(rafthttp.Pausable)
  287. p.Pause()
  288. }
  289. func (r *raftNode) resumeSending() {
  290. p := r.transport.(rafthttp.Pausable)
  291. p.Resume()
  292. }
  293. // advanceTicks advances ticks of Raft node.
  294. // This can be used for fast-forwarding election
  295. // ticks in multi data-center deployments, thus
  296. // speeding up election process.
  297. func (r *raftNode) advanceTicks(ticks int) {
  298. for i := 0; i < ticks; i++ {
  299. r.tick()
  300. }
  301. }
  302. func startNode(cfg *ServerConfig, cl *membership.RaftCluster, ids []types.ID) (id types.ID, n raft.Node, s *raft.MemoryStorage, w *wal.WAL) {
  303. var err error
  304. member := cl.MemberByName(cfg.Name)
  305. metadata := pbutil.MustMarshal(
  306. &pb.Metadata{
  307. NodeID: uint64(member.ID),
  308. ClusterID: uint64(cl.ID()),
  309. },
  310. )
  311. if w, err = wal.Create(cfg.WALDir(), metadata); err != nil {
  312. plog.Fatalf("create wal error: %v", err)
  313. }
  314. peers := make([]raft.Peer, len(ids))
  315. for i, id := range ids {
  316. ctx, err := json.Marshal((*cl).Member(id))
  317. if err != nil {
  318. plog.Panicf("marshal member should never fail: %v", err)
  319. }
  320. peers[i] = raft.Peer{ID: uint64(id), Context: ctx}
  321. }
  322. id = member.ID
  323. plog.Infof("starting member %s in cluster %s", id, cl.ID())
  324. s = raft.NewMemoryStorage()
  325. c := &raft.Config{
  326. ID: uint64(id),
  327. ElectionTick: cfg.ElectionTicks,
  328. HeartbeatTick: 1,
  329. Storage: s,
  330. MaxSizePerMsg: maxSizePerMsg,
  331. MaxInflightMsgs: maxInflightMsgs,
  332. CheckQuorum: true,
  333. }
  334. n = raft.StartNode(c, peers)
  335. raftStatusMu.Lock()
  336. raftStatus = n.Status
  337. raftStatusMu.Unlock()
  338. return id, n, s, w
  339. }
  340. func restartNode(cfg *ServerConfig, snapshot *raftpb.Snapshot) (types.ID, *membership.RaftCluster, raft.Node, *raft.MemoryStorage, *wal.WAL) {
  341. var walsnap walpb.Snapshot
  342. if snapshot != nil {
  343. walsnap.Index, walsnap.Term = snapshot.Metadata.Index, snapshot.Metadata.Term
  344. }
  345. w, id, cid, st, ents := readWAL(cfg.WALDir(), walsnap)
  346. plog.Infof("restarting member %s in cluster %s at commit index %d", id, cid, st.Commit)
  347. cl := membership.NewCluster("")
  348. cl.SetID(cid)
  349. s := raft.NewMemoryStorage()
  350. if snapshot != nil {
  351. s.ApplySnapshot(*snapshot)
  352. }
  353. s.SetHardState(st)
  354. s.Append(ents)
  355. c := &raft.Config{
  356. ID: uint64(id),
  357. ElectionTick: cfg.ElectionTicks,
  358. HeartbeatTick: 1,
  359. Storage: s,
  360. MaxSizePerMsg: maxSizePerMsg,
  361. MaxInflightMsgs: maxInflightMsgs,
  362. CheckQuorum: true,
  363. }
  364. n := raft.RestartNode(c)
  365. raftStatusMu.Lock()
  366. raftStatus = n.Status
  367. raftStatusMu.Unlock()
  368. return id, cl, n, s, w
  369. }
  370. func restartAsStandaloneNode(cfg *ServerConfig, snapshot *raftpb.Snapshot) (types.ID, *membership.RaftCluster, raft.Node, *raft.MemoryStorage, *wal.WAL) {
  371. var walsnap walpb.Snapshot
  372. if snapshot != nil {
  373. walsnap.Index, walsnap.Term = snapshot.Metadata.Index, snapshot.Metadata.Term
  374. }
  375. w, id, cid, st, ents := readWAL(cfg.WALDir(), walsnap)
  376. // discard the previously uncommitted entries
  377. for i, ent := range ents {
  378. if ent.Index > st.Commit {
  379. plog.Infof("discarding %d uncommitted WAL entries ", len(ents)-i)
  380. ents = ents[:i]
  381. break
  382. }
  383. }
  384. // force append the configuration change entries
  385. toAppEnts := createConfigChangeEnts(getIDs(snapshot, ents), uint64(id), st.Term, st.Commit)
  386. ents = append(ents, toAppEnts...)
  387. // force commit newly appended entries
  388. err := w.Save(raftpb.HardState{}, toAppEnts)
  389. if err != nil {
  390. plog.Fatalf("%v", err)
  391. }
  392. if len(ents) != 0 {
  393. st.Commit = ents[len(ents)-1].Index
  394. }
  395. plog.Printf("forcing restart of member %s in cluster %s at commit index %d", id, cid, st.Commit)
  396. cl := membership.NewCluster("")
  397. cl.SetID(cid)
  398. s := raft.NewMemoryStorage()
  399. if snapshot != nil {
  400. s.ApplySnapshot(*snapshot)
  401. }
  402. s.SetHardState(st)
  403. s.Append(ents)
  404. c := &raft.Config{
  405. ID: uint64(id),
  406. ElectionTick: cfg.ElectionTicks,
  407. HeartbeatTick: 1,
  408. Storage: s,
  409. MaxSizePerMsg: maxSizePerMsg,
  410. MaxInflightMsgs: maxInflightMsgs,
  411. }
  412. n := raft.RestartNode(c)
  413. raftStatus = n.Status
  414. return id, cl, n, s, w
  415. }
  416. // getIDs returns an ordered set of IDs included in the given snapshot and
  417. // the entries. The given snapshot/entries can contain two kinds of
  418. // ID-related entry:
  419. // - ConfChangeAddNode, in which case the contained ID will be added into the set.
  420. // - ConfChangeRemoveNode, in which case the contained ID will be removed from the set.
  421. func getIDs(snap *raftpb.Snapshot, ents []raftpb.Entry) []uint64 {
  422. ids := make(map[uint64]bool)
  423. if snap != nil {
  424. for _, id := range snap.Metadata.ConfState.Nodes {
  425. ids[id] = true
  426. }
  427. }
  428. for _, e := range ents {
  429. if e.Type != raftpb.EntryConfChange {
  430. continue
  431. }
  432. var cc raftpb.ConfChange
  433. pbutil.MustUnmarshal(&cc, e.Data)
  434. switch cc.Type {
  435. case raftpb.ConfChangeAddNode:
  436. ids[cc.NodeID] = true
  437. case raftpb.ConfChangeRemoveNode:
  438. delete(ids, cc.NodeID)
  439. case raftpb.ConfChangeUpdateNode:
  440. // do nothing
  441. default:
  442. plog.Panicf("ConfChange Type should be either ConfChangeAddNode or ConfChangeRemoveNode!")
  443. }
  444. }
  445. sids := make(types.Uint64Slice, 0, len(ids))
  446. for id := range ids {
  447. sids = append(sids, id)
  448. }
  449. sort.Sort(sids)
  450. return []uint64(sids)
  451. }
  452. // createConfigChangeEnts creates a series of Raft entries (i.e.
  453. // EntryConfChange) to remove the set of given IDs from the cluster. The ID
  454. // `self` is _not_ removed, even if present in the set.
  455. // If `self` is not inside the given ids, it creates a Raft entry to add a
  456. // default member with the given `self`.
  457. func createConfigChangeEnts(ids []uint64, self uint64, term, index uint64) []raftpb.Entry {
  458. ents := make([]raftpb.Entry, 0)
  459. next := index + 1
  460. found := false
  461. for _, id := range ids {
  462. if id == self {
  463. found = true
  464. continue
  465. }
  466. cc := &raftpb.ConfChange{
  467. Type: raftpb.ConfChangeRemoveNode,
  468. NodeID: id,
  469. }
  470. e := raftpb.Entry{
  471. Type: raftpb.EntryConfChange,
  472. Data: pbutil.MustMarshal(cc),
  473. Term: term,
  474. Index: next,
  475. }
  476. ents = append(ents, e)
  477. next++
  478. }
  479. if !found {
  480. m := membership.Member{
  481. ID: types.ID(self),
  482. RaftAttributes: membership.RaftAttributes{PeerURLs: []string{"http://localhost:2380"}},
  483. }
  484. ctx, err := json.Marshal(m)
  485. if err != nil {
  486. plog.Panicf("marshal member should never fail: %v", err)
  487. }
  488. cc := &raftpb.ConfChange{
  489. Type: raftpb.ConfChangeAddNode,
  490. NodeID: self,
  491. Context: ctx,
  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. }
  501. return ents
  502. }