pickfirst.go 7.2 KB

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