util.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "log"
  20. "os"
  21. "strings"
  22. "github.com/coreos/etcd/clientv3"
  23. "github.com/coreos/etcd/pkg/report"
  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. if sresp, serr := c.Status(context.TODO(), ep); serr == nil {
  41. leaderId = sresp.Leader
  42. break
  43. }
  44. }
  45. for _, m := range resp.Members {
  46. if m.ID == leaderId {
  47. leaderEps = m.ClientURLs
  48. return
  49. }
  50. }
  51. fmt.Fprintf(os.Stderr, "failed to find a leader endpoint\n")
  52. os.Exit(1)
  53. }
  54. func mustCreateConn() *clientv3.Client {
  55. connEndpoints := leaderEps
  56. if len(connEndpoints) == 0 {
  57. connEndpoints = []string{endpoints[dialTotal%len(endpoints)]}
  58. dialTotal++
  59. }
  60. cfg := clientv3.Config{
  61. Endpoints: connEndpoints,
  62. DialTimeout: dialTimeout,
  63. }
  64. if !tls.Empty() {
  65. cfgtls, err := tls.ClientConfig()
  66. if err != nil {
  67. fmt.Fprintf(os.Stderr, "bad tls config: %v\n", err)
  68. os.Exit(1)
  69. }
  70. cfg.TLS = cfgtls
  71. }
  72. if len(user) != 0 {
  73. splitted := strings.SplitN(user, ":", 2)
  74. if len(splitted) != 2 {
  75. fmt.Fprintf(os.Stderr, "bad user information: %s\n", user)
  76. os.Exit(1)
  77. }
  78. cfg.Username = splitted[0]
  79. cfg.Password = splitted[1]
  80. }
  81. client, err := clientv3.New(cfg)
  82. if targetLeader && len(leaderEps) == 0 {
  83. mustFindLeaderEndpoints(client)
  84. client.Close()
  85. return mustCreateConn()
  86. }
  87. clientv3.SetLogger(log.New(os.Stderr, "grpc", 0))
  88. if err != nil {
  89. fmt.Fprintf(os.Stderr, "dial error: %v\n", err)
  90. os.Exit(1)
  91. }
  92. return client
  93. }
  94. func mustCreateClients(totalClients, totalConns uint) []*clientv3.Client {
  95. conns := make([]*clientv3.Client, totalConns)
  96. for i := range conns {
  97. conns[i] = mustCreateConn()
  98. }
  99. clients := make([]*clientv3.Client, totalClients)
  100. for i := range clients {
  101. clients[i] = conns[i%int(totalConns)]
  102. }
  103. return clients
  104. }
  105. func mustRandBytes(n int) []byte {
  106. rb := make([]byte, n)
  107. _, err := rand.Read(rb)
  108. if err != nil {
  109. fmt.Fprintf(os.Stderr, "failed to generate value: %v\n", err)
  110. os.Exit(1)
  111. }
  112. return rb
  113. }
  114. func newReport() report.Report {
  115. p := "%4.4f"
  116. if precise {
  117. p = "%g"
  118. }
  119. if sample {
  120. return report.NewReportSample(p)
  121. }
  122. return report.NewReport(p)
  123. }
  124. func newWeightedReport() report.Report {
  125. p := "%4.4f"
  126. if precise {
  127. p = "%g"
  128. }
  129. if sample {
  130. return report.NewReportSample(p)
  131. }
  132. return report.NewWeightedReport(report.NewReport(p), p)
  133. }