balancer.go 10 KB

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