balancer_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. var wg sync.WaitGroup
  134. wg.Add(100)
  135. cctx, cancel := context.WithCancel(context.TODO())
  136. for j := 0; j < 100; j++ {
  137. go func() {
  138. defer wg.Done()
  139. kvc.Range(cctx, &pb.RangeRequest{}, grpc.FailFast(false))
  140. }()
  141. }
  142. // balancer.Close() might block
  143. // if balancer and grpc deadlock each other.
  144. bclosec, cclosec := make(chan struct{}), make(chan struct{})
  145. go func() {
  146. defer close(bclosec)
  147. sb.Close()
  148. }()
  149. go func() {
  150. defer close(cclosec)
  151. conn.Close()
  152. }()
  153. select {
  154. case <-bclosec:
  155. case <-time.After(3 * time.Second):
  156. testutil.FatalStack(t, "balancer close timeout")
  157. }
  158. select {
  159. case <-cclosec:
  160. case <-time.After(3 * time.Second):
  161. t.Fatal("grpc conn close timeout")
  162. }
  163. cancel()
  164. wg.Wait()
  165. }
  166. }
  167. // killConnListener listens incoming conn and kills it immediately.
  168. type killConnListener struct {
  169. wg sync.WaitGroup
  170. eps []string
  171. stopc chan struct{}
  172. t *testing.T
  173. }
  174. func newKillConnListener(t *testing.T, size int) *killConnListener {
  175. kcl := &killConnListener{stopc: make(chan struct{}), t: t}
  176. for i := 0; i < size; i++ {
  177. ln, err := net.Listen("tcp", ":0")
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. kcl.eps = append(kcl.eps, ln.Addr().String())
  182. kcl.wg.Add(1)
  183. go kcl.listen(ln)
  184. }
  185. return kcl
  186. }
  187. func (kcl *killConnListener) endpoints() []string {
  188. return kcl.eps
  189. }
  190. func (kcl *killConnListener) listen(l net.Listener) {
  191. go func() {
  192. defer kcl.wg.Done()
  193. for {
  194. conn, err := l.Accept()
  195. select {
  196. case <-kcl.stopc:
  197. return
  198. default:
  199. }
  200. if err != nil {
  201. kcl.t.Fatal(err)
  202. }
  203. time.Sleep(1 * time.Millisecond)
  204. conn.Close()
  205. }
  206. }()
  207. <-kcl.stopc
  208. l.Close()
  209. }
  210. func (kcl *killConnListener) close() {
  211. close(kcl.stopc)
  212. kcl.wg.Wait()
  213. }