util.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. Copyright 2013 CoreOS Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package test
  14. import (
  15. "errors"
  16. "fmt"
  17. "io/ioutil"
  18. "net"
  19. "net/http"
  20. "os"
  21. "strconv"
  22. "time"
  23. "github.com/coreos/etcd/third_party/github.com/coreos/go-etcd/etcd"
  24. )
  25. var client = http.Client{
  26. Transport: &http.Transport{
  27. Dial: dialTimeoutFast,
  28. },
  29. }
  30. // Sending set commands
  31. func Set(stop chan bool) {
  32. stopSet := false
  33. i := 0
  34. c := etcd.NewClient(nil)
  35. for {
  36. key := fmt.Sprintf("%s_%v", "foo", i)
  37. result, err := c.Set(key, "bar", 0)
  38. if err != nil || result.Node.Key != "/"+key || result.Node.Value != "bar" {
  39. select {
  40. case <-stop:
  41. stopSet = true
  42. default:
  43. }
  44. }
  45. select {
  46. case <-stop:
  47. stopSet = true
  48. default:
  49. }
  50. if stopSet {
  51. break
  52. }
  53. i++
  54. }
  55. stop <- true
  56. }
  57. func WaitForServer(host string, client http.Client, scheme string) error {
  58. path := fmt.Sprintf("%s://%s/v2/keys/", scheme, host)
  59. var resp *http.Response
  60. var err error
  61. for i := 0; i < 10; i++ {
  62. time.Sleep(1 * time.Second)
  63. resp, err = client.Get(path)
  64. if err == nil && resp.StatusCode == 200 {
  65. return nil
  66. }
  67. }
  68. return errors.New(fmt.Sprintf("etcd server was not reachable in a long time, last-time response and error: %v; %v", resp, err))
  69. }
  70. // Create a cluster of etcd nodes
  71. func CreateCluster(size int, procAttr *os.ProcAttr, ssl bool) ([][]string, []*os.Process, error) {
  72. argGroup := make([][]string, size)
  73. sslServer1 := []string{"-peer-ca-file=../../fixtures/ca/ca.crt",
  74. "-peer-cert-file=../../fixtures/ca/server.crt",
  75. "-peer-key-file=../../fixtures/ca/server.key.insecure",
  76. }
  77. sslServer2 := []string{"-peer-ca-file=../../fixtures/ca/ca.crt",
  78. "-peer-cert-file=../../fixtures/ca/server2.crt",
  79. "-peer-key-file=../../fixtures/ca/server2.key.insecure",
  80. }
  81. for i := 0; i < size; i++ {
  82. if i == 0 {
  83. argGroup[i] = []string{"etcd", "-data-dir=/tmp/node1", "-name=node1"}
  84. if ssl {
  85. argGroup[i] = append(argGroup[i], sslServer1...)
  86. }
  87. } else {
  88. strI := strconv.Itoa(i + 1)
  89. argGroup[i] = []string{"etcd", "-name=node" + strI, fmt.Sprintf("-addr=127.0.0.1:%d", 4001+i), fmt.Sprintf("-peer-addr=127.0.0.1:%d", 7001+i), "-data-dir=/tmp/node" + strI, "-peers=127.0.0.1:7001"}
  90. if ssl {
  91. argGroup[i] = append(argGroup[i], sslServer2...)
  92. }
  93. }
  94. }
  95. etcds := make([]*os.Process, size)
  96. for i := range etcds {
  97. var err error
  98. etcds[i], err = os.StartProcess(EtcdBinPath, append(argGroup[i], "-f"), procAttr)
  99. if err != nil {
  100. return nil, nil, err
  101. }
  102. // The problem is that if the master isn't up then the children
  103. // have to retry. This retry can take upwards of 15 seconds
  104. // which slows tests way down and some of them fail.
  105. //
  106. // Waiting for each server to start when ssl is a workaround.
  107. // Autotest machines are dramatically slow, and it could spend
  108. // several seconds to build TSL connections between servers. That
  109. // is extremely terribe when the second machine joins the cluster
  110. // because the cluster is out of work at this time. The guy
  111. // tries to join during this time will fail, and current implementation
  112. // makes it fail after just one-time try(bug in #661). This
  113. // makes the cluster start with N-1 machines.
  114. // TODO(yichengq): It should be fixed.
  115. if i == 0 || ssl {
  116. client := buildClient()
  117. err = WaitForServer("127.0.0.1:400"+strconv.Itoa(i+1), client, "http")
  118. if err != nil {
  119. return nil, nil, err
  120. }
  121. }
  122. }
  123. return argGroup, etcds, nil
  124. }
  125. // Destroy all the nodes in the cluster
  126. func DestroyCluster(etcds []*os.Process) error {
  127. for _, etcd := range etcds {
  128. err := etcd.Kill()
  129. if err != nil {
  130. panic(err.Error())
  131. }
  132. etcd.Release()
  133. }
  134. return nil
  135. }
  136. //
  137. func Monitor(size int, allowDeadNum int, leaderChan chan string, all chan bool, stop chan bool) {
  138. leaderMap := make(map[int]string)
  139. baseAddrFormat := "http://0.0.0.0:400%d"
  140. for {
  141. knownLeader := "unknown"
  142. dead := 0
  143. var i int
  144. for i = 0; i < size; i++ {
  145. leader, err := getLeader(fmt.Sprintf(baseAddrFormat, i+1))
  146. if err == nil {
  147. leaderMap[i] = leader
  148. if knownLeader == "unknown" {
  149. knownLeader = leader
  150. } else {
  151. if leader != knownLeader {
  152. break
  153. }
  154. }
  155. } else {
  156. dead++
  157. if dead > allowDeadNum {
  158. break
  159. }
  160. }
  161. }
  162. if i == size {
  163. select {
  164. case <-stop:
  165. return
  166. case <-leaderChan:
  167. leaderChan <- knownLeader
  168. default:
  169. leaderChan <- knownLeader
  170. }
  171. }
  172. if dead == 0 {
  173. select {
  174. case <-all:
  175. all <- true
  176. default:
  177. all <- true
  178. }
  179. }
  180. time.Sleep(time.Millisecond * 10)
  181. }
  182. }
  183. func getLeader(addr string) (string, error) {
  184. resp, err := client.Get(addr + "/v1/leader")
  185. if err != nil {
  186. return "", err
  187. }
  188. if resp.StatusCode != http.StatusOK {
  189. resp.Body.Close()
  190. return "", fmt.Errorf("no leader")
  191. }
  192. b, err := ioutil.ReadAll(resp.Body)
  193. resp.Body.Close()
  194. if err != nil {
  195. return "", err
  196. }
  197. return string(b), nil
  198. }
  199. // Dial with timeout
  200. func dialTimeoutFast(network, addr string) (net.Conn, error) {
  201. return net.DialTimeout(network, addr, time.Millisecond*10)
  202. }