util.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2015 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 cmd
  15. import (
  16. "crypto/rand"
  17. "fmt"
  18. "log"
  19. "os"
  20. "strings"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/pkg/report"
  23. "golang.org/x/net/context"
  24. )
  25. var (
  26. // dialTotal counts the number of mustCreateConn calls so that endpoint
  27. // connections can be handed out in round-robin order
  28. dialTotal int
  29. // leaderEps is a cache for holding endpoints of a leader node
  30. leaderEps []string
  31. )
  32. func mustFindLeaderEndpoints(c *clientv3.Client) {
  33. resp, lerr := c.MemberList(context.TODO())
  34. if lerr != nil {
  35. fmt.Fprintf(os.Stderr, "failed to get a member list: %s\n", lerr)
  36. os.Exit(1)
  37. }
  38. leaderId := uint64(0)
  39. for _, ep := range c.Endpoints() {
  40. resp, serr := c.Status(context.TODO(), ep)
  41. if serr == nil {
  42. leaderId = resp.Leader
  43. break
  44. }
  45. }
  46. for _, m := range resp.Members {
  47. if m.ID == leaderId {
  48. leaderEps = m.ClientURLs
  49. return
  50. }
  51. }
  52. fmt.Fprintf(os.Stderr, "failed to find a leader endpoint\n")
  53. os.Exit(1)
  54. }
  55. func mustCreateConn() *clientv3.Client {
  56. connEndpoints := leaderEps
  57. if len(connEndpoints) == 0 {
  58. connEndpoints = []string{endpoints[dialTotal%len(endpoints)]}
  59. dialTotal++
  60. }
  61. cfg := clientv3.Config{
  62. Endpoints: connEndpoints,
  63. DialTimeout: dialTimeout,
  64. }
  65. if !tls.Empty() {
  66. cfgtls, err := tls.ClientConfig()
  67. if err != nil {
  68. fmt.Fprintf(os.Stderr, "bad tls config: %v\n", err)
  69. os.Exit(1)
  70. }
  71. cfg.TLS = cfgtls
  72. }
  73. if len(user) != 0 {
  74. splitted := strings.SplitN(user, ":", 2)
  75. if len(splitted) != 2 {
  76. fmt.Fprintf(os.Stderr, "bad user information: %s\n", user)
  77. os.Exit(1)
  78. }
  79. cfg.Username = splitted[0]
  80. cfg.Password = splitted[1]
  81. }
  82. client, err := clientv3.New(cfg)
  83. if targetLeader && len(leaderEps) == 0 {
  84. mustFindLeaderEndpoints(client)
  85. client.Close()
  86. return mustCreateConn()
  87. }
  88. clientv3.SetLogger(log.New(os.Stderr, "grpc", 0))
  89. if err != nil {
  90. fmt.Fprintf(os.Stderr, "dial error: %v\n", err)
  91. os.Exit(1)
  92. }
  93. return client
  94. }
  95. func mustCreateClients(totalClients, totalConns uint) []*clientv3.Client {
  96. conns := make([]*clientv3.Client, totalConns)
  97. for i := range conns {
  98. conns[i] = mustCreateConn()
  99. }
  100. clients := make([]*clientv3.Client, totalClients)
  101. for i := range clients {
  102. clients[i] = conns[i%int(totalConns)]
  103. }
  104. return clients
  105. }
  106. func mustRandBytes(n int) []byte {
  107. rb := make([]byte, n)
  108. _, err := rand.Read(rb)
  109. if err != nil {
  110. fmt.Fprintf(os.Stderr, "failed to generate value: %v\n", err)
  111. os.Exit(1)
  112. }
  113. return rb
  114. }
  115. func newReport() report.Report {
  116. p := "%4.4f"
  117. if precise {
  118. p = "%g"
  119. }
  120. if sample {
  121. return report.NewReportSample(p)
  122. }
  123. return report.NewReport(p)
  124. }