watch.go 12 KB

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