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