force_cluster.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "log"
  16. "sort"
  17. "github.com/coreos/etcd/pkg/pbutil"
  18. "github.com/coreos/etcd/pkg/types"
  19. "github.com/coreos/etcd/raft"
  20. "github.com/coreos/etcd/raft/raftpb"
  21. "github.com/coreos/etcd/wal"
  22. )
  23. func restartAsStandaloneNode(cfg *ServerConfig, index uint64, snapshot *raftpb.Snapshot) (id types.ID, n raft.Node, w *wal.WAL) {
  24. var err error
  25. if w, err = wal.OpenAtIndex(cfg.WALDir(), index); err != nil {
  26. log.Fatalf("etcdserver: open wal error: %v", err)
  27. }
  28. id, cid, st, ents, err := readWAL(w, index)
  29. if err != nil {
  30. log.Fatalf("etcdserver: read wal error: %v", err)
  31. }
  32. cfg.Cluster.SetID(cid)
  33. // discard the previously uncommitted entries
  34. if len(ents) != 0 {
  35. ents = ents[:st.Commit+1]
  36. }
  37. // force append the configuration change entries
  38. toAppEnts := createConfigChangeEnts(getIDs(snapshot, ents), uint64(id), st.Term, st.Commit)
  39. ents = append(ents, toAppEnts...)
  40. // force commit newly appended entries
  41. for _, e := range toAppEnts {
  42. err := w.SaveEntry(&e)
  43. if err != nil {
  44. log.Fatalf("etcdserver: %v", err)
  45. }
  46. }
  47. if len(ents) != 0 {
  48. st.Commit = ents[len(ents)-1].Index
  49. }
  50. log.Printf("etcdserver: forcing restart of member %s in cluster %s at commit index %d", id, cfg.Cluster.ID(), st.Commit)
  51. n = raft.RestartNode(uint64(id), 10, 1, snapshot, st, ents)
  52. return
  53. }
  54. // getIDs returns an ordered set of IDs included in the given snapshot and
  55. // the entries. The given snapshot/entries can contain two kinds of
  56. // ID-related entry:
  57. // - ConfChangeAddNode, in which case the contained ID will be added into the set.
  58. // - ConfChangeAddRemove, in which case the contained ID will be removed from the set.
  59. func getIDs(snap *raftpb.Snapshot, ents []raftpb.Entry) []uint64 {
  60. ids := make(map[uint64]bool)
  61. if snap != nil {
  62. for _, id := range snap.Nodes {
  63. ids[id] = true
  64. }
  65. }
  66. for _, e := range ents {
  67. if e.Type != raftpb.EntryConfChange {
  68. continue
  69. }
  70. var cc raftpb.ConfChange
  71. pbutil.MustUnmarshal(&cc, e.Data)
  72. switch cc.Type {
  73. case raftpb.ConfChangeAddNode:
  74. ids[cc.NodeID] = true
  75. case raftpb.ConfChangeRemoveNode:
  76. delete(ids, cc.NodeID)
  77. default:
  78. log.Panicf("ConfChange Type should be either ConfChangeAddNode or ConfChangeRemoveNode!")
  79. }
  80. }
  81. sids := make(types.Uint64Slice, 0)
  82. for id := range ids {
  83. sids = append(sids, id)
  84. }
  85. sort.Sort(sids)
  86. return []uint64(sids)
  87. }
  88. // createConfigChangeEnts creates a series of Raft entries (i.e.
  89. // EntryConfChange) to remove the set of given IDs from the cluster. The ID
  90. // `self` is _not_ removed, even if present in the set.
  91. func createConfigChangeEnts(ids []uint64, self uint64, term, index uint64) []raftpb.Entry {
  92. ents := make([]raftpb.Entry, 0)
  93. next := index + 1
  94. for _, id := range ids {
  95. if id == self {
  96. continue
  97. }
  98. cc := &raftpb.ConfChange{
  99. Type: raftpb.ConfChangeRemoveNode,
  100. NodeID: id,
  101. }
  102. e := raftpb.Entry{
  103. Type: raftpb.EntryConfChange,
  104. Data: pbutil.MustMarshal(cc),
  105. Term: term,
  106. Index: next,
  107. }
  108. ents = append(ents, e)
  109. next++
  110. }
  111. return ents
  112. }