Skip to content

Commit

Permalink
[YUNIKORN-2770] Simplify Application.GetTask() (#882)
Browse files Browse the repository at this point in the history
Closes: #882

Signed-off-by: Peter Bacsko <[email protected]>
  • Loading branch information
pbacsko committed Jul 26, 2024
1 parent 498b8ea commit 97b29d6
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 21 deletions.
8 changes: 2 additions & 6 deletions pkg/cache/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,10 @@ func (app *Application) canHandle(ev events.ApplicationEvent) bool {
return app.sm.Can(ev.GetEvent())
}

func (app *Application) GetTask(taskID string) (*Task, error) {
func (app *Application) GetTask(taskID string) *Task {
app.lock.RLock()
defer app.lock.RUnlock()
if task, ok := app.taskMap[taskID]; ok {
return task, nil
}
return nil, fmt.Errorf("task %s doesn't exist in application %s",
taskID, app.applicationID)
return app.taskMap[taskID]
}

func (app *Application) GetApplicationID() string {
Expand Down
4 changes: 1 addition & 3 deletions pkg/cache/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,9 +1184,7 @@ func TestPlaceholderTimeoutEvents(t *testing.T) {
})
assert.Assert(t, task1 != nil)
assert.Equal(t, task1.GetTaskID(), "task02")

_, taskErr := app.GetTask("task02")
assert.NilError(t, taskErr, "Task should exist")
assert.Assert(t, app.GetTask("task02") != nil, "Task should exist")

task1.allocationKey = allocationKey

Expand Down
10 changes: 5 additions & 5 deletions pkg/cache/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (ctx *Context) ensureAppAndTaskCreated(pod *v1.Pod) {
}

// add task if it doesn't already exist
if _, taskErr := app.GetTask(string(pod.UID)); taskErr != nil {
if task := app.GetTask(string(pod.UID)); task == nil {
ctx.addTask(&AddTaskRequest{
Metadata: taskMeta,
})
Expand Down Expand Up @@ -1097,8 +1097,8 @@ func (ctx *Context) addTask(request *AddTaskRequest) *Task {
zap.String("appID", request.Metadata.ApplicationID),
zap.String("taskID", request.Metadata.TaskID))
if app := ctx.getApplication(request.Metadata.ApplicationID); app != nil {
existingTask, err := app.GetTask(request.Metadata.TaskID)
if err != nil {
existingTask := app.GetTask(request.Metadata.TaskID)
if existingTask == nil {
var originator bool

// Is this task the originator of the application?
Expand Down Expand Up @@ -1156,8 +1156,8 @@ func (ctx *Context) getTask(appID string, taskID string) *Task {
zap.String("appID", appID))
return nil
}
task, err := app.GetTask(taskID)
if err != nil {
task := app.GetTask(taskID)
if task == nil {
log.Log(log.ShimContext).Debug("task is not found in applications",
zap.String("taskID", taskID),
zap.String("appID", appID))
Expand Down
6 changes: 2 additions & 4 deletions pkg/cache/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,7 @@ func TestRecoverTask(t *testing.T) {
for _, tt := range taskInfoVerifiers {
t.Run(tt.taskID, func(t *testing.T) {
// verify the info for the recovered task
rt, err := app.GetTask(tt.taskID)
assert.NilError(t, err)
rt := app.GetTask(tt.taskID)
assert.Equal(t, rt.GetTaskState(), tt.expectedState)
assert.Equal(t, rt.allocationKey, tt.expectedAllocationKey)
assert.Equal(t, rt.pod.Name, tt.expectedPodName)
Expand Down Expand Up @@ -2142,9 +2141,8 @@ func TestTaskRemoveOnCompletion(t *testing.T) {

// check removal
app.Schedule()
appTask, err := app.GetTask(taskUID1)
appTask := app.GetTask(taskUID1)
assert.Assert(t, appTask == nil)
assert.Error(t, err, "task task00001 doesn't exist in application app01")
}

func TestAssumePod(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/scheduler_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func NewSchedulerPlugin(_ context.Context, _ runtime.Object, handle framework.Ha

func (sp *YuniKornSchedulerPlugin) getTask(appID, taskID string) (app *cache.Application, task *cache.Task, ok bool) {
if app := sp.context.GetApplication(appID); app != nil {
if task, err := app.GetTask(taskID); err == nil {
if task := app.GetTask(taskID); task != nil {
return app, task, true
}
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/shim/scheduler_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ func (fc *MockScheduler) waitAndAssertTaskState(t *testing.T, appID, taskID, exp
assert.Equal(t, app != nil, true)
assert.Equal(t, app.GetApplicationID(), appID)

task, err := app.GetTask(taskID)
assert.NilError(t, err, "Task retrieval failed")
task := app.GetTask(taskID)
deadline := time.Now().Add(10 * time.Second)
for {
if task.GetTaskState() == expectedState {
Expand Down

0 comments on commit 97b29d6

Please sign in to comment.