-
Notifications
You must be signed in to change notification settings - Fork 891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use transition history if enabled when long poll history #7185
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2024 Temporal Technologies Inc. All rights reserved. | ||
// | ||
// 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 transitionhistory | ||
|
||
import ( | ||
persistencespb "go.temporal.io/server/api/persistence/v1" | ||
"go.temporal.io/server/common" | ||
) | ||
|
||
func CopyVersionedTransitions( | ||
transitions []*persistencespb.VersionedTransition, | ||
) []*persistencespb.VersionedTransition { | ||
if transitions == nil { | ||
return nil | ||
} | ||
copied := make([]*persistencespb.VersionedTransition, len(transitions)) | ||
for i, t := range transitions { | ||
copied[i] = CopyVersionedTransition(t) | ||
} | ||
return copied | ||
} | ||
|
||
func CopyVersionedTransition( | ||
transition *persistencespb.VersionedTransition, | ||
) *persistencespb.VersionedTransition { | ||
if transition == nil { | ||
return nil | ||
} | ||
return common.CloneProto(transition) | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -34,11 +34,13 @@ import ( | |||||||||||||||||
"go.temporal.io/api/serviceerror" | ||||||||||||||||||
taskqueuepb "go.temporal.io/api/taskqueue/v1" | ||||||||||||||||||
"go.temporal.io/server/api/historyservice/v1" | ||||||||||||||||||
persistencespb "go.temporal.io/server/api/persistence/v1" | ||||||||||||||||||
"go.temporal.io/server/common" | ||||||||||||||||||
"go.temporal.io/server/common/definition" | ||||||||||||||||||
"go.temporal.io/server/common/locks" | ||||||||||||||||||
"go.temporal.io/server/common/log/tag" | ||||||||||||||||||
"go.temporal.io/server/common/namespace" | ||||||||||||||||||
"go.temporal.io/server/common/persistence/transitionhistory" | ||||||||||||||||||
"go.temporal.io/server/common/persistence/versionhistory" | ||||||||||||||||||
serviceerrors "go.temporal.io/server/common/serviceerror" | ||||||||||||||||||
"go.temporal.io/server/service/history/events" | ||||||||||||||||||
|
@@ -83,6 +85,7 @@ func GetOrPollMutableState( | |||||||||||||||||
workflowKey, | ||||||||||||||||||
request.VersionHistoryItem.GetVersion(), | ||||||||||||||||||
request.VersionHistoryItem.GetEventId(), | ||||||||||||||||||
request.VersionedTransition, | ||||||||||||||||||
workflowConsistencyChecker, | ||||||||||||||||||
) | ||||||||||||||||||
if err != nil { | ||||||||||||||||||
|
@@ -99,22 +102,37 @@ func GetOrPollMutableState( | |||||||||||||||||
} | ||||||||||||||||||
request.VersionHistoryItem = lastVersionHistoryItem | ||||||||||||||||||
} | ||||||||||||||||||
// Use the latest event id + event version as the branch identifier. This pair is unique across clusters. | ||||||||||||||||||
// We return the full version histories. Callers need to fetch the last version history item from current branch | ||||||||||||||||||
// and use the last version history item in following calls. | ||||||||||||||||||
if !versionhistory.ContainsVersionHistoryItem(currentVersionHistory, request.VersionHistoryItem) { | ||||||||||||||||||
logItem, err := versionhistory.GetLastVersionHistoryItem(currentVersionHistory) | ||||||||||||||||||
if err != nil { | ||||||||||||||||||
return nil, err | ||||||||||||||||||
transitionHistory := response.GetTransitionHistory() | ||||||||||||||||||
if len(transitionHistory) != 0 { | ||||||||||||||||||
if request.VersionedTransition != nil { | ||||||||||||||||||
if workflow.TransitionHistoryStalenessCheck(transitionHistory, request.VersionedTransition) != nil { | ||||||||||||||||||
logger.Warn(fmt.Sprintf("Request versioned transition and transition history don't match. Request: %v, current: %v", | ||||||||||||||||||
request.VersionedTransition, | ||||||||||||||||||
transitionHistory[len(transitionHistory)-1]), | ||||||||||||||||||
tag.WorkflowNamespaceID(workflowKey.GetNamespaceID()), | ||||||||||||||||||
tag.WorkflowID(workflowKey.GetWorkflowID()), | ||||||||||||||||||
tag.WorkflowRunID(workflowKey.GetRunID())) | ||||||||||||||||||
return nil, serviceerrors.NewCurrentBranchChanged(response.CurrentBranchToken, request.CurrentBranchToken) | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
} else { | ||||||||||||||||||
// Use the latest event id + event version as the branch identifier. This pair is unique across clusters. | ||||||||||||||||||
// We return the full version histories. Callers need to fetch the last version history item from current branch | ||||||||||||||||||
// and use the last version history item in following calls. | ||||||||||||||||||
if !versionhistory.ContainsVersionHistoryItem(currentVersionHistory, request.VersionHistoryItem) { | ||||||||||||||||||
logItem, err := versionhistory.GetLastVersionHistoryItem(currentVersionHistory) | ||||||||||||||||||
if err != nil { | ||||||||||||||||||
return nil, err | ||||||||||||||||||
} | ||||||||||||||||||
logger.Warn("Request history branch and current history branch don't match", | ||||||||||||||||||
tag.Value(logItem), | ||||||||||||||||||
tag.TokenLastEventVersion(request.VersionHistoryItem.GetVersion()), | ||||||||||||||||||
tag.TokenLastEventID(request.VersionHistoryItem.GetEventId()), | ||||||||||||||||||
tag.WorkflowNamespaceID(workflowKey.GetNamespaceID()), | ||||||||||||||||||
tag.WorkflowID(workflowKey.GetWorkflowID()), | ||||||||||||||||||
tag.WorkflowRunID(workflowKey.GetRunID())) | ||||||||||||||||||
return nil, serviceerrors.NewCurrentBranchChanged(response.CurrentBranchToken, request.CurrentBranchToken) | ||||||||||||||||||
} | ||||||||||||||||||
logger.Warn("Request history branch and current history branch don't match", | ||||||||||||||||||
tag.Value(logItem), | ||||||||||||||||||
tag.TokenLastEventVersion(request.VersionHistoryItem.GetVersion()), | ||||||||||||||||||
tag.TokenLastEventID(request.VersionHistoryItem.GetEventId()), | ||||||||||||||||||
tag.WorkflowNamespaceID(workflowKey.GetNamespaceID()), | ||||||||||||||||||
tag.WorkflowID(workflowKey.GetWorkflowID()), | ||||||||||||||||||
tag.WorkflowRunID(workflowKey.GetRunID())) | ||||||||||||||||||
return nil, serviceerrors.NewCurrentBranchChanged(response.CurrentBranchToken, request.CurrentBranchToken) | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
// expectedNextEventID is 0 when caller want to get the current next event ID without blocking. | ||||||||||||||||||
|
@@ -138,6 +156,7 @@ func GetOrPollMutableState( | |||||||||||||||||
workflowKey, | ||||||||||||||||||
request.VersionHistoryItem.GetVersion(), | ||||||||||||||||||
request.VersionHistoryItem.GetEventId(), | ||||||||||||||||||
request.VersionedTransition, | ||||||||||||||||||
workflowConsistencyChecker, | ||||||||||||||||||
) | ||||||||||||||||||
if err != nil { | ||||||||||||||||||
|
@@ -147,8 +166,19 @@ func GetOrPollMutableState( | |||||||||||||||||
if err != nil { | ||||||||||||||||||
return nil, err | ||||||||||||||||||
} | ||||||||||||||||||
// TODO: update to use transition version history | ||||||||||||||||||
if !versionhistory.ContainsVersionHistoryItem(currentVersionHistory, request.VersionHistoryItem) { | ||||||||||||||||||
|
||||||||||||||||||
transitionHistory := response.GetTransitionHistory() | ||||||||||||||||||
if len(transitionHistory) != 0 { | ||||||||||||||||||
if request.VersionedTransition != nil && workflow.TransitionHistoryStalenessCheck(transitionHistory, request.VersionedTransition) != nil { | ||||||||||||||||||
logger.Warn(fmt.Sprintf("Request versioned transition and transition history prior to polling the mutable state. Request: %v, current: %v", | ||||||||||||||||||
request.VersionedTransition, | ||||||||||||||||||
transitionHistory[len(transitionHistory)-1]), | ||||||||||||||||||
tag.WorkflowNamespaceID(workflowKey.GetNamespaceID()), | ||||||||||||||||||
tag.WorkflowID(workflowKey.GetWorkflowID()), | ||||||||||||||||||
tag.WorkflowRunID(workflowKey.GetRunID())) | ||||||||||||||||||
return nil, serviceerrors.NewCurrentBranchChanged(response.CurrentBranchToken, request.CurrentBranchToken) | ||||||||||||||||||
} | ||||||||||||||||||
} else if !versionhistory.ContainsVersionHistoryItem(currentVersionHistory, request.VersionHistoryItem) { | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm shall we fallback to version history check when either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have the same question on if this is a valid case on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it will happen when we start to rollout state based replication so we have to handle it. I think we can just validate version history in that case. |
||||||||||||||||||
logItem, err := versionhistory.GetLastVersionHistoryItem(currentVersionHistory) | ||||||||||||||||||
if err != nil { | ||||||||||||||||||
return nil, err | ||||||||||||||||||
|
@@ -190,6 +220,7 @@ func GetOrPollMutableState( | |||||||||||||||||
} | ||||||||||||||||||
response.CurrentBranchToken = eventVersionHistory.GetBranchToken() | ||||||||||||||||||
response.VersionHistories = event.VersionHistories | ||||||||||||||||||
response.TransitionHistory = event.TransitionHistory | ||||||||||||||||||
|
||||||||||||||||||
notifiedEventVersionItem, err := versionhistory.GetLastVersionHistoryItem(eventVersionHistory) | ||||||||||||||||||
if err != nil { | ||||||||||||||||||
|
@@ -199,8 +230,18 @@ func GetOrPollMutableState( | |||||||||||||||||
if versionhistory.CompareVersionHistoryItem(notifiedEventVersionItem, request.VersionHistoryItem) < 0 { | ||||||||||||||||||
continue | ||||||||||||||||||
} | ||||||||||||||||||
// TODO: update to use transition version history | ||||||||||||||||||
if !versionhistory.ContainsVersionHistoryItem(eventVersionHistory, request.VersionHistoryItem) { | ||||||||||||||||||
transitionHistory := response.GetTransitionHistory() | ||||||||||||||||||
if len(transitionHistory) != 0 { | ||||||||||||||||||
if request.VersionedTransition != nil && workflow.TransitionHistoryStalenessCheck(transitionHistory, request.VersionedTransition) != nil { | ||||||||||||||||||
logger.Warn(fmt.Sprintf("Request versioned transition and transition history don't match after polling the mutable state. Request: %v, current: %v", | ||||||||||||||||||
request.VersionedTransition, | ||||||||||||||||||
transitionHistory[len(transitionHistory)-1]), | ||||||||||||||||||
tag.WorkflowNamespaceID(workflowKey.GetNamespaceID()), | ||||||||||||||||||
tag.WorkflowID(workflowKey.GetWorkflowID()), | ||||||||||||||||||
tag.WorkflowRunID(workflowKey.GetRunID())) | ||||||||||||||||||
return nil, serviceerrors.NewCurrentBranchChanged(response.CurrentBranchToken, request.CurrentBranchToken) | ||||||||||||||||||
} | ||||||||||||||||||
} else if !versionhistory.ContainsVersionHistoryItem(eventVersionHistory, request.VersionHistoryItem) { | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same Q here. |
||||||||||||||||||
logger.Warn("Request history branch and current history branch don't match after polling the mutable state", | ||||||||||||||||||
tag.Value(notifiedEventVersionItem), | ||||||||||||||||||
tag.TokenLastEventVersion(request.VersionHistoryItem.GetVersion()), | ||||||||||||||||||
|
@@ -261,6 +302,7 @@ func GetMutableStateWithConsistencyCheck( | |||||||||||||||||
workflowKey definition.WorkflowKey, | ||||||||||||||||||
currentVersion int64, | ||||||||||||||||||
currentEventID int64, | ||||||||||||||||||
versionedTransition *persistencespb.VersionedTransition, | ||||||||||||||||||
workflowConsistencyChecker WorkflowConsistencyChecker, | ||||||||||||||||||
) (_ *historyservice.GetMutableStateResponse, retError error) { | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -274,7 +316,14 @@ func GetMutableStateWithConsistencyCheck( | |||||||||||||||||
ctx, | ||||||||||||||||||
nil, | ||||||||||||||||||
func(mutableState workflow.MutableState) bool { | ||||||||||||||||||
mutableState.GetExecutionInfo().GetVersionHistories() | ||||||||||||||||||
transitionHistory := mutableState.GetExecutionInfo().GetTransitionHistory() | ||||||||||||||||||
if len(transitionHistory) != 0 { | ||||||||||||||||||
if versionedTransition != nil && workflow.TransitionHistoryStalenessCheck(transitionHistory, versionedTransition) != nil { | ||||||||||||||||||
return false | ||||||||||||||||||
} | ||||||||||||||||||
return true | ||||||||||||||||||
Comment on lines
+320
to
+324
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
currentVersionHistory, err := versionhistory.GetCurrentVersionHistory(mutableState.GetExecutionInfo().GetVersionHistories()) | ||||||||||||||||||
if err != nil { | ||||||||||||||||||
return false | ||||||||||||||||||
|
@@ -360,7 +409,7 @@ func MutableStateToGetResponse( | |||||||||||||||||
AssignedBuildId: mutableState.GetAssignedBuildId(), | ||||||||||||||||||
InheritedBuildId: mutableState.GetInheritedBuildId(), | ||||||||||||||||||
MostRecentWorkerVersionStamp: mostRecentWorkerVersionStamp, | ||||||||||||||||||
TransitionHistory: mutableState.GetExecutionInfo().TransitionHistory, | ||||||||||||||||||
TransitionHistory: transitionhistory.CopyVersionedTransitions(mutableState.GetExecutionInfo().TransitionHistory), | ||||||||||||||||||
VersioningInfo: mutableState.GetExecutionInfo().VersioningInfo, | ||||||||||||||||||
}, nil | ||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ import ( | |
"go.temporal.io/api/workflowservice/v1" | ||
historyspb "go.temporal.io/server/api/history/v1" | ||
"go.temporal.io/server/api/historyservice/v1" | ||
persistencespb "go.temporal.io/server/api/persistence/v1" | ||
tokenspb "go.temporal.io/server/api/token/v1" | ||
"go.temporal.io/server/common" | ||
"go.temporal.io/server/common/failure" | ||
|
@@ -76,7 +77,8 @@ func Invoke( | |
expectedNextEventID int64, | ||
currentBranchToken []byte, | ||
versionHistoryItem *historyspb.VersionHistoryItem, | ||
) ([]byte, string, int64, int64, bool, *historyspb.VersionHistoryItem, error) { | ||
versionedTransition *persistencespb.VersionedTransition, | ||
) ([]byte, string, int64, int64, bool, *historyspb.VersionHistoryItem, *persistencespb.VersionedTransition, error) { | ||
response, err := api.GetOrPollMutableState( | ||
ctx, | ||
shardContext, | ||
|
@@ -86,29 +88,38 @@ func Invoke( | |
ExpectedNextEventId: expectedNextEventID, | ||
CurrentBranchToken: currentBranchToken, | ||
VersionHistoryItem: versionHistoryItem, | ||
VersionedTransition: versionedTransition, | ||
}, | ||
workflowConsistencyChecker, | ||
eventNotifier, | ||
) | ||
if err != nil { | ||
return nil, "", 0, 0, false, nil, err | ||
return nil, "", 0, 0, false, nil, nil, err | ||
} | ||
|
||
isWorkflowRunning := response.GetWorkflowStatus() == enumspb.WORKFLOW_EXECUTION_STATUS_RUNNING | ||
currentVersionHistory, err := versionhistory.GetCurrentVersionHistory(response.GetVersionHistories()) | ||
if err != nil { | ||
return nil, "", 0, 0, false, nil, err | ||
return nil, "", 0, 0, false, nil, nil, err | ||
} | ||
lastVersionHistoryItem, err := versionhistory.GetLastVersionHistoryItem(currentVersionHistory) | ||
if err != nil { | ||
return nil, "", 0, 0, false, nil, err | ||
return nil, "", 0, 0, false, nil, nil, err | ||
} | ||
|
||
var lastVersionedTransition *persistencespb.VersionedTransition | ||
transitionHistory := response.GetTransitionHistory() | ||
if transitionHistory != nil { | ||
lastVersionedTransition = transitionHistory[len(transitionHistory)-1] | ||
} | ||
Comment on lines
+110
to
+114
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: make this a util function as well for getting the last versioned transition? I think I have seen this logic in couple of places. |
||
|
||
return response.CurrentBranchToken, | ||
response.Execution.GetRunId(), | ||
response.GetLastFirstEventId(), | ||
response.GetNextEventId(), | ||
isWorkflowRunning, | ||
lastVersionHistoryItem, | ||
lastVersionedTransition, | ||
nil | ||
} | ||
|
||
|
@@ -140,8 +151,8 @@ func Invoke( | |
if !isCloseEventOnly { | ||
queryNextEventID = continuationToken.GetNextEventId() | ||
} | ||
continuationToken.BranchToken, _, lastFirstEventID, nextEventID, isWorkflowRunning, continuationToken.VersionHistoryItem, err = | ||
queryHistory(namespaceID, execution, queryNextEventID, continuationToken.BranchToken, continuationToken.VersionHistoryItem) | ||
continuationToken.BranchToken, _, lastFirstEventID, nextEventID, isWorkflowRunning, continuationToken.VersionHistoryItem, continuationToken.VersionedTransition, err = | ||
queryHistory(namespaceID, execution, queryNextEventID, continuationToken.BranchToken, continuationToken.VersionHistoryItem, continuationToken.VersionedTransition) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -154,8 +165,8 @@ func Invoke( | |
if !isCloseEventOnly { | ||
queryNextEventID = common.FirstEventID | ||
} | ||
continuationToken.BranchToken, runID, lastFirstEventID, nextEventID, isWorkflowRunning, continuationToken.VersionHistoryItem, err = | ||
queryHistory(namespaceID, execution, queryNextEventID, nil, nil) | ||
continuationToken.BranchToken, runID, lastFirstEventID, nextEventID, isWorkflowRunning, continuationToken.VersionHistoryItem, continuationToken.VersionedTransition, err = | ||
queryHistory(namespaceID, execution, queryNextEventID, nil, nil, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.