etcd.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2018 The etcd Authors
  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 main
  15. import (
  16. "context"
  17. "fmt"
  18. "io/ioutil"
  19. "net/url"
  20. "os"
  21. "strings"
  22. "time"
  23. "go.etcd.io/etcd/clientv3"
  24. "go.etcd.io/etcd/embed"
  25. "go.uber.org/zap"
  26. )
  27. func newEmbedURLs(n int) (urls []url.URL) {
  28. urls = make([]url.URL, n)
  29. for i := 0; i < n; i++ {
  30. u, _ := url.Parse(fmt.Sprintf("unix://localhost:%d%06d", os.Getpid(), i))
  31. urls[i] = *u
  32. }
  33. return urls
  34. }
  35. func setupEmbedCfg(cfg *embed.Config, curls, purls, ics []url.URL) {
  36. cfg.Logger = "zap"
  37. cfg.LogOutputs = []string{"/dev/null"}
  38. // []string{"stderr"} to enable server logging
  39. cfg.Debug = false
  40. var err error
  41. cfg.Dir, err = ioutil.TempDir(os.TempDir(), fmt.Sprintf("%016X", time.Now().UnixNano()))
  42. if err != nil {
  43. panic(err)
  44. }
  45. os.RemoveAll(cfg.Dir)
  46. cfg.ClusterState = "new"
  47. cfg.LCUrls, cfg.ACUrls = curls, curls
  48. cfg.LPUrls, cfg.APUrls = purls, purls
  49. cfg.InitialCluster = ""
  50. for i := range ics {
  51. cfg.InitialCluster += fmt.Sprintf(",%d=%s", i, ics[i].String())
  52. }
  53. cfg.InitialCluster = cfg.InitialCluster[1:]
  54. }
  55. func getCommand(exec, name, dir, cURL, pURL, cluster string) string {
  56. s := fmt.Sprintf("%s --name %s --data-dir %s --listen-client-urls %s --advertise-client-urls %s ",
  57. exec, name, dir, cURL, cURL)
  58. s += fmt.Sprintf("--listen-peer-urls %s --initial-advertise-peer-urls %s ", pURL, pURL)
  59. s += fmt.Sprintf("--initial-cluster %s ", cluster)
  60. return s + "--initial-cluster-token tkn --initial-cluster-state new"
  61. }
  62. func write(ep string) {
  63. cli, err := clientv3.New(clientv3.Config{Endpoints: []string{strings.Replace(ep, "/metrics", "", 1)}})
  64. if err != nil {
  65. lg.Panic("failed to create client", zap.Error(err))
  66. }
  67. defer cli.Close()
  68. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  69. defer cancel()
  70. _, err = cli.Put(ctx, "____test", "")
  71. if err != nil {
  72. lg.Panic("failed to write test key", zap.Error(err))
  73. }
  74. _, err = cli.Get(ctx, "____test")
  75. if err != nil {
  76. lg.Panic("failed to read test key", zap.Error(err))
  77. }
  78. _, err = cli.Delete(ctx, "____test")
  79. if err != nil {
  80. lg.Panic("failed to delete test key", zap.Error(err))
  81. }
  82. cli.Watch(ctx, "____test", clientv3.WithCreatedNotify())
  83. }