Skip to content
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

[KS-590] Auto-approval for workflow spec deletion #15414

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/services/feeds/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,27 @@ func (s *service) DeleteJob(ctx context.Context, args *DeleteJobArgs) (int64, er
logger.Errorw("Failed to push metrics for job proposal deletion", "err", err)
}

// auto-cancellation for Workflow specs
if !proposal.ExternalJobID.Valid {
logger.Infow("ExternalJobID is null", "id", proposal.ID, "name", proposal.Name)
return proposal.ID, nil
}
job, err := s.jobORM.FindJobByExternalJobID(ctx, proposal.ExternalJobID.UUID)
bolekk marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah just copy the blob that @giogam linked :D including the proposal.ExternalJobID.Valid check since that would return false when the job is not in approved status.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we treat the NoRows error in a special way? Why not just catch any error?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically it is the ORM's responsibility to hide the sql details by catching this error internally and returning nil instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the only issue here is that the ORM isn't returning nil here. List will return empty arrays when no record is found but get will typically return no row errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I'm confused - is my code going to work or not? If there's any error, I give up and don't delete. That should be fine if nothing is found. What else can go wrong?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see it just logs it and doesn't return, and with the .valid check before that, it looks like it should be fine now

// NOTE: at this stage, we don't know if this job is of Workflow type
// so we don't want to return an error
logger.Infow("FindJobByExternalJobID failed", "id", proposal.ID, "externalJobID", proposal.ExternalJobID.UUID, "name", proposal.Name)
return proposal.ID, nil
}
if job.WorkflowSpecID != nil { // this is a Workflow job
specID := int64(*job.WorkflowSpecID)
if err := s.CancelSpec(ctx, proposal.ID); err != nil {
logger.Errorw("Failed to auto-cancel workflow spec", "id", specID, "err", err, "name", job.Name)
return 0, fmt.Errorf("failed to auto-cancel workflow spec %d: %w", specID, err)
}
logger.Infow("Successfully auto-cancelled a workflow spec", "id", specID)
}

return proposal.ID, nil
}

Expand Down
47 changes: 46 additions & 1 deletion core/services/feeds/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1306,12 +1306,25 @@ func Test_Service_DeleteJob(t *testing.T) {
}

approved = feeds.JobProposal{
ID: 1,
ID: 321,
FeedsManagerID: 1,
RemoteUUID: remoteUUID,
ExternalJobID: uuid.NullUUID{UUID: uuid.New(), Valid: true},
Status: feeds.JobProposalStatusApproved,
}

wfSpecID = int32(4321)
workflowJob = job.Job{
ID: 1,
WorkflowSpecID: &wfSpecID,
}
spec = &feeds.JobProposalSpec{
ID: 20,
Status: feeds.SpecStatusApproved,
JobProposalID: approved.ID,
Version: 1,
}

httpTimeout = *commonconfig.MustNewDuration(1 * time.Second)
)

Expand All @@ -1328,6 +1341,7 @@ func Test_Service_DeleteJob(t *testing.T) {
svc.orm.On("GetJobProposalByRemoteUUID", mock.Anything, approved.RemoteUUID).Return(&approved, nil)
svc.orm.On("DeleteProposal", mock.Anything, approved.ID).Return(nil)
svc.orm.On("CountJobProposalsByStatus", mock.Anything).Return(&feeds.JobProposalCounts{}, nil)
svc.jobORM.On("FindJobByExternalJobID", mock.Anything, approved.ExternalJobID.UUID).Return(job.Job{}, sql.ErrNoRows)
},
args: args,
wantID: approved.ID,
Expand Down Expand Up @@ -1371,6 +1385,37 @@ func Test_Service_DeleteJob(t *testing.T) {
args: args,
wantErr: "DeleteProposal failed",
},
{
name: "Delete workflow-spec with auto-cancellation",
before: func(svc *TestService) {
svc.orm.On("GetJobProposalByRemoteUUID", mock.Anything, approved.RemoteUUID).Return(&approved, nil)
svc.orm.On("DeleteProposal", mock.Anything, approved.ID).Return(nil)
svc.orm.On("CountJobProposalsByStatus", mock.Anything).Return(&feeds.JobProposalCounts{}, nil)
svc.jobORM.On("FindJobByExternalJobID", mock.Anything, approved.ExternalJobID.UUID).Return(workflowJob, nil)

// mocks for CancelSpec()
svc.orm.On("GetSpec", mock.Anything, approved.ID).Return(spec, nil)
svc.orm.On("GetJobProposal", mock.Anything, approved.ID).Return(&approved, nil)
svc.connMgr.On("GetClient", mock.Anything).Return(svc.fmsClient, nil)

svc.orm.On("CancelSpec", mock.Anything, approved.ID).Return(nil)
svc.jobORM.On("FindJobByExternalJobID", mock.Anything, approved.ExternalJobID.UUID).Return(workflowJob, nil)
svc.spawner.On("DeleteJob", mock.Anything, mock.Anything, workflowJob.ID).Return(nil)

svc.fmsClient.On("CancelledJob",
mock.MatchedBy(func(ctx context.Context) bool { return true }),
&proto.CancelledJobRequest{
Uuid: approved.RemoteUUID.String(),
Version: int64(spec.Version),
},
).Return(&proto.CancelledJobResponse{}, nil)
svc.orm.On("CountJobProposalsByStatus", mock.Anything).Return(&feeds.JobProposalCounts{}, nil)
svc.orm.On("WithDataSource", mock.Anything).Return(feeds.ORM(svc.orm))
svc.jobORM.On("WithDataSource", mock.Anything).Return(job.ORM(svc.jobORM))
},
args: args,
wantID: approved.ID,
},
}

for _, tc := range testCases {
Expand Down
Loading