Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 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 | |
| 19 | package grpc |
| 20 | |
| 21 | import ( |
| 22 | "fmt" |
| 23 | "sync" |
| 24 | |
| 25 | "google.golang.org/grpc/balancer" |
| 26 | "google.golang.org/grpc/connectivity" |
| 27 | "google.golang.org/grpc/grpclog" |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 28 | "google.golang.org/grpc/internal/buffer" |
| 29 | "google.golang.org/grpc/internal/grpcsync" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 30 | "google.golang.org/grpc/resolver" |
| 31 | ) |
| 32 | |
| 33 | // scStateUpdate contains the subConn and the new state it changed to. |
| 34 | type scStateUpdate struct { |
| 35 | sc balancer.SubConn |
| 36 | state connectivity.State |
| 37 | } |
| 38 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 39 | // ccBalancerWrapper is a wrapper on top of cc for balancers. |
| 40 | // It implements balancer.ClientConn interface. |
| 41 | type ccBalancerWrapper struct { |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 42 | cc *ClientConn |
| 43 | balancerMu sync.Mutex // synchronizes calls to the balancer |
| 44 | balancer balancer.Balancer |
| 45 | scBuffer *buffer.Unbounded |
| 46 | done *grpcsync.Event |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 47 | |
| 48 | mu sync.Mutex |
| 49 | subConns map[*acBalancerWrapper]struct{} |
| 50 | } |
| 51 | |
| 52 | func newCCBalancerWrapper(cc *ClientConn, b balancer.Builder, bopts balancer.BuildOptions) *ccBalancerWrapper { |
| 53 | ccb := &ccBalancerWrapper{ |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 54 | cc: cc, |
| 55 | scBuffer: buffer.NewUnbounded(), |
| 56 | done: grpcsync.NewEvent(), |
| 57 | subConns: make(map[*acBalancerWrapper]struct{}), |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 58 | } |
| 59 | go ccb.watcher() |
| 60 | ccb.balancer = b.Build(ccb, bopts) |
| 61 | return ccb |
| 62 | } |
| 63 | |
| 64 | // watcher balancer functions sequentially, so the balancer can be implemented |
| 65 | // lock-free. |
| 66 | func (ccb *ccBalancerWrapper) watcher() { |
| 67 | for { |
| 68 | select { |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 69 | case t := <-ccb.scBuffer.Get(): |
| 70 | ccb.scBuffer.Load() |
| 71 | if ccb.done.HasFired() { |
| 72 | break |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 73 | } |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 74 | ccb.balancerMu.Lock() |
| 75 | su := t.(*scStateUpdate) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 76 | if ub, ok := ccb.balancer.(balancer.V2Balancer); ok { |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 77 | ub.UpdateSubConnState(su.sc, balancer.SubConnState{ConnectivityState: su.state}) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 78 | } else { |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 79 | ccb.balancer.HandleSubConnStateChange(su.sc, su.state) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 80 | } |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 81 | ccb.balancerMu.Unlock() |
| 82 | case <-ccb.done.Done(): |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 83 | } |
| 84 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 85 | if ccb.done.HasFired() { |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 86 | ccb.balancer.Close() |
| 87 | ccb.mu.Lock() |
| 88 | scs := ccb.subConns |
| 89 | ccb.subConns = nil |
| 90 | ccb.mu.Unlock() |
| 91 | for acbw := range scs { |
| 92 | ccb.cc.removeAddrConn(acbw.getAddrConn(), errConnDrain) |
| 93 | } |
divyadesai | 1900913 | 2020-03-04 12:58:08 +0000 | [diff] [blame] | 94 | ccb.UpdateBalancerState(connectivity.Connecting, nil) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 95 | return |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func (ccb *ccBalancerWrapper) close() { |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 101 | ccb.done.Fire() |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | func (ccb *ccBalancerWrapper) handleSubConnStateChange(sc balancer.SubConn, s connectivity.State) { |
| 105 | // When updating addresses for a SubConn, if the address in use is not in |
| 106 | // the new addresses, the old ac will be tearDown() and a new ac will be |
| 107 | // created. tearDown() generates a state change with Shutdown state, we |
| 108 | // don't want the balancer to receive this state change. So before |
| 109 | // tearDown() on the old ac, ac.acbw (acWrapper) will be set to nil, and |
| 110 | // this function will be called with (nil, Shutdown). We don't need to call |
| 111 | // balancer method in this case. |
| 112 | if sc == nil { |
| 113 | return |
| 114 | } |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 115 | ccb.scBuffer.Put(&scStateUpdate{ |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 116 | sc: sc, |
| 117 | state: s, |
| 118 | }) |
| 119 | } |
| 120 | |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 121 | func (ccb *ccBalancerWrapper) updateClientConnState(ccs *balancer.ClientConnState) error { |
| 122 | ccb.balancerMu.Lock() |
| 123 | defer ccb.balancerMu.Unlock() |
| 124 | if ub, ok := ccb.balancer.(balancer.V2Balancer); ok { |
| 125 | return ub.UpdateClientConnState(*ccs) |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 126 | } |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 127 | ccb.balancer.HandleResolvedAddrs(ccs.ResolverState.Addresses, nil) |
| 128 | return nil |
| 129 | } |
| 130 | |
| 131 | func (ccb *ccBalancerWrapper) resolverError(err error) { |
| 132 | if ub, ok := ccb.balancer.(balancer.V2Balancer); ok { |
| 133 | ccb.balancerMu.Lock() |
| 134 | ub.ResolverError(err) |
| 135 | ccb.balancerMu.Unlock() |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 136 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | func (ccb *ccBalancerWrapper) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) { |
| 140 | if len(addrs) <= 0 { |
| 141 | return nil, fmt.Errorf("grpc: cannot create SubConn with empty address list") |
| 142 | } |
| 143 | ccb.mu.Lock() |
| 144 | defer ccb.mu.Unlock() |
| 145 | if ccb.subConns == nil { |
| 146 | return nil, fmt.Errorf("grpc: ClientConn balancer wrapper was closed") |
| 147 | } |
| 148 | ac, err := ccb.cc.newAddrConn(addrs, opts) |
| 149 | if err != nil { |
| 150 | return nil, err |
| 151 | } |
| 152 | acbw := &acBalancerWrapper{ac: ac} |
| 153 | acbw.ac.mu.Lock() |
| 154 | ac.acbw = acbw |
| 155 | acbw.ac.mu.Unlock() |
| 156 | ccb.subConns[acbw] = struct{}{} |
| 157 | return acbw, nil |
| 158 | } |
| 159 | |
| 160 | func (ccb *ccBalancerWrapper) RemoveSubConn(sc balancer.SubConn) { |
| 161 | acbw, ok := sc.(*acBalancerWrapper) |
| 162 | if !ok { |
| 163 | return |
| 164 | } |
| 165 | ccb.mu.Lock() |
| 166 | defer ccb.mu.Unlock() |
| 167 | if ccb.subConns == nil { |
| 168 | return |
| 169 | } |
| 170 | delete(ccb.subConns, acbw) |
| 171 | ccb.cc.removeAddrConn(acbw.getAddrConn(), errConnDrain) |
| 172 | } |
| 173 | |
| 174 | func (ccb *ccBalancerWrapper) UpdateBalancerState(s connectivity.State, p balancer.Picker) { |
| 175 | ccb.mu.Lock() |
| 176 | defer ccb.mu.Unlock() |
| 177 | if ccb.subConns == nil { |
| 178 | return |
| 179 | } |
| 180 | // Update picker before updating state. Even though the ordering here does |
| 181 | // not matter, it can lead to multiple calls of Pick in the common start-up |
| 182 | // case where we wait for ready and then perform an RPC. If the picker is |
| 183 | // updated later, we could call the "connecting" picker when the state is |
| 184 | // updated, and then call the "ready" picker after the picker gets updated. |
| 185 | ccb.cc.blockingpicker.updatePicker(p) |
| 186 | ccb.cc.csMgr.updateState(s) |
| 187 | } |
| 188 | |
| 189 | func (ccb *ccBalancerWrapper) ResolveNow(o resolver.ResolveNowOption) { |
| 190 | ccb.cc.resolveNow(o) |
| 191 | } |
| 192 | |
| 193 | func (ccb *ccBalancerWrapper) Target() string { |
| 194 | return ccb.cc.target |
| 195 | } |
| 196 | |
| 197 | // acBalancerWrapper is a wrapper on top of ac for balancers. |
| 198 | // It implements balancer.SubConn interface. |
| 199 | type acBalancerWrapper struct { |
| 200 | mu sync.Mutex |
| 201 | ac *addrConn |
| 202 | } |
| 203 | |
| 204 | func (acbw *acBalancerWrapper) UpdateAddresses(addrs []resolver.Address) { |
| 205 | acbw.mu.Lock() |
| 206 | defer acbw.mu.Unlock() |
| 207 | if len(addrs) <= 0 { |
| 208 | acbw.ac.tearDown(errConnDrain) |
| 209 | return |
| 210 | } |
| 211 | if !acbw.ac.tryUpdateAddrs(addrs) { |
| 212 | cc := acbw.ac.cc |
| 213 | opts := acbw.ac.scopts |
| 214 | acbw.ac.mu.Lock() |
| 215 | // Set old ac.acbw to nil so the Shutdown state update will be ignored |
| 216 | // by balancer. |
| 217 | // |
| 218 | // TODO(bar) the state transition could be wrong when tearDown() old ac |
| 219 | // and creating new ac, fix the transition. |
| 220 | acbw.ac.acbw = nil |
| 221 | acbw.ac.mu.Unlock() |
| 222 | acState := acbw.ac.getState() |
| 223 | acbw.ac.tearDown(errConnDrain) |
| 224 | |
| 225 | if acState == connectivity.Shutdown { |
| 226 | return |
| 227 | } |
| 228 | |
| 229 | ac, err := cc.newAddrConn(addrs, opts) |
| 230 | if err != nil { |
| 231 | grpclog.Warningf("acBalancerWrapper: UpdateAddresses: failed to newAddrConn: %v", err) |
| 232 | return |
| 233 | } |
| 234 | acbw.ac = ac |
| 235 | ac.mu.Lock() |
| 236 | ac.acbw = acbw |
| 237 | ac.mu.Unlock() |
| 238 | if acState != connectivity.Idle { |
| 239 | ac.connect() |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | func (acbw *acBalancerWrapper) Connect() { |
| 245 | acbw.mu.Lock() |
| 246 | defer acbw.mu.Unlock() |
| 247 | acbw.ac.connect() |
| 248 | } |
| 249 | |
| 250 | func (acbw *acBalancerWrapper) getAddrConn() *addrConn { |
| 251 | acbw.mu.Lock() |
| 252 | defer acbw.mu.Unlock() |
| 253 | return acbw.ac |
| 254 | } |