Files
tasks/internal/effect/buffer.go
Matt McShaneandGitHub 9a1d30b245 Refactor the Update type to a state machine (#4297)
This is a step in the direction of being able to process messages one at
a time rather than in a batch. Individual message processing will be
required to respect protocols (like update) that need message processing
that is ordered with respect to other commands. Additionally we lay the
groundwork for async updates and outcome polling.

A summary of what changed:

- Write update.Update as a protocol state machine that encapsulates all
  message processing, validation, event writing, and metric emission
  related to an update.
- Make explicit the set of "Provisional" state transitions wherein the
  Update has recieved and reacted to a protocol message but cannot make
  externally observable changes like returning update outcomes to RPC
  callers until it knows that changes have been written to stable
  storage.
- The effect package is introduced as a utility to buffer and later
  apply or cancel a set of effects.
- With more responsibility in the Update type, UpdateRegistry is
  simplified.
- UpdateRegistry can restore its state (mainly in-flight Accepted
  updates) from MutableState.
- Update message validation is moved into the update package.
- UpdateRegistry differentiates between having incomplete updates (which
  prevents a workflow from closing) and having messages to send (which
  may trigger the creation of a new workflow task)
- Removed a chunk of processing from workflowTaskHandler that is now
  encapsulated in the Update state machine, allowing task handling to be
  more decoupled from update protocol semantics.
2023-05-10 12:14:02 -07:00

70 lines
2.5 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 effect
import "context"
// Buffer holds a set of effect and rollback functions that can be invoked as a
// batch with a defined order. Once either Apply or Cancel is called, all
// buffered effects are cleared. This type is not threadsafe. The zero-value of
// a Buffer is a valid state.
type Buffer struct {
effects []func(context.Context)
cancels []func(context.Context)
}
// OnAfterCommit adds the provided effect function to set of such functions to
// be invoked when Buffer.Apply is called.
func (b *Buffer) OnAfterCommit(effect func(context.Context)) {
b.effects = append(b.effects, effect)
}
// OnAfterRollback adds the provided effect function to set of such functions to
// be invoked when Buffer.Cancel is called.
func (b *Buffer) OnAfterRollback(effect func(context.Context)) {
b.cancels = append(b.cancels, effect)
}
// Apply invokes the buffered effect functions in the order that they were added
// to this Buffer.
func (b *Buffer) Apply(ctx context.Context) {
for _, effect := range b.effects {
effect(ctx)
}
b.effects = nil
b.cancels = nil
}
// Cancel invokes the buffered rollback functions in the reverse of the order
// that they were added to this Buffer.
func (b *Buffer) Cancel(ctx context.Context) {
b.effects = nil
for i := len(b.cancels) - 1; i >= 0; i-- {
b.cancels[i](ctx)
}
b.cancels = nil
b.effects = nil
}