force_cluster.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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) (id types.ID, n raft.Node, w *wal.WAL) {
  25. var err error
  26. if w, err = wal.OpenAtIndex(cfg.WALDir(), index); err != nil {
  27. log.Fatalf("etcdserver: open wal error: %v", err)
  28. }
  29. id, cid, st, ents, err := readWAL(w, index)
  30. if err != nil {
  31. log.Fatalf("etcdserver: read wal error: %v", err)
  32. }
  33. cfg.Cluster.SetID(cid)
  34. // discard the previously uncommitted entries
  35. if len(ents) != 0 {
  36. ents = ents[:st.Commit+1]
  37. }
  38. // force append the configuration change entries
  39. toAppEnts := createConfigChangeEnts(getIDs(snapshot, ents), uint64(id), st.Term, st.Commit)
  40. ents = append(ents, toAppEnts...)
  41. // force commit newly appended entries
  42. for _, e := range toAppEnts {
  43. err := w.SaveEntry(&e)
  44. if err != nil {
  45. log.Fatalf("etcdserver: %v", err)
  46. }
  47. }
  48. if len(ents) != 0 {
  49. st.Commit = ents[len(ents)-1].Index
  50. }
  51. log.Printf("etcdserver: forcing restart of member %s in cluster %s at commit index %d", id, cfg.Cluster.ID(), st.Commit)
  52. n = raft.RestartNode(uint64(id), 10, 1, snapshot, st, ents)
  53. return
  54. }
  55. // getIDs returns an ordered set of IDs included in the given snapshot and
  56. // the entries. The given snapshot/entries can contain two kinds of
  57. // ID-related entry:
  58. // - ConfChangeAddNode, in which case the contained ID will be added into the set.
  59. // - ConfChangeAddRemove, in which case the contained ID will be removed from the set.
  60. func getIDs(snap *raftpb.Snapshot, ents []raftpb.Entry) []uint64 {
  61. ids := make(map[uint64]bool)
  62. if snap != nil {
  63. for _, id := range snap.Nodes {
  64. ids[id] = true
  65. }
  66. }
  67. for _, e := range ents {
  68. if e.Type != raftpb.EntryConfChange {
  69. continue
  70. }
  71. var cc raftpb.ConfChange
  72. pbutil.MustUnmarshal(&cc, e.Data)
  73. switch cc.Type {
  74. case raftpb.ConfChangeAddNode:
  75. ids[cc.NodeID] = true
  76. case raftpb.ConfChangeRemoveNode:
  77. delete(ids, cc.NodeID)
  78. default:
  79. log.Panicf("ConfChange Type should be either ConfChangeAddNode or ConfChangeRemoveNode!")
  80. }
  81. }
  82. sids := make(types.Uint64Slice, 0)
  83. for id := range ids {
  84. sids = append(sids, id)
  85. }
  86. sort.Sort(sids)
  87. return []uint64(sids)
  88. }
  89. // createConfigChangeEnts creates a series of Raft entries (i.e.
  90. // EntryConfChange) to remove the set of given IDs from the cluster. The ID
  91. // `self` is _not_ removed, even if present in the set.
  92. // If `self` is not inside the given ids, it creates a Raft entry to add a
  93. // default member with the given `self`.
  94. func createConfigChangeEnts(ids []uint64, self uint64, term, index uint64) []raftpb.Entry {
  95. ents := make([]raftpb.Entry, 0)
  96. next := index + 1
  97. found := false
  98. for _, id := range ids {
  99. if id == self {
  100. found = true
  101. continue
  102. }
  103. cc := &raftpb.ConfChange{
  104. Type: raftpb.ConfChangeRemoveNode,
  105. NodeID: id,
  106. }
  107. e := raftpb.Entry{
  108. Type: raftpb.EntryConfChange,
  109. Data: pbutil.MustMarshal(cc),
  110. Term: term,
  111. Index: next,
  112. }
  113. ents = append(ents, e)
  114. next++
  115. }
  116. if !found {
  117. m := Member{
  118. ID: types.ID(self),
  119. RaftAttributes: RaftAttributes{PeerURLs: []string{"http://localhost:7001", "http://localhost:2380"}},
  120. }
  121. ctx, err := json.Marshal(m)
  122. if err != nil {
  123. log.Panicf("marshal member should never fail: %v", err)
  124. }
  125. cc := &raftpb.ConfChange{
  126. Type: raftpb.ConfChangeAddNode,
  127. NodeID: self,
  128. Context: ctx,
  129. }
  130. e := raftpb.Entry{
  131. Type: raftpb.EntryConfChange,
  132. Data: pbutil.MustMarshal(cc),
  133. Term: term,
  134. Index: next,
  135. }
  136. ents = append(ents, e)
  137. }
  138. return ents
  139. }