balancer_v1_wrapper.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. "sync"
  21. "google.golang.org/grpc/balancer"
  22. "google.golang.org/grpc/connectivity"
  23. "google.golang.org/grpc/grpclog"
  24. "google.golang.org/grpc/resolver"
  25. )
  26. type balancerWrapperBuilder struct {
  27. b Balancer // The v1 balancer.
  28. }
  29. func (bwb *balancerWrapperBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
  30. bwb.b.Start(opts.Target.Endpoint, BalancerConfig{
  31. DialCreds: opts.DialCreds,
  32. Dialer: opts.Dialer,
  33. })
  34. _, pickfirst := bwb.b.(*pickFirst)
  35. bw := &balancerWrapper{
  36. balancer: bwb.b,
  37. pickfirst: pickfirst,
  38. cc: cc,
  39. targetAddr: opts.Target.Endpoint,
  40. startCh: make(chan struct{}),
  41. conns: make(map[resolver.Address]balancer.SubConn),
  42. connSt: make(map[balancer.SubConn]*scState),
  43. csEvltr: &balancer.ConnectivityStateEvaluator{},
  44. state: connectivity.Idle,
  45. }
  46. cc.UpdateState(balancer.State{ConnectivityState: connectivity.Idle, Picker: bw})
  47. go bw.lbWatcher()
  48. return bw
  49. }
  50. func (bwb *balancerWrapperBuilder) Name() string {
  51. return "wrapper"
  52. }
  53. type scState struct {
  54. addr Address // The v1 address type.
  55. s connectivity.State
  56. down func(error)
  57. }
  58. type balancerWrapper struct {
  59. balancer Balancer // The v1 balancer.
  60. pickfirst bool
  61. cc balancer.ClientConn
  62. targetAddr string // Target without the scheme.
  63. mu sync.Mutex
  64. conns map[resolver.Address]balancer.SubConn
  65. connSt map[balancer.SubConn]*scState
  66. // This channel is closed when handling the first resolver result.
  67. // lbWatcher blocks until this is closed, to avoid race between
  68. // - NewSubConn is created, cc wants to notify balancer of state changes;
  69. // - Build hasn't return, cc doesn't have access to balancer.
  70. startCh chan struct{}
  71. // To aggregate the connectivity state.
  72. csEvltr *balancer.ConnectivityStateEvaluator
  73. state connectivity.State
  74. }
  75. // lbWatcher watches the Notify channel of the balancer and manages
  76. // connections accordingly.
  77. func (bw *balancerWrapper) lbWatcher() {
  78. <-bw.startCh
  79. notifyCh := bw.balancer.Notify()
  80. if notifyCh == nil {
  81. // There's no resolver in the balancer. Connect directly.
  82. a := resolver.Address{
  83. Addr: bw.targetAddr,
  84. Type: resolver.Backend,
  85. }
  86. sc, err := bw.cc.NewSubConn([]resolver.Address{a}, balancer.NewSubConnOptions{})
  87. if err != nil {
  88. grpclog.Warningf("Error creating connection to %v. Err: %v", a, err)
  89. } else {
  90. bw.mu.Lock()
  91. bw.conns[a] = sc
  92. bw.connSt[sc] = &scState{
  93. addr: Address{Addr: bw.targetAddr},
  94. s: connectivity.Idle,
  95. }
  96. bw.mu.Unlock()
  97. sc.Connect()
  98. }
  99. return
  100. }
  101. for addrs := range notifyCh {
  102. grpclog.Infof("balancerWrapper: got update addr from Notify: %v", addrs)
  103. if bw.pickfirst {
  104. var (
  105. oldA resolver.Address
  106. oldSC balancer.SubConn
  107. )
  108. bw.mu.Lock()
  109. for oldA, oldSC = range bw.conns {
  110. break
  111. }
  112. bw.mu.Unlock()
  113. if len(addrs) <= 0 {
  114. if oldSC != nil {
  115. // Teardown old sc.
  116. bw.mu.Lock()
  117. delete(bw.conns, oldA)
  118. delete(bw.connSt, oldSC)
  119. bw.mu.Unlock()
  120. bw.cc.RemoveSubConn(oldSC)
  121. }
  122. continue
  123. }
  124. var newAddrs []resolver.Address
  125. for _, a := range addrs {
  126. newAddr := resolver.Address{
  127. Addr: a.Addr,
  128. Type: resolver.Backend, // All addresses from balancer are all backends.
  129. ServerName: "",
  130. Metadata: a.Metadata,
  131. }
  132. newAddrs = append(newAddrs, newAddr)
  133. }
  134. if oldSC == nil {
  135. // Create new sc.
  136. sc, err := bw.cc.NewSubConn(newAddrs, balancer.NewSubConnOptions{})
  137. if err != nil {
  138. grpclog.Warningf("Error creating connection to %v. Err: %v", newAddrs, err)
  139. } else {
  140. bw.mu.Lock()
  141. // For pickfirst, there should be only one SubConn, so the
  142. // address doesn't matter. All states updating (up and down)
  143. // and picking should all happen on that only SubConn.
  144. bw.conns[resolver.Address{}] = sc
  145. bw.connSt[sc] = &scState{
  146. addr: addrs[0], // Use the first address.
  147. s: connectivity.Idle,
  148. }
  149. bw.mu.Unlock()
  150. sc.Connect()
  151. }
  152. } else {
  153. bw.mu.Lock()
  154. bw.connSt[oldSC].addr = addrs[0]
  155. bw.mu.Unlock()
  156. oldSC.UpdateAddresses(newAddrs)
  157. }
  158. } else {
  159. var (
  160. add []resolver.Address // Addresses need to setup connections.
  161. del []balancer.SubConn // Connections need to tear down.
  162. )
  163. resAddrs := make(map[resolver.Address]Address)
  164. for _, a := range addrs {
  165. resAddrs[resolver.Address{
  166. Addr: a.Addr,
  167. Type: resolver.Backend, // All addresses from balancer are all backends.
  168. ServerName: "",
  169. Metadata: a.Metadata,
  170. }] = a
  171. }
  172. bw.mu.Lock()
  173. for a := range resAddrs {
  174. if _, ok := bw.conns[a]; !ok {
  175. add = append(add, a)
  176. }
  177. }
  178. for a, c := range bw.conns {
  179. if _, ok := resAddrs[a]; !ok {
  180. del = append(del, c)
  181. delete(bw.conns, a)
  182. // Keep the state of this sc in bw.connSt until its state becomes Shutdown.
  183. }
  184. }
  185. bw.mu.Unlock()
  186. for _, a := range add {
  187. sc, err := bw.cc.NewSubConn([]resolver.Address{a}, balancer.NewSubConnOptions{})
  188. if err != nil {
  189. grpclog.Warningf("Error creating connection to %v. Err: %v", a, err)
  190. } else {
  191. bw.mu.Lock()
  192. bw.conns[a] = sc
  193. bw.connSt[sc] = &scState{
  194. addr: resAddrs[a],
  195. s: connectivity.Idle,
  196. }
  197. bw.mu.Unlock()
  198. sc.Connect()
  199. }
  200. }
  201. for _, c := range del {
  202. bw.cc.RemoveSubConn(c)
  203. }
  204. }
  205. }
  206. }
  207. func (bw *balancerWrapper) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
  208. bw.mu.Lock()
  209. defer bw.mu.Unlock()
  210. scSt, ok := bw.connSt[sc]
  211. if !ok {
  212. return
  213. }
  214. if s == connectivity.Idle {
  215. sc.Connect()
  216. }
  217. oldS := scSt.s
  218. scSt.s = s
  219. if oldS != connectivity.Ready && s == connectivity.Ready {
  220. scSt.down = bw.balancer.Up(scSt.addr)
  221. } else if oldS == connectivity.Ready && s != connectivity.Ready {
  222. if scSt.down != nil {
  223. scSt.down(errConnClosing)
  224. }
  225. }
  226. sa := bw.csEvltr.RecordTransition(oldS, s)
  227. if bw.state != sa {
  228. bw.state = sa
  229. }
  230. bw.cc.UpdateState(balancer.State{ConnectivityState: bw.state, Picker: bw})
  231. if s == connectivity.Shutdown {
  232. // Remove state for this sc.
  233. delete(bw.connSt, sc)
  234. }
  235. }
  236. func (bw *balancerWrapper) HandleResolvedAddrs([]resolver.Address, error) {
  237. bw.mu.Lock()
  238. defer bw.mu.Unlock()
  239. select {
  240. case <-bw.startCh:
  241. default:
  242. close(bw.startCh)
  243. }
  244. // There should be a resolver inside the balancer.
  245. // All updates here, if any, are ignored.
  246. }
  247. func (bw *balancerWrapper) Close() {
  248. bw.mu.Lock()
  249. defer bw.mu.Unlock()
  250. select {
  251. case <-bw.startCh:
  252. default:
  253. close(bw.startCh)
  254. }
  255. bw.balancer.Close()
  256. }
  257. // The picker is the balancerWrapper itself.
  258. // It either blocks or returns error, consistent with v1 balancer Get().
  259. func (bw *balancerWrapper) Pick(info balancer.PickInfo) (result balancer.PickResult, err error) {
  260. failfast := true // Default failfast is true.
  261. if ss, ok := rpcInfoFromContext(info.Ctx); ok {
  262. failfast = ss.failfast
  263. }
  264. a, p, err := bw.balancer.Get(info.Ctx, BalancerGetOptions{BlockingWait: !failfast})
  265. if err != nil {
  266. return balancer.PickResult{}, toRPCErr(err)
  267. }
  268. if p != nil {
  269. result.Done = func(balancer.DoneInfo) { p() }
  270. defer func() {
  271. if err != nil {
  272. p()
  273. }
  274. }()
  275. }
  276. bw.mu.Lock()
  277. defer bw.mu.Unlock()
  278. if bw.pickfirst {
  279. // Get the first sc in conns.
  280. for _, result.SubConn = range bw.conns {
  281. return result, nil
  282. }
  283. return balancer.PickResult{}, balancer.ErrNoSubConnAvailable
  284. }
  285. var ok1 bool
  286. result.SubConn, ok1 = bw.conns[resolver.Address{
  287. Addr: a.Addr,
  288. Type: resolver.Backend,
  289. ServerName: "",
  290. Metadata: a.Metadata,
  291. }]
  292. s, ok2 := bw.connSt[result.SubConn]
  293. if !ok1 || !ok2 {
  294. // This can only happen due to a race where Get() returned an address
  295. // that was subsequently removed by Notify. In this case we should
  296. // retry always.
  297. return balancer.PickResult{}, balancer.ErrNoSubConnAvailable
  298. }
  299. switch s.s {
  300. case connectivity.Ready, connectivity.Idle:
  301. return result, nil
  302. case connectivity.Shutdown, connectivity.TransientFailure:
  303. // If the returned sc has been shut down or is in transient failure,
  304. // return error, and this RPC will fail or wait for another picker (if
  305. // non-failfast).
  306. return balancer.PickResult{}, balancer.ErrTransientFailure
  307. default:
  308. // For other states (connecting or unknown), the v1 balancer would
  309. // traditionally wait until ready and then issue the RPC. Returning
  310. // ErrNoSubConnAvailable will be a slight improvement in that it will
  311. // allow the balancer to choose another address in case others are
  312. // connected.
  313. return balancer.PickResult{}, balancer.ErrNoSubConnAvailable
  314. }
  315. }