balancer.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clientv3
  15. import (
  16. "context"
  17. "net/url"
  18. "strings"
  19. "sync"
  20. "google.golang.org/grpc"
  21. "google.golang.org/grpc/codes"
  22. "google.golang.org/grpc/status"
  23. )
  24. // ErrNoAddrAvilable is returned by Get() when the balancer does not have
  25. // any active connection to endpoints at the time.
  26. // This error is returned only when opts.BlockingWait is true.
  27. var ErrNoAddrAvilable = status.Error(codes.Unavailable, "there is no address available")
  28. type notifyMsg int
  29. const (
  30. notifyReset notifyMsg = iota
  31. notifyNext
  32. )
  33. // simpleBalancer does the bare minimum to expose multiple eps
  34. // to the grpc reconnection code path
  35. type simpleBalancer struct {
  36. // addrs are the client's endpoint addresses for grpc
  37. addrs []grpc.Address
  38. // eps holds the raw endpoints from the client
  39. eps []string
  40. // notifyCh notifies grpc of the set of addresses for connecting
  41. notifyCh chan []grpc.Address
  42. // readyc closes once the first connection is up
  43. readyc chan struct{}
  44. readyOnce sync.Once
  45. // mu protects all fields below.
  46. mu sync.RWMutex
  47. // upc closes when pinAddr transitions from empty to non-empty or the balancer closes.
  48. upc chan struct{}
  49. // downc closes when grpc calls down() on pinAddr
  50. downc chan struct{}
  51. // stopc is closed to signal updateNotifyLoop should stop.
  52. stopc chan struct{}
  53. // donec closes when all goroutines are exited
  54. donec chan struct{}
  55. // updateAddrsC notifies updateNotifyLoop to update addrs.
  56. updateAddrsC chan notifyMsg
  57. // grpc issues TLS cert checks using the string passed into dial so
  58. // that string must be the host. To recover the full scheme://host URL,
  59. // have a map from hosts to the original endpoint.
  60. hostPort2ep map[string]string
  61. // pinAddr is the currently pinned address; set to the empty string on
  62. // initialization and shutdown.
  63. pinAddr string
  64. closed bool
  65. }
  66. func newSimpleBalancer(eps []string) *simpleBalancer {
  67. notifyCh := make(chan []grpc.Address)
  68. addrs := eps2addrs(eps)
  69. sb := &simpleBalancer{
  70. addrs: addrs,
  71. eps: eps,
  72. notifyCh: notifyCh,
  73. readyc: make(chan struct{}),
  74. upc: make(chan struct{}),
  75. stopc: make(chan struct{}),
  76. downc: make(chan struct{}),
  77. donec: make(chan struct{}),
  78. updateAddrsC: make(chan notifyMsg),
  79. hostPort2ep: getHostPort2ep(eps),
  80. }
  81. close(sb.downc)
  82. go sb.updateNotifyLoop()
  83. return sb
  84. }
  85. func (b *simpleBalancer) Start(target string, config grpc.BalancerConfig) error { return nil }
  86. func (b *simpleBalancer) ConnectNotify() <-chan struct{} {
  87. b.mu.Lock()
  88. defer b.mu.Unlock()
  89. return b.upc
  90. }
  91. func (b *simpleBalancer) ready() <-chan struct{} { return b.readyc }
  92. func (b *simpleBalancer) endpoint(hostPort string) string {
  93. b.mu.Lock()
  94. defer b.mu.Unlock()
  95. return b.hostPort2ep[hostPort]
  96. }
  97. func (b *simpleBalancer) endpoints() []string {
  98. b.mu.RLock()
  99. defer b.mu.RUnlock()
  100. return b.eps
  101. }
  102. func (b *simpleBalancer) pinned() string {
  103. b.mu.RLock()
  104. defer b.mu.RUnlock()
  105. return b.pinAddr
  106. }
  107. func getHostPort2ep(eps []string) map[string]string {
  108. hm := make(map[string]string, len(eps))
  109. for i := range eps {
  110. _, host, _ := parseEndpoint(eps[i])
  111. hm[host] = eps[i]
  112. }
  113. return hm
  114. }
  115. func (b *simpleBalancer) updateAddrs(eps ...string) {
  116. np := getHostPort2ep(eps)
  117. b.mu.Lock()
  118. match := len(np) == len(b.hostPort2ep)
  119. for k, v := range np {
  120. if b.hostPort2ep[k] != v {
  121. match = false
  122. break
  123. }
  124. }
  125. if match {
  126. // same endpoints, so no need to update address
  127. b.mu.Unlock()
  128. return
  129. }
  130. b.hostPort2ep = np
  131. b.addrs, b.eps = eps2addrs(eps), eps
  132. // updating notifyCh can trigger new connections,
  133. // only update addrs if all connections are down
  134. // or addrs does not include pinAddr.
  135. update := !hasAddr(b.addrs, b.pinAddr)
  136. b.mu.Unlock()
  137. if update {
  138. select {
  139. case b.updateAddrsC <- notifyNext:
  140. case <-b.stopc:
  141. }
  142. }
  143. }
  144. func (b *simpleBalancer) next() {
  145. b.mu.RLock()
  146. downc := b.downc
  147. b.mu.RUnlock()
  148. select {
  149. case b.updateAddrsC <- notifyNext:
  150. case <-b.stopc:
  151. }
  152. // wait until disconnect so new RPCs are not issued on old connection
  153. select {
  154. case <-downc:
  155. case <-b.stopc:
  156. }
  157. }
  158. func hasAddr(addrs []grpc.Address, targetAddr string) bool {
  159. for _, addr := range addrs {
  160. if targetAddr == addr.Addr {
  161. return true
  162. }
  163. }
  164. return false
  165. }
  166. func (b *simpleBalancer) updateNotifyLoop() {
  167. defer close(b.donec)
  168. for {
  169. b.mu.RLock()
  170. upc, downc, addr := b.upc, b.downc, b.pinAddr
  171. b.mu.RUnlock()
  172. // downc or upc should be closed
  173. select {
  174. case <-downc:
  175. downc = nil
  176. default:
  177. }
  178. select {
  179. case <-upc:
  180. upc = nil
  181. default:
  182. }
  183. switch {
  184. case downc == nil && upc == nil:
  185. // stale
  186. select {
  187. case <-b.stopc:
  188. return
  189. default:
  190. }
  191. case downc == nil:
  192. b.notifyAddrs(notifyReset)
  193. select {
  194. case <-upc:
  195. case msg := <-b.updateAddrsC:
  196. b.notifyAddrs(msg)
  197. case <-b.stopc:
  198. return
  199. }
  200. case upc == nil:
  201. select {
  202. // close connections that are not the pinned address
  203. case b.notifyCh <- []grpc.Address{{Addr: addr}}:
  204. case <-downc:
  205. case <-b.stopc:
  206. return
  207. }
  208. select {
  209. case <-downc:
  210. b.notifyAddrs(notifyReset)
  211. case msg := <-b.updateAddrsC:
  212. b.notifyAddrs(msg)
  213. case <-b.stopc:
  214. return
  215. }
  216. }
  217. }
  218. }
  219. func (b *simpleBalancer) notifyAddrs(msg notifyMsg) {
  220. if msg == notifyNext {
  221. select {
  222. case b.notifyCh <- []grpc.Address{}:
  223. case <-b.stopc:
  224. return
  225. }
  226. }
  227. b.mu.RLock()
  228. addrs := b.addrs
  229. pinAddr := b.pinAddr
  230. downc := b.downc
  231. b.mu.RUnlock()
  232. var waitDown bool
  233. if pinAddr != "" {
  234. waitDown = true
  235. for _, a := range addrs {
  236. if a.Addr == pinAddr {
  237. waitDown = false
  238. }
  239. }
  240. }
  241. select {
  242. case b.notifyCh <- addrs:
  243. if waitDown {
  244. select {
  245. case <-downc:
  246. case <-b.stopc:
  247. }
  248. }
  249. case <-b.stopc:
  250. }
  251. }
  252. func (b *simpleBalancer) Up(addr grpc.Address) func(error) {
  253. f, _ := b.up(addr)
  254. return f
  255. }
  256. func (b *simpleBalancer) up(addr grpc.Address) (func(error), bool) {
  257. b.mu.Lock()
  258. defer b.mu.Unlock()
  259. // gRPC might call Up after it called Close. We add this check
  260. // to "fix" it up at application layer. Otherwise, will panic
  261. // if b.upc is already closed.
  262. if b.closed {
  263. return func(err error) {}, false
  264. }
  265. // gRPC might call Up on a stale address.
  266. // Prevent updating pinAddr with a stale address.
  267. if !hasAddr(b.addrs, addr.Addr) {
  268. return func(err error) {}, false
  269. }
  270. if b.pinAddr != "" {
  271. if logger.V(4) {
  272. logger.Infof("clientv3/balancer: %q is up but not pinned (already pinned %q)", addr.Addr, b.pinAddr)
  273. }
  274. return func(err error) {}, false
  275. }
  276. // notify waiting Get()s and pin first connected address
  277. close(b.upc)
  278. b.downc = make(chan struct{})
  279. b.pinAddr = addr.Addr
  280. if logger.V(4) {
  281. logger.Infof("clientv3/balancer: pin %q", addr.Addr)
  282. }
  283. // notify client that a connection is up
  284. b.readyOnce.Do(func() { close(b.readyc) })
  285. return func(err error) {
  286. b.mu.Lock()
  287. b.upc = make(chan struct{})
  288. close(b.downc)
  289. b.pinAddr = ""
  290. b.mu.Unlock()
  291. if logger.V(4) {
  292. logger.Infof("clientv3/balancer: unpin %q (%q)", addr.Addr, err.Error())
  293. }
  294. }, true
  295. }
  296. func (b *simpleBalancer) Get(ctx context.Context, opts grpc.BalancerGetOptions) (grpc.Address, func(), error) {
  297. var (
  298. addr string
  299. closed bool
  300. )
  301. // If opts.BlockingWait is false (for fail-fast RPCs), it should return
  302. // an address it has notified via Notify immediately instead of blocking.
  303. if !opts.BlockingWait {
  304. b.mu.RLock()
  305. closed = b.closed
  306. addr = b.pinAddr
  307. b.mu.RUnlock()
  308. if closed {
  309. return grpc.Address{Addr: ""}, nil, grpc.ErrClientConnClosing
  310. }
  311. if addr == "" {
  312. return grpc.Address{Addr: ""}, nil, ErrNoAddrAvilable
  313. }
  314. return grpc.Address{Addr: addr}, func() {}, nil
  315. }
  316. for {
  317. b.mu.RLock()
  318. ch := b.upc
  319. b.mu.RUnlock()
  320. select {
  321. case <-ch:
  322. case <-b.donec:
  323. return grpc.Address{Addr: ""}, nil, grpc.ErrClientConnClosing
  324. case <-ctx.Done():
  325. return grpc.Address{Addr: ""}, nil, ctx.Err()
  326. }
  327. b.mu.RLock()
  328. closed = b.closed
  329. addr = b.pinAddr
  330. b.mu.RUnlock()
  331. // Close() which sets b.closed = true can be called before Get(), Get() must exit if balancer is closed.
  332. if closed {
  333. return grpc.Address{Addr: ""}, nil, grpc.ErrClientConnClosing
  334. }
  335. if addr != "" {
  336. break
  337. }
  338. }
  339. return grpc.Address{Addr: addr}, func() {}, nil
  340. }
  341. func (b *simpleBalancer) Notify() <-chan []grpc.Address { return b.notifyCh }
  342. func (b *simpleBalancer) Close() error {
  343. b.mu.Lock()
  344. // In case gRPC calls close twice. TODO: remove the checking
  345. // when we are sure that gRPC wont call close twice.
  346. if b.closed {
  347. b.mu.Unlock()
  348. <-b.donec
  349. return nil
  350. }
  351. b.closed = true
  352. close(b.stopc)
  353. b.pinAddr = ""
  354. // In the case of following scenario:
  355. // 1. upc is not closed; no pinned address
  356. // 2. client issues an RPC, calling invoke(), which calls Get(), enters for loop, blocks
  357. // 3. client.conn.Close() calls balancer.Close(); closed = true
  358. // 4. for loop in Get() never exits since ctx is the context passed in by the client and may not be canceled
  359. // we must close upc so Get() exits from blocking on upc
  360. select {
  361. case <-b.upc:
  362. default:
  363. // terminate all waiting Get()s
  364. close(b.upc)
  365. }
  366. b.mu.Unlock()
  367. // wait for updateNotifyLoop to finish
  368. <-b.donec
  369. close(b.notifyCh)
  370. return nil
  371. }
  372. func getHost(ep string) string {
  373. url, uerr := url.Parse(ep)
  374. if uerr != nil || !strings.Contains(ep, "://") {
  375. return ep
  376. }
  377. return url.Host
  378. }
  379. func eps2addrs(eps []string) []grpc.Address {
  380. addrs := make([]grpc.Address, len(eps))
  381. for i := range eps {
  382. addrs[i].Addr = getHost(eps[i])
  383. }
  384. return addrs
  385. }