Files
tasks/common/archiver/gcloud/visibility_archiver.go
Michael SnowdenandGitHub d5c62e926f Make all Go source file names snake case (#4558)
<!-- Describe what has changed in this PR -->
**What changed?**
I renamed all of our Go source code files so that they're snake case
instead of camel case.

<!-- Tell your future self why have you made these changes -->
**Why?**
This was an annoying inconsistency we carried over from much older code
in the repo.

<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->
**How did you test it?**
The renaming process was part of an automated script
(https://gist.github.com/MichaelSnowden/47576fffe27aa1cae6b26eaccec7ab55),
so it should be consistent. Also, I don't see us doing anything in our
codebase that involves parsing our own source files by path:

<img width="687" alt="image"
src="https://github.com/temporalio/temporal/assets/5942963/c6b873b0-d841-40d1-8b0f-2fe9d8079a53">

*PS: here's a link to the GPT convo I had to generate this renaming
script:
https://chat.openai.com/share/7f2d011a-a8da-4fa4-9c6d-e09102fdd49f*

<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->
**Potential risks**
Mainly merge conflicts, but I used `git mv` so that these are renames
instead of deletes/adds, so that shouldn't be a big issue.

<!-- Is this PR a hotfix candidate or require that a notification be
sent to the broader community? (Yes/No) -->
**Is hotfix candidate?**
No.
2023-07-03 14:23:56 -04:00

335 lines
12 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 gcloud
import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
"time"
"go.temporal.io/api/serviceerror"
archiverspb "go.temporal.io/server/api/archiver/v1"
"go.temporal.io/server/common/archiver"
"go.temporal.io/server/common/archiver/gcloud/connector"
"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"
"go.temporal.io/server/common/searchattribute"
)
const (
errEncodeVisibilityRecord = "failed to encode visibility record"
indexKeyStartTimeout = "startTimeout"
indexKeyCloseTimeout = "closeTimeout"
timeoutInSeconds = 5
)
var (
errRetryable = errors.New("retryable error")
)
type (
visibilityArchiver struct {
container *archiver.VisibilityBootstrapContainer
gcloudStorage connector.Client
queryParser QueryParser
}
queryVisibilityToken struct {
Offset int
}
queryVisibilityRequest struct {
namespaceID string
pageSize int
nextPageToken []byte
parsedQuery *parsedQuery
}
)
func newVisibilityArchiver(container *archiver.VisibilityBootstrapContainer, storage connector.Client) *visibilityArchiver {
return &visibilityArchiver{
container: container,
gcloudStorage: storage,
queryParser: NewQueryParser(),
}
}
// NewVisibilityArchiver creates a new archiver.VisibilityArchiver based on filestore
func NewVisibilityArchiver(container *archiver.VisibilityBootstrapContainer, config *config.GstorageArchiver) (archiver.VisibilityArchiver, error) {
storage, err := connector.NewClient(context.Background(), config)
return newVisibilityArchiver(container, storage), err
}
// Archive is used to archive one workflow visibility record.
// Check the Archive() method of the HistoryArchiver interface in Step 2 for parameters' meaning and requirements.
// The only difference is that the ArchiveOption parameter won't include an option for recording process.
// Please make sure your implementation is lossless. If any in-memory batching mechanism is used, then those batched records will be lost during server restarts.
// This method will be invoked when workflow closes. Note that because of conflict resolution, it is possible for a workflow to through the closing process multiple times, which means that this method can be invoked more than once after a workflow closes.
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.HistoryArchiverScope), metrics.NamespaceTag(request.Namespace))
featureCatalog := archiver.GetFeatureCatalog(opts...)
startTime := time.Now().UTC()
defer func() {
handler.Timer(metrics.ServiceLatency.GetMetricName()).Record(time.Since(startTime))
if err != nil {
if isRetryableError(err) {
handler.Counter(metrics.VisibilityArchiverArchiveTransientErrorCount.GetMetricName()).Record(1)
} else {
handler.Counter(metrics.VisibilityArchiverArchiveNonRetryableErrorCount.GetMetricName()).Record(1)
if featureCatalog.NonRetryableError != nil {
err = featureCatalog.NonRetryableError()
}
}
}
}()
logger := archiver.TagLoggerWithArchiveVisibilityRequestAndURI(v.container.Logger, request, URI.String())
if err := v.ValidateURI(URI); err != nil {
if isRetryableError(err) {
logger.Error(archiver.ArchiveTransientErrorMsg, tag.ArchivalArchiveFailReason(archiver.ErrReasonInvalidURI), tag.Error(err))
return err
}
logger.Error(archiver.ArchiveNonRetryableErrorMsg, tag.ArchivalArchiveFailReason(archiver.ErrReasonInvalidURI), tag.Error(err))
return err
}
if err := archiver.ValidateVisibilityArchivalRequest(request); err != nil {
logger.Error(archiver.ArchiveNonRetryableErrorMsg, tag.ArchivalArchiveFailReason(archiver.ErrReasonInvalidArchiveRequest), tag.Error(err))
return err
}
encodedVisibilityRecord, err := encode(request)
if err != nil {
logger.Error(archiver.ArchiveNonRetryableErrorMsg, tag.ArchivalArchiveFailReason(errEncodeVisibilityRecord), tag.Error(err))
return err
}
// The filename has the format: closeTimestamp_hash(runID).visibility
// This format allows the archiver to sort all records without reading the file contents
filename := constructVisibilityFilename(request.GetNamespaceId(), request.WorkflowTypeName, request.GetWorkflowId(), request.GetRunId(), indexKeyCloseTimeout, timestamp.TimeValue(request.CloseTime))
if err := v.gcloudStorage.Upload(ctx, URI, filename, encodedVisibilityRecord); err != nil {
logger.Error(archiver.ArchiveTransientErrorMsg, tag.ArchivalArchiveFailReason(errWriteFile), tag.Error(err))
return errRetryable
}
filename = constructVisibilityFilename(request.GetNamespaceId(), request.WorkflowTypeName, request.GetWorkflowId(), request.GetRunId(), indexKeyStartTimeout, timestamp.TimeValue(request.StartTime))
if err := v.gcloudStorage.Upload(ctx, URI, filename, encodedVisibilityRecord); err != nil {
logger.Error(archiver.ArchiveTransientErrorMsg, tag.ArchivalArchiveFailReason(errWriteFile), tag.Error(err))
return errRetryable
}
handler.Counter(metrics.VisibilityArchiveSuccessCount.GetMetricName()).Record(1)
return nil
}
// Query is used to retrieve archived visibility records.
// Check the Get() method of the HistoryArchiver interface in Step 2 for parameters' meaning and requirements.
// The request includes a string field called query, which describes what kind of visibility records should be returned. For example, it can be some SQL-like syntax query string.
// Your implementation is responsible for parsing and validating the query, and also returning all visibility records that match the query.
// Currently the maximum context timeout passed into the method is 3 minutes, so it's ok if this method takes a long time to run.
func (v *visibilityArchiver) Query(
ctx context.Context,
URI archiver.URI,
request *archiver.QueryVisibilityRequest,
saTypeMap searchattribute.NameTypeMap,
) (*archiver.QueryVisibilityResponse, error) {
if err := v.ValidateURI(URI); err != nil {
return nil, &serviceerror.InvalidArgument{Message: archiver.ErrInvalidURI.Error()}
}
if err := archiver.ValidateQueryRequest(request); err != nil {
return nil, &serviceerror.InvalidArgument{Message: 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.InvalidArgument{Message: err.Error()}
}
if parsedQuery.emptyResult {
return &archiver.QueryVisibilityResponse{}, nil
}
return v.query(
ctx,
URI,
&queryVisibilityRequest{
namespaceID: request.NamespaceID,
pageSize: request.PageSize,
nextPageToken: request.NextPageToken,
parsedQuery: parsedQuery,
},
saTypeMap,
)
}
func (v *visibilityArchiver) query(
ctx context.Context,
uri archiver.URI,
request *queryVisibilityRequest,
saTypeMap searchattribute.NameTypeMap,
) (*archiver.QueryVisibilityResponse, error) {
prefix := constructVisibilityFilenamePrefix(request.namespaceID, indexKeyCloseTimeout)
if !request.parsedQuery.closeTime.IsZero() {
prefix = constructTimeBasedSearchKey(
request.namespaceID,
indexKeyCloseTimeout,
request.parsedQuery.closeTime,
*request.parsedQuery.searchPrecision,
)
}
if !request.parsedQuery.startTime.IsZero() {
prefix = constructTimeBasedSearchKey(
request.namespaceID,
indexKeyStartTimeout,
request.parsedQuery.startTime,
*request.parsedQuery.searchPrecision,
)
}
return v.queryPrefix(ctx, uri, request, saTypeMap, prefix)
}
func (v *visibilityArchiver) queryAll(
ctx context.Context,
URI archiver.URI,
request *archiver.QueryVisibilityRequest,
saTypeMap searchattribute.NameTypeMap,
) (*archiver.QueryVisibilityResponse, error) {
return v.queryPrefix(ctx, URI, &queryVisibilityRequest{
namespaceID: request.NamespaceID,
pageSize: request.PageSize,
nextPageToken: request.NextPageToken,
parsedQuery: &parsedQuery{},
}, saTypeMap, request.NamespaceID)
}
func (v *visibilityArchiver) queryPrefix(ctx context.Context, uri archiver.URI, request *queryVisibilityRequest, saTypeMap searchattribute.NameTypeMap, prefix string) (*archiver.QueryVisibilityResponse, error) {
token, err := v.parseToken(request.nextPageToken)
if err != nil {
return nil, err
}
filters := make([]connector.Precondition, 0)
if request.parsedQuery.workflowID != nil {
filters = append(filters, newWorkflowIDPrecondition(hash(*request.parsedQuery.workflowID)))
}
if request.parsedQuery.runID != nil {
filters = append(filters, newWorkflowIDPrecondition(hash(*request.parsedQuery.runID)))
}
if request.parsedQuery.workflowType != nil {
filters = append(filters, newWorkflowIDPrecondition(hash(*request.parsedQuery.workflowType)))
}
filenames, completed, currentCursorPos, err := v.gcloudStorage.QueryWithFilters(ctx, uri, prefix, request.pageSize, token.Offset, filters)
if err != nil {
return nil, &serviceerror.InvalidArgument{Message: err.Error()}
}
response := &archiver.QueryVisibilityResponse{}
for _, file := range filenames {
encodedRecord, err := v.gcloudStorage.Get(ctx, uri, fmt.Sprintf("%s/%s", request.namespaceID, filepath.Base(file)))
if err != nil {
return nil, &serviceerror.InvalidArgument{Message: err.Error()}
}
record, err := decodeVisibilityRecord(encodedRecord)
if err != nil {
return nil, &serviceerror.InvalidArgument{Message: err.Error()}
}
executionInfo, err := convertToExecutionInfo(record, saTypeMap)
if err != nil {
return nil, serviceerror.NewInternal(err.Error())
}
response.Executions = append(response.Executions, executionInfo)
}
if !completed {
newToken := &queryVisibilityToken{
Offset: currentCursorPos,
}
encodedToken, err := serializeToken(newToken)
if err != nil {
return nil, &serviceerror.InvalidArgument{Message: err.Error()}
}
response.NextPageToken = encodedToken
}
return response, nil
}
func (v *visibilityArchiver) parseToken(nextPageToken []byte) (*queryVisibilityToken, error) {
token := new(queryVisibilityToken)
if nextPageToken != nil {
var err error
token, err = deserializeQueryVisibilityToken(nextPageToken)
if err != nil {
return nil, &serviceerror.InvalidArgument{Message: archiver.ErrNextPageTokenCorrupted.Error()}
}
}
return token, nil
}
// ValidateURI is used to define what a valid URI for an implementation is.
func (v *visibilityArchiver) ValidateURI(URI archiver.URI) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), timeoutInSeconds*time.Second)
defer cancel()
if err = v.validateURI(URI); err == nil {
_, err = v.gcloudStorage.Exist(ctx, URI, "")
}
return
}
func (v *visibilityArchiver) validateURI(URI archiver.URI) (err error) {
if URI.Scheme() != URIScheme {
return archiver.ErrURISchemeMismatch
}
if URI.Path() == "" || URI.Hostname() == "" {
return archiver.ErrInvalidURI
}
return
}