cluster.go 4.6 KB

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