-
Notifications
You must be signed in to change notification settings - Fork 778
[Refactor]: CreateRun add initial service and response structure #6853
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
Open
yuhuan130
wants to merge
9
commits into
flyteorg:v2
Choose a base branch
from
yuhuan130:feat/6824-refactor-create-run
base: v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7a4926e
added some tests
yuhuan130 cd019f7
basic transformer
yuhuan130 1fd35b1
added initial structure for CreateRun
yuhuan130 d271440
structure finished, needed double check
yuhuan130 217ee5c
double checked logic and test
yuhuan130 38c3ea1
Merge branch 'v2' into feat/6824-refactor-create-run
yuhuan130 aa3c6bd
chore: regenerate Cargo.lock with latest dependencies
yuhuan130 442bcc1
added 1 version of script test
yuhuan130 c0d7851
Merge upstream/v2 and resolve Cargo.lock conflict
yuhuan130 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| package transformers | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "google.golang.org/protobuf/encoding/protojson" | ||
| "google.golang.org/protobuf/types/known/timestamppb" | ||
| "gorm.io/datatypes" | ||
|
|
||
| "github.com/flyteorg/flyte/v2/flytestdlib/logger" | ||
| "github.com/flyteorg/flyte/v2/gen/go/flyteidl2/common" | ||
| "github.com/flyteorg/flyte/v2/gen/go/flyteidl2/core" | ||
| "github.com/flyteorg/flyte/v2/gen/go/flyteidl2/workflow" | ||
| "github.com/flyteorg/flyte/v2/runs/repository/models" | ||
| ) | ||
|
|
||
| const InitialPhase = "PHASE_QUEUED" | ||
|
|
||
| // CreateRunRequestToModel converts CreateRunRequest protobuf to Run domain model | ||
| func CreateRunRequestToModel(ctx context.Context, req *workflow.CreateRunRequest) (*models.Run, error) { | ||
| // Determine run ID | ||
| var runID *common.RunIdentifier | ||
| switch id := req.Id.(type) { | ||
| case *workflow.CreateRunRequest_RunId: | ||
| runID = id.RunId | ||
| case *workflow.CreateRunRequest_ProjectId: | ||
| // Generate a run name | ||
| runID = &common.RunIdentifier{ | ||
| Org: id.ProjectId.Organization, | ||
| Project: id.ProjectId.Name, | ||
| Domain: id.ProjectId.Domain, | ||
| Name: fmt.Sprintf("run-%d", time.Now().Unix()), | ||
| } | ||
| logger.Debugf(ctx, "Generated run name: %s", runID.Name) | ||
| default: | ||
| return nil, fmt.Errorf("invalid run ID type") | ||
| } | ||
|
|
||
| // Build ActionSpec | ||
| actionSpec := &workflow.ActionSpec{ | ||
| ActionId: &common.ActionIdentifier{ | ||
| Run: runID, | ||
| Name: runID.Name, | ||
| }, | ||
| ParentActionName: nil, | ||
| RunSpec: req.RunSpec, | ||
| InputUri: "", | ||
| RunOutputBase: "", | ||
| } | ||
|
|
||
| // Set the task spec | ||
| switch taskSpec := req.Task.(type) { | ||
| case *workflow.CreateRunRequest_TaskSpec: | ||
| actionSpec.Spec = &workflow.ActionSpec_Task{ | ||
| Task: &workflow.TaskAction{ | ||
| Spec: taskSpec.TaskSpec, | ||
| }, | ||
| } | ||
| case *workflow.CreateRunRequest_TaskId: | ||
| actionSpec.Spec = &workflow.ActionSpec_Task{ | ||
| Task: &workflow.TaskAction{ | ||
| Id: taskSpec.TaskId, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // Serialize ActionSpec | ||
| actionSpecBytes, err := protojson.Marshal(actionSpec) | ||
| if err != nil { | ||
| logger.Errorf(ctx, "Failed to marshal ActionSpec: %v", err) | ||
| return nil, fmt.Errorf("failed to marshal action spec: %w", err) | ||
| } | ||
|
|
||
| // Create Run model | ||
| run := &models.Run{ | ||
| Org: runID.Org, | ||
| Project: runID.Project, | ||
| Domain: runID.Domain, | ||
| Name: runID.Name, | ||
| ParentActionName: nil, | ||
| Phase: InitialPhase, | ||
| ActionSpec: datatypes.JSON(actionSpecBytes), | ||
| ActionDetails: datatypes.JSON([]byte("{}")), // Empty details initially | ||
| } | ||
|
|
||
| logger.Infof(ctx, "Created run model: %s/%s/%s/%s", run.Org, run.Project, run.Domain, run.Name) | ||
| return run, nil | ||
| } | ||
|
|
||
| // RunModelToCreateRunResponse converts a domain model Run to a CreateRunResponse | ||
| func RunModelToCreateRunResponse(run *models.Run, source workflow.RunSource) *workflow.CreateRunResponse { | ||
| if run == nil { | ||
| return nil | ||
| } | ||
|
|
||
| // Build the action identifier | ||
| actionID := &common.ActionIdentifier{ | ||
| Run: &common.RunIdentifier{ | ||
| Org: run.Org, | ||
| Project: run.Project, | ||
| Domain: run.Domain, | ||
| Name: run.Name, | ||
| }, | ||
| Name: run.Name, // For root actions, action name = run name | ||
| } | ||
|
|
||
| // Build action status | ||
| actionStatus := &workflow.ActionStatus{ | ||
| Phase: DBPhaseToProtobufPhase(run.Phase), | ||
| StartTime: timestamppb.New(run.CreatedAt), | ||
| Attempts: 0, | ||
| CacheStatus: core.CatalogCacheStatus_CACHE_DISABLED, | ||
| } | ||
|
|
||
| // Build action metadata | ||
| actionMetadata := &workflow.ActionMetadata{ | ||
| Source: source, // ← Use the passed-in source | ||
| Parent: "", | ||
| ActionType: workflow.ActionType_ACTION_TYPE_TASK, | ||
| } | ||
|
|
||
| // Build the complete response | ||
| return &workflow.CreateRunResponse{ | ||
| Run: &workflow.Run{ | ||
| Action: &workflow.Action{ | ||
| Id: actionID, | ||
| Status: actionStatus, | ||
| Metadata: actionMetadata, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func DBPhaseToProtobufPhase(dbPhase string) common.ActionPhase { | ||
| protoPhaseStr := "ACTION_" + dbPhase // "PHASE_QUEUED" → "ACTION_PHASE_QUEUED" | ||
|
|
||
| if val, ok := common.ActionPhase_value[protoPhaseStr]; ok { | ||
| return common.ActionPhase(val) | ||
| } | ||
|
|
||
| return common.ActionPhase_ACTION_PHASE_UNSPECIFIED | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I would prefer isolate service layer and repository layer to prevent the transformer function depends on service request. That is, let's not pass the
CreateRunRequestand transform here. Instead, do things in service layer, and just passrunIDandactionSpecinto here. Like what we did in:flyte/runs/service/task_service.go
Lines 53 to 54 in 442bcc1
In this case, we can reuse the run proto to run model transform function in other places rather than only for create run function
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.
Ahh~ I see. No problem