watch.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. // Copyright 2016 CoreOS, Inc.
  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. v3rpc "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. storagepb "github.com/coreos/etcd/storage/storagepb"
  21. "golang.org/x/net/context"
  22. "google.golang.org/grpc"
  23. )
  24. type WatchChan <-chan WatchResponse
  25. type Watcher interface {
  26. // Watch watches on a key or prefix. The watched events will be returned
  27. // through the returned channel.
  28. // If the watch is slow or the required rev is compacted, the watch request
  29. // might be canceled from the server-side and the chan will be closed.
  30. // 'opts' can be: 'WithRev' and/or 'WitchPrefix'.
  31. Watch(ctx context.Context, key string, opts ...OpOption) WatchChan
  32. // Close closes the watcher and cancels all watch requests.
  33. Close() error
  34. }
  35. type WatchResponse struct {
  36. Header pb.ResponseHeader
  37. Events []*storagepb.Event
  38. // CompactRevision is the minimum revision the watcher may receive.
  39. CompactRevision int64
  40. // Canceled is used to indicate watch failure.
  41. // If the watch failed and the stream was about to close, before the channel is closed,
  42. // the channel sends a final response that has Canceled set to true with a non-nil Err().
  43. Canceled bool
  44. }
  45. // Err is the error value if this WatchResponse holds an error.
  46. func (wr *WatchResponse) Err() error {
  47. if wr.CompactRevision != 0 {
  48. return v3rpc.ErrCompacted
  49. }
  50. if wr.Canceled {
  51. return v3rpc.ErrFutureRev
  52. }
  53. return nil
  54. }
  55. // IsProgressNotify returns true if the WatchResponse is progress notification.
  56. func (wr *WatchResponse) IsProgressNotify() bool {
  57. return len(wr.Events) == 0 && !wr.Canceled
  58. }
  59. // watcher implements the Watcher interface
  60. type watcher struct {
  61. c *Client
  62. conn *grpc.ClientConn
  63. remote pb.WatchClient
  64. // ctx controls internal remote.Watch requests
  65. ctx context.Context
  66. cancel context.CancelFunc
  67. // streams holds all active watchers
  68. streams map[int64]*watcherStream
  69. // mu protects the streams map
  70. mu sync.RWMutex
  71. // reqc sends a watch request from Watch() to the main goroutine
  72. reqc chan *watchRequest
  73. // respc receives data from the watch client
  74. respc chan *pb.WatchResponse
  75. // stopc is sent to the main goroutine to stop all processing
  76. stopc chan struct{}
  77. // donec closes to broadcast shutdown
  78. donec chan struct{}
  79. // errc transmits errors from grpc Recv
  80. errc chan error
  81. }
  82. // watchRequest is issued by the subscriber to start a new watcher
  83. type watchRequest struct {
  84. ctx context.Context
  85. key string
  86. end string
  87. rev int64
  88. // progressNotify is for progress updates.
  89. progressNotify bool
  90. // retc receives a chan WatchResponse once the watcher is established
  91. retc chan chan WatchResponse
  92. }
  93. // watcherStream represents a registered watcher
  94. type watcherStream struct {
  95. initReq watchRequest
  96. // outc publishes watch responses to subscriber
  97. outc chan<- WatchResponse
  98. // recvc buffers watch responses before publishing
  99. recvc chan *WatchResponse
  100. id int64
  101. // lastRev is revision last successfully sent over outc
  102. lastRev int64
  103. // resumec indicates the stream must recover at a given revision
  104. resumec chan int64
  105. }
  106. func NewWatcher(c *Client) Watcher {
  107. ctx, cancel := context.WithCancel(context.Background())
  108. conn := c.ActiveConnection()
  109. w := &watcher{
  110. c: c,
  111. conn: conn,
  112. remote: pb.NewWatchClient(conn),
  113. ctx: ctx,
  114. cancel: cancel,
  115. streams: make(map[int64]*watcherStream),
  116. respc: make(chan *pb.WatchResponse),
  117. reqc: make(chan *watchRequest),
  118. stopc: make(chan struct{}),
  119. donec: make(chan struct{}),
  120. errc: make(chan error, 1),
  121. }
  122. go w.run()
  123. return w
  124. }
  125. // Watch posts a watch request to run() and waits for a new watcher channel
  126. func (w *watcher) Watch(ctx context.Context, key string, opts ...OpOption) WatchChan {
  127. ow := opWatch(key, opts...)
  128. retc := make(chan chan WatchResponse, 1)
  129. wr := &watchRequest{
  130. ctx: ctx,
  131. key: string(ow.key),
  132. end: string(ow.end),
  133. rev: ow.rev,
  134. progressNotify: ow.progressNotify,
  135. retc: retc,
  136. }
  137. ok := false
  138. // submit request
  139. select {
  140. case w.reqc <- wr:
  141. ok = true
  142. case <-wr.ctx.Done():
  143. case <-w.donec:
  144. }
  145. // receive channel
  146. if ok {
  147. select {
  148. case ret := <-retc:
  149. return ret
  150. case <-ctx.Done():
  151. case <-w.donec:
  152. }
  153. }
  154. // couldn't create channel; return closed channel
  155. ch := make(chan WatchResponse)
  156. close(ch)
  157. return ch
  158. }
  159. func (w *watcher) Close() error {
  160. select {
  161. case w.stopc <- struct{}{}:
  162. case <-w.donec:
  163. }
  164. <-w.donec
  165. return <-w.errc
  166. }
  167. func (w *watcher) addStream(resp *pb.WatchResponse, pendingReq *watchRequest) {
  168. if pendingReq == nil {
  169. // no pending request; ignore
  170. return
  171. }
  172. if resp.Canceled || resp.CompactRevision != 0 {
  173. // a cancel at id creation time means the start revision has
  174. // been compacted out of the store
  175. ret := make(chan WatchResponse, 1)
  176. ret <- WatchResponse{
  177. Header: *resp.Header,
  178. CompactRevision: resp.CompactRevision,
  179. Canceled: true}
  180. close(ret)
  181. pendingReq.retc <- ret
  182. return
  183. }
  184. ret := make(chan WatchResponse)
  185. if resp.WatchId == -1 {
  186. // failed; no channel
  187. close(ret)
  188. pendingReq.retc <- ret
  189. return
  190. }
  191. ws := &watcherStream{
  192. initReq: *pendingReq,
  193. id: resp.WatchId,
  194. outc: ret,
  195. // buffered so unlikely to block on sending while holding mu
  196. recvc: make(chan *WatchResponse, 4),
  197. resumec: make(chan int64),
  198. }
  199. if pendingReq.rev == 0 {
  200. // note the header revision so that a put following a current watcher
  201. // disconnect will arrive on the watcher channel after reconnect
  202. ws.initReq.rev = resp.Header.Revision
  203. }
  204. w.mu.Lock()
  205. w.streams[ws.id] = ws
  206. w.mu.Unlock()
  207. // send messages to subscriber
  208. go w.serveStream(ws)
  209. // pass back the subscriber channel for the watcher
  210. pendingReq.retc <- ret
  211. }
  212. // closeStream closes the watcher resources and removes it
  213. func (w *watcher) closeStream(ws *watcherStream) {
  214. // cancels request stream; subscriber receives nil channel
  215. close(ws.initReq.retc)
  216. // close subscriber's channel
  217. close(ws.outc)
  218. // shutdown serveStream
  219. close(ws.recvc)
  220. delete(w.streams, ws.id)
  221. }
  222. // run is the root of the goroutines for managing a watcher client
  223. func (w *watcher) run() {
  224. defer func() {
  225. close(w.donec)
  226. w.cancel()
  227. }()
  228. // start a stream with the etcd grpc server
  229. wc, wcerr := w.newWatchClient()
  230. if wcerr != nil {
  231. w.errc <- wcerr
  232. return
  233. }
  234. var pendingReq, failedReq *watchRequest
  235. curReqC := w.reqc
  236. cancelSet := make(map[int64]struct{})
  237. for {
  238. select {
  239. // Watch() requested
  240. case pendingReq = <-curReqC:
  241. // no more watch requests until there's a response
  242. curReqC = nil
  243. if err := wc.Send(pendingReq.toPB()); err == nil {
  244. // pendingReq now waits on w.respc
  245. break
  246. }
  247. failedReq = pendingReq
  248. // New events from the watch client
  249. case pbresp := <-w.respc:
  250. switch {
  251. case pbresp.Created:
  252. // response to pending req, try to add
  253. w.addStream(pbresp, pendingReq)
  254. pendingReq = nil
  255. curReqC = w.reqc
  256. case pbresp.Canceled:
  257. delete(cancelSet, pbresp.WatchId)
  258. default:
  259. // dispatch to appropriate watch stream
  260. if ok := w.dispatchEvent(pbresp); ok {
  261. break
  262. }
  263. // watch response on unexpected watch id; cancel id
  264. if _, ok := cancelSet[pbresp.WatchId]; ok {
  265. break
  266. }
  267. cancelSet[pbresp.WatchId] = struct{}{}
  268. cr := &pb.WatchRequest_CancelRequest{
  269. CancelRequest: &pb.WatchCancelRequest{
  270. WatchId: pbresp.WatchId,
  271. },
  272. }
  273. req := &pb.WatchRequest{RequestUnion: cr}
  274. wc.Send(req)
  275. }
  276. // watch client failed to recv; spawn another if possible
  277. // TODO report watch client errors from errc?
  278. case <-w.errc:
  279. if wc, wcerr = w.newWatchClient(); wcerr != nil {
  280. w.errc <- wcerr
  281. return
  282. }
  283. curReqC = w.reqc
  284. if pendingReq != nil {
  285. failedReq = pendingReq
  286. }
  287. cancelSet = make(map[int64]struct{})
  288. case <-w.stopc:
  289. w.errc <- nil
  290. return
  291. }
  292. // send failed; queue for retry
  293. if failedReq != nil {
  294. go func(wr *watchRequest) {
  295. select {
  296. case w.reqc <- wr:
  297. case <-wr.ctx.Done():
  298. case <-w.donec:
  299. }
  300. }(pendingReq)
  301. failedReq = nil
  302. pendingReq = nil
  303. }
  304. }
  305. }
  306. // dispatchEvent sends a WatchResponse to the appropriate watcher stream
  307. func (w *watcher) dispatchEvent(pbresp *pb.WatchResponse) bool {
  308. w.mu.RLock()
  309. defer w.mu.RUnlock()
  310. ws, ok := w.streams[pbresp.WatchId]
  311. if ok {
  312. wr := &WatchResponse{
  313. Header: *pbresp.Header,
  314. Events: pbresp.Events,
  315. CompactRevision: pbresp.CompactRevision,
  316. Canceled: pbresp.Canceled}
  317. ws.recvc <- wr
  318. }
  319. return ok
  320. }
  321. // serveWatchClient forwards messages from the grpc stream to run()
  322. func (w *watcher) serveWatchClient(wc pb.Watch_WatchClient) {
  323. for {
  324. resp, err := wc.Recv()
  325. if err != nil {
  326. select {
  327. case w.errc <- err:
  328. case <-w.donec:
  329. }
  330. return
  331. }
  332. select {
  333. case w.respc <- resp:
  334. case <-w.donec:
  335. return
  336. }
  337. }
  338. }
  339. // serveStream forwards watch responses from run() to the subscriber
  340. func (w *watcher) serveStream(ws *watcherStream) {
  341. emptyWr := &WatchResponse{}
  342. wrs := []*WatchResponse{}
  343. resuming := false
  344. closing := false
  345. for !closing {
  346. curWr := emptyWr
  347. outc := ws.outc
  348. if len(wrs) > 0 {
  349. curWr = wrs[0]
  350. } else {
  351. outc = nil
  352. }
  353. select {
  354. case outc <- *curWr:
  355. if wrs[0].Err() != nil {
  356. closing = true
  357. break
  358. }
  359. var newRev int64
  360. if len(wrs[0].Events) > 0 {
  361. newRev = wrs[0].Events[len(wrs[0].Events)-1].Kv.ModRevision
  362. } else {
  363. newRev = wrs[0].Header.Revision
  364. }
  365. if newRev != ws.lastRev {
  366. ws.lastRev = newRev
  367. }
  368. wrs[0] = nil
  369. wrs = wrs[1:]
  370. case wr, ok := <-ws.recvc:
  371. if !ok {
  372. // shutdown from closeStream
  373. return
  374. }
  375. // resume up to last seen event if disconnected
  376. if resuming {
  377. resuming = false
  378. // trim events already seen
  379. for i := 0; i < len(wr.Events); i++ {
  380. if wr.Events[i].Kv.ModRevision > ws.lastRev {
  381. wr.Events = wr.Events[i:]
  382. break
  383. }
  384. }
  385. // only forward new events
  386. if wr.Events[0].Kv.ModRevision == ws.lastRev {
  387. break
  388. }
  389. }
  390. // TODO don't keep buffering if subscriber stops reading
  391. wrs = append(wrs, wr)
  392. case resumeRev := <-ws.resumec:
  393. if resumeRev != ws.lastRev {
  394. panic("unexpected resume revision")
  395. }
  396. wrs = nil
  397. resuming = true
  398. case <-w.donec:
  399. closing = true
  400. case <-ws.initReq.ctx.Done():
  401. closing = true
  402. }
  403. }
  404. w.mu.Lock()
  405. w.closeStream(ws)
  406. w.mu.Unlock()
  407. // lazily send cancel message if events on missing id
  408. }
  409. func (w *watcher) newWatchClient() (pb.Watch_WatchClient, error) {
  410. ws, rerr := w.resume()
  411. if rerr != nil {
  412. return nil, rerr
  413. }
  414. go w.serveWatchClient(ws)
  415. return ws, nil
  416. }
  417. // resume creates a new WatchClient with all current watchers reestablished
  418. func (w *watcher) resume() (ws pb.Watch_WatchClient, err error) {
  419. for {
  420. if ws, err = w.openWatchClient(); err != nil {
  421. break
  422. } else if err = w.resumeWatchers(ws); err == nil {
  423. break
  424. }
  425. }
  426. return ws, err
  427. }
  428. // openWatchClient retries opening a watchclient until retryConnection fails
  429. func (w *watcher) openWatchClient() (ws pb.Watch_WatchClient, err error) {
  430. for {
  431. if ws, err = w.remote.Watch(w.ctx); ws != nil {
  432. break
  433. } else if isHalted(w.ctx, err) {
  434. return nil, err
  435. }
  436. newConn, nerr := w.c.retryConnection(w.conn, nil)
  437. if nerr != nil {
  438. return nil, nerr
  439. }
  440. w.conn = newConn
  441. w.remote = pb.NewWatchClient(w.conn)
  442. }
  443. return ws, nil
  444. }
  445. // resumeWatchers rebuilds every registered watcher on a new client
  446. func (w *watcher) resumeWatchers(wc pb.Watch_WatchClient) error {
  447. streams := []*watcherStream{}
  448. w.mu.RLock()
  449. for _, ws := range w.streams {
  450. streams = append(streams, ws)
  451. }
  452. w.mu.RUnlock()
  453. for _, ws := range streams {
  454. // reconstruct watcher from initial request
  455. if ws.lastRev != 0 {
  456. ws.initReq.rev = ws.lastRev
  457. }
  458. if err := wc.Send(ws.initReq.toPB()); err != nil {
  459. return err
  460. }
  461. // wait for request ack
  462. resp, err := wc.Recv()
  463. if err != nil {
  464. return err
  465. } else if len(resp.Events) != 0 || resp.Created != true {
  466. return fmt.Errorf("watcher: unexpected response (%+v)", resp)
  467. }
  468. // id may be different since new remote watcher; update map
  469. w.mu.Lock()
  470. delete(w.streams, ws.id)
  471. ws.id = resp.WatchId
  472. w.streams[ws.id] = ws
  473. w.mu.Unlock()
  474. ws.resumec <- ws.lastRev
  475. }
  476. return nil
  477. }
  478. // toPB converts an internal watch request structure to its protobuf messagefunc (wr *watchRequest)
  479. func (wr *watchRequest) toPB() *pb.WatchRequest {
  480. req := &pb.WatchCreateRequest{
  481. StartRevision: wr.rev,
  482. Key: []byte(wr.key),
  483. RangeEnd: []byte(wr.end),
  484. ProgressNotify: wr.progressNotify,
  485. }
  486. cr := &pb.WatchRequest_CreateRequest{CreateRequest: req}
  487. return &pb.WatchRequest{RequestUnion: cr}
  488. }