import_snap_command.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. fmt.Printf("cannot read snapshot file %s\n", c.String("snap"))
  34. os.Exit(1)
  35. }
  36. st := store.New()
  37. err = st.Recovery(d)
  38. if err != nil {
  39. fmt.Printf("cannot recover the snapshot file: %v\n", err)
  40. os.Exit(1)
  41. }
  42. endpoints, err := getEndpoints(c)
  43. if err != nil {
  44. handleError(ErrorFromEtcd, err)
  45. }
  46. tr, err := getTransport(c)
  47. if err != nil {
  48. handleError(ErrorFromEtcd, err)
  49. }
  50. wg := &sync.WaitGroup{}
  51. setc := make(chan set)
  52. concurrent := c.Int("c")
  53. fmt.Printf("starting to import snapshot %s with %d clients\n", c.String("snap"), concurrent)
  54. for i := 0; i < concurrent; i++ {
  55. client := etcd.NewClient(endpoints)
  56. client.SetTransport(tr)
  57. if c.GlobalBool("debug") {
  58. go dumpCURL(client)
  59. }
  60. if ok := client.SyncCluster(); !ok {
  61. handleError(FailedToConnectToHost, errors.New("cannot sync with the cluster using endpoints "+strings.Join(endpoints, ", ")))
  62. }
  63. wg.Add(1)
  64. go runSet(client, setc, wg)
  65. }
  66. all, err := st.Get("/", true, true)
  67. if err != nil {
  68. handleError(ErrorFromEtcd, err)
  69. }
  70. n := copyKeys(all.Node, setc)
  71. close(setc)
  72. wg.Wait()
  73. fmt.Printf("finished importing %d keys\n", n)
  74. }
  75. func copyKeys(n *store.NodeExtern, setc chan set) int {
  76. num := 0
  77. if !n.Dir {
  78. setc <- set{n.Key, *n.Value, n.TTL}
  79. return 1
  80. }
  81. log.Println("entrying dir:", n.Key)
  82. for _, nn := range n.Nodes {
  83. sub := copyKeys(nn, setc)
  84. num += sub
  85. }
  86. return num
  87. }
  88. func runSet(c *etcd.Client, setc chan set, wg *sync.WaitGroup) {
  89. for s := range setc {
  90. log.Println("copying key:", s.key)
  91. if s.ttl != 0 && s.ttl < 300 {
  92. log.Printf("extending key %s's ttl to 300 seconds", s.key)
  93. s.ttl = 5 * 60
  94. }
  95. _, err := c.Set(s.key, s.value, uint64(s.ttl))
  96. if err != nil {
  97. log.Fatalf("failed to copy key: %v\n", err)
  98. }
  99. }
  100. wg.Done()
  101. }