Files
tasks/common/persistence/history_task_queue_manager_test.go
Tim Deeb-SwihartandGitHub 95169a53c3 Replace gogo/protobuf with google/protobuf (#5032)
**What changed?**

gogo/protobuf has been replaced with Google's official go compiler. 

**Why?**

gogo/protobuf has been deprecated for some time and the community is
moving on, building new tools (like vtproto) atop google's v2 compiler.

**How did you test it?**

`make test`

**Potential risks**

1. The change from embedded gogo-generated-structs to
google-generated-pointers-to-structs created a risk of nil pointer
exceptions. I've fixed all the ones our tests found but it's possible
there are more lurking in the new code.
2. This change may cause our performance to decrease. Certainly
encoding/deconding of proto objects will become slower, but the overuse
of pointers by the google compiler may negatively affect our overall
performance. We'll need to keep an eye on the GC stats
3. This breaks the HTTP API. We will not support [shortand payload
encoding](https://github.com/temporalio/proposals/blob/master/api/http-api.md#payload-formatting)
in this first pass; that will come once this initial work is in testing.

**Breaking changes for developers**

- `*time.Time` in proto structs will now be
[timestamppb.Timestamp](https://pkg.go.dev/google.golang.org/[email protected]/types/known/timestamppb#section-documentation)
- `*time.Duration` will now be
[durationpb.Duration](https://pkg.go.dev/google.golang.org/protobuf/types/known/durationpb)
- V2-generated structs embed locks, so you cannot dereference them. `go
vet` will scream at you about this. If you need a copy, use
`proto.Clone`.
- If the performance of this sucks then I will either update our code
generator to add shallow-clone methods or hand-roll the ones we need
- Proto enums will, when formatted to JSON, now be in
`SCREAMING_SNAKE_CASE` rather than `PascalCase`. We decided (in
discussion with the SDK team) that now was as good a time as any to rip
the bandage off.
- Proto objects, or objects embedding protos, cannot be compared using
`reflect.DeepEqual` or _anything_ that uses it. This includes `testify`
and `mock` equality testers!
- You will need to use the `common/testing/protorequire`,
`common/testing/protoassert`, or `common/testing/protomock` packages
instead. I've implemented proto-compatible matchers and assertions there
for all cases I've encountered
- If you need `reflect.DeepEqual` for any reason you can use
`go.temporal.io/api/temporalproto.DeepEqual` instead

Note that history loading will not be impacted by the JSON changes: I
rewrote history loading to dynamically fix incoming history JSON data
(like all our other sdks); you can find this code in [my fork of our go
API](https://github.com/tdeebswihart/temporal-api-go/blob/master/internal/temporalhistoryv1/load.go)
alongside its tests.

**🚨Sharp Edges Introduced🚨**

Beware `*timestamppb.Timestamp.AsTime()`. If you need to extract a time
value from a proto time (timestamppb) **always** make sure to check
whether it's nil first. When the proto object is `nil` `AsTime()` will
return a non-zero time at the proto epoch: UTC midnight on January 1,
1970.

I've made this mistake multiple times during this transition and each
time it's been a pain to debug

**Is hotfix candidate?**

No.
2023-11-21 15:19:37 -08:00

183 lines
6.2 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 persistence_test
import (
"context"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/persistence"
"go.temporal.io/server/service/history/tasks"
)
// TestQueueKey_Determinism tests that the queue name generated from a QueueKey is deterministic. This is important to
// test for because we don't want to accidentally change the queue name generation algorithm and break the mapping of
// queue keys to queue names.
func TestQueueKey_Determinism(t *testing.T) {
name := persistence.QueueKey{
Category: tasks.CategoryTransfer,
SourceCluster: "a",
TargetCluster: "b",
}.GetQueueName()
assert.Equal(t, name, "1_a_b_5aAf7hTg")
}
// TestQueueKey_Conflicts tests that unique tuples of cluster names containing the delimiter character will not produce
// names with conflicts when used to form queue names.
func TestQueueKey_Conflicts(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
explanation string
source1 string
target1 string
source2 string
target2 string
}{
{
name: "(a,b_c) and (a_b,c)",
explanation: "If we just concatenate the cluster names with the queue name delimiter, both of these would" +
" produce the same queue name: 1_a_b_c",
source1: "a",
target1: "b_c",
source2: "a_b",
target2: "c",
},
{
name: "(x_,x) and (x,_x)",
explanation: "If we concatenate the cluster names with the queue name delimiter and a hash of the" +
" concatenated cluster names, both of these would produce the same queue name: 1_x__x_<hash(x_x)>",
source1: "x_",
target1: "x",
source2: "x",
target2: "_x",
},
} {
t.Run(tc.name, func(t *testing.T) {
k1 := persistence.QueueKey{
Category: tasks.CategoryTransfer,
SourceCluster: tc.source1,
TargetCluster: tc.target1,
}.GetQueueName()
k2 := persistence.QueueKey{
Category: tasks.CategoryTransfer,
SourceCluster: tc.source2,
TargetCluster: tc.target2,
}.GetQueueName()
// This test would fail if we did something naive to form the queue key like <category>_<source>_<target>.
assert.NotEqual(t, k1, k2,
"Two pairs of cluster names which are the same when concatenated with the queue name "+
"delimiter should not have the same queue name.", tc.explanation)
})
}
}
func TestHistoryTaskQueueManager_ErrSerializeTaskToEnqueue(t *testing.T) {
t.Parallel()
task := tasks.NewFakeTask(definition.WorkflowKey{}, tasks.Category{}, time.Time{})
m := persistence.NewHistoryTaskQueueManager(nil)
_, err := m.EnqueueTask(context.Background(), &persistence.EnqueueTaskRequest{
Task: task,
SourceShardID: 1,
})
assert.ErrorContains(t, err, persistence.ErrMsgSerializeTaskToEnqueue, "EnqueueTask should return "+
"ErrMsgSerializeTaskToEnqueue when the task cannot be serialized due to an invalid task category")
}
func TestHistoryTaskQueueManager_InvalidShardID(t *testing.T) {
t.Parallel()
task := &tasks.WorkflowTask{}
m := persistence.NewHistoryTaskQueueManager(nil)
_, err := m.EnqueueTask(context.Background(), &persistence.EnqueueTaskRequest{
Task: task,
SourceShardID: 0,
})
assert.ErrorIs(t, err, persistence.ErrShardIDInvalid)
}
// corruptQueue is a QueueV2 implementation that returns a single message that cannot be deserialized into a task.
type corruptQueue struct {
persistence.QueueV2
}
func (f corruptQueue) ReadMessages(
context.Context,
*persistence.InternalReadMessagesRequest,
) (*persistence.InternalReadMessagesResponse, error) {
return &persistence.InternalReadMessagesResponse{
Messages: []persistence.QueueV2Message{
{
Data: &commonpb.DataBlob{
EncodingType: enumspb.ENCODING_TYPE_PROTO3,
Data: []byte("some bytes that cannot be deserialized into a task"),
},
},
},
NextPageToken: nil,
}, nil
}
func TestHistoryTaskQueueManager_ReadTasks_ErrDeserializeRawHistoryTask(t *testing.T) {
t.Parallel()
m := persistence.NewHistoryTaskQueueManager(corruptQueue{})
_, err := m.ReadTasks(context.Background(), &persistence.ReadTasksRequest{
QueueKey: persistence.QueueKey{
Category: tasks.CategoryTransfer,
},
PageSize: 1,
})
assert.ErrorContains(t, err, persistence.ErrMsgDeserializeRawHistoryTask,
"ReadTasks should return ErrMsgDeserializeRawHistoryTask when the raw task cannot be deserialized"+
" due to an error in the persistence layer")
}
func TestHistoryTaskQueueManager_ReadTasks_NonPositivePageSize(t *testing.T) {
t.Parallel()
m := persistence.NewHistoryTaskQueueManager(corruptQueue{})
for _, pageSize := range []int{0, -1} {
_, err := m.ReadTasks(context.Background(), &persistence.ReadTasksRequest{
QueueKey: persistence.QueueKey{
Category: tasks.Category{},
},
PageSize: pageSize,
})
assert.ErrorIs(t, err, persistence.ErrReadTasksNonPositivePageSize, "ReadTasks should return "+
"ErrReadTasksNonPositivePageSize when the request's page size is: "+strconv.Itoa(pageSize))
}
}