Skip to content
New issue

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

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

Already on GitHub? # to your account

Improve task range completion prerequisite check #4754

Merged
merged 1 commit into from
Aug 10, 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
2 changes: 1 addition & 1 deletion service/history/queues/queue_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func (p *queueBase) checkpoint() {
// for the queue
p.metricsHandler.Counter(metrics.TaskBatchCompleteCounter.GetMetricName()).Record(1)
if newExclusiveDeletionHighWatermark.CompareTo(p.exclusiveDeletionHighWatermark) > 0 ||
p.updateShardRangeID() {
(p.updateShardRangeID() && newExclusiveDeletionHighWatermark.CompareTo(tasks.MinimumKey) > 0) {
// when shard rangeID is updated, perform range completion again in case the underlying persistence implementation
// serves traffic based on the persisted shardInfo
err := p.rangeCompleteTasks(p.exclusiveDeletionHighWatermark, newExclusiveDeletionHighWatermark)
Expand Down
72 changes: 71 additions & 1 deletion service/history/queues/queue_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package queues
import (
"context"
"errors"
"math/rand"
"testing"
"time"

Expand Down Expand Up @@ -420,7 +421,7 @@ func (s *queueBaseSuite) TestProcessNewRange() {
s.True(base.nonReadableScope.Range.Equals(NewRange(scopes[0].Range.ExclusiveMax, tasks.MaximumKey)))
}

func (s *queueBaseSuite) TestCheckPoint_WithPendingTasks() {
func (s *queueBaseSuite) TestCheckPoint_WithPendingTasks_PerformRangeCompletion() {
scopeMinKey := tasks.MaximumKey
readerScopes := map[int64][]Scope{}
readerIDs := []int64{DefaultReaderId, 2, 3}
Expand Down Expand Up @@ -504,6 +505,75 @@ func (s *queueBaseSuite) TestCheckPoint_WithPendingTasks() {
s.True(scopeMinKey.CompareTo(base.exclusiveDeletionHighWatermark) == 0)
}

func (s *queueBaseSuite) TestCheckPoint_WithPendingTasks_SkipRangeCompletion() {
// task range completion should be skipped when there's no task to delete
scopeMinKey := tasks.MinimumKey
readerScopes := map[int64][]Scope{
DefaultReaderId: {
{
Range: NewRange(scopeMinKey, tasks.NewKey(time.Now(), rand.Int63())),
Predicate: predicates.Universal[tasks.Task](),
},
},
}
queueState := &queueState{
readerScopes: readerScopes,
exclusiveReaderHighWatermark: tasks.MaximumKey,
}
persistenceState := ToPersistenceQueueState(queueState)

mockShard := shard.NewTestContext(
s.controller,
&persistencespb.ShardInfo{
ShardId: 0,
RangeId: 10,
QueueStates: map[int32]*persistencespb.QueueState{
tasks.CategoryIDTimer: persistenceState,
},
},
s.config,
)
mockShard.Resource.ClusterMetadata.EXPECT().GetCurrentClusterName().Return(cluster.TestCurrentClusterName).AnyTimes()
mockShard.Resource.ClusterMetadata.EXPECT().GetAllClusterInfo().Return(cluster.TestAllClusterInfo).AnyTimes()
mockShard.Resource.ExecutionMgr.EXPECT().RegisterHistoryTaskReader(gomock.Any(), gomock.Any()).Return(nil).Times(len(readerScopes))

base := newQueueBase(
mockShard,
tasks.CategoryTimer,
nil,
s.mockScheduler,
s.mockRescheduler,
NewNoopPriorityAssigner(),
nil,
s.options,
s.rateLimiter,
NoopReaderCompletionFn,
s.logger,
s.metricsHandler,
)
base.checkpointTimer = time.NewTimer(s.options.CheckpointInterval())

s.True(scopeMinKey.CompareTo(base.exclusiveDeletionHighWatermark) == 0)

// set to a smaller value so that delete will be triggered
currentLowWatermark := tasks.MinimumKey
base.exclusiveDeletionHighWatermark = currentLowWatermark

gomock.InOrder(
mockShard.Resource.ExecutionMgr.EXPECT().UpdateHistoryTaskReaderProgress(gomock.Any(), gomock.Any()).Times(len(readerScopes)),
mockShard.Resource.ShardMgr.EXPECT().UpdateShard(gomock.Any(), gomock.Any()).DoAndReturn(
func(_ context.Context, request *persistence.UpdateShardRequest) error {
s.QueueStateEqual(persistenceState, request.ShardInfo.QueueStates[tasks.CategoryIDTimer])
return nil
},
).Times(1),
)

base.checkpoint()

s.True(scopeMinKey.CompareTo(base.exclusiveDeletionHighWatermark) == 0)
}

func (s *queueBaseSuite) TestCheckPoint_NoPendingTasks() {
exclusiveReaderHighWatermark := NewRandomKey()
queueState := &queueState{
Expand Down