import_snap_command.go 3.1 KB

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