-
Notifications
You must be signed in to change notification settings - Fork 811
/
visibility_archiver.go
383 lines (350 loc) · 13.9 KB
/
visibility_archiver.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// 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 s3store
import (
"context"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"go.temporal.io/api/serviceerror"
workflowpb "go.temporal.io/api/workflow/v1"
"go.temporal.io/server/common/searchattribute"
archiverspb "go.temporal.io/server/api/archiver/v1"
"go.temporal.io/server/common/archiver"
"go.temporal.io/server/common/config"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/primitives/timestamp"
)
type (
visibilityArchiver struct {
container *archiver.VisibilityBootstrapContainer
s3cli s3iface.S3API
queryParser QueryParser
}
queryVisibilityRequest struct {
namespaceID string
pageSize int
nextPageToken []byte
parsedQuery *parsedQuery
}
indexToArchive struct {
primaryIndex string
primaryIndexValue string
secondaryIndex string
secondaryIndexTimestamp time.Time
}
)
const (
errEncodeVisibilityRecord = "failed to encode visibility record"
secondaryIndexKeyStartTimeout = "startTimeout"
secondaryIndexKeyCloseTimeout = "closeTimeout"
primaryIndexKeyWorkflowTypeName = "workflowTypeName"
primaryIndexKeyWorkflowID = "workflowID"
)
// NewVisibilityArchiver creates a new archiver.VisibilityArchiver based on s3
func NewVisibilityArchiver(
container *archiver.VisibilityBootstrapContainer,
config *config.S3Archiver,
) (archiver.VisibilityArchiver, error) {
return newVisibilityArchiver(container, config)
}
func newVisibilityArchiver(
container *archiver.VisibilityBootstrapContainer,
config *config.S3Archiver) (*visibilityArchiver, error) {
s3Config := &aws.Config{
Endpoint: config.Endpoint,
Region: aws.String(config.Region),
S3ForcePathStyle: aws.Bool(config.S3ForcePathStyle),
LogLevel: (*aws.LogLevelType)(&config.LogLevel),
}
sess, err := session.NewSession(s3Config)
if err != nil {
return nil, err
}
return &visibilityArchiver{
container: container,
s3cli: s3.New(sess),
queryParser: NewQueryParser(),
}, nil
}
func (v *visibilityArchiver) Archive(
ctx context.Context,
URI archiver.URI,
request *archiverspb.VisibilityRecord,
opts ...archiver.ArchiveOption,
) (err error) {
handler := v.container.MetricsHandler.WithTags(metrics.OperationTag(metrics.VisibilityArchiverScope), metrics.NamespaceTag(request.Namespace))
featureCatalog := archiver.GetFeatureCatalog(opts...)
startTime := time.Now().UTC()
logger := archiver.TagLoggerWithArchiveVisibilityRequestAndURI(v.container.Logger, request, URI.String())
archiveFailReason := ""
defer func() {
handler.Timer(metrics.ServiceLatency.Name()).Record(time.Since(startTime))
if err != nil {
if isRetryableError(err) {
handler.Counter(metrics.VisibilityArchiverArchiveTransientErrorCount.Name()).Record(1)
logger.Error(archiver.ArchiveTransientErrorMsg, tag.ArchivalArchiveFailReason(archiveFailReason), tag.Error(err))
} else {
handler.Counter(metrics.VisibilityArchiverArchiveNonRetryableErrorCount.Name()).Record(1)
logger.Error(archiver.ArchiveNonRetryableErrorMsg, tag.ArchivalArchiveFailReason(archiveFailReason), tag.Error(err))
if featureCatalog.NonRetryableError != nil {
err = featureCatalog.NonRetryableError()
}
}
}
}()
if err := SoftValidateURI(URI); err != nil {
archiveFailReason = archiver.ErrReasonInvalidURI
return err
}
if err := archiver.ValidateVisibilityArchivalRequest(request); err != nil {
archiveFailReason = archiver.ErrReasonInvalidArchiveRequest
return err
}
encodedVisibilityRecord, err := Encode(request)
if err != nil {
archiveFailReason = errEncodeVisibilityRecord
return err
}
indexes := createIndexesToArchive(request)
// Upload archive to all indexes
for _, element := range indexes {
key := constructTimestampIndex(URI.Path(), request.GetNamespaceId(), element.primaryIndex, element.primaryIndexValue, element.secondaryIndex, element.secondaryIndexTimestamp, request.GetRunId())
if err := Upload(ctx, v.s3cli, URI, key, encodedVisibilityRecord); err != nil {
archiveFailReason = errWriteKey
return err
}
}
handler.Counter(metrics.VisibilityArchiveSuccessCount.Name()).Record(1)
return nil
}
func createIndexesToArchive(request *archiverspb.VisibilityRecord) []indexToArchive {
return []indexToArchive{
{primaryIndexKeyWorkflowTypeName, request.WorkflowTypeName, secondaryIndexKeyCloseTimeout, timestamp.TimeValue(request.CloseTime)},
{primaryIndexKeyWorkflowTypeName, request.WorkflowTypeName, secondaryIndexKeyStartTimeout, timestamp.TimeValue(request.StartTime)},
{primaryIndexKeyWorkflowID, request.GetWorkflowId(), secondaryIndexKeyCloseTimeout, timestamp.TimeValue(request.CloseTime)},
{primaryIndexKeyWorkflowID, request.GetWorkflowId(), secondaryIndexKeyStartTimeout, timestamp.TimeValue(request.StartTime)},
}
}
func (v *visibilityArchiver) Query(
ctx context.Context,
URI archiver.URI,
request *archiver.QueryVisibilityRequest,
saTypeMap searchattribute.NameTypeMap,
) (*archiver.QueryVisibilityResponse, error) {
if err := SoftValidateURI(URI); err != nil {
return nil, serviceerror.NewInvalidArgument(archiver.ErrInvalidURI.Error())
}
if err := archiver.ValidateQueryRequest(request); err != nil {
return nil, serviceerror.NewInvalidArgument(archiver.ErrInvalidQueryVisibilityRequest.Error())
}
if strings.TrimSpace(request.Query) == "" {
return v.queryAll(ctx, URI, request, saTypeMap)
}
parsedQuery, err := v.queryParser.Parse(request.Query)
if err != nil {
return nil, serviceerror.NewInvalidArgument(err.Error())
}
return v.query(
ctx,
URI,
&queryVisibilityRequest{
namespaceID: request.NamespaceID,
pageSize: request.PageSize,
nextPageToken: request.NextPageToken,
parsedQuery: parsedQuery,
},
saTypeMap,
)
}
// queryAll returns all workflow executions in the archive.
func (v *visibilityArchiver) queryAll(
ctx context.Context,
uri archiver.URI,
request *archiver.QueryVisibilityRequest,
saTypeMap searchattribute.NameTypeMap,
) (*archiver.QueryVisibilityResponse, error) {
// remaining is the number of workflow executions left to return before we reach pageSize.
remaining := request.PageSize
nextPageToken := request.NextPageToken
var executions []*workflowpb.WorkflowExecutionInfo
// We need to loop because the number of workflow executions returned by each call to query may be fewer than
// pageSize. This is because we may have to skip some workflow executions after querying S3 (client-side filtering)
// because there are 2 entries in S3 for each workflow execution indexed by workflowTypeName (one for closeTimeout
// and one for startTimeout), and we only want to return one entry per workflow execution. See
// createIndexesToArchive for a list of all indexes.
for {
searchPrefix := constructVisibilitySearchPrefix(uri.Path(), request.NamespaceID)
// We suffix searchPrefix with workflowTypeName because the data in S3 is duplicated across combinations of 2
// different primary indices (workflowID and workflowTypeName) and 2 different secondary indices (closeTimeout
// and startTimeout). We only want to return one entry per workflow execution, but the full path to the S3 key
// is <primaryIndexKey>/<primaryIndexValue>/<secondaryIndexKey>/<secondaryIndexValue>/<runID>, and we don't have
// the primaryIndexValue when we make the call to query, so we can only specify the primaryIndexKey.
searchPrefix += "/" + primaryIndexKeyWorkflowTypeName
// The pageSize we supply here is actually the maximum number of keys to fetch from S3. For each execution,
// there should be 2 keys in S3 for this prefix, so you might think that we should multiply the pageSize by 2.
// However, if we do that, we may end up returning more than pageSize workflow executions to the end user of
// this API. This is because we aren't guaranteed that both keys for a given workflow execution will be returned
// in the same call. For example, if the user supplies a pageSize of 1, and we specify a maximum number of keys
// of 2 to S3, we may get back entries from S3 for 2 different workflow executions. You might think that we can
// just truncate this result to 1 workflow execution, but then the nextPageToken would be incorrect. So, we may
// need to make multiple calls to S3 to get the correct number of workflow executions, which will probably make
// this API call slower.
res, err := v.queryPrefix(ctx, uri, &queryVisibilityRequest{
namespaceID: request.NamespaceID,
pageSize: remaining,
nextPageToken: nextPageToken,
parsedQuery: &parsedQuery{},
}, saTypeMap, searchPrefix, func(key string) bool {
// We only want to return entries for the closeTimeout secondary index, which will always be of the form:
// .../closeTimeout/<closeTimeout>/<runID>, so we split the key on "/" and check that the third-to-last
// element is "closeTimeout".
elements := strings.Split(key, "/")
return len(elements) >= 3 && elements[len(elements)-3] == secondaryIndexKeyCloseTimeout
})
if err != nil {
return nil, err
}
nextPageToken = res.NextPageToken
executions = append(executions, res.Executions...)
remaining -= len(res.Executions)
if len(nextPageToken) == 0 || remaining <= 0 {
break
}
}
return &archiver.QueryVisibilityResponse{
Executions: executions,
NextPageToken: nextPageToken,
}, nil
}
func (v *visibilityArchiver) query(
ctx context.Context,
URI archiver.URI,
request *queryVisibilityRequest,
saTypeMap searchattribute.NameTypeMap,
) (*archiver.QueryVisibilityResponse, error) {
primaryIndex := primaryIndexKeyWorkflowTypeName
primaryIndexValue := request.parsedQuery.workflowTypeName
if request.parsedQuery.workflowID != nil {
primaryIndex = primaryIndexKeyWorkflowID
primaryIndexValue = request.parsedQuery.workflowID
}
prefix := constructIndexedVisibilitySearchPrefix(
URI.Path(),
request.namespaceID,
primaryIndex,
*primaryIndexValue,
secondaryIndexKeyCloseTimeout,
) + "/"
if request.parsedQuery.closeTime != nil {
prefix = constructTimeBasedSearchKey(
URI.Path(),
request.namespaceID,
primaryIndex,
*primaryIndexValue,
secondaryIndexKeyCloseTimeout,
*request.parsedQuery.closeTime,
*request.parsedQuery.searchPrecision,
)
}
if request.parsedQuery.startTime != nil {
prefix = constructTimeBasedSearchKey(
URI.Path(),
request.namespaceID,
primaryIndex,
*primaryIndexValue,
secondaryIndexKeyStartTimeout,
*request.parsedQuery.startTime,
*request.parsedQuery.searchPrecision,
)
}
return v.queryPrefix(ctx, URI, request, saTypeMap, prefix, nil)
}
// queryPrefix returns all workflow executions in the archive that match the given prefix. The keyFilter function is an
// optional filter that can be used to further filter the results. If keyFilter returns false for a given key, that key
// will be skipped, and the object will not be downloaded from S3 or included in the results.
func (v *visibilityArchiver) queryPrefix(
ctx context.Context,
uri archiver.URI,
request *queryVisibilityRequest,
saTypeMap searchattribute.NameTypeMap,
prefix string,
keyFilter func(key string) bool,
) (*archiver.QueryVisibilityResponse, error) {
ctx, cancel := ensureContextTimeout(ctx)
defer cancel()
var token *string
if request.nextPageToken != nil {
token = deserializeQueryVisibilityToken(request.nextPageToken)
}
results, err := v.s3cli.ListObjectsV2WithContext(ctx, &s3.ListObjectsV2Input{
Bucket: aws.String(uri.Hostname()),
Prefix: aws.String(prefix),
MaxKeys: aws.Int64(int64(request.pageSize)),
ContinuationToken: token,
})
if err != nil {
if isRetryableError(err) {
return nil, serviceerror.NewUnavailable(err.Error())
}
return nil, serviceerror.NewInvalidArgument(err.Error())
}
if len(results.Contents) == 0 {
return &archiver.QueryVisibilityResponse{}, nil
}
response := &archiver.QueryVisibilityResponse{}
if *results.IsTruncated {
response.NextPageToken = serializeQueryVisibilityToken(*results.NextContinuationToken)
}
for _, item := range results.Contents {
if keyFilter != nil && !keyFilter(*item.Key) {
continue
}
encodedRecord, err := Download(ctx, v.s3cli, uri, *item.Key)
if err != nil {
return nil, serviceerror.NewUnavailable(err.Error())
}
record, err := decodeVisibilityRecord(encodedRecord)
if err != nil {
return nil, serviceerror.NewInternal(err.Error())
}
executionInfo, err := convertToExecutionInfo(record, saTypeMap)
if err != nil {
return nil, serviceerror.NewInternal(err.Error())
}
response.Executions = append(response.Executions, executionInfo)
}
return response, nil
}
func (v *visibilityArchiver) ValidateURI(URI archiver.URI) error {
err := SoftValidateURI(URI)
if err != nil {
return err
}
return BucketExists(context.TODO(), v.s3cli, URI)
}