cluster.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 main
  15. import (
  16. "fmt"
  17. "math/rand"
  18. "net"
  19. "strings"
  20. "time"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  22. etcdclient "github.com/coreos/etcd/client"
  23. "github.com/coreos/etcd/tools/functional-tester/etcd-agent/client"
  24. )
  25. type cluster struct {
  26. agentEndpoints []string
  27. datadir string
  28. Size int
  29. Agents []client.Agent
  30. Stressers []Stresser
  31. Names []string
  32. ClientURLs []string
  33. }
  34. // newCluster starts and returns a new cluster. The caller should call Terminate when finished, to shut it down.
  35. func newCluster(agentEndpoints []string, datadir string) (*cluster, error) {
  36. c := &cluster{
  37. agentEndpoints: agentEndpoints,
  38. datadir: datadir,
  39. }
  40. if err := c.Bootstrap(); err != nil {
  41. return nil, err
  42. }
  43. return c, nil
  44. }
  45. func (c *cluster) Bootstrap() error {
  46. size := len(c.agentEndpoints)
  47. agents := make([]client.Agent, size)
  48. names := make([]string, size)
  49. clientURLs := make([]string, size)
  50. peerURLs := make([]string, size)
  51. members := make([]string, size)
  52. for i, u := range c.agentEndpoints {
  53. var err error
  54. agents[i], err = client.NewAgent(u)
  55. if err != nil {
  56. return err
  57. }
  58. names[i] = fmt.Sprintf("etcd-%d", i)
  59. host, _, err := net.SplitHostPort(u)
  60. if err != nil {
  61. return err
  62. }
  63. clientURLs[i] = fmt.Sprintf("http://%s:2379", host)
  64. peerURLs[i] = fmt.Sprintf("http://%s:2380", host)
  65. members[i] = fmt.Sprintf("%s=%s", names[i], peerURLs[i])
  66. }
  67. clusterStr := strings.Join(members, ",")
  68. token := fmt.Sprint(rand.Int())
  69. for i, a := range agents {
  70. _, err := a.Start(
  71. "-name", names[i],
  72. "-data-dir", c.datadir,
  73. "-advertise-client-urls", clientURLs[i],
  74. "-listen-client-urls", clientURLs[i],
  75. "-initial-advertise-peer-urls", peerURLs[i],
  76. "-listen-peer-urls", peerURLs[i],
  77. "-initial-cluster-token", token,
  78. "-initial-cluster", clusterStr,
  79. "-initial-cluster-state", "new",
  80. )
  81. if err != nil {
  82. // cleanup
  83. for j := 0; j < i; j++ {
  84. agents[j].Terminate()
  85. }
  86. return err
  87. }
  88. }
  89. stressers := make([]Stresser, len(clientURLs))
  90. for i, u := range clientURLs {
  91. s := &stresser{
  92. Endpoint: u,
  93. N: 200,
  94. }
  95. go s.Stress()
  96. stressers[i] = s
  97. }
  98. c.Size = size
  99. c.Agents = agents
  100. c.Stressers = stressers
  101. c.Names = names
  102. c.ClientURLs = clientURLs
  103. return nil
  104. }
  105. func (c *cluster) WaitHealth() error {
  106. var err error
  107. for i := 0; i < 10; i++ {
  108. err = setHealthKey(c.ClientURLs)
  109. if err == nil {
  110. return nil
  111. }
  112. time.Sleep(time.Second)
  113. }
  114. return err
  115. }
  116. func (c *cluster) Report() (success, failure int) {
  117. for _, stress := range c.Stressers {
  118. s, f := stress.Report()
  119. success += s
  120. failure += f
  121. }
  122. return
  123. }
  124. func (c *cluster) Cleanup() error {
  125. var lasterr error
  126. for _, a := range c.Agents {
  127. if err := a.Cleanup(); err != nil {
  128. lasterr = err
  129. }
  130. }
  131. for _, s := range c.Stressers {
  132. s.Cancel()
  133. }
  134. return lasterr
  135. }
  136. func (c *cluster) Terminate() {
  137. for _, a := range c.Agents {
  138. a.Terminate()
  139. }
  140. for _, s := range c.Stressers {
  141. s.Cancel()
  142. }
  143. }
  144. // setHealthKey sets health key on all given urls.
  145. func setHealthKey(us []string) error {
  146. for _, u := range us {
  147. cfg := etcdclient.Config{
  148. Endpoints: []string{u},
  149. }
  150. c, err := etcdclient.New(cfg)
  151. if err != nil {
  152. return err
  153. }
  154. kapi := etcdclient.NewKeysAPI(c)
  155. _, err = kapi.Set(context.TODO(), "health", "good", nil)
  156. if err != nil {
  157. return err
  158. }
  159. }
  160. return nil
  161. }