import_snap_command.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package command
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "sync"
  8. "time"
  9. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  10. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  11. "github.com/coreos/etcd/client"
  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 valid 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. wg := &sync.WaitGroup{}
  44. setc := make(chan set)
  45. concurrent := c.Int("c")
  46. fmt.Printf("starting to import snapshot %s with %d clients\n", c.String("snap"), concurrent)
  47. for i := 0; i < concurrent; i++ {
  48. go runSet(mustNewKeyAPI(c), setc, wg)
  49. }
  50. all, err := st.Get("/", true, true)
  51. if err != nil {
  52. handleError(ExitServerError, err)
  53. }
  54. n := copyKeys(all.Node, setc)
  55. hiddens := c.StringSlice("hidden")
  56. for _, h := range hiddens {
  57. allh, err := st.Get(h, true, true)
  58. if err != nil {
  59. handleError(ExitServerError, err)
  60. }
  61. n += copyKeys(allh.Node, setc)
  62. }
  63. close(setc)
  64. wg.Wait()
  65. fmt.Printf("finished importing %d keys\n", n)
  66. }
  67. func copyKeys(n *store.NodeExtern, setc chan set) int {
  68. num := 0
  69. if !n.Dir {
  70. setc <- set{n.Key, *n.Value, n.TTL}
  71. return 1
  72. }
  73. log.Println("entering dir:", n.Key)
  74. for _, nn := range n.Nodes {
  75. sub := copyKeys(nn, setc)
  76. num += sub
  77. }
  78. return num
  79. }
  80. func runSet(ki client.KeysAPI, setc chan set, wg *sync.WaitGroup) {
  81. for s := range setc {
  82. log.Println("copying key:", s.key)
  83. if s.ttl != 0 && s.ttl < 300 {
  84. log.Printf("extending key %s's ttl to 300 seconds", s.key)
  85. s.ttl = 5 * 60
  86. }
  87. _, err := ki.Set(context.TODO(), s.key, s.value, &client.SetOptions{TTL: time.Duration(s.ttl) * time.Second})
  88. if err != nil {
  89. log.Fatalf("failed to copy key: %v\n", err)
  90. }
  91. }
  92. wg.Done()
  93. }