balancer_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 clientv3
  15. import (
  16. "errors"
  17. "net"
  18. "sync"
  19. "testing"
  20. "time"
  21. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. "golang.org/x/net/context"
  24. "google.golang.org/grpc"
  25. )
  26. var (
  27. endpoints = []string{"localhost:2379", "localhost:22379", "localhost:32379"}
  28. )
  29. func TestBalancerGetUnblocking(t *testing.T) {
  30. sb := newSimpleBalancer(endpoints)
  31. defer sb.Close()
  32. if addrs := <-sb.Notify(); len(addrs) != len(endpoints) {
  33. t.Errorf("Initialize newSimpleBalancer should have triggered Notify() chan, but it didn't")
  34. }
  35. unblockingOpts := grpc.BalancerGetOptions{BlockingWait: false}
  36. _, _, err := sb.Get(context.Background(), unblockingOpts)
  37. if err != ErrNoAddrAvilable {
  38. t.Errorf("Get() with no up endpoints should return ErrNoAddrAvailable, got: %v", err)
  39. }
  40. down1 := sb.Up(grpc.Address{Addr: endpoints[1]})
  41. if addrs := <-sb.Notify(); len(addrs) != 1 {
  42. t.Errorf("first Up() should have triggered balancer to send the first connected address via Notify chan so that other connections can be closed")
  43. }
  44. down2 := sb.Up(grpc.Address{Addr: endpoints[2]})
  45. addrFirst, putFun, err := sb.Get(context.Background(), unblockingOpts)
  46. if err != nil {
  47. t.Errorf("Get() with up endpoints should success, got %v", err)
  48. }
  49. if addrFirst.Addr != endpoints[1] {
  50. t.Errorf("Get() didn't return expected address, got %v", addrFirst)
  51. }
  52. if putFun == nil {
  53. t.Errorf("Get() returned unexpected nil put function")
  54. }
  55. addrSecond, _, _ := sb.Get(context.Background(), unblockingOpts)
  56. if addrFirst.Addr != addrSecond.Addr {
  57. t.Errorf("Get() didn't return the same address as previous call, got %v and %v", addrFirst, addrSecond)
  58. }
  59. down1(errors.New("error"))
  60. if addrs := <-sb.Notify(); len(addrs) != len(endpoints) {
  61. t.Errorf("closing the only connection should triggered balancer to send the all endpoints via Notify chan so that we can establish a connection")
  62. }
  63. down2(errors.New("error"))
  64. _, _, err = sb.Get(context.Background(), unblockingOpts)
  65. if err != ErrNoAddrAvilable {
  66. t.Errorf("Get() with no up endpoints should return ErrNoAddrAvailable, got: %v", err)
  67. }
  68. }
  69. func TestBalancerGetBlocking(t *testing.T) {
  70. sb := newSimpleBalancer(endpoints)
  71. defer sb.Close()
  72. if addrs := <-sb.Notify(); len(addrs) != len(endpoints) {
  73. t.Errorf("Initialize newSimpleBalancer should have triggered Notify() chan, but it didn't")
  74. }
  75. blockingOpts := grpc.BalancerGetOptions{BlockingWait: true}
  76. ctx, _ := context.WithTimeout(context.Background(), time.Millisecond*100)
  77. _, _, err := sb.Get(ctx, blockingOpts)
  78. if err != context.DeadlineExceeded {
  79. t.Errorf("Get() with no up endpoints should timeout, got %v", err)
  80. }
  81. downC := make(chan func(error), 1)
  82. go func() {
  83. // ensure sb.Up() will be called after sb.Get() to see if Up() releases blocking Get()
  84. time.Sleep(time.Millisecond * 100)
  85. f := sb.Up(grpc.Address{Addr: endpoints[1]})
  86. if addrs := <-sb.Notify(); len(addrs) != 1 {
  87. t.Errorf("first Up() should have triggered balancer to send the first connected address via Notify chan so that other connections can be closed")
  88. }
  89. downC <- f
  90. }()
  91. addrFirst, putFun, err := sb.Get(context.Background(), blockingOpts)
  92. if err != nil {
  93. t.Errorf("Get() with up endpoints should success, got %v", err)
  94. }
  95. if addrFirst.Addr != endpoints[1] {
  96. t.Errorf("Get() didn't return expected address, got %v", addrFirst)
  97. }
  98. if putFun == nil {
  99. t.Errorf("Get() returned unexpected nil put function")
  100. }
  101. down1 := <-downC
  102. down2 := sb.Up(grpc.Address{Addr: endpoints[2]})
  103. addrSecond, _, _ := sb.Get(context.Background(), blockingOpts)
  104. if addrFirst.Addr != addrSecond.Addr {
  105. t.Errorf("Get() didn't return the same address as previous call, got %v and %v", addrFirst, addrSecond)
  106. }
  107. down1(errors.New("error"))
  108. if addrs := <-sb.Notify(); len(addrs) != len(endpoints) {
  109. t.Errorf("closing the only connection should triggered balancer to send the all endpoints via Notify chan so that we can establish a connection")
  110. }
  111. down2(errors.New("error"))
  112. ctx, _ = context.WithTimeout(context.Background(), time.Millisecond*100)
  113. _, _, err = sb.Get(ctx, blockingOpts)
  114. if err != context.DeadlineExceeded {
  115. t.Errorf("Get() with no up endpoints should timeout, got %v", err)
  116. }
  117. }
  118. // TestBalancerDoNotBlockOnClose ensures that balancer and grpc don't deadlock each other
  119. // due to rapid open/close conn. The deadlock causes balancer.Close() to block forever.
  120. // See issue: https://github.com/coreos/etcd/issues/7283 for more detail.
  121. func TestBalancerDoNotBlockOnClose(t *testing.T) {
  122. defer testutil.AfterTest(t)
  123. kcl := newKillConnListener(t, 3)
  124. defer kcl.close()
  125. for i := 0; i < 5; i++ {
  126. sb := newSimpleBalancer(kcl.endpoints())
  127. conn, err := grpc.Dial("", grpc.WithInsecure(), grpc.WithBalancer(sb))
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. kvc := pb.NewKVClient(conn)
  132. <-sb.readyc
  133. for j := 0; j < 100; j++ {
  134. go kvc.Range(context.TODO(), &pb.RangeRequest{}, grpc.FailFast(false))
  135. }
  136. // balancer.Close() might block
  137. // if balancer and grpc deadlock each other.
  138. closec := make(chan struct{})
  139. go func() {
  140. defer close(closec)
  141. sb.Close()
  142. }()
  143. go conn.Close()
  144. select {
  145. case <-closec:
  146. case <-time.After(3 * time.Second):
  147. testutil.FatalStack(t, "balancer close timeout")
  148. }
  149. }
  150. }
  151. // killConnListener listens incoming conn and kills it immediately.
  152. type killConnListener struct {
  153. wg sync.WaitGroup
  154. eps []string
  155. stopc chan struct{}
  156. t *testing.T
  157. }
  158. func newKillConnListener(t *testing.T, size int) *killConnListener {
  159. kcl := &killConnListener{stopc: make(chan struct{}), t: t}
  160. for i := 0; i < size; i++ {
  161. ln, err := net.Listen("tcp", ":0")
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. kcl.eps = append(kcl.eps, ln.Addr().String())
  166. kcl.wg.Add(1)
  167. go kcl.listen(ln)
  168. }
  169. return kcl
  170. }
  171. func (kcl *killConnListener) endpoints() []string {
  172. return kcl.eps
  173. }
  174. func (kcl *killConnListener) listen(l net.Listener) {
  175. go func() {
  176. defer kcl.wg.Done()
  177. for {
  178. conn, err := l.Accept()
  179. select {
  180. case <-kcl.stopc:
  181. return
  182. default:
  183. }
  184. if err != nil {
  185. kcl.t.Fatal(err)
  186. }
  187. time.Sleep(1 * time.Millisecond)
  188. conn.Close()
  189. }
  190. }()
  191. <-kcl.stopc
  192. l.Close()
  193. }
  194. func (kcl *killConnListener) close() {
  195. close(kcl.stopc)
  196. kcl.wg.Wait()
  197. }