picker_wrapper.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. package grpc
  19. import (
  20. "io"
  21. "sync"
  22. "sync/atomic"
  23. "golang.org/x/net/context"
  24. "google.golang.org/grpc/balancer"
  25. "google.golang.org/grpc/codes"
  26. "google.golang.org/grpc/grpclog"
  27. "google.golang.org/grpc/internal/channelz"
  28. "google.golang.org/grpc/metadata"
  29. "google.golang.org/grpc/resolver"
  30. "google.golang.org/grpc/status"
  31. "google.golang.org/grpc/transport"
  32. )
  33. // pickerWrapper is a wrapper of balancer.Picker. It blocks on certain pick
  34. // actions and unblock when there's a picker update.
  35. type pickerWrapper struct {
  36. mu sync.Mutex
  37. done bool
  38. blockingCh chan struct{}
  39. picker balancer.Picker
  40. // The latest connection happened.
  41. connErrMu sync.Mutex
  42. connErr error
  43. stickinessMDKey atomic.Value
  44. stickiness *stickyStore
  45. }
  46. func newPickerWrapper() *pickerWrapper {
  47. bp := &pickerWrapper{
  48. blockingCh: make(chan struct{}),
  49. stickiness: newStickyStore(),
  50. }
  51. return bp
  52. }
  53. func (bp *pickerWrapper) updateConnectionError(err error) {
  54. bp.connErrMu.Lock()
  55. bp.connErr = err
  56. bp.connErrMu.Unlock()
  57. }
  58. func (bp *pickerWrapper) connectionError() error {
  59. bp.connErrMu.Lock()
  60. err := bp.connErr
  61. bp.connErrMu.Unlock()
  62. return err
  63. }
  64. func (bp *pickerWrapper) updateStickinessMDKey(newKey string) {
  65. // No need to check ok because mdKey == "" if ok == false.
  66. if oldKey, _ := bp.stickinessMDKey.Load().(string); oldKey != newKey {
  67. bp.stickinessMDKey.Store(newKey)
  68. bp.stickiness.reset(newKey)
  69. }
  70. }
  71. func (bp *pickerWrapper) getStickinessMDKey() string {
  72. // No need to check ok because mdKey == "" if ok == false.
  73. mdKey, _ := bp.stickinessMDKey.Load().(string)
  74. return mdKey
  75. }
  76. func (bp *pickerWrapper) clearStickinessState() {
  77. if oldKey := bp.getStickinessMDKey(); oldKey != "" {
  78. // There's no need to reset store if mdKey was "".
  79. bp.stickiness.reset(oldKey)
  80. }
  81. }
  82. // updatePicker is called by UpdateBalancerState. It unblocks all blocked pick.
  83. func (bp *pickerWrapper) updatePicker(p balancer.Picker) {
  84. bp.mu.Lock()
  85. if bp.done {
  86. bp.mu.Unlock()
  87. return
  88. }
  89. bp.picker = p
  90. // bp.blockingCh should never be nil.
  91. close(bp.blockingCh)
  92. bp.blockingCh = make(chan struct{})
  93. bp.mu.Unlock()
  94. }
  95. func doneChannelzWrapper(acw *acBalancerWrapper, done func(balancer.DoneInfo)) func(balancer.DoneInfo) {
  96. acw.mu.Lock()
  97. ac := acw.ac
  98. acw.mu.Unlock()
  99. ac.incrCallsStarted()
  100. return func(b balancer.DoneInfo) {
  101. if b.Err != nil && b.Err != io.EOF {
  102. ac.incrCallsFailed()
  103. } else {
  104. ac.incrCallsSucceeded()
  105. }
  106. if done != nil {
  107. done(b)
  108. }
  109. }
  110. }
  111. // pick returns the transport that will be used for the RPC.
  112. // It may block in the following cases:
  113. // - there's no picker
  114. // - the current picker returns ErrNoSubConnAvailable
  115. // - the current picker returns other errors and failfast is false.
  116. // - the subConn returned by the current picker is not READY
  117. // When one of these situations happens, pick blocks until the picker gets updated.
  118. func (bp *pickerWrapper) pick(ctx context.Context, failfast bool, opts balancer.PickOptions) (transport.ClientTransport, func(balancer.DoneInfo), error) {
  119. mdKey := bp.getStickinessMDKey()
  120. stickyKey, isSticky := stickyKeyFromContext(ctx, mdKey)
  121. // Potential race here: if stickinessMDKey is updated after the above two
  122. // lines, and this pick is a sticky pick, the following put could add an
  123. // entry to sticky store with an outdated sticky key.
  124. //
  125. // The solution: keep the current md key in sticky store, and at the
  126. // beginning of each get/put, check the mdkey against store.curMDKey.
  127. // - Cons: one more string comparing for each get/put.
  128. // - Pros: the string matching happens inside get/put, so the overhead for
  129. // non-sticky RPCs will be minimal.
  130. if isSticky {
  131. if t, ok := bp.stickiness.get(mdKey, stickyKey); ok {
  132. // Done function returned is always nil.
  133. return t, nil, nil
  134. }
  135. }
  136. var (
  137. p balancer.Picker
  138. ch chan struct{}
  139. )
  140. for {
  141. bp.mu.Lock()
  142. if bp.done {
  143. bp.mu.Unlock()
  144. return nil, nil, ErrClientConnClosing
  145. }
  146. if bp.picker == nil {
  147. ch = bp.blockingCh
  148. }
  149. if ch == bp.blockingCh {
  150. // This could happen when either:
  151. // - bp.picker is nil (the previous if condition), or
  152. // - has called pick on the current picker.
  153. bp.mu.Unlock()
  154. select {
  155. case <-ctx.Done():
  156. return nil, nil, ctx.Err()
  157. case <-ch:
  158. }
  159. continue
  160. }
  161. ch = bp.blockingCh
  162. p = bp.picker
  163. bp.mu.Unlock()
  164. subConn, done, err := p.Pick(ctx, opts)
  165. if err != nil {
  166. switch err {
  167. case balancer.ErrNoSubConnAvailable:
  168. continue
  169. case balancer.ErrTransientFailure:
  170. if !failfast {
  171. continue
  172. }
  173. return nil, nil, status.Errorf(codes.Unavailable, "%v, latest connection error: %v", err, bp.connectionError())
  174. default:
  175. // err is some other error.
  176. return nil, nil, toRPCErr(err)
  177. }
  178. }
  179. acw, ok := subConn.(*acBalancerWrapper)
  180. if !ok {
  181. grpclog.Infof("subconn returned from pick is not *acBalancerWrapper")
  182. continue
  183. }
  184. if t, ok := acw.getAddrConn().getReadyTransport(); ok {
  185. if isSticky {
  186. bp.stickiness.put(mdKey, stickyKey, acw)
  187. }
  188. if channelz.IsOn() {
  189. return t, doneChannelzWrapper(acw, done), nil
  190. }
  191. return t, done, nil
  192. }
  193. grpclog.Infof("blockingPicker: the picked transport is not ready, loop back to repick")
  194. // If ok == false, ac.state is not READY.
  195. // A valid picker always returns READY subConn. This means the state of ac
  196. // just changed, and picker will be updated shortly.
  197. // continue back to the beginning of the for loop to repick.
  198. }
  199. }
  200. func (bp *pickerWrapper) close() {
  201. bp.mu.Lock()
  202. defer bp.mu.Unlock()
  203. if bp.done {
  204. return
  205. }
  206. bp.done = true
  207. close(bp.blockingCh)
  208. }
  209. const stickinessKeyCountLimit = 1000
  210. type stickyStoreEntry struct {
  211. acw *acBalancerWrapper
  212. addr resolver.Address
  213. }
  214. type stickyStore struct {
  215. mu sync.Mutex
  216. // curMDKey is check before every get/put to avoid races. The operation will
  217. // abort immediately when the given mdKey is different from the curMDKey.
  218. curMDKey string
  219. store *linkedMap
  220. }
  221. func newStickyStore() *stickyStore {
  222. return &stickyStore{
  223. store: newLinkedMap(),
  224. }
  225. }
  226. // reset clears the map in stickyStore, and set the currentMDKey to newMDKey.
  227. func (ss *stickyStore) reset(newMDKey string) {
  228. ss.mu.Lock()
  229. ss.curMDKey = newMDKey
  230. ss.store.clear()
  231. ss.mu.Unlock()
  232. }
  233. // stickyKey is the key to look up in store. mdKey will be checked against
  234. // curMDKey to avoid races.
  235. func (ss *stickyStore) put(mdKey, stickyKey string, acw *acBalancerWrapper) {
  236. ss.mu.Lock()
  237. defer ss.mu.Unlock()
  238. if mdKey != ss.curMDKey {
  239. return
  240. }
  241. // TODO(stickiness): limit the total number of entries.
  242. ss.store.put(stickyKey, &stickyStoreEntry{
  243. acw: acw,
  244. addr: acw.getAddrConn().getCurAddr(),
  245. })
  246. if ss.store.len() > stickinessKeyCountLimit {
  247. ss.store.removeOldest()
  248. }
  249. }
  250. // stickyKey is the key to look up in store. mdKey will be checked against
  251. // curMDKey to avoid races.
  252. func (ss *stickyStore) get(mdKey, stickyKey string) (transport.ClientTransport, bool) {
  253. ss.mu.Lock()
  254. defer ss.mu.Unlock()
  255. if mdKey != ss.curMDKey {
  256. return nil, false
  257. }
  258. entry, ok := ss.store.get(stickyKey)
  259. if !ok {
  260. return nil, false
  261. }
  262. ac := entry.acw.getAddrConn()
  263. if ac.getCurAddr() != entry.addr {
  264. ss.store.remove(stickyKey)
  265. return nil, false
  266. }
  267. t, ok := ac.getReadyTransport()
  268. if !ok {
  269. ss.store.remove(stickyKey)
  270. return nil, false
  271. }
  272. return t, true
  273. }
  274. // Get one value from metadata in ctx with key stickinessMDKey.
  275. //
  276. // It returns "", false if stickinessMDKey is an empty string.
  277. func stickyKeyFromContext(ctx context.Context, stickinessMDKey string) (string, bool) {
  278. if stickinessMDKey == "" {
  279. return "", false
  280. }
  281. md, added, ok := metadata.FromOutgoingContextRaw(ctx)
  282. if !ok {
  283. return "", false
  284. }
  285. if vv, ok := md[stickinessMDKey]; ok {
  286. if len(vv) > 0 {
  287. return vv[0], true
  288. }
  289. }
  290. for _, ss := range added {
  291. for i := 0; i < len(ss)-1; i += 2 {
  292. if ss[i] == stickinessMDKey {
  293. return ss[i+1], true
  294. }
  295. }
  296. }
  297. return "", false
  298. }