cluster.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. // 500000 100B key (50MB)
  98. KeySize: 100,
  99. KeySuffixRange: 500000,
  100. N: 200,
  101. }
  102. go s.Stress()
  103. stressers[i] = s
  104. }
  105. c.Size = size
  106. c.Agents = agents
  107. c.Stressers = stressers
  108. c.Names = names
  109. c.ClientURLs = clientURLs
  110. return nil
  111. }
  112. func (c *cluster) WaitHealth() error {
  113. var err error
  114. for i := 0; i < 10; i++ {
  115. err = setHealthKey(c.ClientURLs)
  116. if err == nil {
  117. return nil
  118. }
  119. time.Sleep(time.Second)
  120. }
  121. return err
  122. }
  123. func (c *cluster) Report() (success, failure int) {
  124. for _, stress := range c.Stressers {
  125. s, f := stress.Report()
  126. success += s
  127. failure += f
  128. }
  129. return
  130. }
  131. func (c *cluster) Cleanup() error {
  132. var lasterr error
  133. for _, a := range c.Agents {
  134. if err := a.Cleanup(); err != nil {
  135. lasterr = err
  136. }
  137. }
  138. for _, s := range c.Stressers {
  139. s.Cancel()
  140. }
  141. return lasterr
  142. }
  143. func (c *cluster) Terminate() {
  144. for _, a := range c.Agents {
  145. a.Terminate()
  146. }
  147. for _, s := range c.Stressers {
  148. s.Cancel()
  149. }
  150. }
  151. func (c *cluster) Status() ClusterStatus {
  152. cs := ClusterStatus{
  153. AgentStatuses: make(map[string]client.Status),
  154. }
  155. for i, a := range c.Agents {
  156. s, err := a.Status()
  157. // TODO: add a.Desc() as a key of the map
  158. desc := c.agentEndpoints[i]
  159. if err != nil {
  160. cs.AgentStatuses[desc] = client.Status{State: "unknown"}
  161. log.Printf("etcd-tester: failed to get the status of agent [%s]", desc)
  162. }
  163. cs.AgentStatuses[desc] = s
  164. }
  165. return cs
  166. }
  167. // setHealthKey sets health key on all given urls.
  168. func setHealthKey(us []string) error {
  169. for _, u := range us {
  170. cfg := etcdclient.Config{
  171. Endpoints: []string{u},
  172. }
  173. c, err := etcdclient.New(cfg)
  174. if err != nil {
  175. return err
  176. }
  177. kapi := etcdclient.NewKeysAPI(c)
  178. _, err = kapi.Set(context.TODO(), "health", "good", nil)
  179. if err != nil {
  180. return err
  181. }
  182. }
  183. return nil
  184. }