You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

172 lines
5.4 KiB

  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 base
  19. import (
  20. "context"
  21. "google.golang.org/grpc/balancer"
  22. "google.golang.org/grpc/connectivity"
  23. "google.golang.org/grpc/grpclog"
  24. "google.golang.org/grpc/resolver"
  25. )
  26. type baseBuilder struct {
  27. name string
  28. pickerBuilder PickerBuilder
  29. config Config
  30. }
  31. func (bb *baseBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
  32. return &baseBalancer{
  33. cc: cc,
  34. pickerBuilder: bb.pickerBuilder,
  35. subConns: make(map[resolver.Address]balancer.SubConn),
  36. scStates: make(map[balancer.SubConn]connectivity.State),
  37. csEvltr: &balancer.ConnectivityStateEvaluator{},
  38. // Initialize picker to a picker that always return
  39. // ErrNoSubConnAvailable, because when state of a SubConn changes, we
  40. // may call UpdateBalancerState with this picker.
  41. picker: NewErrPicker(balancer.ErrNoSubConnAvailable),
  42. config: bb.config,
  43. }
  44. }
  45. func (bb *baseBuilder) Name() string {
  46. return bb.name
  47. }
  48. type baseBalancer struct {
  49. cc balancer.ClientConn
  50. pickerBuilder PickerBuilder
  51. csEvltr *balancer.ConnectivityStateEvaluator
  52. state connectivity.State
  53. subConns map[resolver.Address]balancer.SubConn
  54. scStates map[balancer.SubConn]connectivity.State
  55. picker balancer.Picker
  56. config Config
  57. }
  58. func (b *baseBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) {
  59. if err != nil {
  60. grpclog.Infof("base.baseBalancer: HandleResolvedAddrs called with error %v", err)
  61. return
  62. }
  63. grpclog.Infoln("base.baseBalancer: got new resolved addresses: ", addrs)
  64. // addrsSet is the set converted from addrs, it's used for quick lookup of an address.
  65. addrsSet := make(map[resolver.Address]struct{})
  66. for _, a := range addrs {
  67. addrsSet[a] = struct{}{}
  68. if _, ok := b.subConns[a]; !ok {
  69. // a is a new address (not existing in b.subConns).
  70. sc, err := b.cc.NewSubConn([]resolver.Address{a}, balancer.NewSubConnOptions{HealthCheckEnabled: b.config.HealthCheck})
  71. if err != nil {
  72. grpclog.Warningf("base.baseBalancer: failed to create new SubConn: %v", err)
  73. continue
  74. }
  75. b.subConns[a] = sc
  76. b.scStates[sc] = connectivity.Idle
  77. sc.Connect()
  78. }
  79. }
  80. for a, sc := range b.subConns {
  81. // a was removed by resolver.
  82. if _, ok := addrsSet[a]; !ok {
  83. b.cc.RemoveSubConn(sc)
  84. delete(b.subConns, a)
  85. // Keep the state of this sc in b.scStates until sc's state becomes Shutdown.
  86. // The entry will be deleted in HandleSubConnStateChange.
  87. }
  88. }
  89. }
  90. // regeneratePicker takes a snapshot of the balancer, and generates a picker
  91. // from it. The picker is
  92. // - errPicker with ErrTransientFailure if the balancer is in TransientFailure,
  93. // - built by the pickerBuilder with all READY SubConns otherwise.
  94. func (b *baseBalancer) regeneratePicker() {
  95. if b.state == connectivity.TransientFailure {
  96. b.picker = NewErrPicker(balancer.ErrTransientFailure)
  97. return
  98. }
  99. readySCs := make(map[resolver.Address]balancer.SubConn)
  100. // Filter out all ready SCs from full subConn map.
  101. for addr, sc := range b.subConns {
  102. if st, ok := b.scStates[sc]; ok && st == connectivity.Ready {
  103. readySCs[addr] = sc
  104. }
  105. }
  106. b.picker = b.pickerBuilder.Build(readySCs)
  107. }
  108. func (b *baseBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
  109. grpclog.Infof("base.baseBalancer: handle SubConn state change: %p, %v", sc, s)
  110. oldS, ok := b.scStates[sc]
  111. if !ok {
  112. grpclog.Infof("base.baseBalancer: got state changes for an unknown SubConn: %p, %v", sc, s)
  113. return
  114. }
  115. b.scStates[sc] = s
  116. switch s {
  117. case connectivity.Idle:
  118. sc.Connect()
  119. case connectivity.Shutdown:
  120. // When an address was removed by resolver, b called RemoveSubConn but
  121. // kept the sc's state in scStates. Remove state for this sc here.
  122. delete(b.scStates, sc)
  123. }
  124. oldAggrState := b.state
  125. b.state = b.csEvltr.RecordTransition(oldS, s)
  126. // Regenerate picker when one of the following happens:
  127. // - this sc became ready from not-ready
  128. // - this sc became not-ready from ready
  129. // - the aggregated state of balancer became TransientFailure from non-TransientFailure
  130. // - the aggregated state of balancer became non-TransientFailure from TransientFailure
  131. if (s == connectivity.Ready) != (oldS == connectivity.Ready) ||
  132. (b.state == connectivity.TransientFailure) != (oldAggrState == connectivity.TransientFailure) {
  133. b.regeneratePicker()
  134. }
  135. b.cc.UpdateBalancerState(b.state, b.picker)
  136. }
  137. // Close is a nop because base balancer doesn't have internal state to clean up,
  138. // and it doesn't need to call RemoveSubConn for the SubConns.
  139. func (b *baseBalancer) Close() {
  140. }
  141. // NewErrPicker returns a picker that always returns err on Pick().
  142. func NewErrPicker(err error) balancer.Picker {
  143. return &errPicker{err: err}
  144. }
  145. type errPicker struct {
  146. err error // Pick() always returns this err.
  147. }
  148. func (p *errPicker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
  149. return nil, nil, p.err
  150. }