watch.go 12 KB

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