util.go 5.5 KB

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