Documentation
¶
Index ¶
- Constants
- Variables
- func After(b *Binder, dep Dependency)
- func GetTaskResultFromLocalRunner[TaskResult any](runner *LocalRunner, taskRef taskid.TaskReference[TaskResult]) (TaskResult, bool)
- func HasDependency(taskSet *TaskSet, dependencyFrom UntypedTask, dependencyTo UntypedTask) (bool, error)
- func NewEqualFilter[T comparable](labelKey TaskLabelKey[T], value T, includeUndefined bool) filter.TypedMapFilter[T]
- func NewLabelSet(labelOpts ...LabelOpt) *typedmap.ReadonlyTypedMap
- func NewRequiredTaskLabel() *requiredTaskLabelImpl
- func WrapErrorWithTaskInformation(ctx context.Context, err error) error
- type Binder
- type Dependency
- type Input
- type InputKind
- type InputSpec
- type Interceptor
- type LabelOpt
- func NewTaskResultRetentionLabel(retain bool) LabelOpt
- func ProvidesTag[TaskResult any](tag Tag[TaskResult], opts ...ProvidesTagOption) LabelOpt
- func WithDescription(description string) LabelOpt
- func WithFeatureGate(featureTaskRef taskid.UntypedTaskReference) LabelOpt
- func WithLabelValue[T any](labelKey TaskLabelKey[T], value T) LabelOpt
- func WithSelectionPriority(priority int) LabelOpt
- func WithTitle(title string) LabelOpt
- type LabelPredicate
- type LocalRunner
- func (r *LocalRunner) AddInterceptor(interceptor Interceptor)
- func (r *LocalRunner) Result() (*typedmap.ReadonlyTypedMap, error)
- func (r *LocalRunner) Run(ctx context.Context) error
- func (r *LocalRunner) TaskRunStatuses() map[string]TaskRunStatus
- func (r *LocalRunner) Tasks() []UntypedTask
- func (r *LocalRunner) Wait() <-chan interface{}
- type OptionalInput
- type ProvidesTagOption
- type Tag
- type TagInput
- type TagReference
- type Task
- func Define[T any](id taskid.TaskImplementationID[T], ...) Task[T]
- func DefineAliasTask[TaskResult any](taskId taskid.TaskImplementationID[TaskResult], ...) Task[TaskResult]
- func DefineConstant[T any](id taskid.TaskImplementationID[T], value T, labelOpts ...LabelOpt) Task[T]
- func DefineTailTask(taskID taskid.TaskImplementationID[struct{}], dependencies []Dependency, ...) Task[struct{}]
- type TaskError
- type TaskLabelKey
- type TaskRunPhase
- type TaskRunStatus
- type TaskRunner
- type TaskSet
- func NewResolvedTaskSet(tasks []UntypedTask, edges []taskid.TaskEdge, ...) *TaskSet
- func NewTaskSet(tasks []UntypedTask) (*TaskSet, error)
- func ResolveGraph(initialTasks []UntypedTask, availableTasks []UntypedTask, ...) (*TaskSet, error)
- func Subset[T any](taskSet *TaskSet, mapFilter filter.TypedMapFilter[T]) *TaskSet
- func (s *TaskSet) Add(newTask UntypedTask) error
- func (s *TaskSet) BoundReferenceIDsForTaskImplWithTag(taskImplementationID string, tag string) []string
- func (s *TaskSet) Edges() []taskid.TaskEdge
- func (s *TaskSet) Get(id string) (UntypedTask, error)
- func (s *TaskSet) GetAll() []UntypedTask
- func (s *TaskSet) IncomingEdges(taskImplID string) []taskid.TaskEdge
- func (s *TaskSet) IsBound(refID string) bool
- func (s *TaskSet) Remove(id string) error
- type UntypedTask
Constants ¶
const DefaultTagPriority = 100
DefaultTagPriority is the default priority assigned to a provided tag when not explicitly specified.
const (
KHISystemPrefix = "khi.google.com/"
)
const LabelKeyProvidedTagPrefix = KHISystemPrefix + "provided-tag/"
LabelKeyProvidedTagPrefix is the prefix used for tag labels on tasks.
const LabelKeyProvidedTagPriorityPrefix = KHISystemPrefix + "provided-tag-priority/"
LabelKeyProvidedTagPriorityPrefix is the prefix used for tag priority labels on tasks.
const LabelKeyProvidedTagTypePrefix = KHISystemPrefix + "provided-tag-type/"
LabelKeyProvidedTagTypePrefix is the prefix used for tag output type labels on tasks.
Variables ¶
var ( // FromActiveFeatures configures the dependency scope to ScopeActiveFeatures. FromActiveFeatures = taskid.ScopeActiveFeatures // FromActiveGraph configures the dependency scope to ScopeActiveGraph. FromActiveGraph = taskid.ScopeActiveGraph // FromAll configures the dependency scope to ScopeAll. FromAll = taskid.ScopeAll )
var LabelKeyFeatureGateTaskRef = NewTaskLabelKey[taskid.UntypedTaskReference](KHISystemPrefix + "feature-gate-task-ref")
LabelKeyFeatureGateTaskRef is a task label key specifying the task reference whose presence in the active graph gates inclusion of this task when resolved via ScopeActiveFeatures.
var LabelKeyRequiredTask = NewTaskLabelKey[bool](KHISystemPrefix + "required-task")
LabelKeyRequiredTask is the task label to tell task resolver to always include the task in the task graph when the task is available.
var LabelKeyTaskDescription = NewTaskLabelKey[string](KHISystemPrefix + "task-description")
LabelKeyTaskDescription is the task label to record a human-readable description of the task.
var LabelKeyTaskResultRetention = NewTaskLabelKey[bool](KHISystemPrefix + "task-result-retention")
LabelKeyTaskResultRetention indicates whether the task result should be retained in the runner after all dependent tasks finish.
var LabelKeyTaskResultType = NewTaskLabelKey[string](KHISystemPrefix + "task-result-type")
LabelKeyTaskResultType is the task label to record the string representation of the task output type.
var LabelKeyTaskSelectionPriority = NewTaskLabelKey[int](KHISystemPrefix + "task-selection-priority")
var LabelKeyTaskTitle = NewTaskLabelKey[string](KHISystemPrefix + "task-title")
LabelKeyTaskTitle is the task label to record a human-readable title of the task.
Functions ¶
func After ¶ added in v0.60.1
func After(b *Binder, dep Dependency)
After declares a dependency that the task only waits for. It intentionally returns no handle, so the task cannot read the value and the declaration cannot become an unused input.
func GetTaskResultFromLocalRunner ¶
func GetTaskResultFromLocalRunner[TaskResult any](runner *LocalRunner, taskRef taskid.TaskReference[TaskResult]) (TaskResult, bool)
GetTaskResultFromLocalRunner is a helper function to safely extract a specific task's result from a LocalRunner's result map. It provides a type-safe way to access results using a TaskReference.
func HasDependency ¶
func HasDependency(taskSet *TaskSet, dependencyFrom UntypedTask, dependencyTo UntypedTask) (bool, error)
HasDependency check if 2 tasks have dependency between them when the task graph was resolved with given task set.
func NewEqualFilter ¶
func NewEqualFilter[T comparable](labelKey TaskLabelKey[T], value T, includeUndefined bool) filter.TypedMapFilter[T]
NewEqualFilter creates a new filter that matches exact label values
func NewLabelSet ¶
func NewLabelSet(labelOpts ...LabelOpt) *typedmap.ReadonlyTypedMap
Construct the LabelSet with required fields.
func NewRequiredTaskLabel ¶
func NewRequiredTaskLabel() *requiredTaskLabelImpl
NewRequiredTaskLabel returns a LabelOpt to mark the task is always included in the result task graph.
Types ¶
type Binder ¶ added in v0.60.1
type Binder struct {
// contains filtered or unexported fields
}
Binder collects the inputs of a task defined with Define. Inputs can only be declared while the bind function passed to Define is running.
type Dependency ¶ added in v0.59.0
type Dependency = taskid.DependencyDescriptor
Dependency represents any task dependency descriptor, such as a point-to-point reference or a fan-in tag reference.
type Input ¶ added in v0.60.1
type Input[T any] struct { // contains filtered or unexported fields }
Input is a handle to a required input declared with Use.
func Use ¶ added in v0.60.1
func Use[T any](b *Binder, ref taskid.TaskReference[T]) Input[T]
Use declares a required input and returns the handle to read its value. The reference must use ScopeAll, the default scope, so that the producer is always pulled into the graph. Use UseOptional for references with a narrower scope. Define prefixes the panic for a wrong scope with the task ID.
type InputKind ¶ added in v0.60.1
type InputKind int
InputKind classifies how a task depends on an input after all declarations of the input are merged.
const ( // InputKindRequired is an input whose value the task reads and whose producer must be in the graph. // Use declares it. An input declared with UseOptional also becomes required when an ordering dependency // on the same reference uses ScopeAll. InputKindRequired InputKind = iota // InputKindOptional is an input whose value the task reads and whose producer may be absent from the graph. // UseOptional declares it. InputKindOptional // InputKindTag is a fan-in input declared with UseTag over all producers of a tag. InputKindTag // InputKindOrdering is a dependency declared only with After. The task waits for it but never reads its value. InputKindOrdering )
type InputSpec ¶ added in v0.60.1
type InputSpec struct {
// Dependency is the dependency descriptor registered in the task graph for this input.
Dependency Dependency
// Kind is the merged kind of every declaration of the input.
Kind InputKind
}
InputSpec describes one input declared on a Binder.
type Interceptor ¶ added in v0.50.0
type Interceptor func(ctx context.Context, task UntypedTask, next func(context.Context) (any, error)) (any, error)
Interceptor is a function that can intercept the execution of a task. It allows injecting custom logic before and after the task execution.
type LabelOpt ¶
LabelOpt implementations wraps setting values to the task albels.
func NewTaskResultRetentionLabel ¶ added in v0.58.0
NewTaskResultRetentionLabel returns a LabelOpt to specify whether the task result should be retained after all dependent tasks finish.
func ProvidesTag ¶ added in v0.59.0
func ProvidesTag[TaskResult any](tag Tag[TaskResult], opts ...ProvidesTagOption) LabelOpt
ProvidesTag returns a LabelOpt declaring that the task provides the given typed tag. Optional ProvidesTagOptions (such as WithTagPriority) can be specified to adjust tag attributes.
func WithDescription ¶ added in v0.61.0
WithDescription returns a LabelOpt to attach a human-readable description to the task.
func WithFeatureGate ¶ added in v0.60.1
func WithFeatureGate(featureTaskRef taskid.UntypedTaskReference) LabelOpt
WithFeatureGate sets the feature gate task reference for a task. When a task with a feature gate is referenced via ScopeActiveFeatures, it is pulled into the graph only if it is already in the graph or its feature gate task is present in the active graph. Tasks without WithFeatureGate are pulled in unconditionally when referenced via ScopeActiveFeatures.
func WithLabelValue ¶
func WithLabelValue[T any](labelKey TaskLabelKey[T], value T) LabelOpt
WithLabelValue creates a LabelOpt to store a single value associated to a label key.
func WithSelectionPriority ¶
type LabelPredicate ¶
type LocalRunner ¶
type LocalRunner struct {
// contains filtered or unexported fields
}
LocalRunner executes a task graph defined by a TaskSet on the local machine. It manages task dependencies, concurrent execution, and result aggregation.
func NewLocalRunner ¶
func NewLocalRunner(taskSet *TaskSet) (*LocalRunner, error)
NewLocalRunner creates and initializes a new LocalRunner for a given TaskSet. The TaskSet must be runnable (i.e., topologically sorted with all dependencies met). It returns an error if the provided TaskSet is not runnable.
func (*LocalRunner) AddInterceptor ¶ added in v0.50.0
func (r *LocalRunner) AddInterceptor(interceptor Interceptor)
AddInterceptor adds an interceptor to the runner. Interceptors are executed in the order they are added.
func (*LocalRunner) Result ¶
func (r *LocalRunner) Result() (*typedmap.ReadonlyTypedMap, error)
Result returns the final results of the task graph execution. It returns a map of task results if the execution was successful, or an error if any task failed or the runner has not yet completed. This method should only be called after the channel from Wait() has been closed.
func (*LocalRunner) Run ¶
func (r *LocalRunner) Run(ctx context.Context) error
Run starts the execution of the task graph in a non-blocking manner. It launches a goroutine to manage the entire execution process. It returns an error if the runner has already been started.
func (*LocalRunner) TaskRunStatuses ¶ added in v0.59.0
func (r *LocalRunner) TaskRunStatuses() map[string]TaskRunStatus
TaskRunStatuses returns a snapshot of the execution state of every task in the runner's task set, keyed by the task implementation ID. The returned map and its values are detached from the runner, so they are safe to read while the task graph keeps running.
func (*LocalRunner) Tasks ¶ added in v0.50.0
func (r *LocalRunner) Tasks() []UntypedTask
func (*LocalRunner) Wait ¶
func (r *LocalRunner) Wait() <-chan interface{}
Wait returns a channel that is closed when the runner finishes executing all tasks in the graph. This is the primary mechanism for waiting for the completion of the entire task set.
type OptionalInput ¶ added in v0.60.1
type OptionalInput[T any] struct { // contains filtered or unexported fields }
OptionalInput is a handle to an optional input declared with UseOptional.
func UseOptional ¶ added in v0.60.1
func UseOptional[T any](b *Binder, ref taskid.TaskReference[T]) OptionalInput[T]
UseOptional declares an input whose producer may be absent from the graph and returns the handle to read it. The reference must use a scope narrower than ScopeAll, such as ScopeActiveGraph or ScopeActiveFeatures. Define prefixes the panic for a wrong scope with the task ID.
type ProvidesTagOption ¶ added in v0.59.0
type ProvidesTagOption interface {
// contains filtered or unexported methods
}
ProvidesTagOption is an option to configure tag provision attributes on a task.
func WithTagPriority ¶ added in v0.59.0
func WithTagPriority(priority int) ProvidesTagOption
WithTagPriority returns a ProvidesTagOption specifying the priority weight of the provided tag during graph resolution and cycle pruning. Lower numerical values indicate higher precedence (e.g. 10 is higher priority than 100). The default priority when unspecified is DefaultTagPriority (100).
type Tag ¶ added in v0.59.0
type Tag[TaskResult any] struct { // contains filtered or unexported fields }
Tag represents a strongly-typed tag identifier that groups task outputs of type TaskResult.
func (Tag[TaskResult]) Ref ¶ added in v0.59.0
func (t Tag[TaskResult]) Ref(opts ...taskid.FanInOption) TagReference[TaskResult]
Ref creates a typed dependency reference to tasks providing this tag with optional configurations.
type TagInput ¶ added in v0.60.1
type TagInput[T any] struct { // contains filtered or unexported fields }
TagInput is a handle to a fan-in input declared with UseTag.
func UseTag ¶ added in v0.60.1
func UseTag[T any](b *Binder, ref TagReference[T]) TagInput[T]
UseTag declares a fan-in input over all producers of a tag and returns the handle to read their results. The tag reference must not use ScopeAll because fan-in resolution only supports ScopeActiveFeatures and ScopeActiveGraph.
type TagReference ¶ added in v0.59.0
type TagReference[TaskResult any] interface { taskid.FanInDescriptor // GetZeroValue returns a zero value of the TaskResult type to preserve type safety. GetZeroValue() TaskResult }
TagReference defines a typed reference to an aggregation of tasks providing a specific tag.
func NewTagReference ¶ added in v0.59.0
func NewTagReference[TaskResult any](tag string, opts ...taskid.FanInOption) TagReference[TaskResult]
NewTagReference creates a new TagReference for the specified tag and options.
type Task ¶
type Task[TaskResult any] interface { UntypedTask // ID returns an unique TaskID of taskid.TaskImplementationID[TaskResult] // The implementation of this function must return a constant value. ID() taskid.TaskImplementationID[TaskResult] Run(ctx context.Context) (TaskResult, error) }
Task is the fundamental interface that all of DAG nodes in KHI task system implements. The implementation of ID and Labels must be deterministic when the application started. The implementation of Sinks and Source must be pure function not depending anything outside of the argument.
func Define ¶ added in v0.60.1
func Define[T any]( id taskid.TaskImplementationID[T], bind func(b *Binder) func(ctx context.Context) (T, error), labelOpts ...LabelOpt, ) Task[T]
Define constructs a task whose dependencies are declared through a Binder. bind runs once when Define is called. It must declare every input with Use, UseOptional, UseTag, or After, and return the run function that reads the inputs through the returned handles. Keep handles in local variables of bind so that the compiler reports inputs that are declared but never read.
func DefineAliasTask ¶ added in v0.60.1
func DefineAliasTask[TaskResult any](taskId taskid.TaskImplementationID[TaskResult], sourceTaskReference taskid.TaskReference[TaskResult], labelOpts ...LabelOpt) Task[TaskResult]
DefineAliasTask defines a task implementation that proxies the result of another task. This is useful for selectively overriding dependencies on a per-task basis.
func DefineConstant ¶ added in v0.60.1
func DefineConstant[T any](id taskid.TaskImplementationID[T], value T, labelOpts ...LabelOpt) Task[T]
DefineConstant constructs a task that has no inputs and always returns value. The same value is returned on every run, so callers must not modify reference types such as slices or maps in it.
func DefineTailTask ¶ added in v0.60.1
func DefineTailTask(taskID taskid.TaskImplementationID[struct{}], dependencies []Dependency, labelOpts ...LabelOpt) Task[struct{}]
DefineTailTask constructs a no-op barrier task that waits for all dependencies without reading their values. Duplicate dependencies are merged into one input by the Binder.
type TaskError ¶ added in v0.60.1
type TaskError struct {
TaskID taskid.UntypedTaskImplementationID
Err error
}
TaskError represents an error that occurred during the execution of a task in a task graph.
type TaskLabelKey ¶
TaskLabelKey is a key of labels given to task.
func LabelKeyProvidedTag ¶ added in v0.59.0
func LabelKeyProvidedTag(tagID string) TaskLabelKey[bool]
LabelKeyProvidedTag returns a TaskLabelKey to record a provided tag on a task.
func LabelKeyProvidedTagPriority ¶ added in v0.59.0
func LabelKeyProvidedTagPriority(tagID string) TaskLabelKey[int]
LabelKeyProvidedTagPriority returns a TaskLabelKey to record the priority of a provided tag on a task.
func LabelKeyProvidedTagType ¶ added in v0.59.0
func LabelKeyProvidedTagType(tagID string) TaskLabelKey[string]
LabelKeyProvidedTagType returns a TaskLabelKey to record the output type of a provided tag on a task.
func NewTaskLabelKey ¶
func NewTaskLabelKey[T any](key string) TaskLabelKey[T]
NewTaskLabelKey returns the key used in labels with type annotation,
type TaskRunPhase ¶ added in v0.59.0
type TaskRunPhase int
TaskRunPhase represents the execution phase of a single task in a task graph.
const ( // TaskRunPhaseWaiting indicates that the task is waiting for its dependencies to complete. TaskRunPhaseWaiting TaskRunPhase = iota // TaskRunPhaseRunning indicates that the task is currently running. TaskRunPhaseRunning // TaskRunPhaseDone indicates that the task finished successfully. TaskRunPhaseDone // TaskRunPhaseError indicates that the task finished with an error. TaskRunPhaseError )
func (TaskRunPhase) String ¶ added in v0.59.0
func (p TaskRunPhase) String() string
String returns the human-readable name of the task run phase.
type TaskRunStatus ¶ added in v0.59.0
TaskRunStatus is an immutable snapshot of the execution state of a single task. StartTime is zero while the task is waiting, and EndTime is zero until the task finishes. Error holds the error returned by the task when Phase is TaskRunPhaseError.
type TaskRunner ¶
type TaskRunner interface {
Run(ctx context.Context) error
Wait() <-chan interface{}
Result() (*typedmap.ReadonlyTypedMap, error)
Tasks() []UntypedTask
TaskRunStatuses() map[string]TaskRunStatus
AddInterceptor(interceptor Interceptor)
}
TaskRunner receives the runnable TaskSet and run tasks with topological sorted order.
type TaskSet ¶
type TaskSet struct {
// contains filtered or unexported fields
}
TaskSet is a collection of tasks and resolved dependency edges. It implements core_contract.TaskGraphMetadata and provides querying for execution order and edges.
func NewResolvedTaskSet ¶ added in v0.59.0
func NewResolvedTaskSet( tasks []UntypedTask, edges []taskid.TaskEdge, boundFanInRefIDsByTaskImplID map[string]map[string][]string, ) *TaskSet
NewResolvedTaskSet creates a new runnable TaskSet with resolved tasks, edges, and metadata.
func NewTaskSet ¶
func NewTaskSet(tasks []UntypedTask) (*TaskSet, error)
NewTaskSet creates a new TaskSet with the given tasks. Returns an error if there are duplicate task IDs.
func ResolveGraph ¶ added in v0.59.0
func ResolveGraph( initialTasks []UntypedTask, availableTasks []UntypedTask, disabledTasks []UntypedTask, ) (*TaskSet, error)
ResolveGraph resolves the task graph starting from initialTasks, drawing dependencies from availableTasks, and excluding disabledTasks. It applies a deterministic 5-phase resolution algorithm and returns a runnable TaskSet containing topologically sorted tasks and concrete TaskEdges.
func Subset ¶
func Subset[T any](taskSet *TaskSet, mapFilter filter.TypedMapFilter[T]) *TaskSet
Subset returns a new TaskSet filtered using the provided type-safe filter
func (*TaskSet) Add ¶
func (s *TaskSet) Add(newTask UntypedTask) error
Add a task definition to current TaskSet. Returns an error when duplicated task Id is assigned on the task.
func (*TaskSet) BoundReferenceIDsForTaskImplWithTag ¶ added in v0.59.0
func (s *TaskSet) BoundReferenceIDsForTaskImplWithTag(taskImplementationID string, tag string) []string
BoundReferenceIDsForTaskImplWithTag returns the list of task reference IDs providing the tag bound specifically to the given task implementation ID.
func (*TaskSet) Get ¶
func (s *TaskSet) Get(id string) (UntypedTask, error)
Get returns a task with the given string task ID notation.
func (*TaskSet) GetAll ¶
func (s *TaskSet) GetAll() []UntypedTask
GetAll returns a copy of all tasks in the set.
func (*TaskSet) IncomingEdges ¶ added in v0.59.0
IncomingEdges returns incoming edges for the given task implementation ID.
type UntypedTask ¶
type UntypedTask interface {
UntypedID() taskid.UntypedTaskImplementationID
// Labels returns KHITaskLabelSet assigned to this task unit.
// The implementation of this function must return a constant value.
Labels() *typedmap.ReadonlyTypedMap
// Inputs returns the inputs declared for this task in declaration order.
Inputs() []InputSpec
// ResultType returns the reflection Type of the task output.
ResultType() reflect.Type
UntypedRun(ctx context.Context) (any, error)
}