etcd4.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2015 CoreOS, Inc.
  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 migrate
  15. import (
  16. "fmt"
  17. "log"
  18. "os"
  19. "path"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/pkg/pbutil"
  22. "github.com/coreos/etcd/pkg/types"
  23. raftpb "github.com/coreos/etcd/raft/raftpb"
  24. "github.com/coreos/etcd/snap"
  25. "github.com/coreos/etcd/wal"
  26. "github.com/coreos/etcd/wal/walpb"
  27. )
  28. // We need an offset in leader election terms, because term 0 is special in 2.0.
  29. const termOffset4to2 = 1
  30. func snapDir4(dataDir string) string {
  31. return path.Join(dataDir, "snapshot")
  32. }
  33. func logFile4(dataDir string) string {
  34. return path.Join(dataDir, "log")
  35. }
  36. func cfgFile4(dataDir string) string {
  37. return path.Join(dataDir, "conf")
  38. }
  39. func snapDir2(dataDir string) string {
  40. return path.Join(dataDir, "snap")
  41. }
  42. func walDir2(dataDir string) string {
  43. return path.Join(dataDir, "wal")
  44. }
  45. func Migrate4To2(dataDir string, name string) error {
  46. // prep new directories
  47. sd2 := snapDir2(dataDir)
  48. if err := os.MkdirAll(sd2, 0700); err != nil {
  49. return fmt.Errorf("failed creating snapshot directory %s: %v", sd2, err)
  50. }
  51. // read v0.4 data
  52. snap4, err := DecodeLatestSnapshot4FromDir(snapDir4(dataDir))
  53. if err != nil {
  54. return err
  55. }
  56. cfg4, err := DecodeConfig4FromFile(cfgFile4(dataDir))
  57. if err != nil {
  58. return err
  59. }
  60. ents4, err := DecodeLog4FromFile(logFile4(dataDir))
  61. if err != nil {
  62. return err
  63. }
  64. nodeIDs := ents4.NodeIDs()
  65. nodeID := GuessNodeID(nodeIDs, snap4, cfg4, name)
  66. if nodeID == 0 {
  67. return fmt.Errorf("Couldn't figure out the node ID from the log or flags, cannot convert")
  68. }
  69. metadata := pbutil.MustMarshal(&pb.Metadata{NodeID: nodeID, ClusterID: 0x04add5})
  70. wd2 := walDir2(dataDir)
  71. w, err := wal.Create(wd2, metadata)
  72. if err != nil {
  73. return fmt.Errorf("failed initializing wal at %s: %v", wd2, err)
  74. }
  75. defer w.Close()
  76. // transform v0.4 data
  77. var snap2 *raftpb.Snapshot
  78. if snap4 == nil {
  79. log.Printf("No snapshot found")
  80. } else {
  81. log.Printf("Found snapshot: lastIndex=%d", snap4.LastIndex)
  82. snap2 = snap4.Snapshot2()
  83. }
  84. st2 := cfg4.HardState2()
  85. // If we've got the most recent snapshot, we can use it's committed index. Still likely less than the current actual index, but worth it for the replay.
  86. if snap2 != nil && st2.Commit < snap2.Metadata.Index {
  87. st2.Commit = snap2.Metadata.Index
  88. }
  89. ents2, err := Entries4To2(ents4)
  90. if err != nil {
  91. return err
  92. }
  93. ents2Len := len(ents2)
  94. log.Printf("Found %d log entries: firstIndex=%d lastIndex=%d", ents2Len, ents2[0].Index, ents2[ents2Len-1].Index)
  95. // set the state term to the biggest term we have ever seen,
  96. // so term of future entries will not be the same with term of old ones.
  97. st2.Term = ents2[ents2Len-1].Term
  98. // explicitly prepend an empty entry as the WAL code expects it
  99. ents2 = append(make([]raftpb.Entry, 1), ents2...)
  100. if err = w.Save(st2, ents2); err != nil {
  101. return err
  102. }
  103. log.Printf("Log migration successful")
  104. // migrate snapshot (if necessary) and logs
  105. var walsnap walpb.Snapshot
  106. if snap2 != nil {
  107. walsnap.Index, walsnap.Term = snap2.Metadata.Index, snap2.Metadata.Term
  108. ss := snap.New(sd2)
  109. if err := ss.SaveSnap(*snap2); err != nil {
  110. return err
  111. }
  112. log.Printf("Snapshot migration successful")
  113. }
  114. if err = w.SaveSnapshot(walsnap); err != nil {
  115. return err
  116. }
  117. return nil
  118. }
  119. func GuessNodeID(nodes map[string]uint64, snap4 *Snapshot4, cfg *Config4, name string) uint64 {
  120. var snapNodes map[string]uint64
  121. if snap4 != nil {
  122. snapNodes = snap4.GetNodesFromStore()
  123. }
  124. // First, use the flag, if set.
  125. if name != "" {
  126. log.Printf("Using suggested name %s", name)
  127. if val, ok := nodes[name]; ok {
  128. log.Printf("Assigning %s the ID %s", name, types.ID(val))
  129. return val
  130. }
  131. if snapNodes != nil {
  132. if val, ok := snapNodes[name]; ok {
  133. log.Printf("Assigning %s the ID %s", name, types.ID(val))
  134. return val
  135. }
  136. }
  137. log.Printf("Name not found, autodetecting...")
  138. }
  139. // Next, look at the snapshot peers, if that exists.
  140. if snap4 != nil {
  141. //snapNodes := make(map[string]uint64)
  142. //for _, p := range snap4.Peers {
  143. //m := generateNodeMember(p.Name, p.ConnectionString, "")
  144. //snapNodes[p.Name] = uint64(m.ID)
  145. //}
  146. for _, p := range cfg.Peers {
  147. delete(snapNodes, p.Name)
  148. }
  149. if len(snapNodes) == 1 {
  150. for nodename, id := range nodes {
  151. log.Printf("Autodetected from snapshot: name %s", nodename)
  152. return id
  153. }
  154. }
  155. }
  156. // Then, try and deduce from the log.
  157. for _, p := range cfg.Peers {
  158. delete(nodes, p.Name)
  159. }
  160. if len(nodes) == 1 {
  161. for nodename, id := range nodes {
  162. log.Printf("Autodetected name %s", nodename)
  163. return id
  164. }
  165. }
  166. return 0
  167. }