watch.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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. "fmt"
  18. "sync"
  19. "time"
  20. v3rpc "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  21. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  22. mvccpb "github.com/coreos/etcd/mvcc/mvccpb"
  23. "google.golang.org/grpc"
  24. "google.golang.org/grpc/codes"
  25. "google.golang.org/grpc/metadata"
  26. "google.golang.org/grpc/status"
  27. )
  28. const (
  29. EventTypeDelete = mvccpb.DELETE
  30. EventTypePut = mvccpb.PUT
  31. closeSendErrTimeout = 250 * time.Millisecond
  32. )
  33. type Event mvccpb.Event
  34. type WatchChan <-chan WatchResponse
  35. type Watcher interface {
  36. // Watch watches on a key or prefix. The watched events will be returned
  37. // through the returned channel. If revisions waiting to be sent over the
  38. // watch are compacted, then the watch will be canceled by the server, the
  39. // client will post a compacted error watch response, and the channel will close.
  40. // If the context "ctx" is canceled or timed out, returned "WatchChan" is closed,
  41. // and "WatchResponse" from this closed channel has zero events and nil "Err()".
  42. // If the context is "context.Background/TODO", returned "WatchChan" will not be closed
  43. // and wait until events happen, except when server returns a non-recoverable error.
  44. // For example, when context passed with "WithRequireLeader" and the connected server
  45. // has no leader, error "etcdserver: no leader" is returned, and then "WatchChan" is
  46. // closed with non-nil "Err()".
  47. // Otherwise, as long as the context has not been canceled or timed out, watch will
  48. // retry on other recoverable errors forever until reconnected.
  49. //
  50. // TODO: explicitly set context error in the last "WatchResponse" message and close channel?
  51. // Currently, client contexts are overwritten with "valCtx" that never closes.
  52. // TODO(v3.4): configure watch retry policy, limit maximum retry number
  53. // (see https://github.com/coreos/etcd/issues/8980)
  54. Watch(ctx context.Context, key string, opts ...OpOption) WatchChan
  55. // Close closes the watcher and cancels all watch requests.
  56. Close() error
  57. }
  58. type WatchResponse struct {
  59. Header pb.ResponseHeader
  60. Events []*Event
  61. // CompactRevision is the minimum revision the watcher may receive.
  62. CompactRevision int64
  63. // Canceled is used to indicate watch failure.
  64. // If the watch failed and the stream was about to close, before the channel is closed,
  65. // the channel sends a final response that has Canceled set to true with a non-nil Err().
  66. Canceled bool
  67. // Created is used to indicate the creation of the watcher.
  68. Created bool
  69. closeErr error
  70. // cancelReason is a reason of canceling watch
  71. cancelReason string
  72. }
  73. // IsCreate returns true if the event tells that the key is newly created.
  74. func (e *Event) IsCreate() bool {
  75. return e.Type == EventTypePut && e.Kv.CreateRevision == e.Kv.ModRevision
  76. }
  77. // IsModify returns true if the event tells that a new value is put on existing key.
  78. func (e *Event) IsModify() bool {
  79. return e.Type == EventTypePut && e.Kv.CreateRevision != e.Kv.ModRevision
  80. }
  81. // Err is the error value if this WatchResponse holds an error.
  82. func (wr *WatchResponse) Err() error {
  83. switch {
  84. case wr.closeErr != nil:
  85. return v3rpc.Error(wr.closeErr)
  86. case wr.CompactRevision != 0:
  87. return v3rpc.ErrCompacted
  88. case wr.Canceled:
  89. if len(wr.cancelReason) != 0 {
  90. return v3rpc.Error(status.Error(codes.FailedPrecondition, wr.cancelReason))
  91. }
  92. return v3rpc.ErrFutureRev
  93. }
  94. return nil
  95. }
  96. // IsProgressNotify returns true if the WatchResponse is progress notification.
  97. func (wr *WatchResponse) IsProgressNotify() bool {
  98. return len(wr.Events) == 0 && !wr.Canceled && !wr.Created && wr.CompactRevision == 0 && wr.Header.Revision != 0
  99. }
  100. // watcher implements the Watcher interface
  101. type watcher struct {
  102. remote pb.WatchClient
  103. callOpts []grpc.CallOption
  104. // mu protects the grpc streams map
  105. mu sync.RWMutex
  106. // streams holds all the active grpc streams keyed by ctx value.
  107. streams map[string]*watchGrpcStream
  108. }
  109. // watchGrpcStream tracks all watch resources attached to a single grpc stream.
  110. type watchGrpcStream struct {
  111. owner *watcher
  112. remote pb.WatchClient
  113. callOpts []grpc.CallOption
  114. // ctx controls internal remote.Watch requests
  115. ctx context.Context
  116. // ctxKey is the key used when looking up this stream's context
  117. ctxKey string
  118. cancel context.CancelFunc
  119. // substreams holds all active watchers on this grpc stream
  120. substreams map[int64]*watcherStream
  121. // resuming holds all resuming watchers on this grpc stream
  122. resuming []*watcherStream
  123. // reqc sends a watch request from Watch() to the main goroutine
  124. reqc chan *watchRequest
  125. // respc receives data from the watch client
  126. respc chan *pb.WatchResponse
  127. // donec closes to broadcast shutdown
  128. donec chan struct{}
  129. // errc transmits errors from grpc Recv to the watch stream reconnect logic
  130. errc chan error
  131. // closingc gets the watcherStream of closing watchers
  132. closingc chan *watcherStream
  133. // wg is Done when all substream goroutines have exited
  134. wg sync.WaitGroup
  135. // resumec closes to signal that all substreams should begin resuming
  136. resumec chan struct{}
  137. // closeErr is the error that closed the watch stream
  138. closeErr error
  139. }
  140. // watchRequest is issued by the subscriber to start a new watcher
  141. type watchRequest struct {
  142. ctx context.Context
  143. key string
  144. end string
  145. rev int64
  146. // send created notification event if this field is true
  147. createdNotify bool
  148. // progressNotify is for progress updates
  149. progressNotify bool
  150. // filters is the list of events to filter out
  151. filters []pb.WatchCreateRequest_FilterType
  152. // get the previous key-value pair before the event happens
  153. prevKV bool
  154. // retc receives a chan WatchResponse once the watcher is established
  155. retc chan chan WatchResponse
  156. }
  157. // watcherStream represents a registered watcher
  158. type watcherStream struct {
  159. // initReq is the request that initiated this request
  160. initReq watchRequest
  161. // outc publishes watch responses to subscriber
  162. outc chan WatchResponse
  163. // recvc buffers watch responses before publishing
  164. recvc chan *WatchResponse
  165. // donec closes when the watcherStream goroutine stops.
  166. donec chan struct{}
  167. // closing is set to true when stream should be scheduled to shutdown.
  168. closing bool
  169. // id is the registered watch id on the grpc stream
  170. id int64
  171. // buf holds all events received from etcd but not yet consumed by the client
  172. buf []*WatchResponse
  173. }
  174. func NewWatcher(c *Client) Watcher {
  175. return NewWatchFromWatchClient(pb.NewWatchClient(c.conn), c)
  176. }
  177. func NewWatchFromWatchClient(wc pb.WatchClient, c *Client) Watcher {
  178. w := &watcher{
  179. remote: wc,
  180. streams: make(map[string]*watchGrpcStream),
  181. }
  182. if c != nil {
  183. w.callOpts = c.callOpts
  184. }
  185. return w
  186. }
  187. // never closes
  188. var valCtxCh = make(chan struct{})
  189. var zeroTime = time.Unix(0, 0)
  190. // ctx with only the values; never Done
  191. type valCtx struct{ context.Context }
  192. func (vc *valCtx) Deadline() (time.Time, bool) { return zeroTime, false }
  193. func (vc *valCtx) Done() <-chan struct{} { return valCtxCh }
  194. func (vc *valCtx) Err() error { return nil }
  195. func (w *watcher) newWatcherGrpcStream(inctx context.Context) *watchGrpcStream {
  196. ctx, cancel := context.WithCancel(&valCtx{inctx})
  197. wgs := &watchGrpcStream{
  198. owner: w,
  199. remote: w.remote,
  200. callOpts: w.callOpts,
  201. ctx: ctx,
  202. ctxKey: streamKeyFromCtx(inctx),
  203. cancel: cancel,
  204. substreams: make(map[int64]*watcherStream),
  205. respc: make(chan *pb.WatchResponse),
  206. reqc: make(chan *watchRequest),
  207. donec: make(chan struct{}),
  208. errc: make(chan error, 1),
  209. closingc: make(chan *watcherStream),
  210. resumec: make(chan struct{}),
  211. }
  212. go wgs.run()
  213. return wgs
  214. }
  215. // Watch posts a watch request to run() and waits for a new watcher channel
  216. func (w *watcher) Watch(ctx context.Context, key string, opts ...OpOption) WatchChan {
  217. ow := opWatch(key, opts...)
  218. var filters []pb.WatchCreateRequest_FilterType
  219. if ow.filterPut {
  220. filters = append(filters, pb.WatchCreateRequest_NOPUT)
  221. }
  222. if ow.filterDelete {
  223. filters = append(filters, pb.WatchCreateRequest_NODELETE)
  224. }
  225. wr := &watchRequest{
  226. ctx: ctx,
  227. createdNotify: ow.createdNotify,
  228. key: string(ow.key),
  229. end: string(ow.end),
  230. rev: ow.rev,
  231. progressNotify: ow.progressNotify,
  232. filters: filters,
  233. prevKV: ow.prevKV,
  234. retc: make(chan chan WatchResponse, 1),
  235. }
  236. ok := false
  237. ctxKey := streamKeyFromCtx(ctx)
  238. // find or allocate appropriate grpc watch stream
  239. w.mu.Lock()
  240. if w.streams == nil {
  241. // closed
  242. w.mu.Unlock()
  243. ch := make(chan WatchResponse)
  244. close(ch)
  245. return ch
  246. }
  247. wgs := w.streams[ctxKey]
  248. if wgs == nil {
  249. wgs = w.newWatcherGrpcStream(ctx)
  250. w.streams[ctxKey] = wgs
  251. }
  252. donec := wgs.donec
  253. reqc := wgs.reqc
  254. w.mu.Unlock()
  255. // couldn't create channel; return closed channel
  256. closeCh := make(chan WatchResponse, 1)
  257. // submit request
  258. select {
  259. case reqc <- wr:
  260. ok = true
  261. case <-wr.ctx.Done():
  262. case <-donec:
  263. if wgs.closeErr != nil {
  264. closeCh <- WatchResponse{closeErr: wgs.closeErr}
  265. break
  266. }
  267. // retry; may have dropped stream from no ctxs
  268. return w.Watch(ctx, key, opts...)
  269. }
  270. // receive channel
  271. if ok {
  272. select {
  273. case ret := <-wr.retc:
  274. return ret
  275. case <-ctx.Done():
  276. case <-donec:
  277. if wgs.closeErr != nil {
  278. closeCh <- WatchResponse{closeErr: wgs.closeErr}
  279. break
  280. }
  281. // retry; may have dropped stream from no ctxs
  282. return w.Watch(ctx, key, opts...)
  283. }
  284. }
  285. close(closeCh)
  286. return closeCh
  287. }
  288. func (w *watcher) Close() (err error) {
  289. w.mu.Lock()
  290. streams := w.streams
  291. w.streams = nil
  292. w.mu.Unlock()
  293. for _, wgs := range streams {
  294. if werr := wgs.close(); werr != nil {
  295. err = werr
  296. }
  297. }
  298. return err
  299. }
  300. func (w *watchGrpcStream) close() (err error) {
  301. w.cancel()
  302. <-w.donec
  303. select {
  304. case err = <-w.errc:
  305. default:
  306. }
  307. return toErr(w.ctx, err)
  308. }
  309. func (w *watcher) closeStream(wgs *watchGrpcStream) {
  310. w.mu.Lock()
  311. close(wgs.donec)
  312. wgs.cancel()
  313. if w.streams != nil {
  314. delete(w.streams, wgs.ctxKey)
  315. }
  316. w.mu.Unlock()
  317. }
  318. func (w *watchGrpcStream) addSubstream(resp *pb.WatchResponse, ws *watcherStream) {
  319. // check watch ID for backward compatibility (<= v3.3)
  320. if resp.WatchId == -1 || (resp.Canceled && resp.CancelReason != "") {
  321. // failed; no channel
  322. close(ws.recvc)
  323. return
  324. }
  325. ws.id = resp.WatchId
  326. w.substreams[ws.id] = ws
  327. }
  328. func (w *watchGrpcStream) sendCloseSubstream(ws *watcherStream, resp *WatchResponse) {
  329. select {
  330. case ws.outc <- *resp:
  331. case <-ws.initReq.ctx.Done():
  332. case <-time.After(closeSendErrTimeout):
  333. }
  334. close(ws.outc)
  335. }
  336. func (w *watchGrpcStream) closeSubstream(ws *watcherStream) {
  337. // send channel response in case stream was never established
  338. select {
  339. case ws.initReq.retc <- ws.outc:
  340. default:
  341. }
  342. // close subscriber's channel
  343. if closeErr := w.closeErr; closeErr != nil && ws.initReq.ctx.Err() == nil {
  344. go w.sendCloseSubstream(ws, &WatchResponse{closeErr: w.closeErr})
  345. } else if ws.outc != nil {
  346. close(ws.outc)
  347. }
  348. if ws.id != -1 {
  349. delete(w.substreams, ws.id)
  350. return
  351. }
  352. for i := range w.resuming {
  353. if w.resuming[i] == ws {
  354. w.resuming[i] = nil
  355. return
  356. }
  357. }
  358. }
  359. // run is the root of the goroutines for managing a watcher client
  360. func (w *watchGrpcStream) run() {
  361. var wc pb.Watch_WatchClient
  362. var closeErr error
  363. // substreams marked to close but goroutine still running; needed for
  364. // avoiding double-closing recvc on grpc stream teardown
  365. closing := make(map[*watcherStream]struct{})
  366. defer func() {
  367. w.closeErr = closeErr
  368. // shutdown substreams and resuming substreams
  369. for _, ws := range w.substreams {
  370. if _, ok := closing[ws]; !ok {
  371. close(ws.recvc)
  372. closing[ws] = struct{}{}
  373. }
  374. }
  375. for _, ws := range w.resuming {
  376. if _, ok := closing[ws]; ws != nil && !ok {
  377. close(ws.recvc)
  378. closing[ws] = struct{}{}
  379. }
  380. }
  381. w.joinSubstreams()
  382. for range closing {
  383. w.closeSubstream(<-w.closingc)
  384. }
  385. w.wg.Wait()
  386. w.owner.closeStream(w)
  387. }()
  388. // start a stream with the etcd grpc server
  389. if wc, closeErr = w.newWatchClient(); closeErr != nil {
  390. return
  391. }
  392. cancelSet := make(map[int64]struct{})
  393. for {
  394. select {
  395. // Watch() requested
  396. case wreq := <-w.reqc:
  397. outc := make(chan WatchResponse, 1)
  398. // TODO: pass custom watch ID?
  399. ws := &watcherStream{
  400. initReq: *wreq,
  401. id: -1,
  402. outc: outc,
  403. // unbuffered so resumes won't cause repeat events
  404. recvc: make(chan *WatchResponse),
  405. }
  406. ws.donec = make(chan struct{})
  407. w.wg.Add(1)
  408. go w.serveSubstream(ws, w.resumec)
  409. // queue up for watcher creation/resume
  410. w.resuming = append(w.resuming, ws)
  411. if len(w.resuming) == 1 {
  412. // head of resume queue, can register a new watcher
  413. wc.Send(ws.initReq.toPB())
  414. }
  415. // New events from the watch client
  416. case pbresp := <-w.respc:
  417. switch {
  418. case pbresp.Created:
  419. // response to head of queue creation
  420. if ws := w.resuming[0]; ws != nil {
  421. w.addSubstream(pbresp, ws)
  422. w.dispatchEvent(pbresp)
  423. w.resuming[0] = nil
  424. }
  425. if ws := w.nextResume(); ws != nil {
  426. wc.Send(ws.initReq.toPB())
  427. }
  428. case pbresp.Canceled && pbresp.CompactRevision == 0:
  429. delete(cancelSet, pbresp.WatchId)
  430. if ws, ok := w.substreams[pbresp.WatchId]; ok {
  431. // signal to stream goroutine to update closingc
  432. close(ws.recvc)
  433. closing[ws] = struct{}{}
  434. }
  435. default:
  436. // dispatch to appropriate watch stream
  437. if ok := w.dispatchEvent(pbresp); ok {
  438. break
  439. }
  440. // watch response on unexpected watch id; cancel id
  441. if _, ok := cancelSet[pbresp.WatchId]; ok {
  442. break
  443. }
  444. cancelSet[pbresp.WatchId] = struct{}{}
  445. cr := &pb.WatchRequest_CancelRequest{
  446. CancelRequest: &pb.WatchCancelRequest{
  447. WatchId: pbresp.WatchId,
  448. },
  449. }
  450. req := &pb.WatchRequest{RequestUnion: cr}
  451. wc.Send(req)
  452. }
  453. // watch client failed on Recv; spawn another if possible
  454. case err := <-w.errc:
  455. if isHaltErr(w.ctx, err) || toErr(w.ctx, err) == v3rpc.ErrNoLeader {
  456. closeErr = err
  457. return
  458. }
  459. if wc, closeErr = w.newWatchClient(); closeErr != nil {
  460. return
  461. }
  462. if ws := w.nextResume(); ws != nil {
  463. wc.Send(ws.initReq.toPB())
  464. }
  465. cancelSet = make(map[int64]struct{})
  466. case <-w.ctx.Done():
  467. return
  468. case ws := <-w.closingc:
  469. w.closeSubstream(ws)
  470. delete(closing, ws)
  471. if len(w.substreams)+len(w.resuming) == 0 {
  472. // no more watchers on this stream, shutdown
  473. return
  474. }
  475. }
  476. }
  477. }
  478. // nextResume chooses the next resuming to register with the grpc stream. Abandoned
  479. // streams are marked as nil in the queue since the head must wait for its inflight registration.
  480. func (w *watchGrpcStream) nextResume() *watcherStream {
  481. for len(w.resuming) != 0 {
  482. if w.resuming[0] != nil {
  483. return w.resuming[0]
  484. }
  485. w.resuming = w.resuming[1:len(w.resuming)]
  486. }
  487. return nil
  488. }
  489. // dispatchEvent sends a WatchResponse to the appropriate watcher stream
  490. func (w *watchGrpcStream) dispatchEvent(pbresp *pb.WatchResponse) bool {
  491. events := make([]*Event, len(pbresp.Events))
  492. for i, ev := range pbresp.Events {
  493. events[i] = (*Event)(ev)
  494. }
  495. // TODO: return watch ID?
  496. wr := &WatchResponse{
  497. Header: *pbresp.Header,
  498. Events: events,
  499. CompactRevision: pbresp.CompactRevision,
  500. Created: pbresp.Created,
  501. Canceled: pbresp.Canceled,
  502. cancelReason: pbresp.CancelReason,
  503. }
  504. ws, ok := w.substreams[pbresp.WatchId]
  505. if !ok {
  506. return false
  507. }
  508. select {
  509. case ws.recvc <- wr:
  510. case <-ws.donec:
  511. return false
  512. }
  513. return true
  514. }
  515. // serveWatchClient forwards messages from the grpc stream to run()
  516. func (w *watchGrpcStream) serveWatchClient(wc pb.Watch_WatchClient) {
  517. for {
  518. resp, err := wc.Recv()
  519. if err != nil {
  520. select {
  521. case w.errc <- err:
  522. case <-w.donec:
  523. }
  524. return
  525. }
  526. select {
  527. case w.respc <- resp:
  528. case <-w.donec:
  529. return
  530. }
  531. }
  532. }
  533. // serveSubstream forwards watch responses from run() to the subscriber
  534. func (w *watchGrpcStream) serveSubstream(ws *watcherStream, resumec chan struct{}) {
  535. if ws.closing {
  536. panic("created substream goroutine but substream is closing")
  537. }
  538. // nextRev is the minimum expected next revision
  539. nextRev := ws.initReq.rev
  540. resuming := false
  541. defer func() {
  542. if !resuming {
  543. ws.closing = true
  544. }
  545. close(ws.donec)
  546. if !resuming {
  547. w.closingc <- ws
  548. }
  549. w.wg.Done()
  550. }()
  551. emptyWr := &WatchResponse{}
  552. for {
  553. curWr := emptyWr
  554. outc := ws.outc
  555. if len(ws.buf) > 0 {
  556. curWr = ws.buf[0]
  557. } else {
  558. outc = nil
  559. }
  560. select {
  561. case outc <- *curWr:
  562. if ws.buf[0].Err() != nil {
  563. return
  564. }
  565. ws.buf[0] = nil
  566. ws.buf = ws.buf[1:]
  567. case wr, ok := <-ws.recvc:
  568. if !ok {
  569. // shutdown from closeSubstream
  570. return
  571. }
  572. if wr.Created {
  573. if ws.initReq.retc != nil {
  574. ws.initReq.retc <- ws.outc
  575. // to prevent next write from taking the slot in buffered channel
  576. // and posting duplicate create events
  577. ws.initReq.retc = nil
  578. // send first creation event only if requested
  579. if ws.initReq.createdNotify {
  580. ws.outc <- *wr
  581. }
  582. // once the watch channel is returned, a current revision
  583. // watch must resume at the store revision. This is necessary
  584. // for the following case to work as expected:
  585. // wch := m1.Watch("a")
  586. // m2.Put("a", "b")
  587. // <-wch
  588. // If the revision is only bound on the first observed event,
  589. // if wch is disconnected before the Put is issued, then reconnects
  590. // after it is committed, it'll miss the Put.
  591. if ws.initReq.rev == 0 {
  592. nextRev = wr.Header.Revision
  593. }
  594. }
  595. } else {
  596. // current progress of watch; <= store revision
  597. nextRev = wr.Header.Revision
  598. }
  599. if len(wr.Events) > 0 {
  600. nextRev = wr.Events[len(wr.Events)-1].Kv.ModRevision + 1
  601. }
  602. ws.initReq.rev = nextRev
  603. // created event is already sent above,
  604. // watcher should not post duplicate events
  605. if wr.Created {
  606. continue
  607. }
  608. // TODO pause channel if buffer gets too large
  609. ws.buf = append(ws.buf, wr)
  610. case <-w.ctx.Done():
  611. return
  612. case <-ws.initReq.ctx.Done():
  613. return
  614. case <-resumec:
  615. resuming = true
  616. return
  617. }
  618. }
  619. // lazily send cancel message if events on missing id
  620. }
  621. func (w *watchGrpcStream) newWatchClient() (pb.Watch_WatchClient, error) {
  622. // mark all substreams as resuming
  623. close(w.resumec)
  624. w.resumec = make(chan struct{})
  625. w.joinSubstreams()
  626. for _, ws := range w.substreams {
  627. ws.id = -1
  628. w.resuming = append(w.resuming, ws)
  629. }
  630. // strip out nils, if any
  631. var resuming []*watcherStream
  632. for _, ws := range w.resuming {
  633. if ws != nil {
  634. resuming = append(resuming, ws)
  635. }
  636. }
  637. w.resuming = resuming
  638. w.substreams = make(map[int64]*watcherStream)
  639. // connect to grpc stream while accepting watcher cancelation
  640. stopc := make(chan struct{})
  641. donec := w.waitCancelSubstreams(stopc)
  642. wc, err := w.openWatchClient()
  643. close(stopc)
  644. <-donec
  645. // serve all non-closing streams, even if there's a client error
  646. // so that the teardown path can shutdown the streams as expected.
  647. for _, ws := range w.resuming {
  648. if ws.closing {
  649. continue
  650. }
  651. ws.donec = make(chan struct{})
  652. w.wg.Add(1)
  653. go w.serveSubstream(ws, w.resumec)
  654. }
  655. if err != nil {
  656. return nil, v3rpc.Error(err)
  657. }
  658. // receive data from new grpc stream
  659. go w.serveWatchClient(wc)
  660. return wc, nil
  661. }
  662. func (w *watchGrpcStream) waitCancelSubstreams(stopc <-chan struct{}) <-chan struct{} {
  663. var wg sync.WaitGroup
  664. wg.Add(len(w.resuming))
  665. donec := make(chan struct{})
  666. for i := range w.resuming {
  667. go func(ws *watcherStream) {
  668. defer wg.Done()
  669. if ws.closing {
  670. if ws.initReq.ctx.Err() != nil && ws.outc != nil {
  671. close(ws.outc)
  672. ws.outc = nil
  673. }
  674. return
  675. }
  676. select {
  677. case <-ws.initReq.ctx.Done():
  678. // closed ws will be removed from resuming
  679. ws.closing = true
  680. close(ws.outc)
  681. ws.outc = nil
  682. w.wg.Add(1)
  683. go func() {
  684. defer w.wg.Done()
  685. w.closingc <- ws
  686. }()
  687. case <-stopc:
  688. }
  689. }(w.resuming[i])
  690. }
  691. go func() {
  692. defer close(donec)
  693. wg.Wait()
  694. }()
  695. return donec
  696. }
  697. // joinSubstreams waits for all substream goroutines to complete.
  698. func (w *watchGrpcStream) joinSubstreams() {
  699. for _, ws := range w.substreams {
  700. <-ws.donec
  701. }
  702. for _, ws := range w.resuming {
  703. if ws != nil {
  704. <-ws.donec
  705. }
  706. }
  707. }
  708. // openWatchClient retries opening a watch client until success or halt.
  709. // manually retry in case "ws==nil && err==nil"
  710. // TODO: remove FailFast=false
  711. func (w *watchGrpcStream) openWatchClient() (ws pb.Watch_WatchClient, err error) {
  712. for {
  713. select {
  714. case <-w.ctx.Done():
  715. if err == nil {
  716. return nil, w.ctx.Err()
  717. }
  718. return nil, err
  719. default:
  720. }
  721. if ws, err = w.remote.Watch(w.ctx, w.callOpts...); ws != nil && err == nil {
  722. break
  723. }
  724. if isHaltErr(w.ctx, err) {
  725. return nil, v3rpc.Error(err)
  726. }
  727. }
  728. return ws, nil
  729. }
  730. // toPB converts an internal watch request structure to its protobuf WatchRequest structure.
  731. func (wr *watchRequest) toPB() *pb.WatchRequest {
  732. req := &pb.WatchCreateRequest{
  733. StartRevision: wr.rev,
  734. Key: []byte(wr.key),
  735. RangeEnd: []byte(wr.end),
  736. ProgressNotify: wr.progressNotify,
  737. Filters: wr.filters,
  738. PrevKv: wr.prevKV,
  739. }
  740. cr := &pb.WatchRequest_CreateRequest{CreateRequest: req}
  741. return &pb.WatchRequest{RequestUnion: cr}
  742. }
  743. func streamKeyFromCtx(ctx context.Context) string {
  744. if md, ok := metadata.FromOutgoingContext(ctx); ok {
  745. return fmt.Sprintf("%+v", md)
  746. }
  747. return ""
  748. }