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