pickfirst.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 grpc
  19. import (
  20. "encoding/json"
  21. "errors"
  22. "fmt"
  23. "google.golang.org/grpc/balancer"
  24. "google.golang.org/grpc/connectivity"
  25. "google.golang.org/grpc/internal/envconfig"
  26. "google.golang.org/grpc/internal/grpcrand"
  27. "google.golang.org/grpc/serviceconfig"
  28. )
  29. // PickFirstBalancerName is the name of the pick_first balancer.
  30. const PickFirstBalancerName = "pick_first"
  31. func newPickfirstBuilder() balancer.Builder {
  32. return &pickfirstBuilder{}
  33. }
  34. type pickfirstBuilder struct{}
  35. func (*pickfirstBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
  36. return &pickfirstBalancer{cc: cc}
  37. }
  38. func (*pickfirstBuilder) Name() string {
  39. return PickFirstBalancerName
  40. }
  41. type pfConfig struct {
  42. serviceconfig.LoadBalancingConfig `json:"-"`
  43. // If set to true, instructs the LB policy to shuffle the order of the list
  44. // of addresses received from the name resolver before attempting to
  45. // connect to them.
  46. ShuffleAddressList bool `json:"shuffleAddressList"`
  47. }
  48. func (*pickfirstBuilder) ParseConfig(js json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
  49. cfg := &pfConfig{}
  50. if err := json.Unmarshal(js, cfg); err != nil {
  51. return nil, fmt.Errorf("pickfirst: unable to unmarshal LB policy config: %s, error: %v", string(js), err)
  52. }
  53. return cfg, nil
  54. }
  55. type pickfirstBalancer struct {
  56. state connectivity.State
  57. cc balancer.ClientConn
  58. subConn balancer.SubConn
  59. cfg *pfConfig
  60. }
  61. func (b *pickfirstBalancer) ResolverError(err error) {
  62. if logger.V(2) {
  63. logger.Infof("pickfirstBalancer: ResolverError called with error: %v", err)
  64. }
  65. if b.subConn == nil {
  66. b.state = connectivity.TransientFailure
  67. }
  68. if b.state != connectivity.TransientFailure {
  69. // The picker will not change since the balancer does not currently
  70. // report an error.
  71. return
  72. }
  73. b.cc.UpdateState(balancer.State{
  74. ConnectivityState: connectivity.TransientFailure,
  75. Picker: &picker{err: fmt.Errorf("name resolver error: %v", err)},
  76. })
  77. }
  78. func (b *pickfirstBalancer) UpdateClientConnState(state balancer.ClientConnState) error {
  79. addrs := state.ResolverState.Addresses
  80. if len(addrs) == 0 {
  81. // The resolver reported an empty address list. Treat it like an error by
  82. // calling b.ResolverError.
  83. if b.subConn != nil {
  84. // Remove the old subConn. All addresses were removed, so it is no longer
  85. // valid.
  86. b.cc.RemoveSubConn(b.subConn)
  87. b.subConn = nil
  88. }
  89. b.ResolverError(errors.New("produced zero addresses"))
  90. return balancer.ErrBadResolverState
  91. }
  92. if state.BalancerConfig != nil {
  93. cfg, ok := state.BalancerConfig.(*pfConfig)
  94. if !ok {
  95. return fmt.Errorf("pickfirstBalancer: received nil or illegal BalancerConfig (type %T): %v", state.BalancerConfig, state.BalancerConfig)
  96. }
  97. b.cfg = cfg
  98. }
  99. if envconfig.PickFirstLBConfig && b.cfg != nil && b.cfg.ShuffleAddressList {
  100. grpcrand.Shuffle(len(addrs), func(i, j int) { addrs[i], addrs[j] = addrs[j], addrs[i] })
  101. }
  102. if b.subConn != nil {
  103. b.cc.UpdateAddresses(b.subConn, addrs)
  104. return nil
  105. }
  106. subConn, err := b.cc.NewSubConn(addrs, balancer.NewSubConnOptions{})
  107. if err != nil {
  108. if logger.V(2) {
  109. logger.Errorf("pickfirstBalancer: failed to NewSubConn: %v", err)
  110. }
  111. b.state = connectivity.TransientFailure
  112. b.cc.UpdateState(balancer.State{
  113. ConnectivityState: connectivity.TransientFailure,
  114. Picker: &picker{err: fmt.Errorf("error creating connection: %v", err)},
  115. })
  116. return balancer.ErrBadResolverState
  117. }
  118. b.subConn = subConn
  119. b.state = connectivity.Idle
  120. b.cc.UpdateState(balancer.State{
  121. ConnectivityState: connectivity.Connecting,
  122. Picker: &picker{err: balancer.ErrNoSubConnAvailable},
  123. })
  124. b.subConn.Connect()
  125. return nil
  126. }
  127. func (b *pickfirstBalancer) UpdateSubConnState(subConn balancer.SubConn, state balancer.SubConnState) {
  128. if logger.V(2) {
  129. logger.Infof("pickfirstBalancer: UpdateSubConnState: %p, %v", subConn, state)
  130. }
  131. if b.subConn != subConn {
  132. if logger.V(2) {
  133. logger.Infof("pickfirstBalancer: ignored state change because subConn is not recognized")
  134. }
  135. return
  136. }
  137. if state.ConnectivityState == connectivity.Shutdown {
  138. b.subConn = nil
  139. return
  140. }
  141. switch state.ConnectivityState {
  142. case connectivity.Ready:
  143. b.cc.UpdateState(balancer.State{
  144. ConnectivityState: state.ConnectivityState,
  145. Picker: &picker{result: balancer.PickResult{SubConn: subConn}},
  146. })
  147. case connectivity.Connecting:
  148. if b.state == connectivity.TransientFailure {
  149. // We stay in TransientFailure until we are Ready. See A62.
  150. return
  151. }
  152. b.cc.UpdateState(balancer.State{
  153. ConnectivityState: state.ConnectivityState,
  154. Picker: &picker{err: balancer.ErrNoSubConnAvailable},
  155. })
  156. case connectivity.Idle:
  157. if b.state == connectivity.TransientFailure {
  158. // We stay in TransientFailure until we are Ready. Also kick the
  159. // subConn out of Idle into Connecting. See A62.
  160. b.subConn.Connect()
  161. return
  162. }
  163. b.cc.UpdateState(balancer.State{
  164. ConnectivityState: state.ConnectivityState,
  165. Picker: &idlePicker{subConn: subConn},
  166. })
  167. case connectivity.TransientFailure:
  168. b.cc.UpdateState(balancer.State{
  169. ConnectivityState: state.ConnectivityState,
  170. Picker: &picker{err: state.ConnectionError},
  171. })
  172. }
  173. b.state = state.ConnectivityState
  174. }
  175. func (b *pickfirstBalancer) Close() {
  176. }
  177. func (b *pickfirstBalancer) ExitIdle() {
  178. if b.subConn != nil && b.state == connectivity.Idle {
  179. b.subConn.Connect()
  180. }
  181. }
  182. type picker struct {
  183. result balancer.PickResult
  184. err error
  185. }
  186. func (p *picker) Pick(balancer.PickInfo) (balancer.PickResult, error) {
  187. return p.result, p.err
  188. }
  189. // idlePicker is used when the SubConn is IDLE and kicks the SubConn into
  190. // CONNECTING when Pick is called.
  191. type idlePicker struct {
  192. subConn balancer.SubConn
  193. }
  194. func (i *idlePicker) Pick(balancer.PickInfo) (balancer.PickResult, error) {
  195. i.subConn.Connect()
  196. return balancer.PickResult{}, balancer.ErrNoSubConnAvailable
  197. }
  198. func init() {
  199. balancer.Register(newPickfirstBuilder())
  200. }