util.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "context"
  17. "crypto/rand"
  18. "fmt"
  19. "os"
  20. "strings"
  21. "github.com/bgentry/speakeasy"
  22. "go.etcd.io/etcd/clientv3"
  23. "go.etcd.io/etcd/pkg/report"
  24. "google.golang.org/grpc/grpclog"
  25. )
  26. var (
  27. // dialTotal counts the number of mustCreateConn calls so that endpoint
  28. // connections can be handed out in round-robin order
  29. dialTotal int
  30. // leaderEps is a cache for holding endpoints of a leader node
  31. leaderEps []string
  32. // cache the username and password for multiple connections
  33. globalUserName string
  34. globalPassword string
  35. )
  36. func mustFindLeaderEndpoints(c *clientv3.Client) {
  37. resp, lerr := c.MemberList(context.TODO())
  38. if lerr != nil {
  39. fmt.Fprintf(os.Stderr, "failed to get a member list: %s\n", lerr)
  40. os.Exit(1)
  41. }
  42. leaderId := uint64(0)
  43. for _, ep := range c.Endpoints() {
  44. if sresp, serr := c.Status(context.TODO(), ep); serr == nil {
  45. leaderId = sresp.Leader
  46. break
  47. }
  48. }
  49. for _, m := range resp.Members {
  50. if m.ID == leaderId {
  51. leaderEps = m.ClientURLs
  52. return
  53. }
  54. }
  55. fmt.Fprintf(os.Stderr, "failed to find a leader endpoint\n")
  56. os.Exit(1)
  57. }
  58. func getUsernamePassword(usernameFlag string) (string, string, error) {
  59. if globalUserName != "" && globalPassword != "" {
  60. return globalUserName, globalPassword, nil
  61. }
  62. colon := strings.Index(usernameFlag, ":")
  63. if colon == -1 {
  64. // Prompt for the password.
  65. password, err := speakeasy.Ask("Password: ")
  66. if err != nil {
  67. return "", "", err
  68. }
  69. globalUserName = usernameFlag
  70. globalPassword = password
  71. } else {
  72. globalUserName = usernameFlag[:colon]
  73. globalPassword = usernameFlag[colon+1:]
  74. }
  75. return globalUserName, globalPassword, nil
  76. }
  77. func mustCreateConn() *clientv3.Client {
  78. connEndpoints := leaderEps
  79. if len(connEndpoints) == 0 {
  80. connEndpoints = []string{endpoints[dialTotal%len(endpoints)]}
  81. dialTotal++
  82. }
  83. cfg := clientv3.Config{
  84. Endpoints: connEndpoints,
  85. DialTimeout: dialTimeout,
  86. }
  87. if !tls.Empty() || tls.TrustedCAFile != "" {
  88. cfgtls, err := tls.ClientConfig()
  89. if err != nil {
  90. fmt.Fprintf(os.Stderr, "bad tls config: %v\n", err)
  91. os.Exit(1)
  92. }
  93. cfg.TLS = cfgtls
  94. }
  95. if len(user) != 0 {
  96. username, password, err := getUsernamePassword(user)
  97. if err != nil {
  98. fmt.Fprintf(os.Stderr, "bad user information: %s %v\n", user, err)
  99. os.Exit(1)
  100. }
  101. cfg.Username = username
  102. cfg.Password = password
  103. }
  104. client, err := clientv3.New(cfg)
  105. if targetLeader && len(leaderEps) == 0 {
  106. mustFindLeaderEndpoints(client)
  107. client.Close()
  108. return mustCreateConn()
  109. }
  110. clientv3.SetLogger(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr))
  111. if err != nil {
  112. fmt.Fprintf(os.Stderr, "dial error: %v\n", err)
  113. os.Exit(1)
  114. }
  115. return client
  116. }
  117. func mustCreateClients(totalClients, totalConns uint) []*clientv3.Client {
  118. conns := make([]*clientv3.Client, totalConns)
  119. for i := range conns {
  120. conns[i] = mustCreateConn()
  121. }
  122. clients := make([]*clientv3.Client, totalClients)
  123. for i := range clients {
  124. clients[i] = conns[i%int(totalConns)]
  125. }
  126. return clients
  127. }
  128. func mustRandBytes(n int) []byte {
  129. rb := make([]byte, n)
  130. _, err := rand.Read(rb)
  131. if err != nil {
  132. fmt.Fprintf(os.Stderr, "failed to generate value: %v\n", err)
  133. os.Exit(1)
  134. }
  135. return rb
  136. }
  137. func newReport() report.Report {
  138. p := "%4.4f"
  139. if precise {
  140. p = "%g"
  141. }
  142. if sample {
  143. return report.NewReportSample(p)
  144. }
  145. return report.NewReport(p)
  146. }
  147. func newWeightedReport() report.Report {
  148. p := "%4.4f"
  149. if precise {
  150. p = "%g"
  151. }
  152. if sample {
  153. return report.NewReportSample(p)
  154. }
  155. return report.NewWeightedReport(report.NewReport(p), p)
  156. }