import_snap_command.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.StringSliceFlag{Name: "hidden", Value: new(cli.StringSlice), Usage: "Hidden key spaces to import from snapshot"},
  26. cli.IntFlag{Name: "c", Value: 10, Usage: "Number of concurrent clients to import the data"},
  27. },
  28. Action: handleImportSnap,
  29. }
  30. }
  31. func handleImportSnap(c *cli.Context) {
  32. d, err := ioutil.ReadFile(c.String("snap"))
  33. if err != nil {
  34. if c.String("snap") == "" {
  35. fmt.Printf("no snapshot file provided (use --snap)\n")
  36. } else {
  37. fmt.Printf("cannot read snapshot file %s\n", c.String("snap"))
  38. }
  39. os.Exit(1)
  40. }
  41. st := store.New()
  42. err = st.Recovery(d)
  43. if err != nil {
  44. fmt.Printf("cannot recover the snapshot file: %v\n", err)
  45. os.Exit(1)
  46. }
  47. endpoints, err := getEndpoints(c)
  48. if err != nil {
  49. handleError(ErrorFromEtcd, err)
  50. }
  51. tr, err := getTransport(c)
  52. if err != nil {
  53. handleError(ErrorFromEtcd, err)
  54. }
  55. wg := &sync.WaitGroup{}
  56. setc := make(chan set)
  57. concurrent := c.Int("c")
  58. fmt.Printf("starting to import snapshot %s with %d clients\n", c.String("snap"), concurrent)
  59. for i := 0; i < concurrent; i++ {
  60. client := etcd.NewClient(endpoints)
  61. client.SetTransport(tr)
  62. if c.GlobalBool("debug") {
  63. go dumpCURL(client)
  64. }
  65. if ok := client.SyncCluster(); !ok {
  66. handleError(FailedToConnectToHost, errors.New("cannot sync with the cluster using endpoints "+strings.Join(endpoints, ", ")))
  67. }
  68. wg.Add(1)
  69. go runSet(client, setc, wg)
  70. }
  71. all, err := st.Get("/", true, true)
  72. if err != nil {
  73. handleError(ErrorFromEtcd, err)
  74. }
  75. n := copyKeys(all.Node, setc)
  76. hiddens := c.StringSlice("hidden")
  77. for _, h := range hiddens {
  78. allh, err := st.Get(h, true, true)
  79. if err != nil {
  80. handleError(ErrorFromEtcd, err)
  81. }
  82. n += copyKeys(allh.Node, setc)
  83. }
  84. close(setc)
  85. wg.Wait()
  86. fmt.Printf("finished importing %d keys\n", n)
  87. }
  88. func copyKeys(n *store.NodeExtern, setc chan set) int {
  89. num := 0
  90. if !n.Dir {
  91. setc <- set{n.Key, *n.Value, n.TTL}
  92. return 1
  93. }
  94. log.Println("entering dir:", n.Key)
  95. for _, nn := range n.Nodes {
  96. sub := copyKeys(nn, setc)
  97. num += sub
  98. }
  99. return num
  100. }
  101. func runSet(c *etcd.Client, setc chan set, wg *sync.WaitGroup) {
  102. for s := range setc {
  103. log.Println("copying key:", s.key)
  104. if s.ttl != 0 && s.ttl < 300 {
  105. log.Printf("extending key %s's ttl to 300 seconds", s.key)
  106. s.ttl = 5 * 60
  107. }
  108. _, err := c.Set(s.key, s.value, uint64(s.ttl))
  109. if err != nil {
  110. log.Fatalf("failed to copy key: %v\n", err)
  111. }
  112. }
  113. wg.Done()
  114. }