blob: 1af88f0a3f1b3d88d677458365328ae823d66b08 [file] [log] [blame]
Scott Bakereee8dd82019-09-24 12:52:34 -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) {
Scott Baker611f6bd2019-10-18 13:45:19 -070070 panic("not implemented")
71}
72
73func (b *baseBalancer) UpdateClientConnState(s balancer.ClientConnState) {
74 // TODO: handle s.ResolverState.Err (log if not nil) once implemented.
75 // TODO: handle s.ResolverState.ServiceConfig?
76 if grpclog.V(2) {
77 grpclog.Infoln("base.baseBalancer: got new ClientConn state: ", s)
Scott Bakereee8dd82019-09-24 12:52:34 -070078 }
Scott Bakereee8dd82019-09-24 12:52:34 -070079 // addrsSet is the set converted from addrs, it's used for quick lookup of an address.
80 addrsSet := make(map[resolver.Address]struct{})
Scott Baker611f6bd2019-10-18 13:45:19 -070081 for _, a := range s.ResolverState.Addresses {
Scott Bakereee8dd82019-09-24 12:52:34 -070082 addrsSet[a] = struct{}{}
83 if _, ok := b.subConns[a]; !ok {
84 // a is a new address (not existing in b.subConns).
85 sc, err := b.cc.NewSubConn([]resolver.Address{a}, balancer.NewSubConnOptions{HealthCheckEnabled: b.config.HealthCheck})
86 if err != nil {
87 grpclog.Warningf("base.baseBalancer: failed to create new SubConn: %v", err)
88 continue
89 }
90 b.subConns[a] = sc
91 b.scStates[sc] = connectivity.Idle
92 sc.Connect()
93 }
94 }
95 for a, sc := range b.subConns {
96 // a was removed by resolver.
97 if _, ok := addrsSet[a]; !ok {
98 b.cc.RemoveSubConn(sc)
99 delete(b.subConns, a)
100 // Keep the state of this sc in b.scStates until sc's state becomes Shutdown.
101 // The entry will be deleted in HandleSubConnStateChange.
102 }
103 }
104}
105
106// regeneratePicker takes a snapshot of the balancer, and generates a picker
107// from it. The picker is
108// - errPicker with ErrTransientFailure if the balancer is in TransientFailure,
109// - built by the pickerBuilder with all READY SubConns otherwise.
110func (b *baseBalancer) regeneratePicker() {
111 if b.state == connectivity.TransientFailure {
112 b.picker = NewErrPicker(balancer.ErrTransientFailure)
113 return
114 }
115 readySCs := make(map[resolver.Address]balancer.SubConn)
116
117 // Filter out all ready SCs from full subConn map.
118 for addr, sc := range b.subConns {
119 if st, ok := b.scStates[sc]; ok && st == connectivity.Ready {
120 readySCs[addr] = sc
121 }
122 }
123 b.picker = b.pickerBuilder.Build(readySCs)
124}
125
126func (b *baseBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
Scott Baker611f6bd2019-10-18 13:45:19 -0700127 panic("not implemented")
128}
129
130func (b *baseBalancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) {
131 s := state.ConnectivityState
132 if grpclog.V(2) {
133 grpclog.Infof("base.baseBalancer: handle SubConn state change: %p, %v", sc, s)
134 }
Scott Bakereee8dd82019-09-24 12:52:34 -0700135 oldS, ok := b.scStates[sc]
136 if !ok {
Scott Baker611f6bd2019-10-18 13:45:19 -0700137 if grpclog.V(2) {
138 grpclog.Infof("base.baseBalancer: got state changes for an unknown SubConn: %p, %v", sc, s)
139 }
Scott Bakereee8dd82019-09-24 12:52:34 -0700140 return
141 }
142 b.scStates[sc] = s
143 switch s {
144 case connectivity.Idle:
145 sc.Connect()
146 case connectivity.Shutdown:
147 // When an address was removed by resolver, b called RemoveSubConn but
148 // kept the sc's state in scStates. Remove state for this sc here.
149 delete(b.scStates, sc)
150 }
151
152 oldAggrState := b.state
153 b.state = b.csEvltr.RecordTransition(oldS, s)
154
155 // Regenerate picker when one of the following happens:
156 // - this sc became ready from not-ready
157 // - this sc became not-ready from ready
158 // - the aggregated state of balancer became TransientFailure from non-TransientFailure
159 // - the aggregated state of balancer became non-TransientFailure from TransientFailure
160 if (s == connectivity.Ready) != (oldS == connectivity.Ready) ||
161 (b.state == connectivity.TransientFailure) != (oldAggrState == connectivity.TransientFailure) {
162 b.regeneratePicker()
163 }
164
165 b.cc.UpdateBalancerState(b.state, b.picker)
166}
167
168// Close is a nop because base balancer doesn't have internal state to clean up,
169// and it doesn't need to call RemoveSubConn for the SubConns.
170func (b *baseBalancer) Close() {
171}
172
173// NewErrPicker returns a picker that always returns err on Pick().
174func NewErrPicker(err error) balancer.Picker {
175 return &errPicker{err: err}
176}
177
178type errPicker struct {
179 err error // Pick() always returns this err.
180}
181
182func (p *errPicker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
183 return nil, nil, p.err
184}