grpclb_picker.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. *
  3. * Copyright 2017 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 grpclb
  19. import (
  20. "sync"
  21. "sync/atomic"
  22. "google.golang.org/grpc/balancer"
  23. lbpb "google.golang.org/grpc/balancer/grpclb/grpc_lb_v1"
  24. "google.golang.org/grpc/codes"
  25. "google.golang.org/grpc/internal/grpcrand"
  26. "google.golang.org/grpc/status"
  27. )
  28. // rpcStats is same as lbpb.ClientStats, except that numCallsDropped is a map
  29. // instead of a slice.
  30. type rpcStats struct {
  31. // Only access the following fields atomically.
  32. numCallsStarted int64
  33. numCallsFinished int64
  34. numCallsFinishedWithClientFailedToSend int64
  35. numCallsFinishedKnownReceived int64
  36. mu sync.Mutex
  37. // map load_balance_token -> num_calls_dropped
  38. numCallsDropped map[string]int64
  39. }
  40. func newRPCStats() *rpcStats {
  41. return &rpcStats{
  42. numCallsDropped: make(map[string]int64),
  43. }
  44. }
  45. func isZeroStats(stats *lbpb.ClientStats) bool {
  46. return len(stats.CallsFinishedWithDrop) == 0 &&
  47. stats.NumCallsStarted == 0 &&
  48. stats.NumCallsFinished == 0 &&
  49. stats.NumCallsFinishedWithClientFailedToSend == 0 &&
  50. stats.NumCallsFinishedKnownReceived == 0
  51. }
  52. // toClientStats converts rpcStats to lbpb.ClientStats, and clears rpcStats.
  53. func (s *rpcStats) toClientStats() *lbpb.ClientStats {
  54. stats := &lbpb.ClientStats{
  55. NumCallsStarted: atomic.SwapInt64(&s.numCallsStarted, 0),
  56. NumCallsFinished: atomic.SwapInt64(&s.numCallsFinished, 0),
  57. NumCallsFinishedWithClientFailedToSend: atomic.SwapInt64(&s.numCallsFinishedWithClientFailedToSend, 0),
  58. NumCallsFinishedKnownReceived: atomic.SwapInt64(&s.numCallsFinishedKnownReceived, 0),
  59. }
  60. s.mu.Lock()
  61. dropped := s.numCallsDropped
  62. s.numCallsDropped = make(map[string]int64)
  63. s.mu.Unlock()
  64. for token, count := range dropped {
  65. stats.CallsFinishedWithDrop = append(stats.CallsFinishedWithDrop, &lbpb.ClientStatsPerToken{
  66. LoadBalanceToken: token,
  67. NumCalls: count,
  68. })
  69. }
  70. return stats
  71. }
  72. func (s *rpcStats) drop(token string) {
  73. atomic.AddInt64(&s.numCallsStarted, 1)
  74. s.mu.Lock()
  75. s.numCallsDropped[token]++
  76. s.mu.Unlock()
  77. atomic.AddInt64(&s.numCallsFinished, 1)
  78. }
  79. func (s *rpcStats) failedToSend() {
  80. atomic.AddInt64(&s.numCallsStarted, 1)
  81. atomic.AddInt64(&s.numCallsFinishedWithClientFailedToSend, 1)
  82. atomic.AddInt64(&s.numCallsFinished, 1)
  83. }
  84. func (s *rpcStats) knownReceived() {
  85. atomic.AddInt64(&s.numCallsStarted, 1)
  86. atomic.AddInt64(&s.numCallsFinishedKnownReceived, 1)
  87. atomic.AddInt64(&s.numCallsFinished, 1)
  88. }
  89. type errPicker struct {
  90. // Pick always returns this err.
  91. err error
  92. }
  93. func (p *errPicker) Pick(balancer.PickInfo) (balancer.PickResult, error) {
  94. return balancer.PickResult{}, p.err
  95. }
  96. // rrPicker does roundrobin on subConns. It's typically used when there's no
  97. // response from remote balancer, and grpclb falls back to the resolved
  98. // backends.
  99. //
  100. // It guaranteed that len(subConns) > 0.
  101. type rrPicker struct {
  102. mu sync.Mutex
  103. subConns []balancer.SubConn // The subConns that were READY when taking the snapshot.
  104. subConnsNext int
  105. }
  106. func newRRPicker(readySCs []balancer.SubConn) *rrPicker {
  107. return &rrPicker{
  108. subConns: readySCs,
  109. subConnsNext: grpcrand.Intn(len(readySCs)),
  110. }
  111. }
  112. func (p *rrPicker) Pick(balancer.PickInfo) (balancer.PickResult, error) {
  113. p.mu.Lock()
  114. defer p.mu.Unlock()
  115. sc := p.subConns[p.subConnsNext]
  116. p.subConnsNext = (p.subConnsNext + 1) % len(p.subConns)
  117. return balancer.PickResult{SubConn: sc}, nil
  118. }
  119. // lbPicker does two layers of picks:
  120. //
  121. // First layer: roundrobin on all servers in serverList, including drops and backends.
  122. // - If it picks a drop, the RPC will fail as being dropped.
  123. // - If it picks a backend, do a second layer pick to pick the real backend.
  124. //
  125. // Second layer: roundrobin on all READY backends.
  126. //
  127. // It's guaranteed that len(serverList) > 0.
  128. type lbPicker struct {
  129. mu sync.Mutex
  130. serverList []*lbpb.Server
  131. serverListNext int
  132. subConns []balancer.SubConn // The subConns that were READY when taking the snapshot.
  133. subConnsNext int
  134. stats *rpcStats
  135. }
  136. func newLBPicker(serverList []*lbpb.Server, readySCs []balancer.SubConn, stats *rpcStats) *lbPicker {
  137. return &lbPicker{
  138. serverList: serverList,
  139. subConns: readySCs,
  140. subConnsNext: grpcrand.Intn(len(readySCs)),
  141. stats: stats,
  142. }
  143. }
  144. func (p *lbPicker) Pick(balancer.PickInfo) (balancer.PickResult, error) {
  145. p.mu.Lock()
  146. defer p.mu.Unlock()
  147. // Layer one roundrobin on serverList.
  148. s := p.serverList[p.serverListNext]
  149. p.serverListNext = (p.serverListNext + 1) % len(p.serverList)
  150. // If it's a drop, return an error and fail the RPC.
  151. if s.Drop {
  152. p.stats.drop(s.LoadBalanceToken)
  153. return balancer.PickResult{}, status.Errorf(codes.Unavailable, "request dropped by grpclb")
  154. }
  155. // If not a drop but there's no ready subConns.
  156. if len(p.subConns) <= 0 {
  157. return balancer.PickResult{}, balancer.ErrNoSubConnAvailable
  158. }
  159. // Return the next ready subConn in the list, also collect rpc stats.
  160. sc := p.subConns[p.subConnsNext]
  161. p.subConnsNext = (p.subConnsNext + 1) % len(p.subConns)
  162. done := func(info balancer.DoneInfo) {
  163. if !info.BytesSent {
  164. p.stats.failedToSend()
  165. } else if info.BytesReceived {
  166. p.stats.knownReceived()
  167. }
  168. }
  169. return balancer.PickResult{SubConn: sc, Done: done}, nil
  170. }
  171. func (p *lbPicker) updateReadySCs(readySCs []balancer.SubConn) {
  172. p.mu.Lock()
  173. defer p.mu.Unlock()
  174. p.subConns = readySCs
  175. p.subConnsNext = p.subConnsNext % len(readySCs)
  176. }