force_cluster.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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, *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. n := raft.RestartNode(uint64(id), 10, 1, snapshot, st, ents)
  50. return id, n, w
  51. }
  52. // getIDs returns an ordered set of IDs included in the given snapshot and
  53. // the entries. The given snapshot/entries can contain two kinds of
  54. // ID-related entry:
  55. // - ConfChangeAddNode, in which case the contained ID will be added into the set.
  56. // - ConfChangeAddRemove, in which case the contained ID will be removed from the set.
  57. func getIDs(snap *raftpb.Snapshot, ents []raftpb.Entry) []uint64 {
  58. ids := make(map[uint64]bool)
  59. if snap != nil {
  60. for _, id := range snap.Nodes {
  61. ids[id] = true
  62. }
  63. }
  64. for _, e := range ents {
  65. if e.Type != raftpb.EntryConfChange {
  66. continue
  67. }
  68. var cc raftpb.ConfChange
  69. pbutil.MustUnmarshal(&cc, e.Data)
  70. switch cc.Type {
  71. case raftpb.ConfChangeAddNode:
  72. ids[cc.NodeID] = true
  73. case raftpb.ConfChangeRemoveNode:
  74. delete(ids, cc.NodeID)
  75. default:
  76. log.Panicf("ConfChange Type should be either ConfChangeAddNode or ConfChangeRemoveNode!")
  77. }
  78. }
  79. sids := make(types.Uint64Slice, 0)
  80. for id := range ids {
  81. sids = append(sids, id)
  82. }
  83. sort.Sort(sids)
  84. return []uint64(sids)
  85. }
  86. // createConfigChangeEnts creates a series of Raft entries (i.e.
  87. // EntryConfChange) to remove the set of given IDs from the cluster. The ID
  88. // `self` is _not_ removed, even if present in the set.
  89. // If `self` is not inside the given ids, it creates a Raft entry to add a
  90. // default member with the given `self`.
  91. func createConfigChangeEnts(ids []uint64, self uint64, term, index uint64) []raftpb.Entry {
  92. ents := make([]raftpb.Entry, 0)
  93. next := index + 1
  94. found := false
  95. for _, id := range ids {
  96. if id == self {
  97. found = true
  98. continue
  99. }
  100. cc := &raftpb.ConfChange{
  101. Type: raftpb.ConfChangeRemoveNode,
  102. NodeID: id,
  103. }
  104. e := raftpb.Entry{
  105. Type: raftpb.EntryConfChange,
  106. Data: pbutil.MustMarshal(cc),
  107. Term: term,
  108. Index: next,
  109. }
  110. ents = append(ents, e)
  111. next++
  112. }
  113. if !found {
  114. m := Member{
  115. ID: types.ID(self),
  116. RaftAttributes: RaftAttributes{PeerURLs: []string{"http://localhost:7001", "http://localhost:2380"}},
  117. }
  118. ctx, err := json.Marshal(m)
  119. if err != nil {
  120. log.Panicf("marshal member should never fail: %v", err)
  121. }
  122. cc := &raftpb.ConfChange{
  123. Type: raftpb.ConfChangeAddNode,
  124. NodeID: self,
  125. Context: ctx,
  126. }
  127. e := raftpb.Entry{
  128. Type: raftpb.EntryConfChange,
  129. Data: pbutil.MustMarshal(cc),
  130. Term: term,
  131. Index: next,
  132. }
  133. ents = append(ents, e)
  134. }
  135. return ents
  136. }