balancer_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. "context"
  17. "errors"
  18. "net"
  19. "sync"
  20. "testing"
  21. "time"
  22. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  23. "github.com/coreos/etcd/pkg/testutil"
  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, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
  77. _, _, err := sb.Get(ctx, blockingOpts)
  78. cancel()
  79. if err != context.DeadlineExceeded {
  80. t.Errorf("Get() with no up endpoints should timeout, got %v", err)
  81. }
  82. downC := make(chan func(error), 1)
  83. go func() {
  84. // ensure sb.Up() will be called after sb.Get() to see if Up() releases blocking Get()
  85. time.Sleep(time.Millisecond * 100)
  86. f := sb.Up(grpc.Address{Addr: endpoints[1]})
  87. if addrs := <-sb.Notify(); len(addrs) != 1 {
  88. t.Errorf("first Up() should have triggered balancer to send the first connected address via Notify chan so that other connections can be closed")
  89. }
  90. downC <- f
  91. }()
  92. addrFirst, putFun, err := sb.Get(context.Background(), blockingOpts)
  93. if err != nil {
  94. t.Errorf("Get() with up endpoints should success, got %v", err)
  95. }
  96. if addrFirst.Addr != endpoints[1] {
  97. t.Errorf("Get() didn't return expected address, got %v", addrFirst)
  98. }
  99. if putFun == nil {
  100. t.Errorf("Get() returned unexpected nil put function")
  101. }
  102. down1 := <-downC
  103. down2 := sb.Up(grpc.Address{Addr: endpoints[2]})
  104. addrSecond, _, _ := sb.Get(context.Background(), blockingOpts)
  105. if addrFirst.Addr != addrSecond.Addr {
  106. t.Errorf("Get() didn't return the same address as previous call, got %v and %v", addrFirst, addrSecond)
  107. }
  108. down1(errors.New("error"))
  109. if addrs := <-sb.Notify(); len(addrs) != len(endpoints) {
  110. t.Errorf("closing the only connection should triggered balancer to send the all endpoints via Notify chan so that we can establish a connection")
  111. }
  112. down2(errors.New("error"))
  113. ctx, cancel = context.WithTimeout(context.Background(), time.Millisecond*100)
  114. _, _, err = sb.Get(ctx, blockingOpts)
  115. cancel()
  116. if err != context.DeadlineExceeded {
  117. t.Errorf("Get() with no up endpoints should timeout, got %v", err)
  118. }
  119. }
  120. // TestBalancerDoNotBlockOnClose ensures that balancer and grpc don't deadlock each other
  121. // due to rapid open/close conn. The deadlock causes balancer.Close() to block forever.
  122. // See issue: https://github.com/coreos/etcd/issues/7283 for more detail.
  123. func TestBalancerDoNotBlockOnClose(t *testing.T) {
  124. defer testutil.AfterTest(t)
  125. kcl := newKillConnListener(t, 3)
  126. defer kcl.close()
  127. for i := 0; i < 5; i++ {
  128. sb := newSimpleBalancer(kcl.endpoints())
  129. conn, err := grpc.Dial("", grpc.WithInsecure(), grpc.WithBalancer(sb))
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. kvc := pb.NewKVClient(conn)
  134. <-sb.readyc
  135. var wg sync.WaitGroup
  136. wg.Add(100)
  137. cctx, cancel := context.WithCancel(context.TODO())
  138. for j := 0; j < 100; j++ {
  139. go func() {
  140. defer wg.Done()
  141. kvc.Range(cctx, &pb.RangeRequest{}, grpc.FailFast(false))
  142. }()
  143. }
  144. // balancer.Close() might block
  145. // if balancer and grpc deadlock each other.
  146. bclosec, cclosec := make(chan struct{}), make(chan struct{})
  147. go func() {
  148. defer close(bclosec)
  149. sb.Close()
  150. }()
  151. go func() {
  152. defer close(cclosec)
  153. conn.Close()
  154. }()
  155. select {
  156. case <-bclosec:
  157. case <-time.After(3 * time.Second):
  158. testutil.FatalStack(t, "balancer close timeout")
  159. }
  160. select {
  161. case <-cclosec:
  162. case <-time.After(3 * time.Second):
  163. t.Fatal("grpc conn close timeout")
  164. }
  165. cancel()
  166. wg.Wait()
  167. }
  168. }
  169. // killConnListener listens incoming conn and kills it immediately.
  170. type killConnListener struct {
  171. wg sync.WaitGroup
  172. eps []string
  173. stopc chan struct{}
  174. t *testing.T
  175. }
  176. func newKillConnListener(t *testing.T, size int) *killConnListener {
  177. kcl := &killConnListener{stopc: make(chan struct{}), t: t}
  178. for i := 0; i < size; i++ {
  179. ln, err := net.Listen("tcp", ":0")
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. kcl.eps = append(kcl.eps, ln.Addr().String())
  184. kcl.wg.Add(1)
  185. go kcl.listen(ln)
  186. }
  187. return kcl
  188. }
  189. func (kcl *killConnListener) endpoints() []string {
  190. return kcl.eps
  191. }
  192. func (kcl *killConnListener) listen(l net.Listener) {
  193. go func() {
  194. defer kcl.wg.Done()
  195. for {
  196. conn, err := l.Accept()
  197. select {
  198. case <-kcl.stopc:
  199. return
  200. default:
  201. }
  202. if err != nil {
  203. kcl.t.Fatal(err)
  204. }
  205. time.Sleep(1 * time.Millisecond)
  206. conn.Close()
  207. }
  208. }()
  209. <-kcl.stopc
  210. l.Close()
  211. }
  212. func (kcl *killConnListener) close() {
  213. close(kcl.stopc)
  214. kcl.wg.Wait()
  215. }