watch.go 18 KB

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