pickfirst.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. *
  3. * Copyright 2022 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 pickfirst contains helper functions to check for pickfirst load
  19. // balancing of RPCs in tests.
  20. package pickfirst
  21. import (
  22. "context"
  23. "fmt"
  24. "time"
  25. "google.golang.org/grpc"
  26. "google.golang.org/grpc/peer"
  27. "google.golang.org/grpc/resolver"
  28. testgrpc "google.golang.org/grpc/interop/grpc_testing"
  29. testpb "google.golang.org/grpc/interop/grpc_testing"
  30. )
  31. // CheckRPCsToBackend makes a bunch of RPCs on the given ClientConn and verifies
  32. // if the RPCs are routed to a peer matching wantAddr.
  33. //
  34. // Returns a non-nil error if context deadline expires before all RPCs begin to
  35. // be routed to the peer matching wantAddr, or if the backend returns RPC errors.
  36. func CheckRPCsToBackend(ctx context.Context, cc *grpc.ClientConn, wantAddr resolver.Address) error {
  37. client := testgrpc.NewTestServiceClient(cc)
  38. peer := &peer.Peer{}
  39. // Make sure that 20 RPCs in a row reach the expected backend. Some
  40. // tests switch from round_robin back to pick_first and call this
  41. // function. None of our tests spin up more than 10 backends. So,
  42. // waiting for 20 RPCs to reach a single backend would a decent
  43. // indicator of having switched to pick_first.
  44. count := 0
  45. for {
  46. time.Sleep(time.Millisecond)
  47. if ctx.Err() != nil {
  48. return fmt.Errorf("timeout waiting for RPC to be routed to %s", wantAddr.Addr)
  49. }
  50. if _, err := client.EmptyCall(ctx, &testpb.Empty{}, grpc.Peer(peer)); err != nil {
  51. // Some tests remove backends and check if pick_first is happening across
  52. // the remaining backends. In such cases, RPCs can initially fail on the
  53. // connection using the removed backend. Just keep retrying and eventually
  54. // the connection using the removed backend will shutdown and will be
  55. // removed.
  56. continue
  57. }
  58. if peer.Addr.String() != wantAddr.Addr {
  59. count = 0
  60. continue
  61. }
  62. count++
  63. if count > 20 {
  64. break
  65. }
  66. }
  67. // Make sure subsequent RPCs are all routed to the same backend.
  68. for i := 0; i < 10; i++ {
  69. if _, err := client.EmptyCall(ctx, &testpb.Empty{}, grpc.Peer(peer)); err != nil {
  70. return fmt.Errorf("EmptyCall() = %v, want <nil>", err)
  71. }
  72. if gotAddr := peer.Addr.String(); gotAddr != wantAddr.Addr {
  73. return fmt.Errorf("rpc sent to peer %q, want peer %q", gotAddr, wantAddr)
  74. }
  75. }
  76. return nil
  77. }