member.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2016 The etcd Authors
  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. "net"
  18. "net/url"
  19. "time"
  20. "golang.org/x/net/context"
  21. "google.golang.org/grpc"
  22. "github.com/coreos/etcd/clientv3"
  23. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  24. "github.com/coreos/etcd/tools/functional-tester/etcd-agent/client"
  25. )
  26. type member struct {
  27. Agent client.Agent
  28. Endpoint string
  29. Name string
  30. ClientURL string
  31. PeerURL string
  32. FailpointURL string
  33. }
  34. func (m *member) ClusterEntry() string { return m.Name + "=" + m.PeerURL }
  35. func (m *member) Flags() []string {
  36. return []string{
  37. "--name", m.Name,
  38. "--listen-client-urls", m.ClientURL,
  39. "--advertise-client-urls", m.ClientURL,
  40. "--listen-peer-urls", m.PeerURL,
  41. "--initial-advertise-peer-urls", m.PeerURL,
  42. "--initial-cluster-state", "new",
  43. }
  44. }
  45. func (m *member) CheckCompact(rev int64) error {
  46. cli, err := m.newClientV3()
  47. if err != nil {
  48. return fmt.Errorf("%v (endpoint %s)", err, m.ClientURL)
  49. }
  50. defer cli.Close()
  51. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  52. wch := cli.Watch(ctx, "\x00", clientv3.WithFromKey(), clientv3.WithRev(rev-1))
  53. wr, ok := <-wch
  54. cancel()
  55. if !ok {
  56. return fmt.Errorf("watch channel terminated (endpoint %s)", m.ClientURL)
  57. }
  58. if wr.CompactRevision != rev {
  59. return fmt.Errorf("got compact revision %v, wanted %v (endpoint %s)", wr.CompactRevision, rev, m.ClientURL)
  60. }
  61. return nil
  62. }
  63. func (m *member) Defrag() error {
  64. plog.Printf("defragmenting %s\n", m.ClientURL)
  65. cli, err := m.newClientV3()
  66. if err != nil {
  67. return err
  68. }
  69. defer cli.Close()
  70. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
  71. _, err = cli.Defragment(ctx, m.ClientURL)
  72. cancel()
  73. if err != nil {
  74. return err
  75. }
  76. plog.Printf("defragmented %s\n", m.ClientURL)
  77. return nil
  78. }
  79. func (m *member) RevHash() (int64, int64, error) {
  80. conn, err := m.dialGRPC()
  81. if err != nil {
  82. return 0, 0, err
  83. }
  84. mt := pb.NewMaintenanceClient(conn)
  85. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  86. resp, err := mt.Hash(ctx, &pb.HashRequest{}, grpc.FailFast(false))
  87. cancel()
  88. conn.Close()
  89. if err != nil {
  90. return 0, 0, err
  91. }
  92. return resp.Header.Revision, int64(resp.Hash), nil
  93. }
  94. func (m *member) Rev(ctx context.Context) (int64, error) {
  95. cli, err := m.newClientV3()
  96. if err != nil {
  97. return 0, err
  98. }
  99. defer cli.Close()
  100. resp, err := cli.Status(ctx, m.ClientURL)
  101. if err != nil {
  102. return 0, err
  103. }
  104. return resp.Header.Revision, nil
  105. }
  106. func (m *member) IsLeader() (bool, error) {
  107. cli, err := m.newClientV3()
  108. if err != nil {
  109. return false, err
  110. }
  111. defer cli.Close()
  112. resp, err := cli.Status(context.Background(), m.ClientURL)
  113. if err != nil {
  114. return false, err
  115. }
  116. return resp.Header.MemberId == resp.Leader, nil
  117. }
  118. func (m *member) SetHealthKeyV3() error {
  119. cli, err := m.newClientV3()
  120. if err != nil {
  121. return fmt.Errorf("%v (%s)", err, m.ClientURL)
  122. }
  123. defer cli.Close()
  124. // give enough time-out in case expensive requests (range/delete) are pending
  125. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  126. _, err = cli.Put(ctx, "health", "good")
  127. cancel()
  128. if err != nil {
  129. return fmt.Errorf("%v (%s)", err, m.ClientURL)
  130. }
  131. return nil
  132. }
  133. func (m *member) newClientV3() (*clientv3.Client, error) {
  134. return clientv3.New(clientv3.Config{
  135. Endpoints: []string{m.ClientURL},
  136. DialTimeout: 5 * time.Second,
  137. })
  138. }
  139. func (m *member) dialGRPC() (*grpc.ClientConn, error) {
  140. return grpc.Dial(m.grpcAddr(), grpc.WithInsecure(), grpc.WithTimeout(5*time.Second), grpc.WithBlock())
  141. }
  142. // grpcAddr gets the host from clientURL so it works with grpc.Dial()
  143. func (m *member) grpcAddr() string {
  144. u, err := url.Parse(m.ClientURL)
  145. if err != nil {
  146. panic(err)
  147. }
  148. return u.Host
  149. }
  150. func (m *member) peerPort() (port int) {
  151. u, err := url.Parse(m.PeerURL)
  152. if err != nil {
  153. panic(err)
  154. }
  155. _, portStr, err := net.SplitHostPort(u.Host)
  156. if err != nil {
  157. panic(err)
  158. }
  159. if _, err = fmt.Sscanf(portStr, "%d", &port); err != nil {
  160. panic(err)
  161. }
  162. return port
  163. }