cluster.go 5.0 KB

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