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

balancer.go 5.7 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. panic("not implemented")
  60. }
  61. func (b *baseBalancer) UpdateResolverState(s resolver.State) {
  62. // TODO: handle s.Err (log if not nil) once implemented.
  63. // TODO: handle s.ServiceConfig?
  64. grpclog.Infoln("base.baseBalancer: got new resolver state: ", s)
  65. // addrsSet is the set converted from addrs, it's used for quick lookup of an address.
  66. addrsSet := make(map[resolver.Address]struct{})
  67. for _, a := range s.Addresses {
  68. addrsSet[a] = struct{}{}
  69. if _, ok := b.subConns[a]; !ok {
  70. // a is a new address (not existing in b.subConns).
  71. sc, err := b.cc.NewSubConn([]resolver.Address{a}, balancer.NewSubConnOptions{HealthCheckEnabled: b.config.HealthCheck})
  72. if err != nil {
  73. grpclog.Warningf("base.baseBalancer: failed to create new SubConn: %v", err)
  74. continue
  75. }
  76. b.subConns[a] = sc
  77. b.scStates[sc] = connectivity.Idle
  78. sc.Connect()
  79. }
  80. }
  81. for a, sc := range b.subConns {
  82. // a was removed by resolver.
  83. if _, ok := addrsSet[a]; !ok {
  84. b.cc.RemoveSubConn(sc)
  85. delete(b.subConns, a)
  86. // Keep the state of this sc in b.scStates until sc's state becomes Shutdown.
  87. // The entry will be deleted in HandleSubConnStateChange.
  88. }
  89. }
  90. }
  91. // regeneratePicker takes a snapshot of the balancer, and generates a picker
  92. // from it. The picker is
  93. // - errPicker with ErrTransientFailure if the balancer is in TransientFailure,
  94. // - built by the pickerBuilder with all READY SubConns otherwise.
  95. func (b *baseBalancer) regeneratePicker() {
  96. if b.state == connectivity.TransientFailure {
  97. b.picker = NewErrPicker(balancer.ErrTransientFailure)
  98. return
  99. }
  100. readySCs := make(map[resolver.Address]balancer.SubConn)
  101. // Filter out all ready SCs from full subConn map.
  102. for addr, sc := range b.subConns {
  103. if st, ok := b.scStates[sc]; ok && st == connectivity.Ready {
  104. readySCs[addr] = sc
  105. }
  106. }
  107. b.picker = b.pickerBuilder.Build(readySCs)
  108. }
  109. func (b *baseBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
  110. panic("not implemented")
  111. }
  112. func (b *baseBalancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) {
  113. s := state.ConnectivityState
  114. grpclog.Infof("base.baseBalancer: handle SubConn state change: %p, %v", sc, s)
  115. oldS, ok := b.scStates[sc]
  116. if !ok {
  117. grpclog.Infof("base.baseBalancer: got state changes for an unknown SubConn: %p, %v", sc, s)
  118. return
  119. }
  120. b.scStates[sc] = s
  121. switch s {
  122. case connectivity.Idle:
  123. sc.Connect()
  124. case connectivity.Shutdown:
  125. // When an address was removed by resolver, b called RemoveSubConn but
  126. // kept the sc's state in scStates. Remove state for this sc here.
  127. delete(b.scStates, sc)
  128. }
  129. oldAggrState := b.state
  130. b.state = b.csEvltr.RecordTransition(oldS, s)
  131. // Regenerate picker when one of the following happens:
  132. // - this sc became ready from not-ready
  133. // - this sc became not-ready from ready
  134. // - the aggregated state of balancer became TransientFailure from non-TransientFailure
  135. // - the aggregated state of balancer became non-TransientFailure from TransientFailure
  136. if (s == connectivity.Ready) != (oldS == connectivity.Ready) ||
  137. (b.state == connectivity.TransientFailure) != (oldAggrState == connectivity.TransientFailure) {
  138. b.regeneratePicker()
  139. }
  140. b.cc.UpdateBalancerState(b.state, b.picker)
  141. }
  142. // Close is a nop because base balancer doesn't have internal state to clean up,
  143. // and it doesn't need to call RemoveSubConn for the SubConns.
  144. func (b *baseBalancer) Close() {
  145. }
  146. // NewErrPicker returns a picker that always returns err on Pick().
  147. func NewErrPicker(err error) balancer.Picker {
  148. return &errPicker{err: err}
  149. }
  150. type errPicker struct {
  151. err error // Pick() always returns this err.
  152. }
  153. func (p *errPicker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
  154. return nil, nil, p.err
  155. }