-
Notifications
You must be signed in to change notification settings - Fork 955
[CHASM] Pure task processing - GetPureTasks, ExecutePureTasks #7701
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1241,6 +1241,83 @@ | |
return false | ||
} | ||
|
||
// GetPureTasks returns all valid, expired/runnable pure tasks within the CHASM | ||
// tree. The CHASM tree is left untouched, even if invalid tasks are | ||
// detected (these are cleaned up as part of transaction close). | ||
func (n *Node) GetPureTasks(deadline time.Time) ([]any, error) { | ||
var componentTasks []*persistencespb.ChasmComponentAttributes_Task | ||
|
||
// Walk the tree to find runnable, valid tasks. | ||
err := n.walk(func(node *Node) error { | ||
Check failure on line 1251 in chasm/tree.go
|
||
// Skip nodes that aren't serialized yet. | ||
if node.serializedNode == nil || node.serializedNode.Metadata == nil { | ||
return nil | ||
} | ||
|
||
componentAttr := node.serializedNode.Metadata.GetComponentAttributes() | ||
// Skip nodes that aren't components. | ||
if componentAttr == nil { | ||
return nil | ||
} | ||
|
||
validateContext := NewContext(context.Background(), n) | ||
for _, task := range componentAttr.GetPureTasks() { | ||
if task.ScheduledTime.AsTime().After(deadline) { | ||
// Pure tasks are stored in-order, so we can skip scanning the rest once we hit | ||
// an unexpired task deadline. | ||
break | ||
} | ||
|
||
if task.PhysicalTaskStatus != physicalTaskStatusCreated { | ||
continue | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm why? We only create physical task for the first pure task, so it's expected that some tasks will have TaskStatusNone. |
||
|
||
// Component value must be prepared for validation to work. | ||
if err := node.prepareComponentValue(validateContext); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: we can probably reuse node.Component() with an empty ComponentRef(). Then we only need to implement access rule in that method and task processing side can benefit from it as well. |
||
return err | ||
} | ||
|
||
// Validate the task. If the task is invalid, skip it for processing (it'll be | ||
// removed when the transaction closes). | ||
ok, err := node.validateComponentTask(validateContext, task) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm I don't think we can do validation of tasks first and then run all validated them. The execution of one task may invalidates another task. so we have to validate t1 -> execute t1 -> validate t2 -> execute t2. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, will fix that. I think moving the validate check into |
||
if !ok { | ||
continue | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
componentTasks = append(componentTasks, task) | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Map serialized component tasks to their deserialized values using the Registry. | ||
taskValues := make([]any, len(componentTasks)) | ||
for idx, componentTask := range componentTasks { | ||
registrableTask, ok := n.registry.task(componentTask.GetType()) | ||
if !ok { | ||
return nil, serviceerror.NewInternal(fmt.Sprintf( | ||
"unregistered CHASM task type '%s'", | ||
componentTask.GetType(), | ||
)) | ||
} | ||
|
||
// TODO - validateComponentTask also calls deserializeTask, should share a cached value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in that case, can we refactor validateComponentTask to take in a deserialized task? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, will do. |
||
taskValue, err := deserializeTask(registrableTask, componentTask.Data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
taskValues[idx] = taskValue.Interface() | ||
} | ||
|
||
return taskValues, nil | ||
} | ||
|
||
func newNode( | ||
base *nodeBase, | ||
parent *Node, | ||
|
@@ -1433,3 +1510,42 @@ | |
|
||
return blob, nil | ||
} | ||
|
||
// ExecutePureTask executes the given taskInstance against the node's component. | ||
func (n *Node) ExecutePureTask(taskInstance any) error { | ||
registrableTask, ok := n.registry.taskFor(taskInstance) | ||
if !ok { | ||
return fmt.Errorf("unknown task type for task instance goType '%s'", reflect.TypeOf(taskInstance).Name()) | ||
} | ||
|
||
if !registrableTask.isPureTask { | ||
return fmt.Errorf("ExecutePureTask called on a SideEffect task '%s'", registrableTask.fqType()) | ||
} | ||
|
||
// Ensure this node's component value is hydrated before execution. | ||
ctx := NewContext(context.Background(), n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the timer queue processor should pass in a base context.Context. |
||
if err := n.prepareComponentValue(ctx); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed, child tasks are called on the proper child Node receiver now. |
||
return err | ||
} | ||
|
||
// TODO - access rule check here? | ||
// TODO - instantiate CHASM engine and attach to context | ||
|
||
executor := registrableTask.handler | ||
if executor == nil { | ||
return fmt.Errorf("no handler registered for task type '%s'", registrableTask.taskType) | ||
} | ||
|
||
fn := reflect.ValueOf(executor).MethodByName("Execute") | ||
result := fn.Call([]reflect.Value{ | ||
reflect.ValueOf(ctx), | ||
reflect.ValueOf(n.value), | ||
reflect.ValueOf(taskInstance), | ||
}) | ||
if !result[0].IsNil() { | ||
//nolint:revive // type cast result is unchecked | ||
return result[0].Interface().(error) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an interesting question here. We are basically assuming for all the tasks processed, they will be invalidated at the end of the transaction. If the validator implementation has a bug and doesn't invalid the task and the task status of that pure task happens to be Created, then we will won't generate a new physical task and the execution will get stuck. If we blindly flip task status to be None, then we will have a infinite loop here. It seems to be the best way is to Validate again after running the task and if the task is still validate return an internal error and DLQ the task. Not a super important issue but want to bring awareness here. Please help create a task to track this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Task to follow up created, OSS-4272 |
||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's use ms as the precision for doing comparison. queues.IsTimeExpired() has some more details.
Also caller can't simply use time.Now() as input, since there might be clock skew, which may cause the timer task to be executed before the scheduled time recorded in the state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to truncate to the same precision as queues.
The other PR with the caller is using
t.Now()
from the timer queue struct's ShardContext, as do other methods, and I'll update it to make use ofIsTimeExpired
for its physical task comparison.