watch.go 21 KB

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