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 grpc |
| 20 | |
| 21 | import ( |
| 22 | "fmt" |
| 23 | "strings" |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 24 | "sync" |
| 25 | "time" |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 26 | |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 27 | "google.golang.org/grpc/balancer" |
| 28 | "google.golang.org/grpc/credentials" |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 29 | "google.golang.org/grpc/grpclog" |
| 30 | "google.golang.org/grpc/internal/channelz" |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 31 | "google.golang.org/grpc/internal/grpcsync" |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 32 | "google.golang.org/grpc/resolver" |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 33 | "google.golang.org/grpc/serviceconfig" |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | // ccResolverWrapper is a wrapper on top of cc for resolvers. |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 37 | // It implements resolver.ClientConn interface. |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 38 | type ccResolverWrapper struct { |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 39 | cc *ClientConn |
| 40 | resolverMu sync.Mutex |
| 41 | resolver resolver.Resolver |
| 42 | done *grpcsync.Event |
| 43 | curState resolver.State |
| 44 | |
| 45 | pollingMu sync.Mutex |
| 46 | polling chan struct{} |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // split2 returns the values from strings.SplitN(s, sep, 2). |
| 50 | // If sep is not found, it returns ("", "", false) instead. |
| 51 | func split2(s, sep string) (string, string, bool) { |
| 52 | spl := strings.SplitN(s, sep, 2) |
| 53 | if len(spl) < 2 { |
| 54 | return "", "", false |
| 55 | } |
| 56 | return spl[0], spl[1], true |
| 57 | } |
| 58 | |
| 59 | // parseTarget splits target into a struct containing scheme, authority and |
| 60 | // endpoint. |
| 61 | // |
| 62 | // If target is not a valid scheme://authority/endpoint, it returns {Endpoint: |
| 63 | // target}. |
| 64 | func parseTarget(target string) (ret resolver.Target) { |
| 65 | var ok bool |
| 66 | ret.Scheme, ret.Endpoint, ok = split2(target, "://") |
| 67 | if !ok { |
| 68 | return resolver.Target{Endpoint: target} |
| 69 | } |
| 70 | ret.Authority, ret.Endpoint, ok = split2(ret.Endpoint, "/") |
| 71 | if !ok { |
| 72 | return resolver.Target{Endpoint: target} |
| 73 | } |
| 74 | return ret |
| 75 | } |
| 76 | |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 77 | // newCCResolverWrapper uses the resolver.Builder to build a Resolver and |
| 78 | // returns a ccResolverWrapper object which wraps the newly built resolver. |
| 79 | func newCCResolverWrapper(cc *ClientConn, rb resolver.Builder) (*ccResolverWrapper, error) { |
| 80 | ccr := &ccResolverWrapper{ |
| 81 | cc: cc, |
| 82 | done: grpcsync.NewEvent(), |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 85 | var credsClone credentials.TransportCredentials |
| 86 | if creds := cc.dopts.copts.TransportCredentials; creds != nil { |
| 87 | credsClone = creds.Clone() |
| 88 | } |
| 89 | rbo := resolver.BuildOptions{ |
| 90 | DisableServiceConfig: cc.dopts.disableServiceConfig, |
| 91 | DialCreds: credsClone, |
| 92 | CredsBundle: cc.dopts.copts.CredsBundle, |
| 93 | Dialer: cc.dopts.copts.Dialer, |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | var err error |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 97 | // We need to hold the lock here while we assign to the ccr.resolver field |
| 98 | // to guard against a data race caused by the following code path, |
| 99 | // rb.Build-->ccr.ReportError-->ccr.poll-->ccr.resolveNow, would end up |
| 100 | // accessing ccr.resolver which is being assigned here. |
| 101 | ccr.resolverMu.Lock() |
| 102 | defer ccr.resolverMu.Unlock() |
| 103 | ccr.resolver, err = rb.Build(cc.parsedTarget, ccr, rbo) |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | return ccr, nil |
| 108 | } |
| 109 | |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 110 | func (ccr *ccResolverWrapper) resolveNow(o resolver.ResolveNowOptions) { |
| 111 | ccr.resolverMu.Lock() |
| 112 | if !ccr.done.HasFired() { |
| 113 | ccr.resolver.ResolveNow(o) |
| 114 | } |
| 115 | ccr.resolverMu.Unlock() |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | func (ccr *ccResolverWrapper) close() { |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 119 | ccr.resolverMu.Lock() |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 120 | ccr.resolver.Close() |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 121 | ccr.done.Fire() |
| 122 | ccr.resolverMu.Unlock() |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 125 | // poll begins or ends asynchronous polling of the resolver based on whether |
| 126 | // err is ErrBadResolverState. |
| 127 | func (ccr *ccResolverWrapper) poll(err error) { |
| 128 | ccr.pollingMu.Lock() |
| 129 | defer ccr.pollingMu.Unlock() |
| 130 | if err != balancer.ErrBadResolverState { |
| 131 | // stop polling |
| 132 | if ccr.polling != nil { |
| 133 | close(ccr.polling) |
| 134 | ccr.polling = nil |
| 135 | } |
| 136 | return |
| 137 | } |
| 138 | if ccr.polling != nil { |
| 139 | // already polling |
| 140 | return |
| 141 | } |
| 142 | p := make(chan struct{}) |
| 143 | ccr.polling = p |
| 144 | go func() { |
| 145 | for i := 0; ; i++ { |
| 146 | ccr.resolveNow(resolver.ResolveNowOptions{}) |
| 147 | t := time.NewTimer(ccr.cc.dopts.resolveNowBackoff(i)) |
| 148 | select { |
| 149 | case <-p: |
| 150 | t.Stop() |
| 151 | return |
| 152 | case <-ccr.done.Done(): |
| 153 | // Resolver has been closed. |
| 154 | t.Stop() |
| 155 | return |
| 156 | case <-t.C: |
| 157 | select { |
| 158 | case <-p: |
| 159 | return |
| 160 | default: |
| 161 | } |
| 162 | // Timer expired; re-resolve. |
| 163 | } |
| 164 | } |
| 165 | }() |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | func (ccr *ccResolverWrapper) UpdateState(s resolver.State) { |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 169 | if ccr.done.HasFired() { |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 170 | return |
| 171 | } |
| 172 | grpclog.Infof("ccResolverWrapper: sending update to cc: %v", s) |
| 173 | if channelz.IsOn() { |
| 174 | ccr.addChannelzTraceEvent(s) |
| 175 | } |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 176 | ccr.curState = s |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 177 | ccr.poll(ccr.cc.updateResolverState(ccr.curState, nil)) |
| 178 | } |
| 179 | |
| 180 | func (ccr *ccResolverWrapper) ReportError(err error) { |
| 181 | if ccr.done.HasFired() { |
| 182 | return |
| 183 | } |
| 184 | grpclog.Warningf("ccResolverWrapper: reporting error to cc: %v", err) |
| 185 | if channelz.IsOn() { |
| 186 | channelz.AddTraceEvent(ccr.cc.channelzID, &channelz.TraceEventDesc{ |
| 187 | Desc: fmt.Sprintf("Resolver reported error: %v", err), |
| 188 | Severity: channelz.CtWarning, |
| 189 | }) |
| 190 | } |
| 191 | ccr.poll(ccr.cc.updateResolverState(resolver.State{}, err)) |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // NewAddress is called by the resolver implementation to send addresses to gRPC. |
| 195 | func (ccr *ccResolverWrapper) NewAddress(addrs []resolver.Address) { |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 196 | if ccr.done.HasFired() { |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 197 | return |
| 198 | } |
| 199 | grpclog.Infof("ccResolverWrapper: sending new addresses to cc: %v", addrs) |
| 200 | if channelz.IsOn() { |
| 201 | ccr.addChannelzTraceEvent(resolver.State{Addresses: addrs, ServiceConfig: ccr.curState.ServiceConfig}) |
| 202 | } |
| 203 | ccr.curState.Addresses = addrs |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 204 | ccr.poll(ccr.cc.updateResolverState(ccr.curState, nil)) |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // NewServiceConfig is called by the resolver implementation to send service |
| 208 | // configs to gRPC. |
| 209 | func (ccr *ccResolverWrapper) NewServiceConfig(sc string) { |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 210 | if ccr.done.HasFired() { |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 211 | return |
| 212 | } |
| 213 | grpclog.Infof("ccResolverWrapper: got new service config: %v", sc) |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 214 | if ccr.cc.dopts.disableServiceConfig { |
| 215 | grpclog.Infof("Service config lookups disabled; ignoring config") |
| 216 | return |
| 217 | } |
| 218 | scpr := parseServiceConfig(sc) |
| 219 | if scpr.Err != nil { |
| 220 | grpclog.Warningf("ccResolverWrapper: error parsing service config: %v", scpr.Err) |
| 221 | if channelz.IsOn() { |
| 222 | channelz.AddTraceEvent(ccr.cc.channelzID, &channelz.TraceEventDesc{ |
| 223 | Desc: fmt.Sprintf("Error parsing service config: %v", scpr.Err), |
| 224 | Severity: channelz.CtWarning, |
| 225 | }) |
| 226 | } |
| 227 | ccr.poll(balancer.ErrBadResolverState) |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 228 | return |
| 229 | } |
| 230 | if channelz.IsOn() { |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 231 | ccr.addChannelzTraceEvent(resolver.State{Addresses: ccr.curState.Addresses, ServiceConfig: scpr}) |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 232 | } |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 233 | ccr.curState.ServiceConfig = scpr |
| 234 | ccr.poll(ccr.cc.updateResolverState(ccr.curState, nil)) |
| 235 | } |
| 236 | |
| 237 | func (ccr *ccResolverWrapper) ParseServiceConfig(scJSON string) *serviceconfig.ParseResult { |
| 238 | return parseServiceConfig(scJSON) |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | func (ccr *ccResolverWrapper) addChannelzTraceEvent(s resolver.State) { |
| 242 | var updates []string |
Arjun E K | 57a7fcb | 2020-01-30 06:44:45 +0000 | [diff] [blame] | 243 | var oldSC, newSC *ServiceConfig |
| 244 | var oldOK, newOK bool |
| 245 | if ccr.curState.ServiceConfig != nil { |
| 246 | oldSC, oldOK = ccr.curState.ServiceConfig.Config.(*ServiceConfig) |
| 247 | } |
| 248 | if s.ServiceConfig != nil { |
| 249 | newSC, newOK = s.ServiceConfig.Config.(*ServiceConfig) |
| 250 | } |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 251 | if oldOK != newOK || (oldOK && newOK && oldSC.rawJSONString != newSC.rawJSONString) { |
| 252 | updates = append(updates, "service config updated") |
| 253 | } |
| 254 | if len(ccr.curState.Addresses) > 0 && len(s.Addresses) == 0 { |
| 255 | updates = append(updates, "resolver returned an empty address list") |
| 256 | } else if len(ccr.curState.Addresses) == 0 && len(s.Addresses) > 0 { |
| 257 | updates = append(updates, "resolver returned new addresses") |
| 258 | } |
| 259 | channelz.AddTraceEvent(ccr.cc.channelzID, &channelz.TraceEventDesc{ |
| 260 | Desc: fmt.Sprintf("Resolver state updated: %+v (%v)", s, strings.Join(updates, "; ")), |
| 261 | Severity: channelz.CtINFO, |
| 262 | }) |
| 263 | } |