balancer.go 9.6 KB

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