Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,784 changes: 2,400 additions & 2,384 deletions api/historyservice/v1/request_response.pb.go

Large diffs are not rendered by default.

299 changes: 158 additions & 141 deletions api/token/v1/message.pb.go

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions common/persistence/transitionhistory/transition_history.go
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
Expand Up @@ -126,6 +126,7 @@ message GetMutableStateRequest {
int64 expected_next_event_id = 3;
bytes current_branch_token = 4;
temporal.server.api.history.v1.VersionHistoryItem version_history_item = 5;
temporal.server.api.persistence.v1.VersionedTransition versioned_transition = 6;
}

message GetMutableStateResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ message HistoryContinuation {
bytes branch_token = 8;
reserved 9;
temporal.server.api.history.v1.VersionHistoryItem version_history_item = 10;
temporal.server.api.persistence.v1.VersionedTransition versioned_transition = 11;
}

message RawHistoryContinuation{
Expand Down
91 changes: 70 additions & 21 deletions service/history/api/get_workflow_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -83,6 +85,7 @@ func GetOrPollMutableState(
workflowKey,
request.VersionHistoryItem.GetVersion(),
request.VersionHistoryItem.GetEventId(),
request.VersionedTransition,
workflowConsistencyChecker,
)
if err != nil {
Expand All @@ -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.
Expand All @@ -138,6 +156,7 @@ func GetOrPollMutableState(
workflowKey,
request.VersionHistoryItem.GetVersion(),
request.VersionHistoryItem.GetEventId(),
request.VersionedTransition,
workflowConsistencyChecker,
)
if err != nil {
Expand All @@ -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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.Warn(fmt.Sprintf("Request versioned transition and transition history prior to polling the mutable state. Request: %v, current: %v",
logger.Warn(fmt.Sprintf("Request versioned transition and transition history don't match 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm shall we fallback to version history check when either transitionHistory or request.VersionedTransition is empty?
Looks right now, if transitionHistory is no empty but request.VersionedTransition is empty, we will just skip the validation priori to polling mutable state.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 transitionHistory is no empty but request.VersionedTransition is empty

Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The 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()),
Expand Down Expand Up @@ -261,6 +302,7 @@ func GetMutableStateWithConsistencyCheck(
workflowKey definition.WorkflowKey,
currentVersion int64,
currentEventID int64,
versionedTransition *persistencespb.VersionedTransition,
workflowConsistencyChecker WorkflowConsistencyChecker,
) (_ *historyservice.GetMutableStateResponse, retError error) {

Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
if len(transitionHistory) != 0 {
if versionedTransition != nil && workflow.TransitionHistoryStalenessCheck(transitionHistory, versionedTransition) != nil {
return false
}
return true
if len(transitionHistory) != 0 && versionedTransition != nil {
return workflow.TransitionHistoryStalenessCheck(transitionHistory, versionedTransition) == nil
}

}

currentVersionHistory, err := versionhistory.GetCurrentVersionHistory(mutableState.GetExecutionInfo().GetVersionHistories())
if err != nil {
return false
Expand Down Expand Up @@ -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
}
27 changes: 19 additions & 8 deletions service/history/api/getworkflowexecutionhistory/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions service/history/events/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ import (
"go.temporal.io/api/serviceerror"
enumsspb "go.temporal.io/server/api/enums/v1"
historyspb "go.temporal.io/server/api/history/v1"
persistencespb "go.temporal.io/server/api/persistence/v1"
"go.temporal.io/server/common"
"go.temporal.io/server/common/clock"
"go.temporal.io/server/common/collection"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/common/persistence/transitionhistory"
"go.temporal.io/server/common/persistence/versionhistory"
)

Expand All @@ -66,6 +68,7 @@ type (
WorkflowState enumsspb.WorkflowExecutionState
WorkflowStatus enumspb.WorkflowExecutionStatus
VersionHistories *historyspb.VersionHistories
TransitionHistory []*persistencespb.VersionedTransition
}

NotifierImpl struct {
Expand Down Expand Up @@ -100,6 +103,7 @@ func NewNotification(
workflowState enumsspb.WorkflowExecutionState,
workflowStatus enumspb.WorkflowExecutionStatus,
versionHistories *historyspb.VersionHistories,
transitionHistory []*persistencespb.VersionedTransition,
) *Notification {

return &Notification{
Expand All @@ -115,6 +119,7 @@ func NewNotification(
WorkflowState: workflowState,
WorkflowStatus: workflowStatus,
VersionHistories: versionhistory.CopyVersionHistories(versionHistories),
TransitionHistory: transitionhistory.CopyVersionedTransitions(transitionHistory),
yycptt marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
13 changes: 11 additions & 2 deletions service/history/events/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
enumspb "go.temporal.io/api/enums/v1"
enumsspb "go.temporal.io/server/api/enums/v1"
historyspb "go.temporal.io/server/api/history/v1"
persistencespb "go.temporal.io/server/api/persistence/v1"
"go.temporal.io/server/common/clock"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/metrics"
Expand Down Expand Up @@ -98,7 +99,11 @@ func (s *notifierSuite) TestSingleSubscriberWatchingEvents() {
versionHistoryItem := versionhistory.NewVersionHistoryItem(nextEventID-1, 1)
currentVersionHistory := versionhistory.NewVersionHistory(branchToken, []*historyspb.VersionHistoryItem{versionHistoryItem})
versionHistories := versionhistory.NewVersionHistories(currentVersionHistory)
historyEvent := NewNotification(namespaceID, execution, lastFirstEventID, lastFirstEventTxnID, nextEventID, previousStartedEventID, workflowState, workflowStatus, versionHistories)
transitionHistory := []*persistencespb.VersionedTransition{
{NamespaceFailoverVersion: 1234, TransitionCount: 1024},
{TransitionCount: 1025},
}
historyEvent := NewNotification(namespaceID, execution, lastFirstEventID, lastFirstEventTxnID, nextEventID, previousStartedEventID, workflowState, workflowStatus, versionHistories, transitionHistory)
timerChan := time.NewTimer(time.Second * 2).C

subscriberID, channel, err := s.notifier.WatchHistoryEvent(definition.NewWorkflowKey(namespaceID, execution.GetWorkflowId(), execution.GetRunId()))
Expand Down Expand Up @@ -133,7 +138,11 @@ func (s *notifierSuite) TestMultipleSubscriberWatchingEvents() {
versionHistoryItem := versionhistory.NewVersionHistoryItem(nextEventID-1, 1)
currentVersionHistory := versionhistory.NewVersionHistory(branchToken, []*historyspb.VersionHistoryItem{versionHistoryItem})
versionHistories := versionhistory.NewVersionHistories(currentVersionHistory)
historyEvent := NewNotification(namespaceID, execution, lastFirstEventID, lastFirstEventTxnID, nextEventID, previousStartedEventID, workflowState, workflowStatus, versionHistories)
transitionHistory := []*persistencespb.VersionedTransition{
{NamespaceFailoverVersion: 1234, TransitionCount: 1024},
{TransitionCount: 1025},
}
historyEvent := NewNotification(namespaceID, execution, lastFirstEventID, lastFirstEventTxnID, nextEventID, previousStartedEventID, workflowState, workflowStatus, versionHistories, transitionHistory)
timerChan := time.NewTimer(time.Second * 5).C

subscriberCount := 100
Expand Down
3 changes: 2 additions & 1 deletion service/history/history_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ func (s *engineSuite) TestGetMutableStateLongPoll_CurrentBranchChanged() {
int64(1),
enumsspb.WORKFLOW_EXECUTION_STATE_CREATED,
enumspb.WORKFLOW_EXECUTION_STATUS_RUNNING,
ms.GetExecutionInfo().GetVersionHistories()),
ms.GetExecutionInfo().GetVersionHistories(),
ms.GetExecutionInfo().GetTransitionHistory()),
)
}

Expand Down
Loading
Loading