force_cluster.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcdserver
  14. import (
  15. "encoding/json"
  16. "log"
  17. "sort"
  18. "github.com/coreos/etcd/pkg/pbutil"
  19. "github.com/coreos/etcd/pkg/types"
  20. "github.com/coreos/etcd/raft"
  21. "github.com/coreos/etcd/raft/raftpb"
  22. "github.com/coreos/etcd/wal"
  23. )
  24. func restartAsStandaloneNode(cfg *ServerConfig, index uint64, snapshot *raftpb.Snapshot) (types.ID, raft.Node, *raft.MemoryStorage, *wal.WAL) {
  25. w, id, cid, st, ents := readWAL(cfg.WALDir(), index)
  26. cfg.Cluster.SetID(cid)
  27. // discard the previously uncommitted entries
  28. for i, ent := range ents {
  29. if ent.Index > st.Commit {
  30. log.Printf("etcdserver: discarding %d uncommited WAL entries ", len(ents)-i)
  31. ents = ents[:i]
  32. break
  33. }
  34. }
  35. // force append the configuration change entries
  36. toAppEnts := createConfigChangeEnts(getIDs(snapshot, ents), uint64(id), st.Term, st.Commit)
  37. ents = append(ents, toAppEnts...)
  38. // force commit newly appended entries
  39. for _, e := range toAppEnts {
  40. err := w.SaveEntry(&e)
  41. if err != nil {
  42. log.Fatalf("etcdserver: %v", err)
  43. }
  44. }
  45. if len(ents) != 0 {
  46. st.Commit = ents[len(ents)-1].Index
  47. }
  48. log.Printf("etcdserver: forcing restart of member %s in cluster %s at commit index %d", id, cfg.Cluster.ID(), st.Commit)
  49. s := raft.NewMemoryStorage()
  50. if snapshot != nil {
  51. s.ApplySnapshot(*snapshot)
  52. }
  53. s.SetHardState(st)
  54. s.Append(ents)
  55. n := raft.RestartNode(uint64(id), 10, 1, s)
  56. return id, n, s, w
  57. }
  58. // getIDs returns an ordered set of IDs included in the given snapshot and
  59. // the entries. The given snapshot/entries can contain two kinds of
  60. // ID-related entry:
  61. // - ConfChangeAddNode, in which case the contained ID will be added into the set.
  62. // - ConfChangeAddRemove, in which case the contained ID will be removed from the set.
  63. func getIDs(snap *raftpb.Snapshot, ents []raftpb.Entry) []uint64 {
  64. ids := make(map[uint64]bool)
  65. if snap != nil {
  66. for _, id := range snap.Metadata.ConfState.Nodes {
  67. ids[id] = true
  68. }
  69. }
  70. for _, e := range ents {
  71. if e.Type != raftpb.EntryConfChange {
  72. continue
  73. }
  74. var cc raftpb.ConfChange
  75. pbutil.MustUnmarshal(&cc, e.Data)
  76. switch cc.Type {
  77. case raftpb.ConfChangeAddNode:
  78. ids[cc.NodeID] = true
  79. case raftpb.ConfChangeRemoveNode:
  80. delete(ids, cc.NodeID)
  81. default:
  82. log.Panicf("ConfChange Type should be either ConfChangeAddNode or ConfChangeRemoveNode!")
  83. }
  84. }
  85. sids := make(types.Uint64Slice, 0)
  86. for id := range ids {
  87. sids = append(sids, id)
  88. }
  89. sort.Sort(sids)
  90. return []uint64(sids)
  91. }
  92. // createConfigChangeEnts creates a series of Raft entries (i.e.
  93. // EntryConfChange) to remove the set of given IDs from the cluster. The ID
  94. // `self` is _not_ removed, even if present in the set.
  95. // If `self` is not inside the given ids, it creates a Raft entry to add a
  96. // default member with the given `self`.
  97. func createConfigChangeEnts(ids []uint64, self uint64, term, index uint64) []raftpb.Entry {
  98. ents := make([]raftpb.Entry, 0)
  99. next := index + 1
  100. found := false
  101. for _, id := range ids {
  102. if id == self {
  103. found = true
  104. continue
  105. }
  106. cc := &raftpb.ConfChange{
  107. Type: raftpb.ConfChangeRemoveNode,
  108. NodeID: id,
  109. }
  110. e := raftpb.Entry{
  111. Type: raftpb.EntryConfChange,
  112. Data: pbutil.MustMarshal(cc),
  113. Term: term,
  114. Index: next,
  115. }
  116. ents = append(ents, e)
  117. next++
  118. }
  119. if !found {
  120. m := Member{
  121. ID: types.ID(self),
  122. RaftAttributes: RaftAttributes{PeerURLs: []string{"http://localhost:7001", "http://localhost:2380"}},
  123. }
  124. ctx, err := json.Marshal(m)
  125. if err != nil {
  126. log.Panicf("marshal member should never fail: %v", err)
  127. }
  128. cc := &raftpb.ConfChange{
  129. Type: raftpb.ConfChangeAddNode,
  130. NodeID: self,
  131. Context: ctx,
  132. }
  133. e := raftpb.Entry{
  134. Type: raftpb.EntryConfChange,
  135. Data: pbutil.MustMarshal(cc),
  136. Term: term,
  137. Index: next,
  138. }
  139. ents = append(ents, e)
  140. }
  141. return ents
  142. }