Files
tasks/service/history/ndc/transaction_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

495 lines
15 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.
//go:generate mockgen -copyright_file ../../../LICENSE -package $GOPACKAGE -source $GOFILE -destination transaction_manager_mock.go
package ndc
import (
"context"
"github.com/pborman/uuid"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
historypb "go.temporal.io/api/history/v1"
"go.temporal.io/api/serviceerror"
"go.temporal.io/server/common/cluster"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/common/persistence"
"go.temporal.io/server/common/persistence/serialization"
"go.temporal.io/server/common/persistence/versionhistory"
"go.temporal.io/server/service/history/shard"
"go.temporal.io/server/service/history/workflow"
wcache "go.temporal.io/server/service/history/workflow/cache"
)
// NOTE: terminology
//
// 1. currentWorkflow means current running / closed workflow
// pointed by the current record in DB
//
// 2. targetWorkflow means the workflow to be replicated
// pointed by the replication task
//
// 3. newWorkflow means the workflow to be replicated, as part of continue as new
// pointed by the replication task
//
// 4. if target workflow and current workflow are the same
// then target workflow is set, current workflow is nil
//
// 5. suppress a workflow means turn a workflow into a zombie
// or terminate a workflow
// Cases to be handled by this file:
//
// create path (there will be only current branch)
// 1. create as current -> nDCTransactionPolicyCreateAsCurrent
// 2. create as zombie -> nDCTransactionPolicyCreateAsZombie
//
// create path (there will be only current branch) + suppress current
// 1. create as current & suppress current -> nDCTransactionPolicySuppressCurrentAndCreateAsCurrent
//
// update to current branch path
// 1. update as current -> nDCTransactionPolicyUpdateAsCurrent
// 2. update as current & new created as current -> nDCTransactionPolicyUpdateAsCurrent
// 3. update as zombie -> nDCTransactionPolicyUpdateAsZombie
// 4. update as zombie & new created as zombie -> nDCTransactionPolicyUpdateAsZombie
//
// backfill to non current branch path
// 1. backfill as is
// 2. backfill as is & new created as zombie
//
// conflict resolve path
// 1. conflict resolve as current -> nDCTransactionPolicyConflictResolveAsCurrent
// 2. conflict resolve as zombie -> nDCTransactionPolicyConflictResolveAsZombie
//
// conflict resolve path + suppress current
// 1. update from zombie to current & suppress current -> nDCTransactionPolicySuppressCurrentAndUpdateAsCurrent
// 2. update from zombie to current & new created as current & suppress current -> nDCTransactionPolicySuppressCurrentAndUpdateAsCurrent
type nDCTransactionPolicy int
const (
nDCTransactionPolicyCreateAsCurrent nDCTransactionPolicy = iota
nDCTransactionPolicyCreateAsZombie
nDCTransactionPolicySuppressCurrentAndCreateAsCurrent
nDCTransactionPolicyUpdateAsCurrent
nDCTransactionPolicyUpdateAsZombie
nDCTransactionPolicyConflictResolveAsCurrent
nDCTransactionPolicyConflictResolveAsZombie
nDCTransactionPolicySuppressCurrentAndUpdateAsCurrent
)
const (
EventsReapplicationResetWorkflowReason = "events-reapplication"
)
type (
TransactionManager interface {
CreateWorkflow(
ctx context.Context,
targetWorkflow Workflow,
) error
UpdateWorkflow(
ctx context.Context,
isWorkflowRebuilt bool,
targetWorkflow Workflow,
newWorkflow Workflow,
) error
BackfillWorkflow(
ctx context.Context,
targetWorkflow Workflow,
targetWorkflowEventsSlice ...*persistence.WorkflowEvents,
) error
CheckWorkflowExists(
ctx context.Context,
namespaceID namespace.ID,
workflowID string,
runID string,
) (bool, error)
GetCurrentWorkflowRunID(
ctx context.Context,
namespaceID namespace.ID,
workflowID string,
) (string, error)
LoadWorkflow(
ctx context.Context,
namespaceID namespace.ID,
workflowID string,
runID string,
) (Workflow, error)
}
transactionMgrImpl struct {
shardContext shard.Context
namespaceRegistry namespace.Registry
workflowCache wcache.Cache
clusterMetadata cluster.Metadata
executionManager persistence.ExecutionManager
serializer serialization.Serializer
metricsHandler metrics.Handler
workflowResetter WorkflowResetter
eventsReapplier EventsReapplier
logger log.Logger
createMgr transactionMgrForNewWorkflow
updateMgr transactionMgrForExistingWorkflow
}
)
var _ TransactionManager = (*transactionMgrImpl)(nil)
func NewTransactionManager(
shardContext shard.Context,
workflowCache wcache.Cache,
eventsReapplier EventsReapplier,
logger log.Logger,
bypassVersionSemanticsCheck bool,
) *transactionMgrImpl {
transactionMgr := &transactionMgrImpl{
shardContext: shardContext,
namespaceRegistry: shardContext.GetNamespaceRegistry(),
workflowCache: workflowCache,
clusterMetadata: shardContext.GetClusterMetadata(),
executionManager: shardContext.GetExecutionManager(),
serializer: shardContext.GetPayloadSerializer(),
metricsHandler: shardContext.GetMetricsHandler(),
workflowResetter: NewWorkflowResetter(
shardContext,
workflowCache,
logger,
),
eventsReapplier: eventsReapplier,
logger: log.With(logger, tag.ComponentHistoryReplicator),
createMgr: nil,
updateMgr: nil,
}
transactionMgr.createMgr = newTransactionMgrForNewWorkflow(shardContext, transactionMgr, bypassVersionSemanticsCheck)
transactionMgr.updateMgr = newNDCTransactionMgrForExistingWorkflow(shardContext, transactionMgr, bypassVersionSemanticsCheck)
return transactionMgr
}
func (r *transactionMgrImpl) CreateWorkflow(
ctx context.Context,
targetWorkflow Workflow,
) error {
return r.createMgr.dispatchForNewWorkflow(
ctx,
targetWorkflow,
)
}
func (r *transactionMgrImpl) UpdateWorkflow(
ctx context.Context,
isWorkflowRebuilt bool,
targetWorkflow Workflow,
newWorkflow Workflow,
) error {
return r.updateMgr.dispatchForExistingWorkflow(
ctx,
isWorkflowRebuilt,
targetWorkflow,
newWorkflow,
)
}
func (r *transactionMgrImpl) BackfillWorkflow(
ctx context.Context,
targetWorkflow Workflow,
targetWorkflowEventsSlice ...*persistence.WorkflowEvents,
) (retError error) {
defer func() {
if rec := recover(); rec != nil {
targetWorkflow.GetReleaseFn()(errPanic)
panic(rec)
} else {
targetWorkflow.GetReleaseFn()(retError)
}
}()
sizeSiff, err := targetWorkflow.GetContext().PersistWorkflowEvents(
ctx,
r.shardContext,
targetWorkflowEventsSlice...,
)
if err != nil {
return err
}
targetWorkflow.GetMutableState().AddHistorySize(sizeSiff)
updateMode, transactionPolicy, err := r.backfillWorkflowEventsReapply(
ctx,
targetWorkflow,
targetWorkflowEventsSlice...,
)
if err != nil {
return err
}
return targetWorkflow.GetContext().UpdateWorkflowExecutionWithNew(
ctx,
r.shardContext,
updateMode,
nil,
nil,
transactionPolicy,
nil,
)
}
func (r *transactionMgrImpl) backfillWorkflowEventsReapply(
ctx context.Context,
targetWorkflow Workflow,
targetWorkflowEventsSlice ...*persistence.WorkflowEvents,
) (persistence.UpdateWorkflowMode, workflow.TransactionPolicy, error) {
isCurrentWorkflow, err := r.isWorkflowCurrent(ctx, targetWorkflow)
if err != nil {
return 0, workflow.TransactionPolicyActive, err
}
isWorkflowRunning := targetWorkflow.GetMutableState().IsWorkflowExecutionRunning()
targetWorkflowActiveCluster := targetWorkflow.GetMutableState().GetNamespaceEntry().ActiveClusterName()
currentCluster := r.clusterMetadata.GetCurrentClusterName()
isActiveCluster := targetWorkflowActiveCluster == currentCluster
// workflow events reapplication
// we need to handle 3 cases
// 1. target workflow is self & self being current & active
// a. workflow still running -> just reapply
// b. workflow closed -> reset current workflow & reapply
// 2. anything not case 1 -> find the current & active workflow to reapply
// case 1
if isCurrentWorkflow && isActiveCluster {
var totalEvents []*historypb.HistoryEvent
for _, events := range targetWorkflowEventsSlice {
totalEvents = append(totalEvents, events.Events...)
}
// case 1.a
if isWorkflowRunning {
if _, err := r.eventsReapplier.ReapplyEvents(
ctx,
targetWorkflow.GetMutableState(),
totalEvents,
targetWorkflow.GetMutableState().GetExecutionState().GetRunId(),
); err != nil {
return 0, workflow.TransactionPolicyActive, err
}
return persistence.UpdateWorkflowModeUpdateCurrent, workflow.TransactionPolicyActive, nil
}
// case 1.b
// need to reset target workflow (which is also the current workflow)
// to accept events to be reapplied
baseMutableState := targetWorkflow.GetMutableState()
namespaceID := namespace.ID(baseMutableState.GetExecutionInfo().NamespaceId)
workflowID := baseMutableState.GetExecutionInfo().WorkflowId
baseRunID := baseMutableState.GetExecutionState().GetRunId()
resetRunID := uuid.New()
baseRebuildLastEventID := baseMutableState.GetLastWorkflowTaskStartedEventID()
baseVersionHistories := baseMutableState.GetExecutionInfo().GetVersionHistories()
baseCurrentVersionHistory, err := versionhistory.GetCurrentVersionHistory(baseVersionHistories)
if err != nil {
return 0, workflow.TransactionPolicyActive, err
}
baseRebuildLastEventVersion, err := versionhistory.GetVersionHistoryEventVersion(baseCurrentVersionHistory, baseRebuildLastEventID)
if err != nil {
return 0, workflow.TransactionPolicyActive, err
}
baseCurrentBranchToken := baseCurrentVersionHistory.GetBranchToken()
baseNextEventID := baseMutableState.GetNextEventID()
err = r.workflowResetter.ResetWorkflow(
ctx,
namespaceID,
workflowID,
baseRunID,
baseCurrentBranchToken,
baseRebuildLastEventID,
baseRebuildLastEventVersion,
baseNextEventID,
resetRunID,
uuid.New(),
targetWorkflow,
EventsReapplicationResetWorkflowReason,
totalEvents,
enumspb.RESET_REAPPLY_TYPE_SIGNAL,
)
switch err.(type) {
case *serviceerror.InvalidArgument:
// no-op. Usually this is due to reset workflow with pending child workflows
r.logger.Warn("Cannot reset workflow. Ignoring reapply events.", tag.Error(err))
// the target workflow is not reset so it is still the current workflow. It need to persist updated version histories.
return persistence.UpdateWorkflowModeUpdateCurrent, workflow.TransactionPolicyPassive, nil
case nil:
// after the reset of target workflow (current workflow) with additional events to be reapplied
// target workflow is no longer the current workflow
return persistence.UpdateWorkflowModeBypassCurrent, workflow.TransactionPolicyPassive, nil
default:
return 0, workflow.TransactionPolicyActive, err
}
}
// case 2
// find the current & active workflow to reapply
if err := targetWorkflow.GetContext().ReapplyEvents(
ctx,
r.shardContext,
targetWorkflowEventsSlice,
); err != nil {
return 0, workflow.TransactionPolicyActive, err
}
if isCurrentWorkflow {
return persistence.UpdateWorkflowModeUpdateCurrent, workflow.TransactionPolicyPassive, nil
}
return persistence.UpdateWorkflowModeBypassCurrent, workflow.TransactionPolicyPassive, nil
}
func (r *transactionMgrImpl) CheckWorkflowExists(
ctx context.Context,
namespaceID namespace.ID,
workflowID string,
runID string,
) (bool, error) {
_, err := r.shardContext.GetWorkflowExecution(
ctx,
&persistence.GetWorkflowExecutionRequest{
ShardID: r.shardContext.GetShardID(),
NamespaceID: namespaceID.String(),
WorkflowID: workflowID,
RunID: runID,
},
)
switch err.(type) {
case nil:
return true, nil
case *serviceerror.NotFound:
return false, nil
default:
return false, err
}
}
func (r *transactionMgrImpl) GetCurrentWorkflowRunID(
ctx context.Context,
namespaceID namespace.ID,
workflowID string,
) (string, error) {
resp, err := r.shardContext.GetCurrentExecution(
ctx,
&persistence.GetCurrentExecutionRequest{
ShardID: r.shardContext.GetShardID(),
NamespaceID: namespaceID.String(),
WorkflowID: workflowID,
},
)
switch err.(type) {
case nil:
return resp.RunID, nil
case *serviceerror.NotFound:
return "", nil
default:
return "", err
}
}
func (r *transactionMgrImpl) LoadWorkflow(
ctx context.Context,
namespaceID namespace.ID,
workflowID string,
runID string,
) (Workflow, error) {
weContext, release, err := r.workflowCache.GetOrCreateWorkflowExecution(
ctx,
r.shardContext,
namespaceID,
&commonpb.WorkflowExecution{
WorkflowId: workflowID,
RunId: runID,
},
workflow.LockPriorityHigh,
)
if err != nil {
return nil, err
}
ms, err := weContext.LoadMutableState(ctx, r.shardContext)
if err != nil {
// no matter what error happen, we need to retry
release(err)
return nil, err
}
return NewWorkflow(r.clusterMetadata, weContext, ms, release), nil
}
func (r *transactionMgrImpl) isWorkflowCurrent(
ctx context.Context,
targetWorkflow Workflow,
) (bool, error) {
// since we are not rebuilding the mutable state (when doing back fill) then we
// can trust the result from IsCurrentWorkflowGuaranteed
if targetWorkflow.GetMutableState().IsCurrentWorkflowGuaranteed() {
return true, nil
}
// target workflow is not guaranteed to be current workflow, do additional check
executionInfo := targetWorkflow.GetMutableState().GetExecutionInfo()
executionState := targetWorkflow.GetMutableState().GetExecutionState()
namespaceID := namespace.ID(executionInfo.NamespaceId)
workflowID := executionInfo.WorkflowId
runID := executionState.RunId
currentRunID, err := r.GetCurrentWorkflowRunID(
ctx,
namespaceID,
workflowID,
)
if err != nil {
return false, err
}
return currentRunID == runID, nil
}