import_snap_command.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 command
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "log"
  19. "os"
  20. "sync"
  21. "time"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  24. "github.com/coreos/etcd/client"
  25. "github.com/coreos/etcd/store"
  26. )
  27. type set struct {
  28. key string
  29. value string
  30. ttl int64
  31. }
  32. func NewImportSnapCommand() cli.Command {
  33. return cli.Command{
  34. Name: "import",
  35. Usage: "import a snapshot to a cluster",
  36. Flags: []cli.Flag{
  37. cli.StringFlag{Name: "snap", Value: "", Usage: "Path to the valid etcd 0.4.x snapshot."},
  38. cli.StringSliceFlag{Name: "hidden", Value: new(cli.StringSlice), Usage: "Hidden key spaces to import from snapshot"},
  39. cli.IntFlag{Name: "c", Value: 10, Usage: "Number of concurrent clients to import the data"},
  40. },
  41. Action: handleImportSnap,
  42. }
  43. }
  44. func handleImportSnap(c *cli.Context) {
  45. d, err := ioutil.ReadFile(c.String("snap"))
  46. if err != nil {
  47. if c.String("snap") == "" {
  48. fmt.Printf("no snapshot file provided (use --snap)\n")
  49. } else {
  50. fmt.Printf("cannot read snapshot file %s\n", c.String("snap"))
  51. }
  52. os.Exit(1)
  53. }
  54. st := store.New()
  55. err = st.Recovery(d)
  56. wg := &sync.WaitGroup{}
  57. setc := make(chan set)
  58. concurrent := c.Int("c")
  59. fmt.Printf("starting to import snapshot %s with %d clients\n", c.String("snap"), concurrent)
  60. for i := 0; i < concurrent; i++ {
  61. go runSet(mustNewKeyAPI(c), setc, wg)
  62. }
  63. all, err := st.Get("/", true, true)
  64. if err != nil {
  65. handleError(ExitServerError, err)
  66. }
  67. n := copyKeys(all.Node, setc)
  68. hiddens := c.StringSlice("hidden")
  69. for _, h := range hiddens {
  70. allh, err := st.Get(h, true, true)
  71. if err != nil {
  72. handleError(ExitServerError, err)
  73. }
  74. n += copyKeys(allh.Node, setc)
  75. }
  76. close(setc)
  77. wg.Wait()
  78. fmt.Printf("finished importing %d keys\n", n)
  79. }
  80. func copyKeys(n *store.NodeExtern, setc chan set) int {
  81. num := 0
  82. if !n.Dir {
  83. setc <- set{n.Key, *n.Value, n.TTL}
  84. return 1
  85. }
  86. log.Println("entering dir:", n.Key)
  87. for _, nn := range n.Nodes {
  88. sub := copyKeys(nn, setc)
  89. num += sub
  90. }
  91. return num
  92. }
  93. func runSet(ki client.KeysAPI, setc chan set, wg *sync.WaitGroup) {
  94. for s := range setc {
  95. log.Println("copying key:", s.key)
  96. if s.ttl != 0 && s.ttl < 300 {
  97. log.Printf("extending key %s's ttl to 300 seconds", s.key)
  98. s.ttl = 5 * 60
  99. }
  100. _, err := ki.Set(context.TODO(), s.key, s.value, &client.SetOptions{TTL: time.Duration(s.ttl) * time.Second})
  101. if err != nil {
  102. log.Fatalf("failed to copy key: %v\n", err)
  103. }
  104. }
  105. wg.Done()
  106. }