Files
Michael SnowdenandGitHub e3aebc4edd Use source cluster shard ID for DLQ (#5078)
<!-- Describe what has changed in this PR -->
**What changed?**
This PR makes us use the shard ID of the task in the original cluster
instead of the shard ID in the current cluster when merging DLQ'd
replication tasks (or writing any task to the DLQ actually).

<!-- Tell your future self why have you made these changes -->
**Why?**
The number of shards between any two clusters for a given namespace
don't have to be the same. One has to be a multiple of the other,
though. However, I noticed a bug when using our local xdc config with
cluster-a and cluster-b:

```
  "Error": "AddTasks failed while re-enqueuing tasks to shard 9 (type: dlq-error-type-invalid-request, retryable: false): Task is for wrong shard: index = 0, task shard = 25, request shard = 9",
```

This error was caused by the fact that the task in question has a
different computed shard ID in cluster-b, where there are only 16
shards, than it does in cluster-a, where there are 32 shards.

<img width="740" alt="Untitled"
src="https://github.com/temporalio/temporal/assets/5942963/ac9c2754-acfc-44ae-bb8d-a98f9ecd6b4d">

Fortunately, the V2 replication DLQ hasn't even been turned on anywhere
yet, so this didn't cause any issues. It was caught as part of testing
before rollout.

<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->
**How did you test it?**
I wrote some unit tests, and I also re-ran the manual test I was doing.
I even generated a significant amount of load with `maru`, forced 10s of
thousands of replication tasks to fail, and successfully merged all of
them.

<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->
**Potential risks**

<!-- Is this PR a hotfix candidate or require that a notification be
sent to the broader community? (Yes/No) -->
**Is hotfix candidate?**
2023-11-07 21:13:47 +00:00

101 lines
3.8 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package deletedlqtaskstest
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.temporal.io/api/serviceerror"
"google.golang.org/grpc/codes"
commonspb "go.temporal.io/server/api/common/v1"
"go.temporal.io/server/api/historyservice/v1"
"go.temporal.io/server/common/persistence"
"go.temporal.io/server/common/persistence/persistencetest"
"go.temporal.io/server/service/history/api/deletedlqtasks"
"go.temporal.io/server/service/history/tasks"
)
func TestInvoke(t *testing.T, manager persistence.HistoryTaskQueueManager) {
ctx := context.Background()
t.Run("HappyPath", func(t *testing.T) {
t.Parallel()
queueKey := persistencetest.GetQueueKey(t, persistencetest.WithQueueType(persistence.QueueTypeHistoryDLQ))
_, err := manager.CreateQueue(ctx, &persistence.CreateQueueRequest{
QueueKey: queueKey,
})
require.NoError(t, err)
for i := 0; i < 3; i++ {
_, err := manager.EnqueueTask(ctx, &persistence.EnqueueTaskRequest{
QueueType: queueKey.QueueType,
SourceCluster: queueKey.SourceCluster,
TargetCluster: queueKey.TargetCluster,
Task: &tasks.WorkflowTask{},
SourceShardID: 1,
})
require.NoError(t, err)
}
_, err = deletedlqtasks.Invoke(ctx, manager, &historyservice.DeleteDLQTasksRequest{
DlqKey: &commonspb.HistoryDLQKey{
TaskCategory: int32(queueKey.Category.ID()),
SourceCluster: queueKey.SourceCluster,
TargetCluster: queueKey.TargetCluster,
},
InclusiveMaxTaskMetadata: &commonspb.HistoryDLQTaskMetadata{
MessageId: persistence.FirstQueueMessageID + 1,
},
}, tasks.NewDefaultTaskCategoryRegistry())
require.NoError(t, err)
resp, err := manager.ReadRawTasks(ctx, &persistence.ReadRawTasksRequest{
QueueKey: queueKey,
PageSize: 10,
})
require.NoError(t, err)
require.Len(t, resp.Tasks, 1)
assert.Equal(t, int64(persistence.FirstQueueMessageID+2), resp.Tasks[0].MessageMetadata.ID)
})
t.Run("QueueDoesNotExist", func(t *testing.T) {
t.Parallel()
queueKey := persistencetest.GetQueueKey(t, persistencetest.WithQueueType(persistence.QueueTypeHistoryDLQ))
_, err := deletedlqtasks.Invoke(ctx, manager, &historyservice.DeleteDLQTasksRequest{
DlqKey: &commonspb.HistoryDLQKey{
TaskCategory: int32(queueKey.Category.ID()),
SourceCluster: queueKey.SourceCluster,
TargetCluster: queueKey.TargetCluster,
},
InclusiveMaxTaskMetadata: &commonspb.HistoryDLQTaskMetadata{
MessageId: persistence.FirstQueueMessageID,
},
}, tasks.NewDefaultTaskCategoryRegistry())
require.Error(t, err)
assert.Equal(t, codes.NotFound, serviceerror.ToStatus(err).Code(), err.Error())
})
}