balancer_v1_wrapper.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. "golang.org/x/net/context"
  22. "google.golang.org/grpc/balancer"
  23. "google.golang.org/grpc/codes"
  24. "google.golang.org/grpc/connectivity"
  25. "google.golang.org/grpc/grpclog"
  26. "google.golang.org/grpc/resolver"
  27. )
  28. type balancerWrapperBuilder struct {
  29. b Balancer // The v1 balancer.
  30. }
  31. func (bwb *balancerWrapperBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
  32. bwb.b.Start(cc.Target(), BalancerConfig{
  33. DialCreds: opts.DialCreds,
  34. Dialer: opts.Dialer,
  35. })
  36. _, pickfirst := bwb.b.(*pickFirst)
  37. bw := &balancerWrapper{
  38. balancer: bwb.b,
  39. pickfirst: pickfirst,
  40. cc: cc,
  41. startCh: make(chan struct{}),
  42. conns: make(map[resolver.Address]balancer.SubConn),
  43. connSt: make(map[balancer.SubConn]*scState),
  44. csEvltr: &connectivityStateEvaluator{},
  45. state: connectivity.Idle,
  46. }
  47. cc.UpdateBalancerState(connectivity.Idle, bw)
  48. go bw.lbWatcher()
  49. return bw
  50. }
  51. func (bwb *balancerWrapperBuilder) Name() string {
  52. return "wrapper"
  53. }
  54. type scState struct {
  55. addr Address // The v1 address type.
  56. s connectivity.State
  57. down func(error)
  58. }
  59. type balancerWrapper struct {
  60. balancer Balancer // The v1 balancer.
  61. pickfirst bool
  62. cc balancer.ClientConn
  63. // To aggregate the connectivity state.
  64. csEvltr *connectivityStateEvaluator
  65. state connectivity.State
  66. mu sync.Mutex
  67. conns map[resolver.Address]balancer.SubConn
  68. connSt map[balancer.SubConn]*scState
  69. // This channel is closed when handling the first resolver result.
  70. // lbWatcher blocks until this is closed, to avoid race between
  71. // - NewSubConn is created, cc wants to notify balancer of state changes;
  72. // - Build hasn't return, cc doesn't have access to balancer.
  73. startCh chan struct{}
  74. }
  75. // lbWatcher watches the Notify channel of the balancer and manages
  76. // connections accordingly.
  77. func (bw *balancerWrapper) lbWatcher() {
  78. <-bw.startCh
  79. grpclog.Infof("balancerWrapper: is pickfirst: %v\n", bw.pickfirst)
  80. notifyCh := bw.balancer.Notify()
  81. if notifyCh == nil {
  82. // There's no resolver in the balancer. Connect directly.
  83. a := resolver.Address{
  84. Addr: bw.cc.Target(),
  85. Type: resolver.Backend,
  86. }
  87. sc, err := bw.cc.NewSubConn([]resolver.Address{a}, balancer.NewSubConnOptions{})
  88. if err != nil {
  89. grpclog.Warningf("Error creating connection to %v. Err: %v", a, err)
  90. } else {
  91. bw.mu.Lock()
  92. bw.conns[a] = sc
  93. bw.connSt[sc] = &scState{
  94. addr: Address{Addr: bw.cc.Target()},
  95. s: connectivity.Idle,
  96. }
  97. bw.mu.Unlock()
  98. sc.Connect()
  99. }
  100. return
  101. }
  102. for addrs := range notifyCh {
  103. grpclog.Infof("balancerWrapper: got update addr from Notify: %v\n", addrs)
  104. if bw.pickfirst {
  105. var (
  106. oldA resolver.Address
  107. oldSC balancer.SubConn
  108. )
  109. bw.mu.Lock()
  110. for oldA, oldSC = range bw.conns {
  111. break
  112. }
  113. bw.mu.Unlock()
  114. if len(addrs) <= 0 {
  115. if oldSC != nil {
  116. // Teardown old sc.
  117. bw.mu.Lock()
  118. delete(bw.conns, oldA)
  119. delete(bw.connSt, oldSC)
  120. bw.mu.Unlock()
  121. bw.cc.RemoveSubConn(oldSC)
  122. }
  123. continue
  124. }
  125. var newAddrs []resolver.Address
  126. for _, a := range addrs {
  127. newAddr := resolver.Address{
  128. Addr: a.Addr,
  129. Type: resolver.Backend, // All addresses from balancer are all backends.
  130. ServerName: "",
  131. Metadata: a.Metadata,
  132. }
  133. newAddrs = append(newAddrs, newAddr)
  134. }
  135. if oldSC == nil {
  136. // Create new sc.
  137. sc, err := bw.cc.NewSubConn(newAddrs, balancer.NewSubConnOptions{})
  138. if err != nil {
  139. grpclog.Warningf("Error creating connection to %v. Err: %v", newAddrs, err)
  140. } else {
  141. bw.mu.Lock()
  142. // For pickfirst, there should be only one SubConn, so the
  143. // address doesn't matter. All states updating (up and down)
  144. // and picking should all happen on that only SubConn.
  145. bw.conns[resolver.Address{}] = sc
  146. bw.connSt[sc] = &scState{
  147. addr: addrs[0], // Use the first address.
  148. s: connectivity.Idle,
  149. }
  150. bw.mu.Unlock()
  151. sc.Connect()
  152. }
  153. } else {
  154. oldSC.UpdateAddresses(newAddrs)
  155. bw.mu.Lock()
  156. bw.connSt[oldSC].addr = addrs[0]
  157. bw.mu.Unlock()
  158. }
  159. } else {
  160. var (
  161. add []resolver.Address // Addresses need to setup connections.
  162. del []balancer.SubConn // Connections need to tear down.
  163. )
  164. resAddrs := make(map[resolver.Address]Address)
  165. for _, a := range addrs {
  166. resAddrs[resolver.Address{
  167. Addr: a.Addr,
  168. Type: resolver.Backend, // All addresses from balancer are all backends.
  169. ServerName: "",
  170. Metadata: a.Metadata,
  171. }] = a
  172. }
  173. bw.mu.Lock()
  174. for a := range resAddrs {
  175. if _, ok := bw.conns[a]; !ok {
  176. add = append(add, a)
  177. }
  178. }
  179. for a, c := range bw.conns {
  180. if _, ok := resAddrs[a]; !ok {
  181. del = append(del, c)
  182. delete(bw.conns, a)
  183. // Keep the state of this sc in bw.connSt until its state becomes Shutdown.
  184. }
  185. }
  186. bw.mu.Unlock()
  187. for _, a := range add {
  188. sc, err := bw.cc.NewSubConn([]resolver.Address{a}, balancer.NewSubConnOptions{})
  189. if err != nil {
  190. grpclog.Warningf("Error creating connection to %v. Err: %v", a, err)
  191. } else {
  192. bw.mu.Lock()
  193. bw.conns[a] = sc
  194. bw.connSt[sc] = &scState{
  195. addr: resAddrs[a],
  196. s: connectivity.Idle,
  197. }
  198. bw.mu.Unlock()
  199. sc.Connect()
  200. }
  201. }
  202. for _, c := range del {
  203. bw.cc.RemoveSubConn(c)
  204. }
  205. }
  206. }
  207. }
  208. func (bw *balancerWrapper) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
  209. grpclog.Infof("balancerWrapper: handle subconn state change: %p, %v", sc, s)
  210. bw.mu.Lock()
  211. defer bw.mu.Unlock()
  212. scSt, ok := bw.connSt[sc]
  213. if !ok {
  214. return
  215. }
  216. if s == connectivity.Idle {
  217. sc.Connect()
  218. }
  219. oldS := scSt.s
  220. scSt.s = s
  221. if oldS != connectivity.Ready && s == connectivity.Ready {
  222. scSt.down = bw.balancer.Up(scSt.addr)
  223. } else if oldS == connectivity.Ready && s != connectivity.Ready {
  224. if scSt.down != nil {
  225. scSt.down(errConnClosing)
  226. }
  227. }
  228. sa := bw.csEvltr.recordTransition(oldS, s)
  229. if bw.state != sa {
  230. bw.state = sa
  231. }
  232. bw.cc.UpdateBalancerState(bw.state, bw)
  233. if s == connectivity.Shutdown {
  234. // Remove state for this sc.
  235. delete(bw.connSt, sc)
  236. }
  237. return
  238. }
  239. func (bw *balancerWrapper) HandleResolvedAddrs([]resolver.Address, error) {
  240. bw.mu.Lock()
  241. defer bw.mu.Unlock()
  242. select {
  243. case <-bw.startCh:
  244. default:
  245. close(bw.startCh)
  246. }
  247. // There should be a resolver inside the balancer.
  248. // All updates here, if any, are ignored.
  249. return
  250. }
  251. func (bw *balancerWrapper) Close() {
  252. bw.mu.Lock()
  253. defer bw.mu.Unlock()
  254. select {
  255. case <-bw.startCh:
  256. default:
  257. close(bw.startCh)
  258. }
  259. bw.balancer.Close()
  260. return
  261. }
  262. // The picker is the balancerWrapper itself.
  263. // Pick should never return ErrNoSubConnAvailable.
  264. // It either blocks or returns error, consistent with v1 balancer Get().
  265. func (bw *balancerWrapper) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
  266. failfast := true // Default failfast is true.
  267. if ss, ok := rpcInfoFromContext(ctx); ok {
  268. failfast = ss.failfast
  269. }
  270. a, p, err := bw.balancer.Get(ctx, BalancerGetOptions{BlockingWait: !failfast})
  271. if err != nil {
  272. return nil, nil, err
  273. }
  274. var done func(balancer.DoneInfo)
  275. if p != nil {
  276. done = func(i balancer.DoneInfo) { p() }
  277. }
  278. var sc balancer.SubConn
  279. bw.mu.Lock()
  280. defer bw.mu.Unlock()
  281. if bw.pickfirst {
  282. // Get the first sc in conns.
  283. for _, sc = range bw.conns {
  284. break
  285. }
  286. } else {
  287. var ok bool
  288. sc, ok = bw.conns[resolver.Address{
  289. Addr: a.Addr,
  290. Type: resolver.Backend,
  291. ServerName: "",
  292. Metadata: a.Metadata,
  293. }]
  294. if !ok && failfast {
  295. return nil, nil, Errorf(codes.Unavailable, "there is no connection available")
  296. }
  297. if s, ok := bw.connSt[sc]; failfast && (!ok || s.s != connectivity.Ready) {
  298. // If the returned sc is not ready and RPC is failfast,
  299. // return error, and this RPC will fail.
  300. return nil, nil, Errorf(codes.Unavailable, "there is no connection available")
  301. }
  302. }
  303. return sc, done, nil
  304. }
  305. // connectivityStateEvaluator gets updated by addrConns when their
  306. // states transition, based on which it evaluates the state of
  307. // ClientConn.
  308. type connectivityStateEvaluator struct {
  309. mu sync.Mutex
  310. numReady uint64 // Number of addrConns in ready state.
  311. numConnecting uint64 // Number of addrConns in connecting state.
  312. numTransientFailure uint64 // Number of addrConns in transientFailure.
  313. }
  314. // recordTransition records state change happening in every subConn and based on
  315. // that it evaluates what aggregated state should be.
  316. // It can only transition between Ready, Connecting and TransientFailure. Other states,
  317. // Idle and Shutdown are transitioned into by ClientConn; in the beginning of the connection
  318. // before any subConn is created ClientConn is in idle state. In the end when ClientConn
  319. // closes it is in Shutdown state.
  320. // TODO Note that in later releases, a ClientConn with no activity will be put into an Idle state.
  321. func (cse *connectivityStateEvaluator) recordTransition(oldState, newState connectivity.State) connectivity.State {
  322. cse.mu.Lock()
  323. defer cse.mu.Unlock()
  324. // Update counters.
  325. for idx, state := range []connectivity.State{oldState, newState} {
  326. updateVal := 2*uint64(idx) - 1 // -1 for oldState and +1 for new.
  327. switch state {
  328. case connectivity.Ready:
  329. cse.numReady += updateVal
  330. case connectivity.Connecting:
  331. cse.numConnecting += updateVal
  332. case connectivity.TransientFailure:
  333. cse.numTransientFailure += updateVal
  334. }
  335. }
  336. // Evaluate.
  337. if cse.numReady > 0 {
  338. return connectivity.Ready
  339. }
  340. if cse.numConnecting > 0 {
  341. return connectivity.Connecting
  342. }
  343. return connectivity.TransientFailure
  344. }