-
Notifications
You must be signed in to change notification settings - Fork 753
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a replication.DLQWriter interface
- Loading branch information
1 parent
ef2deea
commit 2d1905d
Showing
6 changed files
with
331 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// 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 replication | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"go.temporal.io/server/common/namespace" | ||
"go.temporal.io/server/common/persistence" | ||
"go.temporal.io/server/common/persistence/serialization" | ||
"go.temporal.io/server/service/history/tasks" | ||
) | ||
|
||
type ( | ||
// DLQWriter is an interface that can be implemented easily by the two different queue solutions that we have. | ||
// - Queue V1 implements this interface via [persistence.ExecutionManager]. | ||
// - Queue V2 will implement this interface via [go.temporal.io/server/service/history/queues.DLQWriter]. | ||
// | ||
// We want this interface to make the migration referenced by [persistence.QueueV2] easier. | ||
DLQWriter interface { | ||
WriteTaskToDLQ(ctx context.Context, request WriteRequest) error | ||
} | ||
// WriteRequest is a request to write a task to the DLQ. | ||
WriteRequest struct { | ||
SourceCluster string | ||
NamespaceID string | ||
WorkflowID string | ||
Task tasks.Task | ||
} | ||
// ExecutionManager is an interface that is implemented by [persistence.ExecutionManager] that contains the | ||
// only method that we need to implement the [DLQWriter] interface. | ||
ExecutionManager interface { | ||
PutReplicationTaskToDLQ(ctx context.Context, request *persistence.PutReplicationTaskToDLQRequest) error | ||
} | ||
// ShardContext is a [go.temporal.io/server/service/history/shard.Context] interface that contains the only method | ||
// that we need to implement the [DLQWriter] interface. | ||
ShardContext[T ExecutionManager] interface { | ||
GetExecutionManager() T | ||
} | ||
// ShardController is a [go.temporal.io/server/service/history/shard.Controller] interface that contains the only | ||
// method that we need to implement the [DLQWriter] interface. | ||
ShardController[T ExecutionManager, U ShardContext[T]] interface { | ||
GetShardByNamespaceWorkflow(namespaceID namespace.ID, workflowID string) (U, error) | ||
} | ||
// This type adapts an [ShardController] to the [DLQWriter] interface. | ||
shardControllerDLQWriter[T ExecutionManager, U ShardContext[T], V ShardController[T, U]] struct { | ||
shardController V | ||
numHistoryShards int | ||
} | ||
) | ||
|
||
var ( | ||
ErrInvalidNumHistoryShards = errors.New("invalid number of history shards") | ||
ErrNonReplicationTask = errors.New("task to be written to DLQ is not a replication task") | ||
) | ||
|
||
// NewShardControllerDLQWriter creates a new DLQWriter that writes to the DLQ using an [ExecutionManager] provided by | ||
// the given shard controller. There's a lot of generics going on here, so let's break it down: | ||
// - [T] is the type of the [ExecutionManager] that the [ShardContext] provides. | ||
// - [U] is the type of the [ShardContext] that the [ShardController] provides. | ||
// - [V] is the type of the [ShardController] that we're adapting to the [DLQWriter] interface. | ||
// | ||
// Why do we need all of these generics? Because we want to decouple this from the shard package, and make it easier to | ||
// write tests without leaving methods unimplemented. | ||
func NewShardControllerDLQWriter[T ExecutionManager, U ShardContext[T], V ShardController[T, U]]( | ||
shardController V, | ||
numHistoryShards int, | ||
) (DLQWriter, error) { | ||
if numHistoryShards <= 0 { | ||
return nil, fmt.Errorf("%w: %v", ErrInvalidNumHistoryShards, numHistoryShards) | ||
} | ||
return &shardControllerDLQWriter[T, U, V]{ | ||
shardController: shardController, | ||
numHistoryShards: numHistoryShards, | ||
}, nil | ||
} | ||
|
||
// WriteTaskToDLQ implements [DLQWriter.WriteTaskToDLQ] by calling [ExecutionManager.PutReplicationTaskToDLQ]. | ||
// It returns an error if the task is not a replication task. | ||
func (e *shardControllerDLQWriter[T, U, V]) WriteTaskToDLQ( | ||
ctx context.Context, | ||
request WriteRequest, | ||
) error { | ||
task := request.Task | ||
shardID := tasks.GetShardIDForTask(task, e.numHistoryShards) | ||
replicationTaskInfo, ok := serialization.ReplicationTaskInfoFromTask(task) | ||
if !ok { | ||
return fmt.Errorf("%w: %T, shardID: %v, taskID: %v", ErrNonReplicationTask, task, shardID, task.GetTaskID()) | ||
} | ||
shardContext, err := e.shardController.GetShardByNamespaceWorkflow( | ||
namespace.ID(request.NamespaceID), | ||
request.WorkflowID, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
executionManager := shardContext.GetExecutionManager() | ||
return executionManager.PutReplicationTaskToDLQ(ctx, &persistence.PutReplicationTaskToDLQRequest{ | ||
SourceClusterName: request.SourceCluster, | ||
ShardID: int32(shardID), | ||
TaskInfo: replicationTaskInfo, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// 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 replication_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.temporal.io/server/common/namespace" | ||
"go.temporal.io/server/common/persistence" | ||
"go.temporal.io/server/service/history/replication" | ||
"go.temporal.io/server/service/history/tasks" | ||
) | ||
|
||
type ( | ||
fakeShardController struct { | ||
requests []*persistence.PutReplicationTaskToDLQRequest | ||
getShardErr error | ||
} | ||
) | ||
|
||
func TestNewExecutionManagerDLQWriter_ErrInvalidNumHistoryShards(t *testing.T) { | ||
t.Parallel() | ||
|
||
_, err := newDLQWriter(newFakeShardController(), 0) | ||
assert.ErrorIs(t, err, replication.ErrInvalidNumHistoryShards) | ||
} | ||
|
||
func TestNewExecutionManagerDLQWriter_NonReplicationTask(t *testing.T) { | ||
t.Parallel() | ||
|
||
shardController := newFakeShardController() | ||
numHistoryShards := 1 | ||
writer, err := newDLQWriter(shardController, numHistoryShards) | ||
require.NoError(t, err) | ||
err = writer.WriteTaskToDLQ(context.Background(), replication.WriteRequest{ | ||
Task: &tasks.WorkflowTask{ | ||
TaskID: 42, | ||
}, | ||
}) | ||
require.ErrorIs(t, err, replication.ErrNonReplicationTask) | ||
assert.ErrorContains(t, err, "42") | ||
} | ||
|
||
func TestNewExecutionManagerDLQWriter_ReplicationTask(t *testing.T) { | ||
t.Parallel() | ||
|
||
shardController := newFakeShardController() | ||
writer, err := replication.NewShardControllerDLQWriter[*fakeShardController, *fakeShardController]( | ||
shardController, | ||
1, | ||
) | ||
require.NoError(t, err) | ||
err = writer.WriteTaskToDLQ(context.Background(), replication.WriteRequest{ | ||
Task: &tasks.HistoryReplicationTask{ | ||
TaskID: 42, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
if assert.Len(t, shardController.requests, 1) { | ||
assert.Equal(t, int32(1), shardController.requests[0].ShardID) | ||
assert.Equal(t, int64(42), shardController.requests[0].TaskInfo.GetTaskId()) | ||
} | ||
} | ||
|
||
func TestNewExecutionManagerDLQWriter_GetShardErr(t *testing.T) { | ||
t.Parallel() | ||
|
||
shardController := newFakeShardController() | ||
shardController.getShardErr = assert.AnError | ||
writer, err := replication.NewShardControllerDLQWriter[*fakeShardController, *fakeShardController]( | ||
shardController, | ||
1, | ||
) | ||
require.NoError(t, err) | ||
err = writer.WriteTaskToDLQ(context.Background(), replication.WriteRequest{ | ||
Task: &tasks.HistoryReplicationTask{ | ||
TaskID: 42, | ||
}, | ||
}) | ||
require.ErrorIs(t, err, assert.AnError) | ||
assert.Empty(t, shardController.requests) | ||
} | ||
|
||
func newDLQWriter(shardController *fakeShardController, numHistoryShards int) (replication.DLQWriter, error) { | ||
writer, err := replication.NewShardControllerDLQWriter[*fakeShardController, *fakeShardController]( | ||
shardController, | ||
numHistoryShards, | ||
) | ||
return writer, err | ||
} | ||
|
||
func newFakeShardController() *fakeShardController { | ||
return &fakeShardController{} | ||
} | ||
|
||
func (f *fakeShardController) GetExecutionManager() *fakeShardController { | ||
return f | ||
} | ||
|
||
func (f *fakeShardController) GetShardByNamespaceWorkflow(namespace.ID, string) (*fakeShardController, error) { | ||
if f.getShardErr != nil { | ||
return nil, f.getShardErr | ||
} | ||
return f, nil | ||
} | ||
|
||
func (f *fakeShardController) PutReplicationTaskToDLQ( | ||
_ context.Context, | ||
request *persistence.PutReplicationTaskToDLQRequest, | ||
) error { | ||
f.requests = append(f.requests, request) | ||
return nil | ||
} |
Oops, something went wrong.