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