balancer_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package test
  19. import (
  20. "context"
  21. "reflect"
  22. "testing"
  23. "time"
  24. "github.com/golang/protobuf/proto"
  25. "google.golang.org/grpc"
  26. "google.golang.org/grpc/balancer"
  27. "google.golang.org/grpc/connectivity"
  28. "google.golang.org/grpc/credentials"
  29. "google.golang.org/grpc/grpclog"
  30. "google.golang.org/grpc/internal/balancerload/orca"
  31. orcapb "google.golang.org/grpc/internal/balancerload/orca/orca_v1"
  32. "google.golang.org/grpc/resolver"
  33. testpb "google.golang.org/grpc/test/grpc_testing"
  34. "google.golang.org/grpc/testdata"
  35. _ "google.golang.org/grpc/internal/balancerload/orca"
  36. )
  37. const testBalancerName = "testbalancer"
  38. // testBalancer creates one subconn with the first address from resolved
  39. // addresses.
  40. //
  41. // It's used to test options for NewSubConn are applies correctly.
  42. type testBalancer struct {
  43. cc balancer.ClientConn
  44. sc balancer.SubConn
  45. newSubConnOptions balancer.NewSubConnOptions
  46. pickOptions []balancer.PickOptions
  47. doneInfo []balancer.DoneInfo
  48. }
  49. func (b *testBalancer) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
  50. b.cc = cc
  51. return b
  52. }
  53. func (*testBalancer) Name() string {
  54. return testBalancerName
  55. }
  56. func (b *testBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) {
  57. // Only create a subconn at the first time.
  58. if err == nil && b.sc == nil {
  59. b.sc, err = b.cc.NewSubConn(addrs, b.newSubConnOptions)
  60. if err != nil {
  61. grpclog.Errorf("testBalancer: failed to NewSubConn: %v", err)
  62. return
  63. }
  64. b.cc.UpdateBalancerState(connectivity.Connecting, &picker{sc: b.sc, bal: b})
  65. b.sc.Connect()
  66. }
  67. }
  68. func (b *testBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
  69. grpclog.Infof("testBalancer: HandleSubConnStateChange: %p, %v", sc, s)
  70. if b.sc != sc {
  71. grpclog.Infof("testBalancer: ignored state change because sc is not recognized")
  72. return
  73. }
  74. if s == connectivity.Shutdown {
  75. b.sc = nil
  76. return
  77. }
  78. switch s {
  79. case connectivity.Ready, connectivity.Idle:
  80. b.cc.UpdateBalancerState(s, &picker{sc: sc, bal: b})
  81. case connectivity.Connecting:
  82. b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrNoSubConnAvailable, bal: b})
  83. case connectivity.TransientFailure:
  84. b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrTransientFailure, bal: b})
  85. }
  86. }
  87. func (b *testBalancer) Close() {
  88. }
  89. type picker struct {
  90. err error
  91. sc balancer.SubConn
  92. bal *testBalancer
  93. }
  94. func (p *picker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
  95. if p.err != nil {
  96. return nil, nil, p.err
  97. }
  98. p.bal.pickOptions = append(p.bal.pickOptions, opts)
  99. return p.sc, func(d balancer.DoneInfo) { p.bal.doneInfo = append(p.bal.doneInfo, d) }, nil
  100. }
  101. func (s) TestCredsBundleFromBalancer(t *testing.T) {
  102. balancer.Register(&testBalancer{
  103. newSubConnOptions: balancer.NewSubConnOptions{
  104. CredsBundle: &testCredsBundle{},
  105. },
  106. })
  107. te := newTest(t, env{name: "creds-bundle", network: "tcp", balancer: ""})
  108. te.tapHandle = authHandle
  109. te.customDialOptions = []grpc.DialOption{
  110. grpc.WithBalancerName(testBalancerName),
  111. }
  112. creds, err := credentials.NewServerTLSFromFile(testdata.Path("server1.pem"), testdata.Path("server1.key"))
  113. if err != nil {
  114. t.Fatalf("Failed to generate credentials %v", err)
  115. }
  116. te.customServerOptions = []grpc.ServerOption{
  117. grpc.Creds(creds),
  118. }
  119. te.startServer(&testServer{})
  120. defer te.tearDown()
  121. cc := te.clientConn()
  122. tc := testpb.NewTestServiceClient(cc)
  123. if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
  124. t.Fatalf("Test failed. Reason: %v", err)
  125. }
  126. }
  127. func (s) TestDoneInfo(t *testing.T) {
  128. for _, e := range listTestEnv() {
  129. testDoneInfo(t, e)
  130. }
  131. }
  132. func testDoneInfo(t *testing.T, e env) {
  133. te := newTest(t, e)
  134. b := &testBalancer{}
  135. balancer.Register(b)
  136. te.customDialOptions = []grpc.DialOption{
  137. grpc.WithBalancerName(testBalancerName),
  138. }
  139. te.userAgent = failAppUA
  140. te.startServer(&testServer{security: e.security})
  141. defer te.tearDown()
  142. cc := te.clientConn()
  143. tc := testpb.NewTestServiceClient(cc)
  144. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  145. defer cancel()
  146. wantErr := detailedError
  147. if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); !reflect.DeepEqual(err, wantErr) {
  148. t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %v", err, wantErr)
  149. }
  150. if _, err := tc.UnaryCall(ctx, &testpb.SimpleRequest{}); err != nil {
  151. t.Fatalf("TestService.UnaryCall(%v, _, _, _) = _, %v; want _, <nil>", ctx, err)
  152. }
  153. if len(b.doneInfo) < 1 || !reflect.DeepEqual(b.doneInfo[0].Err, wantErr) {
  154. t.Fatalf("b.doneInfo = %v; want b.doneInfo[0].Err = %v", b.doneInfo, wantErr)
  155. }
  156. if len(b.doneInfo) < 2 || !reflect.DeepEqual(b.doneInfo[1].Trailer, testTrailerMetadata) {
  157. t.Fatalf("b.doneInfo = %v; want b.doneInfo[1].Trailer = %v", b.doneInfo, testTrailerMetadata)
  158. }
  159. if len(b.pickOptions) != len(b.doneInfo) {
  160. t.Fatalf("Got %d picks, but %d doneInfo, want equal amount", len(b.pickOptions), len(b.doneInfo))
  161. }
  162. // To test done() is always called, even if it's returned with a non-Ready
  163. // SubConn.
  164. //
  165. // Stop server and at the same time send RPCs. There are chances that picker
  166. // is not updated in time, causing a non-Ready SubConn to be returned.
  167. finished := make(chan struct{})
  168. go func() {
  169. for i := 0; i < 20; i++ {
  170. tc.UnaryCall(ctx, &testpb.SimpleRequest{})
  171. }
  172. close(finished)
  173. }()
  174. te.srv.Stop()
  175. <-finished
  176. if len(b.pickOptions) != len(b.doneInfo) {
  177. t.Fatalf("Got %d picks, %d doneInfo, want equal amount", len(b.pickOptions), len(b.doneInfo))
  178. }
  179. }
  180. func (s) TestDoneLoads(t *testing.T) {
  181. for _, e := range listTestEnv() {
  182. testDoneLoads(t, e)
  183. }
  184. }
  185. func testDoneLoads(t *testing.T, e env) {
  186. b := &testBalancer{}
  187. balancer.Register(b)
  188. testLoad := &orcapb.LoadReport{
  189. CpuUtilization: 0.31,
  190. MemUtilization: 0.41,
  191. NicInUtilization: 0.59,
  192. NicOutUtilization: 0.26,
  193. RequestCostOrUtilization: nil,
  194. }
  195. ss := &stubServer{
  196. emptyCall: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
  197. grpc.SetTrailer(ctx, orca.ToMetadata(testLoad))
  198. return &testpb.Empty{}, nil
  199. },
  200. }
  201. if err := ss.Start(nil, grpc.WithBalancerName(testBalancerName)); err != nil {
  202. t.Fatalf("error starting testing server: %v", err)
  203. }
  204. defer ss.Stop()
  205. tc := testpb.NewTestServiceClient(ss.cc)
  206. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  207. defer cancel()
  208. if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
  209. t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %v", err, nil)
  210. }
  211. poWant := []balancer.PickOptions{
  212. {FullMethodName: "/grpc.testing.TestService/EmptyCall"},
  213. }
  214. if !reflect.DeepEqual(b.pickOptions, poWant) {
  215. t.Fatalf("b.pickOptions = %v; want %v", b.pickOptions, poWant)
  216. }
  217. if len(b.doneInfo) < 1 {
  218. t.Fatalf("b.doneInfo = %v, want length 1", b.doneInfo)
  219. }
  220. gotLoad, _ := b.doneInfo[0].ServerLoad.(*orcapb.LoadReport)
  221. if !proto.Equal(gotLoad, testLoad) {
  222. t.Fatalf("b.doneInfo[0].ServerLoad = %v; want = %v", b.doneInfo[0].ServerLoad, testLoad)
  223. }
  224. }