/
api.go
152 lines (132 loc) · 5.09 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// 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 addtasks
import (
"context"
"fmt"
commonpb "go.temporal.io/api/common/v1"
"go.temporal.io/api/serviceerror"
"go.temporal.io/server/api/historyservice/v1"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/persistence"
"go.temporal.io/server/service/history/shard"
"go.temporal.io/server/service/history/tasks"
)
type (
// TaskDeserializer is a trimmed version of [go.temporal.io/server/common/persistence/serialization.Serializer] that
// requires only the DeserializeTask method.
TaskDeserializer interface {
DeserializeTask(category tasks.Category, blob commonpb.DataBlob) (tasks.Task, error)
}
)
const (
// maxTasksPerRequest is the maximum number of tasks that can be added in a single AddTasks API call. We set this to
// prevent the history service from OOMing when a client sends a request with a large number of tasks because we
// will deserialize all tasks in memory before adding them to the queue.
maxTasksPerRequest = 1000
)
// Invoke is the implementation of the history service's AddTasks API. This exposes the [shard.Context.AddTasks] API via
// the history service. This method works by batching tasks by workflow run, and then invoking the relevant shard's
// AddTasks API for each task batch. See [historyservice.HistoryServiceClient.AddTasks] for more details. We don't do
// any validation on the shard ID because that must have been done by whoever provided the shard.Context to this method.
func Invoke(
ctx context.Context,
shardContext shard.Context,
deserializer TaskDeserializer,
numShards int,
req *historyservice.AddTasksRequest,
) (*historyservice.AddTasksResponse, error) {
if len(req.Tasks) > maxTasksPerRequest {
return nil, serviceerror.NewInvalidArgument(fmt.Sprintf(
"Too many tasks in request: %d > %d",
len(req.Tasks),
maxTasksPerRequest,
))
}
if len(req.Tasks) == 0 {
return nil, serviceerror.NewInvalidArgument("No tasks in request")
}
taskBatches := make(map[definition.WorkflowKey]map[tasks.Category][]tasks.Task)
for i, task := range req.Tasks {
if task == nil {
return nil, serviceerror.NewInvalidArgument(fmt.Sprintf("Nil task at index: %d", i))
}
category, ok := getCategoryByName(task.Category)
if !ok {
return nil, serviceerror.NewInvalidArgument(fmt.Sprintf(
"Invalid task category: %s",
task.Category,
))
}
if task.Blob == nil {
return nil, serviceerror.NewInvalidArgument(fmt.Sprintf(
"Task blob is nil at index: %d",
i,
))
}
deserializedTask, err := deserializer.DeserializeTask(category, *task.Blob)
if err != nil {
return nil, err
}
shardID := tasks.GetShardIDForTask(deserializedTask, numShards)
if shardID != int(req.ShardId) {
return nil, serviceerror.NewInvalidArgument(fmt.Sprintf(
"Task is for wrong shard: index = %d, task shard = %d, request shard = %d",
i, shardID, req.ShardId,
))
}
workflowKey := definition.NewWorkflowKey(
deserializedTask.GetNamespaceID(),
deserializedTask.GetWorkflowID(),
deserializedTask.GetRunID(),
)
if _, ok := taskBatches[workflowKey]; !ok {
taskBatches[workflowKey] = make(map[tasks.Category][]tasks.Task, 1)
}
taskBatches[workflowKey][category] = append(taskBatches[workflowKey][category], deserializedTask)
}
for workflowKey, taskBatch := range taskBatches {
err := shardContext.AddTasks(ctx, &persistence.AddHistoryTasksRequest{
ShardID: shardContext.GetShardID(),
RangeID: shardContext.GetRangeID(),
NamespaceID: workflowKey.NamespaceID,
WorkflowID: workflowKey.WorkflowID,
RunID: workflowKey.RunID,
Tasks: taskBatch,
})
if err != nil {
return nil, err
}
}
return &historyservice.AddTasksResponse{}, nil
}
func getCategoryByName(categoryName string) (tasks.Category, bool) {
categories := tasks.GetCategories()
for _, category := range categories {
if category.Name() == categoryName {
return category, true
}
}
return tasks.Category{}, false
}