import_snap_command.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package command
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. "strings"
  9. "sync"
  10. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  11. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
  12. "github.com/coreos/etcd/store"
  13. )
  14. type set struct {
  15. key string
  16. value string
  17. ttl int64
  18. }
  19. func NewImportSnapCommand() cli.Command {
  20. return cli.Command{
  21. Name: "import",
  22. Usage: "import a snapshot to a cluster",
  23. Flags: []cli.Flag{
  24. cli.StringFlag{Name: "snap", Value: "", Usage: "Path to the vaild etcd 0.4.x snapshot."},
  25. cli.IntFlag{Name: "c", Value: 10, Usage: "Number of concurrent clients to import the data"},
  26. },
  27. Action: handleImportSnap,
  28. }
  29. }
  30. func handleImportSnap(c *cli.Context) {
  31. d, err := ioutil.ReadFile(c.String("snap"))
  32. if err != nil {
  33. if c.String("snap") == "" {
  34. fmt.Printf("no snapshot file provided (use --snap)")
  35. } else {
  36. fmt.Printf("cannot read snapshot file %s\n", c.String("snap"))
  37. }
  38. os.Exit(1)
  39. }
  40. st := store.New()
  41. err = st.Recovery(d)
  42. if err != nil {
  43. fmt.Printf("cannot recover the snapshot file: %v\n", err)
  44. os.Exit(1)
  45. }
  46. endpoints, err := getEndpoints(c)
  47. if err != nil {
  48. handleError(ErrorFromEtcd, err)
  49. }
  50. tr, err := getTransport(c)
  51. if err != nil {
  52. handleError(ErrorFromEtcd, err)
  53. }
  54. wg := &sync.WaitGroup{}
  55. setc := make(chan set)
  56. concurrent := c.Int("c")
  57. fmt.Printf("starting to import snapshot %s with %d clients\n", c.String("snap"), concurrent)
  58. for i := 0; i < concurrent; i++ {
  59. client := etcd.NewClient(endpoints)
  60. client.SetTransport(tr)
  61. if c.GlobalBool("debug") {
  62. go dumpCURL(client)
  63. }
  64. if ok := client.SyncCluster(); !ok {
  65. handleError(FailedToConnectToHost, errors.New("cannot sync with the cluster using endpoints "+strings.Join(endpoints, ", ")))
  66. }
  67. wg.Add(1)
  68. go runSet(client, setc, wg)
  69. }
  70. all, err := st.Get("/", true, true)
  71. if err != nil {
  72. handleError(ErrorFromEtcd, err)
  73. }
  74. n := copyKeys(all.Node, setc)
  75. close(setc)
  76. wg.Wait()
  77. fmt.Printf("finished importing %d keys\n", n)
  78. }
  79. func copyKeys(n *store.NodeExtern, setc chan set) int {
  80. num := 0
  81. if !n.Dir {
  82. setc <- set{n.Key, *n.Value, n.TTL}
  83. return 1
  84. }
  85. log.Println("entering dir:", n.Key)
  86. for _, nn := range n.Nodes {
  87. sub := copyKeys(nn, setc)
  88. num += sub
  89. }
  90. return num
  91. }
  92. func runSet(c *etcd.Client, setc chan set, wg *sync.WaitGroup) {
  93. for s := range setc {
  94. log.Println("copying key:", s.key)
  95. if s.ttl != 0 && s.ttl < 300 {
  96. log.Printf("extending key %s's ttl to 300 seconds", s.key)
  97. s.ttl = 5 * 60
  98. }
  99. _, err := c.Set(s.key, s.value, uint64(s.ttl))
  100. if err != nil {
  101. log.Fatalf("failed to copy key: %v\n", err)
  102. }
  103. }
  104. wg.Done()
  105. }