balancer.go 9.8 KB

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