Files
tasks/common/persistence/visibility/manager/visibility_manager.go
Tim Deeb-SwihartandGitHub 95169a53c3 Replace gogo/protobuf with google/protobuf (#5032)
**What changed?**

gogo/protobuf has been replaced with Google's official go compiler. 

**Why?**

gogo/protobuf has been deprecated for some time and the community is
moving on, building new tools (like vtproto) atop google's v2 compiler.

**How did you test it?**

`make test`

**Potential risks**

1. The change from embedded gogo-generated-structs to
google-generated-pointers-to-structs created a risk of nil pointer
exceptions. I've fixed all the ones our tests found but it's possible
there are more lurking in the new code.
2. This change may cause our performance to decrease. Certainly
encoding/deconding of proto objects will become slower, but the overuse
of pointers by the google compiler may negatively affect our overall
performance. We'll need to keep an eye on the GC stats
3. This breaks the HTTP API. We will not support [shortand payload
encoding](https://github.com/temporalio/proposals/blob/master/api/http-api.md#payload-formatting)
in this first pass; that will come once this initial work is in testing.

**Breaking changes for developers**

- `*time.Time` in proto structs will now be
[timestamppb.Timestamp](https://pkg.go.dev/google.golang.org/[email protected]/types/known/timestamppb#section-documentation)
- `*time.Duration` will now be
[durationpb.Duration](https://pkg.go.dev/google.golang.org/protobuf/types/known/durationpb)
- V2-generated structs embed locks, so you cannot dereference them. `go
vet` will scream at you about this. If you need a copy, use
`proto.Clone`.
- If the performance of this sucks then I will either update our code
generator to add shallow-clone methods or hand-roll the ones we need
- Proto enums will, when formatted to JSON, now be in
`SCREAMING_SNAKE_CASE` rather than `PascalCase`. We decided (in
discussion with the SDK team) that now was as good a time as any to rip
the bandage off.
- Proto objects, or objects embedding protos, cannot be compared using
`reflect.DeepEqual` or _anything_ that uses it. This includes `testify`
and `mock` equality testers!
- You will need to use the `common/testing/protorequire`,
`common/testing/protoassert`, or `common/testing/protomock` packages
instead. I've implemented proto-compatible matchers and assertions there
for all cases I've encountered
- If you need `reflect.DeepEqual` for any reason you can use
`go.temporal.io/api/temporalproto.DeepEqual` instead

Note that history loading will not be impacted by the JSON changes: I
rewrote history loading to dynamically fix incoming history JSON data
(like all our other sdks); you can find this code in [my fork of our go
API](https://github.com/tdeebswihart/temporal-api-go/blob/master/internal/temporalhistoryv1/load.go)
alongside its tests.

**🚨Sharp Edges Introduced🚨**

Beware `*timestamppb.Timestamp.AsTime()`. If you need to extract a time
value from a proto time (timestamppb) **always** make sure to check
whether it's nil first. When the proto object is `nil` `AsTime()` will
return a non-zero time at the proto epoch: UTC midnight on January 1,
1970.

I've made this mistake multiple times during this transition and each
time it's been a pain to debug

**Is hotfix candidate?**

No.
2023-11-21 15:19:37 -08:00

214 lines
9.1 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 manager
// -aux_files is required here due to Closeable interface being in another file.
//go:generate mockgen -copyright_file ../../../../LICENSE -package $GOPACKAGE -source $GOFILE -destination visibility_manager_mock.go -aux_files go.temporal.io/server/common/persistence=../../data_interfaces.go
import (
"context"
"time"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
workflowpb "go.temporal.io/api/workflow/v1"
"go.temporal.io/api/workflowservice/v1"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/common/persistence"
)
type (
// VisibilityManager is used to manage the visibility store
VisibilityManager interface {
persistence.Closeable
GetReadStoreName(nsName namespace.Name) string
GetStoreNames() []string
HasStoreName(stName string) bool
GetIndexName() string
ValidateCustomSearchAttributes(searchAttributes map[string]any) (map[string]any, error)
// Write APIs.
RecordWorkflowExecutionStarted(ctx context.Context, request *RecordWorkflowExecutionStartedRequest) error
RecordWorkflowExecutionClosed(ctx context.Context, request *RecordWorkflowExecutionClosedRequest) error
UpsertWorkflowExecution(ctx context.Context, request *UpsertWorkflowExecutionRequest) error
DeleteWorkflowExecution(ctx context.Context, request *VisibilityDeleteWorkflowExecutionRequest) error
// Read APIs.
ListOpenWorkflowExecutions(ctx context.Context, request *ListWorkflowExecutionsRequest) (*ListWorkflowExecutionsResponse, error)
ListClosedWorkflowExecutions(ctx context.Context, request *ListWorkflowExecutionsRequest) (*ListWorkflowExecutionsResponse, error)
ListOpenWorkflowExecutionsByType(ctx context.Context, request *ListWorkflowExecutionsByTypeRequest) (*ListWorkflowExecutionsResponse, error)
ListClosedWorkflowExecutionsByType(ctx context.Context, request *ListWorkflowExecutionsByTypeRequest) (*ListWorkflowExecutionsResponse, error)
ListOpenWorkflowExecutionsByWorkflowID(ctx context.Context, request *ListWorkflowExecutionsByWorkflowIDRequest) (*ListWorkflowExecutionsResponse, error)
ListClosedWorkflowExecutionsByWorkflowID(ctx context.Context, request *ListWorkflowExecutionsByWorkflowIDRequest) (*ListWorkflowExecutionsResponse, error)
ListClosedWorkflowExecutionsByStatus(ctx context.Context, request *ListClosedWorkflowExecutionsByStatusRequest) (*ListWorkflowExecutionsResponse, error)
ListWorkflowExecutions(ctx context.Context, request *ListWorkflowExecutionsRequestV2) (*ListWorkflowExecutionsResponse, error)
ScanWorkflowExecutions(ctx context.Context, request *ListWorkflowExecutionsRequestV2) (*ListWorkflowExecutionsResponse, error)
CountWorkflowExecutions(ctx context.Context, request *CountWorkflowExecutionsRequest) (*CountWorkflowExecutionsResponse, error)
GetWorkflowExecution(ctx context.Context, request *GetWorkflowExecutionRequest) (*GetWorkflowExecutionResponse, error)
}
VisibilityRequestBase struct {
NamespaceID namespace.ID
Namespace namespace.Name // namespace.Name is not persisted.
Execution *commonpb.WorkflowExecution
WorkflowTypeName string
StartTime time.Time
Status enumspb.WorkflowExecutionStatus
ExecutionTime time.Time
TaskID int64 // not persisted, used as condition update version for ES
ShardID int32 // not persisted
Memo *commonpb.Memo
TaskQueue string
SearchAttributes *commonpb.SearchAttributes
ParentExecution *commonpb.WorkflowExecution
}
// RecordWorkflowExecutionStartedRequest is used to add a record of a newly started execution
RecordWorkflowExecutionStartedRequest struct {
*VisibilityRequestBase
}
// RecordWorkflowExecutionClosedRequest is used to add a record of a closed execution
RecordWorkflowExecutionClosedRequest struct {
*VisibilityRequestBase
CloseTime time.Time
HistoryLength int64
HistorySizeBytes int64
StateTransitionCount int64
}
// UpsertWorkflowExecutionRequest is used to upsert workflow execution
UpsertWorkflowExecutionRequest struct {
*VisibilityRequestBase
}
// ListWorkflowExecutionsRequest is used to list executions in a namespace
ListWorkflowExecutionsRequest struct {
NamespaceID namespace.ID
Namespace namespace.Name // namespace.Name is not persisted.
NamespaceDivision string
EarliestStartTime time.Time
LatestStartTime time.Time
// Maximum number of workflow executions per page
PageSize int
// Token to continue reading next page of workflow executions.
// Pass in empty slice for first page.
NextPageToken []byte
}
// ListWorkflowExecutionsRequestV2 is used to list executions in a namespace
ListWorkflowExecutionsRequestV2 struct {
NamespaceID namespace.ID
Namespace namespace.Name // namespace.Name is not persisted.
PageSize int // Maximum number of workflow executions per page
// Token to continue reading next page of workflow executions.
// Pass in empty slice for first page.
NextPageToken []byte
Query string
}
// ListWorkflowExecutionsResponse is the response to ListWorkflowExecutionsRequest
ListWorkflowExecutionsResponse struct {
Executions []*workflowpb.WorkflowExecutionInfo
// Token to read next page if there are more workflow executions beyond page size.
// Use this to set NextPageToken on ListWorkflowExecutionsRequest to read the next page.
NextPageToken []byte
}
// CountWorkflowExecutionsRequest is request from CountWorkflowExecutions
CountWorkflowExecutionsRequest struct {
NamespaceID namespace.ID
Namespace namespace.Name // namespace.Name is not persisted.
Query string
}
// CountWorkflowExecutionsResponse is response to CountWorkflowExecutions
CountWorkflowExecutionsResponse struct {
Count int64 // sum of counts in Groups
Groups []*workflowservice.CountWorkflowExecutionsResponse_AggregationGroup
}
// ListWorkflowExecutionsByTypeRequest is used to list executions of
// a specific type in a namespace
ListWorkflowExecutionsByTypeRequest struct {
*ListWorkflowExecutionsRequest
WorkflowTypeName string
}
// ListWorkflowExecutionsByWorkflowIDRequest is used to list executions that
// have specific WorkflowID in a namespace
ListWorkflowExecutionsByWorkflowIDRequest struct {
*ListWorkflowExecutionsRequest
WorkflowID string
}
// ListClosedWorkflowExecutionsByStatusRequest is used to list executions that
// have specific close status
ListClosedWorkflowExecutionsByStatusRequest struct {
*ListWorkflowExecutionsRequest
Status enumspb.WorkflowExecutionStatus
}
// VisibilityDeleteWorkflowExecutionRequest contains the request params for DeleteWorkflowExecution call
VisibilityDeleteWorkflowExecutionRequest struct {
NamespaceID namespace.ID
RunID string
WorkflowID string
TaskID int64
StartTime time.Time // if start time is not empty, delete record from open_execution for cassandra db
CloseTime time.Time // if end time is not empty, delete record from closed_execution for cassandra db
}
// GetWorkflowExecutionRequest is request from GetWorkflowExecution
GetWorkflowExecutionRequest struct {
NamespaceID namespace.ID
Namespace namespace.Name // namespace.Name is not persisted
RunID string
WorkflowID string
StartTime time.Time // if start time is not empty, search record from open_execution for cassandra db
CloseTime time.Time // if end time is not empty, search record from closed_execution for cassandra db
}
// GetWorkflowExecutionResponse is response to GetWorkflowExecution
GetWorkflowExecutionResponse struct {
Execution *workflowpb.WorkflowExecutionInfo
}
)
func (r *ListWorkflowExecutionsRequest) OverrideToken(token []byte) {
r.NextPageToken = token
}
func (r *ListWorkflowExecutionsRequest) GetToken() []byte {
return r.NextPageToken
}
func (r *ListWorkflowExecutionsRequest) OverridePageSize(pageSize int) {
r.PageSize = pageSize
}
func (r *ListWorkflowExecutionsRequest) GetPageSize() int {
return r.PageSize
}