Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use executable task tracker to dedup tasks when stream is re-established #4310

Merged
merged 3 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions service/history/replication/executable_task_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type (
Timestamp time.Time
}
ExecutableTaskTracker interface {
TrackTasks(highWatermarkInfo WatermarkInfo, tasks ...TrackableExecutableTask)
TrackTasks(highWatermarkInfo WatermarkInfo, tasks ...TrackableExecutableTask) []TrackableExecutableTask
LowWatermark() *WatermarkInfo
Cancel()
}
Expand Down Expand Up @@ -77,18 +77,20 @@ func NewExecutableTaskTracker(
}
}

// TrackTasks add tasks for tracking,
// TrackTasks add tasks for tracking, return valid tasks (dedup)
// if task tracker is cancelled, then newly added tasks will also be cancelled
func (t *ExecutableTaskTrackerImpl) TrackTasks(
highWatermarkInfo WatermarkInfo,
tasks ...TrackableExecutableTask,
) {
) []TrackableExecutableTask {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add a comment about what is the return tasks?

filteredTasks := make([]TrackableExecutableTask, 0, len(tasks))

t.Lock()
defer t.Unlock()

// need to assume source side send replication tasks in order
if t.highWatermarkInfo != nil && highWatermarkInfo.Watermark <= t.highWatermarkInfo.Watermark {
return
return filteredTasks
}

lastTaskID := int64(-1)
Expand All @@ -103,6 +105,7 @@ Loop:
}
t.taskQueue.PushBack(task)
t.taskIDs[task.TaskID()] = struct{}{}
filteredTasks = append(filteredTasks, task)
lastTaskID = task.TaskID()
}

Expand All @@ -118,6 +121,7 @@ Loop:
if t.cancelled {
t.cancelLocked()
}
return filteredTasks
}

func (t *ExecutableTaskTrackerImpl) LowWatermark() *WatermarkInfo {
Expand Down
6 changes: 4 additions & 2 deletions service/history/replication/executable_task_tracker_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 28 additions & 14 deletions service/history/replication/executable_task_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ func (s *executableTaskTrackerSuite) TestTrackTasks() {
Timestamp: time.Unix(0, rand.Int63()),
}

s.taskTracker.TrackTasks(highWatermark0, task0)
tasks := s.taskTracker.TrackTasks(highWatermark0, task0)
s.Equal([]TrackableExecutableTask{task0}, tasks)

taskIDs := []int64{}
for element := s.taskTracker.taskQueue.Front(); element != nil; element = element.Next() {
Expand All @@ -99,7 +100,8 @@ func (s *executableTaskTrackerSuite) TestTrackTasks() {
Timestamp: time.Unix(0, rand.Int63()),
}

s.taskTracker.TrackTasks(highWatermark2, task1, task2)
tasks = s.taskTracker.TrackTasks(highWatermark2, task1, task2)
s.Equal([]TrackableExecutableTask{task1, task2}, tasks)

taskIDs = []int64{}
for element := s.taskTracker.taskQueue.Front(); element != nil; element = element.Next() {
Expand All @@ -116,8 +118,10 @@ func (s *executableTaskTrackerSuite) TestTrackTasks_Duplication() {
Watermark: task0.TaskID(),
Timestamp: time.Unix(0, rand.Int63()),
}
s.taskTracker.TrackTasks(highWatermark0, task0)
s.taskTracker.TrackTasks(highWatermark0, task0)
tasks := s.taskTracker.TrackTasks(highWatermark0, task0)
s.Equal([]TrackableExecutableTask{task0}, tasks)
tasks = s.taskTracker.TrackTasks(highWatermark0, task0)
s.Equal([]TrackableExecutableTask{}, tasks)

taskIDs := []int64{}
for element := s.taskTracker.taskQueue.Front(); element != nil; element = element.Next() {
Expand All @@ -132,7 +136,8 @@ func (s *executableTaskTrackerSuite) TestTrackTasks_Duplication() {
Watermark: task1.TaskID() + 1,
Timestamp: time.Unix(0, rand.Int63()),
}
s.taskTracker.TrackTasks(highWatermark1, task1)
tasks = s.taskTracker.TrackTasks(highWatermark1, task1)
s.Equal([]TrackableExecutableTask{task1}, tasks)

taskIDs = []int64{}
for element := s.taskTracker.taskQueue.Front(); element != nil; element = element.Next() {
Expand All @@ -147,7 +152,8 @@ func (s *executableTaskTrackerSuite) TestTrackTasks_Duplication() {
Watermark: task2.TaskID() + 1,
Timestamp: time.Unix(0, rand.Int63()),
}
s.taskTracker.TrackTasks(highWatermark2, task1, task2)
tasks = s.taskTracker.TrackTasks(highWatermark2, task1, task2)
s.Equal([]TrackableExecutableTask{task2}, tasks)

taskIDs = []int64{}
for element := s.taskTracker.taskQueue.Front(); element != nil; element = element.Next() {
Expand All @@ -167,7 +173,8 @@ func (s *executableTaskTrackerSuite) TestTrackTasks_Cancellation() {
}

s.taskTracker.Cancel()
s.taskTracker.TrackTasks(highWatermark0, task0)
tasks := s.taskTracker.TrackTasks(highWatermark0, task0)
s.Equal([]TrackableExecutableTask{task0}, tasks)

taskIDs := []int64{}
for element := s.taskTracker.taskQueue.Front(); element != nil; element = element.Next() {
Expand Down Expand Up @@ -197,7 +204,8 @@ func (s *executableTaskTrackerSuite) TestLowWatermark_AckedTask() {
Watermark: task0.TaskID() + 1,
Timestamp: time.Unix(0, rand.Int63()),
}
s.taskTracker.TrackTasks(highWatermark0, task0)
tasks := s.taskTracker.TrackTasks(highWatermark0, task0)
s.Equal([]TrackableExecutableTask{task0}, tasks)

lowWatermark := s.taskTracker.LowWatermark()
s.Equal(highWatermark0, *lowWatermark)
Expand All @@ -220,7 +228,8 @@ func (s *executableTaskTrackerSuite) TestLowWatermark_NackedTask_Success() {
Watermark: task0.TaskID() + 1,
Timestamp: time.Unix(0, rand.Int63()),
}
s.taskTracker.TrackTasks(highWatermark0, task0)
tasks := s.taskTracker.TrackTasks(highWatermark0, task0)
s.Equal([]TrackableExecutableTask{task0}, tasks)

lowWatermark := s.taskTracker.LowWatermark()
s.Equal(highWatermark0, *lowWatermark)
Expand All @@ -239,10 +248,11 @@ func (s *executableTaskTrackerSuite) TestLowWatermark_NackedTask_Error() {
task0.EXPECT().State().Return(ctasks.TaskStateNacked).AnyTimes()
task0.EXPECT().MarkPoisonPill().Return(errors.New("random error"))

s.taskTracker.TrackTasks(WatermarkInfo{
tasks := s.taskTracker.TrackTasks(WatermarkInfo{
Watermark: task0.TaskID() + 1,
Timestamp: time.Unix(0, rand.Int63()),
}, task0)
s.Equal([]TrackableExecutableTask{task0}, tasks)

lowWatermark := s.taskTracker.LowWatermark()
s.Equal(WatermarkInfo{
Expand All @@ -263,10 +273,11 @@ func (s *executableTaskTrackerSuite) TestLowWatermark_AbortedTask() {
task0.EXPECT().TaskCreationTime().Return(time.Unix(0, rand.Int63())).AnyTimes()
task0.EXPECT().State().Return(ctasks.TaskStateAborted).AnyTimes()

s.taskTracker.TrackTasks(WatermarkInfo{
tasks := s.taskTracker.TrackTasks(WatermarkInfo{
Watermark: task0.TaskID() + 1,
Timestamp: time.Unix(0, rand.Int63()),
}, task0)
s.Equal([]TrackableExecutableTask{task0}, tasks)

lowWatermark := s.taskTracker.LowWatermark()
s.Equal(WatermarkInfo{
Expand All @@ -287,10 +298,11 @@ func (s *executableTaskTrackerSuite) TestLowWatermark_CancelledTask() {
task0.EXPECT().TaskCreationTime().Return(time.Unix(0, rand.Int63())).AnyTimes()
task0.EXPECT().State().Return(ctasks.TaskStateCancelled).AnyTimes()

s.taskTracker.TrackTasks(WatermarkInfo{
tasks := s.taskTracker.TrackTasks(WatermarkInfo{
Watermark: task0.TaskID() + 1,
Timestamp: time.Unix(0, rand.Int63()),
}, task0)
s.Equal([]TrackableExecutableTask{task0}, tasks)

lowWatermark := s.taskTracker.LowWatermark()
s.Equal(WatermarkInfo{
Expand All @@ -311,10 +323,11 @@ func (s *executableTaskTrackerSuite) TestLowWatermark_PendingTask() {
task0.EXPECT().TaskCreationTime().Return(time.Unix(0, rand.Int63())).AnyTimes()
task0.EXPECT().State().Return(ctasks.TaskStatePending).AnyTimes()

s.taskTracker.TrackTasks(WatermarkInfo{
tasks := s.taskTracker.TrackTasks(WatermarkInfo{
Watermark: task0.TaskID() + 1,
Timestamp: time.Unix(0, rand.Int63()),
}, task0)
s.Equal([]TrackableExecutableTask{task0}, tasks)

lowWatermark := s.taskTracker.LowWatermark()
s.Equal(WatermarkInfo{
Expand All @@ -338,7 +351,8 @@ func (s *executableTaskTrackerSuite) TestCancellation() {
Timestamp: time.Unix(0, rand.Int63()),
}

s.taskTracker.TrackTasks(highWatermark0, task0)
tasks := s.taskTracker.TrackTasks(highWatermark0, task0)
s.Equal([]TrackableExecutableTask{task0}, tasks)
s.taskTracker.Cancel()

taskIDs := []int64{}
Expand Down
5 changes: 2 additions & 3 deletions service/history/replication/stream_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,10 @@ func (r *StreamReceiver) processMessages(
)
highWatermark := streamResp.Resp.GetMessages().LastTaskId
highWatermarkTime := timestamp.TimeValue(streamResp.Resp.GetMessages().LastTaskTime)
r.taskTracker.TrackTasks(WatermarkInfo{
for _, task := range r.taskTracker.TrackTasks(WatermarkInfo{
Watermark: highWatermark,
Timestamp: highWatermarkTime,
}, tasks...)
for _, task := range tasks {
}, tasks...) {
r.ProcessToolBox.TaskScheduler.Submit(task)
}
}
Expand Down
5 changes: 3 additions & 2 deletions service/history/replication/stream_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,13 @@ func (s *streamReceiverSuite) TestProcessMessage_TrackSubmit() {
s.stream.respChan <- streamResp
close(s.stream.respChan)

s.taskTracker.EXPECT().TrackTasks(gomock.Any(), gomock.Any()).Do(
func(highWatermarkInfo WatermarkInfo, tasks ...TrackableExecutableTask) {
s.taskTracker.EXPECT().TrackTasks(gomock.Any(), gomock.Any()).DoAndReturn(
func(highWatermarkInfo WatermarkInfo, tasks ...TrackableExecutableTask) []TrackableExecutableTask {
s.Equal(streamResp.Resp.GetMessages().LastTaskId, highWatermarkInfo.Watermark)
s.Equal(*streamResp.Resp.GetMessages().LastTaskTime, highWatermarkInfo.Timestamp)
s.Equal(1, len(tasks))
s.IsType(&ExecutableUnknownTask{}, tasks[0])
return []TrackableExecutableTask{tasks[0]}
},
)

Expand Down