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

Write tests for cdb.InsertWorkflowExecutionWithTasks #5688

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
62 changes: 12 additions & 50 deletions common/persistence/nosql/nosqlplugin/cassandra/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,11 @@ func (db *cdb) InsertWorkflowExecutionWithTasks(
return err
}

err = createTransferTasks(batch, shardID, domainID, workflowID, transferTasks)
if err != nil {
return err
}
err = createReplicationTasks(batch, shardID, domainID, workflowID, replicationTasks)
if err != nil {
return err
}
err = createCrossClusterTasks(batch, shardID, domainID, workflowID, crossClusterTasks)
if err != nil {
return err
}
err = createTimerTasks(batch, shardID, domainID, workflowID, timerTasks)
if err != nil {
return err
}

err = assertShardRangeID(batch, shardID, shardCondition.RangeID)
if err != nil {
return err
}
createTransferTasks(batch, shardID, domainID, workflowID, transferTasks)
createReplicationTasks(batch, shardID, domainID, workflowID, replicationTasks)
createCrossClusterTasks(batch, shardID, domainID, workflowID, crossClusterTasks)
createTimerTasks(batch, shardID, domainID, workflowID, timerTasks)
assertShardRangeID(batch, shardID, shardCondition.RangeID)

return executeCreateWorkflowBatchTransaction(db.session, batch, currentWorkflowRequest, execution, shardCondition)
}
Expand Down Expand Up @@ -179,27 +163,11 @@ func (db *cdb) UpdateWorkflowExecutionWithTasks(
}
}

err = createTransferTasks(batch, shardID, domainID, workflowID, transferTasks)
if err != nil {
return err
}
err = createReplicationTasks(batch, shardID, domainID, workflowID, replicationTasks)
if err != nil {
return err
}
err = createCrossClusterTasks(batch, shardID, domainID, workflowID, crossClusterTasks)
if err != nil {
return err
}
err = createTimerTasks(batch, shardID, domainID, workflowID, timerTasks)
if err != nil {
return err
}

err = assertShardRangeID(batch, shardID, shardCondition.RangeID)
if err != nil {
return err
}
createTransferTasks(batch, shardID, domainID, workflowID, transferTasks)
createReplicationTasks(batch, shardID, domainID, workflowID, replicationTasks)
createCrossClusterTasks(batch, shardID, domainID, workflowID, crossClusterTasks)
createTimerTasks(batch, shardID, domainID, workflowID, timerTasks)
assertShardRangeID(batch, shardID, shardCondition.RangeID)

return executeUpdateWorkflowBatchTransaction(db.session, batch, currentWorkflowRequest, previousNextEventIDCondition, shardCondition)
}
Expand Down Expand Up @@ -751,16 +719,10 @@ func (db *cdb) InsertReplicationTask(ctx context.Context, tasks []*nosqlplugin.R
shardID := shardCondition.ShardID
batch := db.session.NewBatch(gocql.LoggedBatch).WithContext(ctx)
for _, task := range tasks {
err := createReplicationTasks(batch, shardID, task.DomainID, task.WorkflowID, []*nosqlplugin.ReplicationTask{task})
if err != nil {
return err
}
createReplicationTasks(batch, shardID, task.DomainID, task.WorkflowID, []*nosqlplugin.ReplicationTask{task})
}

err := assertShardRangeID(batch, shardID, shardCondition.RangeID)
if err != nil {
return err
}
assertShardRangeID(batch, shardID, shardCondition.RangeID)

previous := make(map[string]interface{})
applied, iter, err := db.session.MapExecuteBatchCAS(batch, previous)
Expand Down
220 changes: 220 additions & 0 deletions common/persistence/nosql/nosqlplugin/cassandra/workflow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
// Copyright (c) 2021 Uber Technologies, Inc.
// Portions of the Software are attributed to Copyright (c) 2020 Temporal 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 cassandra

import (
"context"
"errors"
"testing"

"github.com/golang/mock/gomock"

"github.com/uber/cadence/common"
"github.com/uber/cadence/common/checksum"
"github.com/uber/cadence/common/config"
"github.com/uber/cadence/common/log/testlogger"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/persistence/nosql/nosqlplugin"
"github.com/uber/cadence/common/persistence/nosql/nosqlplugin/cassandra/gocql"
)

func TestInsertWorkflowExecutionWithTasks(t *testing.T) {
tests := []struct {
name string
request *nosqlplugin.CurrentWorkflowWriteRequest
execution *nosqlplugin.WorkflowExecutionRequest
transferTasks []*nosqlplugin.TransferTask
crossClusterTasks []*nosqlplugin.CrossClusterTask
replicationTasks []*nosqlplugin.ReplicationTask
timerTasks []*nosqlplugin.TimerTask
shardCondition *nosqlplugin.ShardCondition
mapExecuteBatchCASErr error
wantErr bool
}{
{
name: "success",
request: &nosqlplugin.CurrentWorkflowWriteRequest{
WriteMode: nosqlplugin.CurrentWorkflowWriteModeNoop,
},
shardCondition: &nosqlplugin.ShardCondition{
ShardID: 1,
},
execution: &nosqlplugin.WorkflowExecutionRequest{
InternalWorkflowExecutionInfo: persistence.InternalWorkflowExecutionInfo{
DomainID: "test-domain-id",
WorkflowID: "test-workflow-id",
CompletionEvent: &persistence.DataBlob{
Encoding: common.EncodingTypeThriftRW,
Data: []byte("test-completion-event"),
},
AutoResetPoints: &persistence.DataBlob{
Encoding: common.EncodingTypeThriftRW,
Data: []byte("test-auto-reset-points"),
},
},
VersionHistories: &persistence.DataBlob{
Encoding: common.EncodingTypeThriftRW,
Data: []byte("test-version-histories"),
},
Checksums: &checksum.Checksum{
Version: 1,
Flavor: checksum.FlavorIEEECRC32OverThriftBinary,
Value: []byte("test-checksum"),
},
},
},
{
name: "createOrUpdateCurrentWorkflow step fails",
request: &nosqlplugin.CurrentWorkflowWriteRequest{
WriteMode: nosqlplugin.CurrentWorkflowWriteMode(-999), // unknown mode will cause failure
},
shardCondition: &nosqlplugin.ShardCondition{
ShardID: 1,
},
execution: &nosqlplugin.WorkflowExecutionRequest{
InternalWorkflowExecutionInfo: persistence.InternalWorkflowExecutionInfo{
DomainID: "test-domain-id",
WorkflowID: "test-workflow-id",
CompletionEvent: &persistence.DataBlob{
Encoding: common.EncodingTypeThriftRW,
Data: []byte("test-completion-event"),
},
AutoResetPoints: &persistence.DataBlob{
Encoding: common.EncodingTypeThriftRW,
Data: []byte("test-auto-reset-points"),
},
},
VersionHistories: &persistence.DataBlob{
Encoding: common.EncodingTypeThriftRW,
Data: []byte("test-version-histories"),
},
Checksums: &checksum.Checksum{
Version: 1,
Flavor: checksum.FlavorIEEECRC32OverThriftBinary,
Value: []byte("test-checksum"),
},
},
wantErr: true,
},
{
name: "createWorkflowExecutionWithMergeMaps step fails",
request: &nosqlplugin.CurrentWorkflowWriteRequest{
WriteMode: nosqlplugin.CurrentWorkflowWriteModeNoop,
},
shardCondition: &nosqlplugin.ShardCondition{
ShardID: 1,
},
execution: &nosqlplugin.WorkflowExecutionRequest{
EventBufferWriteMode: nosqlplugin.EventBufferWriteModeAppend, // this will cause failure
InternalWorkflowExecutionInfo: persistence.InternalWorkflowExecutionInfo{
DomainID: "test-domain-id",
WorkflowID: "test-workflow-id",
CompletionEvent: &persistence.DataBlob{
Encoding: common.EncodingTypeThriftRW,
Data: []byte("test-completion-event"),
},
AutoResetPoints: &persistence.DataBlob{
Encoding: common.EncodingTypeThriftRW,
Data: []byte("test-auto-reset-points"),
},
},
VersionHistories: &persistence.DataBlob{
Encoding: common.EncodingTypeThriftRW,
Data: []byte("test-version-histories"),
},
Checksums: &checksum.Checksum{
Version: 1,
Flavor: checksum.FlavorIEEECRC32OverThriftBinary,
Value: []byte("test-checksum"),
},
},
wantErr: true,
},
{
name: "executeCreateWorkflowBatchTransaction step fails",
mapExecuteBatchCASErr: errors.New("some random error"), // this will cause failure
request: &nosqlplugin.CurrentWorkflowWriteRequest{
WriteMode: nosqlplugin.CurrentWorkflowWriteModeNoop,
},
shardCondition: &nosqlplugin.ShardCondition{
ShardID: 1,
},
execution: &nosqlplugin.WorkflowExecutionRequest{
EventBufferWriteMode: nosqlplugin.EventBufferWriteModeNone,
InternalWorkflowExecutionInfo: persistence.InternalWorkflowExecutionInfo{
DomainID: "test-domain-id",
WorkflowID: "test-workflow-id",
CompletionEvent: &persistence.DataBlob{
Encoding: common.EncodingTypeThriftRW,
Data: []byte("test-completion-event"),
},
AutoResetPoints: &persistence.DataBlob{
Encoding: common.EncodingTypeThriftRW,
Data: []byte("test-auto-reset-points"),
},
},
VersionHistories: &persistence.DataBlob{
Encoding: common.EncodingTypeThriftRW,
Data: []byte("test-version-histories"),
},
Checksums: &checksum.Checksum{
Version: 1,
Flavor: checksum.FlavorIEEECRC32OverThriftBinary,
Value: []byte("test-checksum"),
},
},
wantErr: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
session := &fakeSession{
iter: &fakeIter{},
mapExecuteBatchCASApplied: true,
mapExecuteBatchCASErr: tc.mapExecuteBatchCASErr,
}
client := gocql.NewMockClient(ctrl)
cfg := &config.NoSQL{}
logger := testlogger.New(t)
dc := &persistence.DynamicConfiguration{}

db := newCassandraDBFromSession(cfg, session, logger, dc, dbWithClient(client))

err := db.InsertWorkflowExecutionWithTasks(
context.Background(),
tc.request,
tc.execution,
tc.transferTasks,
tc.crossClusterTasks,
tc.replicationTasks,
tc.timerTasks,
tc.shardCondition,
)

if (err != nil) != tc.wantErr {
t.Errorf("InsertWorkflowExecutionWithTasks() error = %v, wantErr %v", err, tc.wantErr)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func newUnknownConditionFailureReason(
}
}

func assertShardRangeID(batch gocql.Batch, shardID int, rangeID int64) error {
func assertShardRangeID(batch gocql.Batch, shardID int, rangeID int64) {
batch.Query(templateUpdateLeaseQuery,
rangeID,
shardID,
Expand All @@ -304,7 +304,6 @@ func assertShardRangeID(batch gocql.Batch, shardID int, rangeID int64) error {
rowTypeShardTaskID,
rangeID,
)
return nil
}

func createTimerTasks(
Expand Down Expand Up @@ -346,7 +345,7 @@ func createReplicationTasks(
domainID string,
workflowID string,
replicationTasks []*nosqlplugin.ReplicationTask,
) error {
) {
for _, task := range replicationTasks {
batch.Query(templateCreateReplicationTaskQuery,
shardID,
Expand All @@ -372,7 +371,6 @@ func createReplicationTasks(
defaultVisibilityTimestamp,
task.TaskID)
}
return nil
}

func createTransferTasks(
Expand All @@ -381,7 +379,7 @@ func createTransferTasks(
domainID string,
workflowID string,
transferTasks []*nosqlplugin.TransferTask,
) error {
) {
for _, task := range transferTasks {
batch.Query(templateCreateTransferTaskQuery,
shardID,
Expand All @@ -408,7 +406,6 @@ func createTransferTasks(
defaultVisibilityTimestamp,
task.TaskID)
}
return nil
}

func createCrossClusterTasks(
Expand All @@ -417,7 +414,7 @@ func createCrossClusterTasks(
domainID string,
workflowID string,
xClusterTasks []*nosqlplugin.CrossClusterTask,
) error {
) {
for _, task := range xClusterTasks {
batch.Query(templateCreateCrossClusterTaskQuery,
shardID,
Expand Down Expand Up @@ -445,7 +442,6 @@ func createCrossClusterTasks(
task.TaskID,
)
}
return nil
}

func resetSignalsRequested(
Expand Down
Loading