update

package
v1.26.0-120.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2024 License: MIT Imports: 25 Imported by: 0

README

Update State Machine

The diagram below depicts the state machine implemented in update.go.

stateDiagram-v2
    [*] --> Admitted: New

    Admitted --> ProvisionallyRequested: onRequestMsg
    ProvisionallyRequested --> Requested: commit

    Requested --> ProvisionallyAccepted: onAcceptanceMsg
    ProvisionallyAccepted --> Accepted: commit

    Requested --> ProvisionallyCompleted: onRejectionMsg
    ProvisionallyCompleted --> Completed: commit

    ProvisionallyAccepted --> ProvisionallyCompleted: onResponse
    Accepted --> ProvisionallyCompleted: onResponse

Documentation

Index

Constants

View Source
const ProtocolV1 = protocol.Type("temporal.api.update.v1")

Variables

View Source
var (
	WorkflowUpdateAbortedErr = serviceerror.NewUnavailable("Workflow Update was aborted.")
)

Functions

This section is empty.

Types

type AbortReason added in v1.24.0

type AbortReason uint32
const (
	AbortReasonRegistryCleared AbortReason = iota + 1
	AbortReasonWorkflowCompleted
)

func (AbortReason) Error added in v1.24.0

func (r AbortReason) Error() error

Error returns an error which will be set to update futures while aborting waiters.

type EventStore added in v1.21.0

type EventStore interface {
	effect.Controller

	// AddWorkflowExecutionUpdateAcceptedEvent writes an update accepted
	// event. The data may not be durable when this function returns.
	AddWorkflowExecutionUpdateAcceptedEvent(
		updateID string,
		acceptedRequestMessageId string,
		acceptedRequestSequencingEventId int64,
		acceptedRequest *updatepb.Request,
	) (*historypb.HistoryEvent, error)

	// AddWorkflowExecutionUpdateCompletedEvent writes an update completed
	// event. The data may not be durable when this function returns.
	AddWorkflowExecutionUpdateCompletedEvent(
		acceptedEventID int64,
		resp *updatepb.Response,
	) (*historypb.HistoryEvent, error)

	// CanAddEvent returns true if an event can be added to the EventStore.
	CanAddEvent() bool
}

EventStore is the interface that an Update needs to read and write events and to be notified when buffered writes have been flushed. It is the expectation of this code that writes to EventStore will return before the data has been made durable. Callbacks attached to the EventStore via OnAfterCommit and OnAfterRollback *must* be called after the EventStore state is successfully written or is discarded.

type Option added in v1.23.0

type Option func(*registry)

func WithInFlightLimit added in v1.21.0

func WithInFlightLimit(f func() int) Option

WithInFlightLimit provides an optional limit to the number of incomplete updates that a Registry instance will allow.

func WithLogger added in v1.21.0

func WithLogger(l log.Logger) Option

WithLogger sets the log.Logger to be used by an UpdateRegistry and its Updates.

func WithMetrics added in v1.21.0

func WithMetrics(m metrics.Handler) Option

WithMetrics sets the metrics.Handler to be used by an UpdateRegistry and its Updates.

func WithTotalLimit added in v1.21.0

func WithTotalLimit(f func() int) Option

WithTotalLimit provides an optional limit to the total number of updates for workflow.

func WithTracerProvider added in v1.21.0

func WithTracerProvider(t trace.TracerProvider) Option

WithTracerProvider sets the trace.TracerProvider (and by extension the trace.Tracer) to be used by an UpdateRegistry and its Updates.

type Registry

type Registry interface {
	// FindOrCreate finds an existing Update or creates a new one. The second
	// return value (bool) indicates whether the Update returned already
	// existed and was found (true) or was not found and has been newly
	// created (false)
	FindOrCreate(ctx context.Context, protocolInstanceID string) (*Update, bool, error)

	// Find finds an existing update in this Registry but does not create a
	// new update if no update is found.
	Find(ctx context.Context, protocolInstanceID string) *Update

	// TryResurrect tries to resurrect the update from the protocol message,
	// whose body contains Acceptance or Rejection message.
	// It returns an error if some unexpected error happened, but if there is not
	// enough data in the message, it just returns a nil update.
	// If the update was successfully resurrected it is added to the registry in stateAdmitted.
	TryResurrect(ctx context.Context, acptOrRejMsg *protocolpb.Message) (*Update, error)

	// HasOutgoingMessages returns true if the registry has any Updates
	// for which outgoing message can be generated.
	// If includeAlreadySent is set to true then it will return true
	// even if update message was already sent but not processed by worker.
	HasOutgoingMessages(includeAlreadySent bool) bool

	// Send returns messages for all Updates that need to be sent to the worker.
	// If includeAlreadySent is set to true then messages will be created even
	// for updates which were already sent but not processed by worker.
	Send(ctx context.Context, includeAlreadySent bool, workflowTaskStartedEventID int64) []*protocolpb.Message

	// RejectUnprocessed reject all updates that are waiting for workflow task to be completed.
	// This method should be called after all messages from worker are handled to make sure
	// that worker processed (rejected or accepted) all updates that were delivered on the workflow task.
	RejectUnprocessed(ctx context.Context, effects effect.Controller) ([]string, error)

	// Abort all incomplete updates in the registry.
	Abort(reason AbortReason)

	Contains(protocolInstanceID string) bool

	// Clear registry and abort all waiters.
	Clear()

	// Len observes the number of incomplete updates in this Registry.
	Len() int

	// GetSize returns the size of the update object
	GetSize() int

	// FailoverVersion of mutable state at the time of registry creation.
	FailoverVersion() int64
}

Registry maintains a set of updates that have been admitted to run against a workflow execution.

func NewRegistry

func NewRegistry(
	store UpdateStore,
	opts ...Option,
) Registry

type Status added in v1.24.0

type Status struct {
	// The most advanced stage reached by update.
	Stage enumspb.UpdateWorkflowExecutionLifecycleStage
	// Outcome of update if it is completed or rejected.
	Outcome *updatepb.Outcome
}

Status describes current status of update from registry.

type Update

type Update struct {
	// contains filtered or unexported fields
}

Update is a state machine for the update protocol. It reads and writes messages from the go.temporal.io/api/update/v1 package. See the diagram in service/history/workflow/update/README.md. The update state machine is straightforward except in that it provides "provisional" in-between states where the update has received a message that has modified its internal state but those changes have not been made visible to clients yet (e.g. accepted or outcome futures have not been set yet). The observable changes are bound to the EventStore's effect.Controller and will be triggered when those effects are applied. State transitions (OnMessage calls) must be done while holding the workflow lock.

func New added in v1.21.0

func New(id string, opts ...updateOpt) *Update

New creates a new Update instance with the provided ID that will call the onComplete callback when it completes.

func (*Update) Admit added in v1.24.0

func (u *Update) Admit(
	req *updatepb.Request,
	eventStore EventStore,
) error

Admit works if the Update is in any state but if the state is anything other than stateCreated then it just early returns a nil error. This effectively gives us update request deduplication by update ID. If the Update is in stateCreated then it builds a protocolpb.Message that will be sent when Send is called.

func (*Update) GetSize added in v1.24.0

func (u *Update) GetSize() int

func (*Update) OnProtocolMessage added in v1.24.0

func (u *Update) OnProtocolMessage(
	protocolMsg *protocolpb.Message,
	eventStore EventStore,
) error

OnProtocolMessage delivers a message to the Update state machine. The Body field of *protocolpb.Message parameter is expected to be one of *updatepb.Response, *updatepb.Rejection, *updatepb.Acceptance. Writes to the EventStore occur synchronously but externally observable effects on this Update (e.g. emitting an Outcome or an Accepted) are registered with the EventStore to be applied after the durable updates are committed. If the EventStore rolls back its effects, this state machine does the same.

If you modify the state machine please update the diagram in service/history/workflow/update/README.md.

func (*Update) Send added in v1.24.0

func (u *Update) Send(
	includeAlreadySent bool,
	sequencingID *protocolpb.Message_EventId,
) *protocolpb.Message

Send moves update from stateAdmitted to stateSent and returns the message to be sent to worker. If update is not in expected stateAdmitted, Send does nothing and returns nil. If includeAlreadySent is set to true then Send will return message even if update was already sent but not processed by worker. If update lacks a request then return nil; the request will be communicated to the worker via an UpdateAdmitted event. Note: once update moved to stateSent it never moves back to stateAdmitted.

func (*Update) WaitLifecycleStage added in v1.23.0

func (u *Update) WaitLifecycleStage(
	ctx context.Context,
	waitStage enumspb.UpdateWorkflowExecutionLifecycleStage,
	softTimeout time.Duration,
) (*Status, error)

WaitLifecycleStage waits until the Update has reached waitStage or a timeout. If the Update reaches waitStage with no timeout, outcome (if any) is returned. If there is a timeout due to the supplied soft timeout, then the most advanced stage known to have been reached is returned together with empty outcome. If there is a timeout due to supplied context deadline expiry, then the error is returned. If waitStage is UNSPECIFIED, current reached Status is returned immediately (even if ctx is expired).

type UpdateStore added in v1.21.0

type UpdateStore interface {
	VisitUpdates(visitor func(updID string, updInfo *persistencespb.UpdateInfo))
	GetUpdateOutcome(ctx context.Context, updateID string) (*updatepb.Outcome, error)
	GetCurrentVersion() int64
	IsWorkflowExecutionRunning() bool
}

UpdateStore represents the update package's requirements for reading updates from the store.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL