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

Move update registry from mutable state to workflow context #4032

Merged
merged 3 commits into from
Mar 10, 2023
Merged
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
2 changes: 1 addition & 1 deletion service/history/api/updateworkflow/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Invoke(
return nil, consts.ErrWorkflowExecutionNotFound
}

upd, duplicate, removeFn := ms.UpdateRegistry().Add(req.GetRequest().GetRequest())
upd, duplicate, removeFn := weCtx.GetContext().UpdateRegistry().Add(req.GetRequest().GetRequest())
if removeFn != nil {
defer removeFn()
}
Expand Down
3 changes: 0 additions & 3 deletions service/history/workflow/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import (
"go.temporal.io/server/service/history/shard"
"go.temporal.io/server/service/history/tests"
"go.temporal.io/server/service/history/workflow"
"go.temporal.io/server/service/history/workflow/update"
)

type (
Expand Down Expand Up @@ -226,7 +225,6 @@ func (s *workflowCacheSuite) TestHistoryCacheClear() {

s.NotNil(ctx.(*workflow.ContextImpl).MutableState)
mock.EXPECT().GetQueryRegistry().Return(workflow.NewQueryRegistry())
mock.EXPECT().UpdateRegistry().Return(update.NewRegistry())
release(errors.New("some random error message"))

// since last time, the release function receive a non-nil error
Expand Down Expand Up @@ -279,7 +277,6 @@ func (s *workflowCacheSuite) TestHistoryCacheConcurrentAccess_Release() {
// all we need is a fake MutableState
mock := workflow.NewMockMutableState(s.controller)
mock.EXPECT().GetQueryRegistry().Return(workflow.NewQueryRegistry())
mock.EXPECT().UpdateRegistry().Return(update.NewRegistry())
ctx.(*workflow.ContextImpl).MutableState = mock
release(errors.New("some random error message"))
}
Expand Down
19 changes: 12 additions & 7 deletions service/history/workflow/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
"go.temporal.io/server/service/history/consts"
"go.temporal.io/server/service/history/shard"
"go.temporal.io/server/service/history/tasks"
"go.temporal.io/server/service/history/workflow/update"
)

const (
Expand Down Expand Up @@ -144,6 +145,8 @@ type (
ctx context.Context,
now time.Time,
) error
// TODO (alex-update): move this from workflow context.
Copy link
Member Author

Choose a reason for hiding this comment

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

This is because long term goal is to to store registry out of workflow cache.

UpdateRegistry() update.Registry
}
)

Expand All @@ -158,9 +161,10 @@ type (
config *configs.Config
transaction Transaction

mutex locks.PriorityMutex
MutableState MutableState
stats *persistencespb.ExecutionStats
mutex locks.PriorityMutex
MutableState MutableState
updateRegistry update.Registry
stats *persistencespb.ExecutionStats
}
)

Expand All @@ -180,6 +184,7 @@ func NewContext(
timeSource: shard.GetTimeSource(),
config: shard.GetConfig(),
mutex: locks.NewPriorityMutex(),
updateRegistry: update.NewRegistry(),
transaction: NewTransaction(shard),
stats: &persistencespb.ExecutionStats{
HistorySize: 0,
Expand Down Expand Up @@ -218,10 +223,6 @@ func (c *ContextImpl) Clear() {
c.metricsHandler.Counter(metrics.WorkflowContextCleared.GetMetricName()).Record(1)
if c.MutableState != nil {
c.MutableState.GetQueryRegistry().Clear()
// Updates are stored in-memory in mutable state. When mutable state is unload due to the error,
// all pending updates must be cleared to notify UpdateWorkflowExecution API callers immediately
// without waiting for timeout.
c.MutableState.UpdateRegistry().Clear()
}
c.MutableState = nil
c.stats = &persistencespb.ExecutionStats{
Expand Down Expand Up @@ -872,6 +873,10 @@ func (c *ContextImpl) ReapplyEvents(
return err
}

func (c *ContextImpl) UpdateRegistry() update.Registry {
return c.updateRegistry
}

// Returns true if execution is forced terminated
func (c *ContextImpl) enforceSizeCheck(
ctx context.Context,
Expand Down
15 changes: 15 additions & 0 deletions service/history/workflow/context_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions service/history/workflow/mutable_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import (
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/common/persistence"
"go.temporal.io/server/service/history/tasks"
"go.temporal.io/server/service/history/workflow/update"
)

type TransactionPolicy int
Expand Down Expand Up @@ -198,8 +197,6 @@ type (
GetWorkflowStateStatus() (enumsspb.WorkflowExecutionState, enumspb.WorkflowExecutionStatus)
GetQueryRegistry() QueryRegistry
IsTransientWorkflowTask() bool
// TODO (alex-update): move this out from mutable state.
UpdateRegistry() update.Registry
ClearTransientWorkflowTask() error
HasBufferedEvents() bool
HasInFlightWorkflowTask() bool
Expand Down
9 changes: 1 addition & 8 deletions service/history/workflow/mutable_state_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ import (
"go.temporal.io/server/service/history/events"
"go.temporal.io/server/service/history/shard"
"go.temporal.io/server/service/history/tasks"
"go.temporal.io/server/service/history/workflow/update"
)

const (
Expand Down Expand Up @@ -171,7 +170,6 @@ type (
taskGenerator TaskGenerator
workflowTaskManager *workflowTaskStateMachine
QueryRegistry QueryRegistry
updateRegistry update.Registry

shard shard.Context
clusterMetadata cluster.Metadata
Expand Down Expand Up @@ -230,8 +228,7 @@ func NewMutableState(
appliedEvents: make(map[string]struct{}),
InsertTasks: make(map[tasks.Category][]tasks.Task),

QueryRegistry: NewQueryRegistry(),
updateRegistry: update.NewRegistry(),
QueryRegistry: NewQueryRegistry(),

shard: shard,
clusterMetadata: shard.GetClusterMetadata(),
Expand Down Expand Up @@ -636,10 +633,6 @@ func (ms *MutableStateImpl) GetQueryRegistry() QueryRegistry {
return ms.QueryRegistry
}

func (ms *MutableStateImpl) UpdateRegistry() update.Registry {
return ms.updateRegistry
}

func (ms *MutableStateImpl) GetActivityScheduledEvent(
ctx context.Context,
scheduledEventID int64,
Expand Down
15 changes: 0 additions & 15 deletions service/history/workflow/mutable_state_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 0 additions & 23 deletions service/history/workflow/update/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"sync"

"github.com/gogo/protobuf/types"
failurepb "go.temporal.io/api/failure/v1"
protocolpb "go.temporal.io/api/protocol/v1"
updatepb "go.temporal.io/api/update/v1"
)
Expand All @@ -41,8 +40,6 @@ type (

HasPending(filterMessages []*protocolpb.Message) bool
ProcessIncomingMessages(messages []*protocolpb.Message) error

Clear()
}

Duplicate bool
Expand Down Expand Up @@ -170,15 +167,6 @@ func (r *RegistryImpl) ProcessIncomingMessages(messages []*protocolpb.Message) e
return nil
}

func (r *RegistryImpl) Clear() {
r.Lock()
defer r.Unlock()
for _, upd := range r.updates {
upd.sendReject(r.clearFailure())
}
r.updates = make(map[string]*Update)
}

func (r *RegistryImpl) getPendingUpdateNoLock(protocolInstanceID string) *Update {
if upd, ok := r.updates[protocolInstanceID]; ok && upd.state == statePending {
return upd
Expand All @@ -193,17 +181,6 @@ func (r *RegistryImpl) getAcceptedUpdateNoLock(protocolInstanceID string) *Updat
return nil
}

func (r *RegistryImpl) clearFailure() *failurepb.Failure {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is not needed anymore.

return &failurepb.Failure{
Message: "update cleared, please retry",
FailureInfo: &failurepb.Failure_ServerFailureInfo{
ServerFailureInfo: &failurepb.ServerFailureInfo{
NonRetryable: false,
},
},
}
}

func (r *RegistryImpl) remove(id string) {
r.Lock()
defer r.Unlock()
Expand Down
9 changes: 5 additions & 4 deletions service/history/workflow/workflow_task_state_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,6 @@ func (m *workflowTaskStateMachine) AddWorkflowTaskFailedEvent(
workflowTask.SuggestContinueAsNew,
workflowTask.HistorySizeBytes,
)
// TODO (alex-update): Do we need to call next line here same as in AddWorkflowTaskCompletedEvent?
m.ms.hBuilder.FlushAndCreateNewBatch()
workflowTask.StartedEventID = startedEvent.GetEventId()
}
Expand Down Expand Up @@ -638,9 +637,11 @@ func (m *workflowTaskStateMachine) FailWorkflowTask(
incrementAttempt bool,
) {
// Increment attempts only if workflow task is failing on non-sticky task queue.
// If it was stick task queue, clear stickiness first and try again before creating transient workflow tas.
incrementAttempt = incrementAttempt && !m.ms.IsStickyTaskQueueEnabled()
m.ms.ClearStickyness()
// If it was stick task queue, clear stickiness first and try again before creating transient workflow task.
if m.ms.IsStickyTaskQueueEnabled() {
incrementAttempt = false
m.ms.ClearStickyness()
}

failWorkflowTaskInfo := &WorkflowTaskInfo{
Version: common.EmptyVersion,
Expand Down
14 changes: 8 additions & 6 deletions service/history/workflowTaskHandlerCallbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
"go.temporal.io/server/service/history/consts"
"go.temporal.io/server/service/history/shard"
"go.temporal.io/server/service/history/workflow"
"go.temporal.io/server/service/history/workflow/update"
)

type (
Expand Down Expand Up @@ -212,7 +213,7 @@ func (handler *workflowTaskHandlerCallbacksImpl) handleWorkflowTaskStarted(
if workflowTask.StartedEventID != common.EmptyEventID {
// If workflow task is started as part of the current request scope then return a positive response
if workflowTask.RequestID == requestID {
resp, err = handler.createRecordWorkflowTaskStartedResponse(mutableState, workflowTask, req.PollRequest.GetIdentity())
resp, err = handler.createRecordWorkflowTaskStartedResponse(mutableState, workflowContext.GetContext().UpdateRegistry(), workflowTask, req.PollRequest.GetIdentity())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -249,7 +250,7 @@ func (handler *workflowTaskHandlerCallbacksImpl) handleWorkflowTaskStarted(
metrics.TaskQueueTypeTag(enumspb.TASK_QUEUE_TYPE_WORKFLOW),
)

resp, err = handler.createRecordWorkflowTaskStartedResponse(mutableState, workflowTask, req.PollRequest.GetIdentity())
resp, err = handler.createRecordWorkflowTaskStartedResponse(mutableState, workflowContext.GetContext().UpdateRegistry(), workflowTask, req.PollRequest.GetIdentity())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -450,7 +451,7 @@ func (handler *workflowTaskHandlerCallbacksImpl) handleWorkflowTaskCompleted(
newMutableState workflow.MutableState
)
// hasPendingUpdates indicates if there are more pending updates (excluding those which are accepted/rejected by this workflow task).
hasPendingUpdates := ms.UpdateRegistry().HasPending(request.GetMessages())
hasPendingUpdates := weContext.UpdateRegistry().HasPending(request.GetMessages())
hasBufferedEvents := ms.HasBufferedEvents()
if err := namespaceEntry.VerifyBinaryChecksum(request.GetBinaryChecksum()); err != nil {
wtFailedCause = newWorkflowTaskFailedCause(
Expand Down Expand Up @@ -671,7 +672,7 @@ func (handler *workflowTaskHandlerCallbacksImpl) handleWorkflowTaskCompleted(
}

// Send update results to gRPC API callers.
err = ms.UpdateRegistry().ProcessIncomingMessages(req.GetCompleteRequest().GetMessages())
err = weContext.UpdateRegistry().ProcessIncomingMessages(req.GetCompleteRequest().GetMessages())
if err != nil {
return nil, err
}
Expand All @@ -690,7 +691,7 @@ func (handler *workflowTaskHandlerCallbacksImpl) handleWorkflowTaskCompleted(
resp := &historyservice.RespondWorkflowTaskCompletedResponse{}
if request.GetReturnNewWorkflowTask() && createNewWorkflowTask {
workflowTask, _ := ms.GetWorkflowTaskInfo(newWorkflowTaskScheduledEventID)
resp.StartedResponse, err = handler.createRecordWorkflowTaskStartedResponse(ms, workflowTask, request.GetIdentity())
resp.StartedResponse, err = handler.createRecordWorkflowTaskStartedResponse(ms, weContext.UpdateRegistry(), workflowTask, request.GetIdentity())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -754,6 +755,7 @@ func (handler *workflowTaskHandlerCallbacksImpl) verifyFirstWorkflowTaskSchedule

func (handler *workflowTaskHandlerCallbacksImpl) createRecordWorkflowTaskStartedResponse(
ms workflow.MutableState,
updateRegistry update.Registry,
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor and not blocking - this seems out of place here. Maybe better to pass the full workflow context and then extract the MS and registry within the function body?

workflowTask *workflow.WorkflowTaskInfo,
identity string,
) (*historyservice.RecordWorkflowTaskStartedResponse, error) {
Expand Down Expand Up @@ -801,7 +803,7 @@ func (handler *workflowTaskHandlerCallbacksImpl) createRecordWorkflowTaskStarted
}
}

response.Messages, err = ms.UpdateRegistry().CreateOutgoingMessages(workflowTask.StartedEventID)
response.Messages, err = updateRegistry.CreateOutgoingMessages(workflowTask.StartedEventID)
if err != nil {
return nil, err
}
Expand Down
Loading