Skip to content

Commit

Permalink
kv-client(cdc): correct conditions of canceling grpc streams (#10237)
Browse files Browse the repository at this point in the history
Signed-off-by: qupeng <[email protected]>
  • Loading branch information
hicqu committed Dec 7, 2023
1 parent 255d810 commit 007edd9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
3 changes: 1 addition & 2 deletions cdc/kv/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,8 +997,7 @@ func (s *eventFeedSession) receiveFromStream(

// always create a new region worker, because `receiveFromStream` is ensured
// to call exactly once from outer code logic
worker := newRegionWorker(s.changefeed, s, addr)

worker := newRegionWorker(s.changefeed, s, addr, pendingRegions)
defer worker.evictAllRegions()

g.Go(func() error {
Expand Down
7 changes: 6 additions & 1 deletion cdc/kv/region_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ type regionWorker struct {

// how many pending input events
inputPending int32

pendingRegions *syncRegionFeedStateMap
}

func newRegionWorker(
changefeedID model.ChangeFeedID, s *eventFeedSession, addr string,
pendingRegions *syncRegionFeedStateMap,
) *regionWorker {
metrics := &regionWorkerMetrics{}
metrics.metricReceivedEventSize = eventSize.WithLabelValues("received")
Expand Down Expand Up @@ -149,6 +152,8 @@ func newRegionWorker(
concurrency: s.client.config.WorkerConcurrent,
metrics: metrics,
inputPending: 0,

pendingRegions: pendingRegions,
}
}

Expand Down Expand Up @@ -184,7 +189,7 @@ func (w *regionWorker) checkShouldExit() error {
empty := w.checkRegionStateEmpty()
// If there is no region maintained by this region worker, exit it and
// cancel the gRPC stream.
if empty {
if empty && w.pendingRegions.len() == 0 {
w.cancelStream(time.Duration(0))
return cerror.ErrRegionWorkerExit.GenWithStackByArgs()
}
Expand Down
4 changes: 2 additions & 2 deletions cdc/kv/region_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestRegionWokerHandleEventEntryEventOutOfOrder(t *testing.T) {
regionspan.ToComparableSpan(span),
0, &tikv.RPCContext{}), 0)
state.start()
worker := newRegionWorker(model.ChangeFeedID{}, s, "")
worker := newRegionWorker(model.ChangeFeedID{}, s, "", newSyncRegionFeedStateMap())
require.Equal(t, 2, cap(worker.outputCh))

// Receive prewrite2 with empty value.
Expand Down Expand Up @@ -313,7 +313,7 @@ func TestRegionWorkerHandleEventsBeforeStartTs(t *testing.T) {
rpcCtx: &tikv.RPCContext{},
}, 0)
s1.start()
w := newRegionWorker(model.ChangeFeedID{}, s, "")
w := newRegionWorker(model.ChangeFeedID{}, s, "", newSyncRegionFeedStateMap())

err := w.handleResolvedTs(ctx, &resolvedTsEvent{
resolvedTs: 5,
Expand Down

0 comments on commit 007edd9

Please sign in to comment.