Debian Codegraph @

vendor/google.golang.org/grpc/balancer_conn_wrappers.go

raw
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 "sync"
23
24 "google.golang.org/grpc/balancer"
25 "google.golang.org/grpc/connectivity"
26 "google.golang.org/grpc/grpclog"
27 "google.golang.org/grpc/resolver"
28 )
29
30 // scStateUpdate contains the subConn and the new state it changed to.
31 type scStateUpdate struct {
32 sc balancer.SubConn
33 state connectivity.State
34 }
35
36 // scStateUpdateBuffer is an unbounded channel for scStateChangeTuple.
37 // TODO make a general purpose buffer that uses interface{}.
38 type scStateUpdateBuffer struct {
39 c chan *scStateUpdate
40 mu sync.Mutex
41 backlog []*scStateUpdate
42 }
43
44 func newSCStateUpdateBuffer() *scStateUpdateBuffer {
45 return &scStateUpdateBuffer{
46 c: make(chan *scStateUpdate, 1),
47 }
48 }
49
50 func (b *scStateUpdateBuffer) put(t *scStateUpdate) {
51 b.mu.Lock()
52 defer b.mu.Unlock()
53 if len(b.backlog) == 0 {
54 select {
55 case b.c <- t:
56 return
57 default:
58 }
59 }
60 b.backlog = append(b.backlog, t)
61 }
62
63 func (b *scStateUpdateBuffer) load() {
64 b.mu.Lock()
65 defer b.mu.Unlock()
66 if len(b.backlog) > 0 {
67 select {
68 case b.c <- b.backlog[0]:
69 b.backlog[0] = nil
70 b.backlog = b.backlog[1:]
71 default:
72 }
73 }
74 }
75
76 // get returns the channel that receives a recvMsg in the buffer.
77 //
78 // Upon receiving, the caller should call load to send another
79 // scStateChangeTuple onto the channel if there is any.
80 func (b *scStateUpdateBuffer) get() <-chan *scStateUpdate {
81 return b.c
82 }
83
84 // resolverUpdate contains the new resolved addresses or error if there's
85 // any.
86 type resolverUpdate struct {
87 addrs []resolver.Address
88 err error
89 }
90
91 // ccBalancerWrapper is a wrapper on top of cc for balancers.
92 // It implements balancer.ClientConn interface.
93 type ccBalancerWrapper struct {
94 cc *ClientConn
95 balancer balancer.Balancer
96 stateChangeQueue *scStateUpdateBuffer
97 resolverUpdateCh chan *resolverUpdate
98 done chan struct{}
99 }
100
101 func newCCBalancerWrapper(cc *ClientConn, b balancer.Builder, bopts balancer.BuildOptions) *ccBalancerWrapper {
102 ccb := &ccBalancerWrapper{
103 cc: cc,
104 stateChangeQueue: newSCStateUpdateBuffer(),
105 resolverUpdateCh: make(chan *resolverUpdate, 1),
106 done: make(chan struct{}),
107 }
108 go ccb.watcher()
109 ccb.balancer = b.Build(ccb, bopts)
110 return ccb
111 }
112
113 // watcher balancer functions sequencially, so the balancer can be implemeneted
114 // lock-free.
115 func (ccb *ccBalancerWrapper) watcher() {
116 for {
117 select {
118 case t := <-ccb.stateChangeQueue.get():
119 ccb.stateChangeQueue.load()
120 ccb.balancer.HandleSubConnStateChange(t.sc, t.state)
121 case t := <-ccb.resolverUpdateCh:
122 ccb.balancer.HandleResolvedAddrs(t.addrs, t.err)
123 case <-ccb.done:
124 }
125
126 select {
127 case <-ccb.done:
128 ccb.balancer.Close()
129 return
130 default:
131 }
132 }
133 }
134
135 func (ccb *ccBalancerWrapper) close() {
136 close(ccb.done)
137 }
138
139 func (ccb *ccBalancerWrapper) handleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
140 // When updating addresses for a SubConn, if the address in use is not in
141 // the new addresses, the old ac will be tearDown() and a new ac will be
142 // created. tearDown() generates a state change with Shutdown state, we
143 // don't want the balancer to receive this state change. So before
144 // tearDown() on the old ac, ac.acbw (acWrapper) will be set to nil, and
145 // this function will be called with (nil, Shutdown). We don't need to call
146 // balancer method in this case.
147 if sc == nil {
148 return
149 }
150 ccb.stateChangeQueue.put(&scStateUpdate{
151 sc: sc,
152 state: s,
153 })
154 }
155
156 func (ccb *ccBalancerWrapper) handleResolvedAddrs(addrs []resolver.Address, err error) {
157 select {
158 case <-ccb.resolverUpdateCh:
159 default:
160 }
161 ccb.resolverUpdateCh <- &resolverUpdate{
162 addrs: addrs,
163 err: err,
164 }
165 }
166
167 func (ccb *ccBalancerWrapper) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
168 grpclog.Infof("ccBalancerWrapper: new subconn: %v", addrs)
169 ac, err := ccb.cc.newAddrConn(addrs)
170 if err != nil {
171 return nil, err
172 }
173 acbw := &acBalancerWrapper{ac: ac}
174 ac.mu.Lock()
175 ac.acbw = acbw
176 ac.mu.Unlock()
177 return acbw, nil
178 }
179
180 func (ccb *ccBalancerWrapper) RemoveSubConn(sc balancer.SubConn) {
181 grpclog.Infof("ccBalancerWrapper: removing subconn")
182 acbw, ok := sc.(*acBalancerWrapper)
183 if !ok {
184 return
185 }
186 ccb.cc.removeAddrConn(acbw.getAddrConn(), errConnDrain)
187 }
188
189 func (ccb *ccBalancerWrapper) UpdateBalancerState(s connectivity.State, p balancer.Picker) {
190 grpclog.Infof("ccBalancerWrapper: updating state and picker called by balancer: %v, %p", s, p)
191 ccb.cc.csMgr.updateState(s)
192 ccb.cc.blockingpicker.updatePicker(p)
193 }
194
195 func (ccb *ccBalancerWrapper) Target() string {
196 return ccb.cc.target
197 }
198
199 // acBalancerWrapper is a wrapper on top of ac for balancers.
200 // It implements balancer.SubConn interface.
201 type acBalancerWrapper struct {
202 mu sync.Mutex
203 ac *addrConn
204 }
205
206 func (acbw *acBalancerWrapper) UpdateAddresses(addrs []resolver.Address) {
207 grpclog.Infof("acBalancerWrapper: UpdateAddresses called with %v", addrs)
208 acbw.mu.Lock()
209 defer acbw.mu.Unlock()
210 if !acbw.ac.tryUpdateAddrs(addrs) {
211 cc := acbw.ac.cc
212 acbw.ac.mu.Lock()
213 // Set old ac.acbw to nil so the Shutdown state update will be ignored
214 // by balancer.
215 //
216 // TODO(bar) the state transition could be wrong when tearDown() old ac
217 // and creating new ac, fix the transition.
218 acbw.ac.acbw = nil
219 acbw.ac.mu.Unlock()
220 acState := acbw.ac.getState()
221 acbw.ac.tearDown(errConnDrain)
222
223 if acState == connectivity.Shutdown {
224 return
225 }
226
227 ac, err := cc.newAddrConn(addrs)
228 if err != nil {
229 grpclog.Warningf("acBalancerWrapper: UpdateAddresses: failed to newAddrConn: %v", err)
230 return
231 }
232 acbw.ac = ac
233 ac.mu.Lock()
234 ac.acbw = acbw
235 ac.mu.Unlock()
236 if acState != connectivity.Idle {
237 ac.connect(false)
238 }
239 }
240 }
241
242 func (acbw *acBalancerWrapper) Connect() {
243 acbw.mu.Lock()
244 defer acbw.mu.Unlock()
245 acbw.ac.connect(false)
246 }
247
248 func (acbw *acBalancerWrapper) getAddrConn() *addrConn {
249 acbw.mu.Lock()
250 defer acbw.mu.Unlock()
251 return acbw.ac
252 }