cluster.go 4.4 KB

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