Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -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 ( |
Menghan Li | a353537 | 2017-10-19 11:32:06 -0700 | [diff] [blame] | 22 | "fmt" |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 23 | "strings" |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 24 | "sync" |
| 25 | "time" |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 26 | |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 27 | "google.golang.org/grpc/balancer" |
Easwar Swaminathan | 88bf070 | 2019-11-04 08:41:14 -0800 | [diff] [blame] | 28 | "google.golang.org/grpc/credentials" |
lyuxuan | acd1429 | 2018-09-12 11:15:32 -0700 | [diff] [blame] | 29 | "google.golang.org/grpc/internal/channelz" |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 30 | "google.golang.org/grpc/internal/grpcsync" |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 31 | "google.golang.org/grpc/resolver" |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 32 | "google.golang.org/grpc/serviceconfig" |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | // ccResolverWrapper is a wrapper on top of cc for resolvers. |
Ning Xie | 5f3da2a | 2019-12-31 02:01:47 +0800 | [diff] [blame] | 36 | // It implements resolver.ClientConn interface. |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 37 | type ccResolverWrapper struct { |
Easwar Swaminathan | 027cd62 | 2019-10-14 15:13:33 -0700 | [diff] [blame] | 38 | cc *ClientConn |
| 39 | resolverMu sync.Mutex |
| 40 | resolver resolver.Resolver |
| 41 | done *grpcsync.Event |
| 42 | curState resolver.State |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 43 | |
Easwar Swaminathan | 027cd62 | 2019-10-14 15:13:33 -0700 | [diff] [blame] | 44 | pollingMu sync.Mutex |
| 45 | polling chan struct{} |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Doug Fawley | cd74fa2 | 2020-01-15 08:54:42 -0800 | [diff] [blame] | 48 | // newCCResolverWrapper uses the resolver.Builder to build a Resolver and |
| 49 | // returns a ccResolverWrapper object which wraps the newly built resolver. |
| 50 | func newCCResolverWrapper(cc *ClientConn, rb resolver.Builder) (*ccResolverWrapper, error) { |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 51 | ccr := &ccResolverWrapper{ |
| 52 | cc: cc, |
| 53 | done: grpcsync.NewEvent(), |
| 54 | } |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 55 | |
Easwar Swaminathan | 88bf070 | 2019-11-04 08:41:14 -0800 | [diff] [blame] | 56 | var credsClone credentials.TransportCredentials |
| 57 | if creds := cc.dopts.copts.TransportCredentials; creds != nil { |
| 58 | credsClone = creds.Clone() |
| 59 | } |
Doug Fawley | 2cb07fc | 2019-11-12 15:23:46 -0800 | [diff] [blame] | 60 | rbo := resolver.BuildOptions{ |
Easwar Swaminathan | 88bf070 | 2019-11-04 08:41:14 -0800 | [diff] [blame] | 61 | DisableServiceConfig: cc.dopts.disableServiceConfig, |
| 62 | DialCreds: credsClone, |
| 63 | CredsBundle: cc.dopts.copts.CredsBundle, |
| 64 | Dialer: cc.dopts.copts.Dialer, |
| 65 | } |
| 66 | |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 67 | var err error |
Easwar Swaminathan | 027cd62 | 2019-10-14 15:13:33 -0700 | [diff] [blame] | 68 | // We need to hold the lock here while we assign to the ccr.resolver field |
| 69 | // to guard against a data race caused by the following code path, |
| 70 | // rb.Build-->ccr.ReportError-->ccr.poll-->ccr.resolveNow, would end up |
| 71 | // accessing ccr.resolver which is being assigned here. |
| 72 | ccr.resolverMu.Lock() |
Easwar Swaminathan | e3baa76 | 2019-12-13 10:55:51 -0800 | [diff] [blame] | 73 | defer ccr.resolverMu.Unlock() |
Easwar Swaminathan | 88bf070 | 2019-11-04 08:41:14 -0800 | [diff] [blame] | 74 | ccr.resolver, err = rb.Build(cc.parsedTarget, ccr, rbo) |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 78 | return ccr, nil |
| 79 | } |
| 80 | |
Doug Fawley | 2cb07fc | 2019-11-12 15:23:46 -0800 | [diff] [blame] | 81 | func (ccr *ccResolverWrapper) resolveNow(o resolver.ResolveNowOptions) { |
Easwar Swaminathan | 027cd62 | 2019-10-14 15:13:33 -0700 | [diff] [blame] | 82 | ccr.resolverMu.Lock() |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 83 | if !ccr.done.HasFired() { |
| 84 | ccr.resolver.ResolveNow(o) |
| 85 | } |
Easwar Swaminathan | 027cd62 | 2019-10-14 15:13:33 -0700 | [diff] [blame] | 86 | ccr.resolverMu.Unlock() |
Menghan Li | 1e1a47f | 2017-11-28 13:16:53 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 89 | func (ccr *ccResolverWrapper) close() { |
Easwar Swaminathan | 027cd62 | 2019-10-14 15:13:33 -0700 | [diff] [blame] | 90 | ccr.resolverMu.Lock() |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 91 | ccr.resolver.Close() |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 92 | ccr.done.Fire() |
Easwar Swaminathan | 027cd62 | 2019-10-14 15:13:33 -0700 | [diff] [blame] | 93 | ccr.resolverMu.Unlock() |
Doug Fawley | 3910b87 | 2019-03-22 10:48:55 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 96 | // poll begins or ends asynchronous polling of the resolver based on whether |
| 97 | // err is ErrBadResolverState. |
| 98 | func (ccr *ccResolverWrapper) poll(err error) { |
Easwar Swaminathan | 027cd62 | 2019-10-14 15:13:33 -0700 | [diff] [blame] | 99 | ccr.pollingMu.Lock() |
| 100 | defer ccr.pollingMu.Unlock() |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 101 | if err != balancer.ErrBadResolverState { |
| 102 | // stop polling |
| 103 | if ccr.polling != nil { |
| 104 | close(ccr.polling) |
| 105 | ccr.polling = nil |
| 106 | } |
| 107 | return |
| 108 | } |
| 109 | if ccr.polling != nil { |
| 110 | // already polling |
| 111 | return |
| 112 | } |
| 113 | p := make(chan struct{}) |
| 114 | ccr.polling = p |
| 115 | go func() { |
| 116 | for i := 0; ; i++ { |
Doug Fawley | 2cb07fc | 2019-11-12 15:23:46 -0800 | [diff] [blame] | 117 | ccr.resolveNow(resolver.ResolveNowOptions{}) |
Easwar Swaminathan | 027cd62 | 2019-10-14 15:13:33 -0700 | [diff] [blame] | 118 | t := time.NewTimer(ccr.cc.dopts.resolveNowBackoff(i)) |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 119 | select { |
| 120 | case <-p: |
| 121 | t.Stop() |
| 122 | return |
| 123 | case <-ccr.done.Done(): |
| 124 | // Resolver has been closed. |
| 125 | t.Stop() |
| 126 | return |
| 127 | case <-t.C: |
| 128 | select { |
| 129 | case <-p: |
| 130 | return |
| 131 | default: |
| 132 | } |
| 133 | // Timer expired; re-resolve. |
| 134 | } |
| 135 | } |
| 136 | }() |
Doug Fawley | 3910b87 | 2019-03-22 10:48:55 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | func (ccr *ccResolverWrapper) UpdateState(s resolver.State) { |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 140 | if ccr.done.HasFired() { |
Doug Fawley | 3910b87 | 2019-03-22 10:48:55 -0700 | [diff] [blame] | 141 | return |
| 142 | } |
Garrett Gutierrez | 506b773 | 2020-06-26 12:04:47 -0700 | [diff] [blame] | 143 | channelz.Infof(logger, ccr.cc.channelzID, "ccResolverWrapper: sending update to cc: %v", s) |
Doug Fawley | 3910b87 | 2019-03-22 10:48:55 -0700 | [diff] [blame] | 144 | if channelz.IsOn() { |
| 145 | ccr.addChannelzTraceEvent(s) |
| 146 | } |
Doug Fawley | 3910b87 | 2019-03-22 10:48:55 -0700 | [diff] [blame] | 147 | ccr.curState = s |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 148 | ccr.poll(ccr.cc.updateResolverState(ccr.curState, nil)) |
| 149 | } |
| 150 | |
| 151 | func (ccr *ccResolverWrapper) ReportError(err error) { |
| 152 | if ccr.done.HasFired() { |
| 153 | return |
| 154 | } |
Garrett Gutierrez | 506b773 | 2020-06-26 12:04:47 -0700 | [diff] [blame] | 155 | channelz.Warningf(logger, ccr.cc.channelzID, "ccResolverWrapper: reporting error to cc: %v", err) |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 156 | ccr.poll(ccr.cc.updateResolverState(resolver.State{}, err)) |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 157 | } |
| 158 | |
lyuxuan | ea5e6da | 2019-04-03 10:50:28 -0700 | [diff] [blame] | 159 | // NewAddress is called by the resolver implementation to send addresses to gRPC. |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 160 | func (ccr *ccResolverWrapper) NewAddress(addrs []resolver.Address) { |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 161 | if ccr.done.HasFired() { |
lyuxuan | eb55fa5 | 2018-11-09 15:31:07 -0800 | [diff] [blame] | 162 | return |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 163 | } |
Garrett Gutierrez | 506b773 | 2020-06-26 12:04:47 -0700 | [diff] [blame] | 164 | channelz.Infof(logger, ccr.cc.channelzID, "ccResolverWrapper: sending new addresses to cc: %v", addrs) |
lyuxuan | eb55fa5 | 2018-11-09 15:31:07 -0800 | [diff] [blame] | 165 | if channelz.IsOn() { |
Doug Fawley | 3910b87 | 2019-03-22 10:48:55 -0700 | [diff] [blame] | 166 | ccr.addChannelzTraceEvent(resolver.State{Addresses: addrs, ServiceConfig: ccr.curState.ServiceConfig}) |
lyuxuan | eb55fa5 | 2018-11-09 15:31:07 -0800 | [diff] [blame] | 167 | } |
Doug Fawley | 3910b87 | 2019-03-22 10:48:55 -0700 | [diff] [blame] | 168 | ccr.curState.Addresses = addrs |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 169 | ccr.poll(ccr.cc.updateResolverState(ccr.curState, nil)) |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 170 | } |
| 171 | |
lyuxuan | ea5e6da | 2019-04-03 10:50:28 -0700 | [diff] [blame] | 172 | // NewServiceConfig is called by the resolver implementation to send service |
Gyuho Lee | 6f35246 | 2018-06-08 15:37:41 -0700 | [diff] [blame] | 173 | // configs to gRPC. |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 174 | func (ccr *ccResolverWrapper) NewServiceConfig(sc string) { |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 175 | if ccr.done.HasFired() { |
lyuxuan | eb55fa5 | 2018-11-09 15:31:07 -0800 | [diff] [blame] | 176 | return |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 177 | } |
Garrett Gutierrez | 506b773 | 2020-06-26 12:04:47 -0700 | [diff] [blame] | 178 | channelz.Infof(logger, ccr.cc.channelzID, "ccResolverWrapper: got new service config: %v", sc) |
Doug Fawley | cb47f38 | 2019-11-22 13:38:04 -0800 | [diff] [blame] | 179 | if ccr.cc.dopts.disableServiceConfig { |
Garrett Gutierrez | 506b773 | 2020-06-26 12:04:47 -0700 | [diff] [blame] | 180 | channelz.Info(logger, ccr.cc.channelzID, "Service config lookups disabled; ignoring config") |
Doug Fawley | cb47f38 | 2019-11-22 13:38:04 -0800 | [diff] [blame] | 181 | return |
| 182 | } |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 183 | scpr := parseServiceConfig(sc) |
| 184 | if scpr.Err != nil { |
Garrett Gutierrez | 506b773 | 2020-06-26 12:04:47 -0700 | [diff] [blame] | 185 | channelz.Warningf(logger, ccr.cc.channelzID, "ccResolverWrapper: error parsing service config: %v", scpr.Err) |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 186 | ccr.poll(balancer.ErrBadResolverState) |
Doug Fawley | d40a995 | 2019-05-30 09:12:58 -0700 | [diff] [blame] | 187 | return |
Doug Fawley | 3910b87 | 2019-03-22 10:48:55 -0700 | [diff] [blame] | 188 | } |
Doug Fawley | d40a995 | 2019-05-30 09:12:58 -0700 | [diff] [blame] | 189 | if channelz.IsOn() { |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 190 | ccr.addChannelzTraceEvent(resolver.State{Addresses: ccr.curState.Addresses, ServiceConfig: scpr}) |
Doug Fawley | d40a995 | 2019-05-30 09:12:58 -0700 | [diff] [blame] | 191 | } |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 192 | ccr.curState.ServiceConfig = scpr |
| 193 | ccr.poll(ccr.cc.updateResolverState(ccr.curState, nil)) |
| 194 | } |
| 195 | |
| 196 | func (ccr *ccResolverWrapper) ParseServiceConfig(scJSON string) *serviceconfig.ParseResult { |
| 197 | return parseServiceConfig(scJSON) |
Menghan Li | 4bbdf23 | 2017-10-02 09:22:57 -0700 | [diff] [blame] | 198 | } |
lyuxuan | acd1429 | 2018-09-12 11:15:32 -0700 | [diff] [blame] | 199 | |
Doug Fawley | 3910b87 | 2019-03-22 10:48:55 -0700 | [diff] [blame] | 200 | func (ccr *ccResolverWrapper) addChannelzTraceEvent(s resolver.State) { |
Doug Fawley | 3910b87 | 2019-03-22 10:48:55 -0700 | [diff] [blame] | 201 | var updates []string |
Doug Fawley | ed563a0 | 2019-10-04 12:59:43 -0700 | [diff] [blame] | 202 | var oldSC, newSC *ServiceConfig |
| 203 | var oldOK, newOK bool |
| 204 | if ccr.curState.ServiceConfig != nil { |
| 205 | oldSC, oldOK = ccr.curState.ServiceConfig.Config.(*ServiceConfig) |
| 206 | } |
| 207 | if s.ServiceConfig != nil { |
| 208 | newSC, newOK = s.ServiceConfig.Config.(*ServiceConfig) |
| 209 | } |
Doug Fawley | d40a995 | 2019-05-30 09:12:58 -0700 | [diff] [blame] | 210 | if oldOK != newOK || (oldOK && newOK && oldSC.rawJSONString != newSC.rawJSONString) { |
Doug Fawley | 3910b87 | 2019-03-22 10:48:55 -0700 | [diff] [blame] | 211 | updates = append(updates, "service config updated") |
| 212 | } |
| 213 | if len(ccr.curState.Addresses) > 0 && len(s.Addresses) == 0 { |
| 214 | updates = append(updates, "resolver returned an empty address list") |
| 215 | } else if len(ccr.curState.Addresses) == 0 && len(s.Addresses) > 0 { |
| 216 | updates = append(updates, "resolver returned new addresses") |
| 217 | } |
Garrett Gutierrez | 506b773 | 2020-06-26 12:04:47 -0700 | [diff] [blame] | 218 | channelz.AddTraceEvent(logger, ccr.cc.channelzID, 0, &channelz.TraceEventDesc{ |
Doug Fawley | 3910b87 | 2019-03-22 10:48:55 -0700 | [diff] [blame] | 219 | Desc: fmt.Sprintf("Resolver state updated: %+v (%v)", s, strings.Join(updates, "; ")), |
George Kechagias | 7464f75 | 2020-10-07 18:50:23 +0300 | [diff] [blame] | 220 | Severity: channelz.CtInfo, |
Doug Fawley | 3910b87 | 2019-03-22 10:48:55 -0700 | [diff] [blame] | 221 | }) |
lyuxuan | acd1429 | 2018-09-12 11:15:32 -0700 | [diff] [blame] | 222 | } |