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

[YUNIKORN-2933] Don't add duplicated taskGroup to app #928

Closed
wants to merge 1 commit into from
Closed
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
41 changes: 35 additions & 6 deletions pkg/cache/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Application struct {
groups []string
taskMap map[string]*Task
tags map[string]string
taskGroups []TaskGroup
taskGroups map[string]TaskGroup
taskGroupsDefinition string
schedulingParamsDefinition string
placeholderOwnerReferences []metav1.OwnerReference
Expand Down Expand Up @@ -80,7 +80,7 @@ func NewApplication(appID, queueName, user string, groups []string, tags map[str
taskMap: taskMap,
tags: tags,
sm: newAppState(),
taskGroups: make([]TaskGroup, 0),
taskGroups: make(map[string]TaskGroup),
lock: &locking.RWMutex{},
schedulerAPI: scheduler,
placeholderTimeoutInSec: 0,
Expand Down Expand Up @@ -163,12 +163,33 @@ func (app *Application) GetSchedulingParamsDefinition() string {
return app.schedulingParamsDefinition
}

// check if the task-groups is correct
func (app *Application) checkTaskGroups(taskGroups []TaskGroup, pod *v1.Pod) {
tgs := make(map[string]TaskGroup)

for _, taskGroup := range taskGroups {
if _, exists := tgs[taskGroup.Name]; exists {
// for duplicated task-group, users will receive the event
log.Log(log.ShimCacheApplication).Warn("duplicate task-group within the task-groups",
zap.String("appID", app.applicationID),
zap.String("groupName", taskGroup.Name))
Comment on lines +173 to +175
Copy link
Contributor

Choose a reason for hiding this comment

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

Make sure you have an unit test which covers this branch.

Copy link
Author

Choose a reason for hiding this comment

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

Make sure you have an unit test which covers this branch.

Well yeah, that 's a good addition. I will provide the unit test which covers this branch.

events.GetRecorder().Eventf(pod.DeepCopy(), nil, v1.EventTypeWarning, "GangScheduling",
"TaskGroupDuplicated", "Application %s has duplicated task-group %s", app.applicationID, taskGroup.Name)
} else {
tgs[taskGroup.Name] = taskGroup
}
}
}

func (app *Application) setTaskGroups(taskGroups []TaskGroup) {
app.lock.Lock()
defer app.lock.Unlock()
app.taskGroups = taskGroups
for _, taskGroup := range app.taskGroups {
app.placeholderAsk = common.Add(app.placeholderAsk, common.GetTGResource(taskGroup.MinResource, int64(taskGroup.MinMember)))
for _, taskGroup := range taskGroups {
// for duplicated task-group, will no longer be added to the app
if _, exists := app.taskGroups[taskGroup.Name]; !exists {
app.taskGroups[taskGroup.Name] = taskGroup
app.placeholderAsk = common.Add(app.placeholderAsk, common.GetTGResource(taskGroup.MinResource, int64(taskGroup.MinMember)))
}
}
}

Expand All @@ -181,7 +202,15 @@ func (app *Application) getPlaceholderAsk() *si.Resource {
func (app *Application) getTaskGroups() []TaskGroup {
app.lock.RLock()
defer app.lock.RUnlock()
return app.taskGroups

if len(app.taskGroups) > 0 {
taskGroups := make([]TaskGroup, 0, len(app.taskGroups))
for _, taskGroup := range app.taskGroups {
taskGroups = append(taskGroups, taskGroup)
}
return taskGroups
}
return nil
}

func (app *Application) setPlaceholderOwnerReferences(ref []metav1.OwnerReference) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/cache/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (ctx *Context) ensureAppAndTaskCreated(pod *v1.Pod, app *Application) {
}
app = ctx.addApplication(&AddApplicationRequest{
Metadata: appMeta,
})
}, pod)
}

// get task metadata
Expand Down Expand Up @@ -957,10 +957,10 @@ func (ctx *Context) AddApplication(request *AddApplicationRequest) *Application
ctx.lock.Lock()
defer ctx.lock.Unlock()

return ctx.addApplication(request)
return ctx.addApplication(request, nil)
}

func (ctx *Context) addApplication(request *AddApplicationRequest) *Application {
func (ctx *Context) addApplication(request *AddApplicationRequest, pod *v1.Pod) *Application {
log.Log(log.ShimContext).Debug("AddApplication", zap.Any("Request", request))
if app := ctx.getApplication(request.Metadata.ApplicationID); app != nil {
return app
Expand All @@ -980,6 +980,7 @@ func (ctx *Context) addApplication(request *AddApplicationRequest) *Application
request.Metadata.Groups,
request.Metadata.Tags,
ctx.apiProvider.GetAPIs().SchedulerAPI)
app.checkTaskGroups(request.Metadata.TaskGroups, pod)
app.setTaskGroups(request.Metadata.TaskGroups)
app.setTaskGroupsDefinition(request.Metadata.Tags[constants.AnnotationTaskGroups])
app.setSchedulingParamsDefinition(request.Metadata.Tags[constants.AnnotationSchedulingPolicyParam])
Expand Down
59 changes: 48 additions & 11 deletions pkg/cache/placeholder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ func TestNewPlaceholder(t *testing.T) {
assert.Equal(t, app.placeholderAsk.Resources[siCommon.Memory].Value, int64(10*1024*1000*1000))
assert.Equal(t, app.placeholderAsk.Resources["pods"].Value, int64(10))

holder := newPlaceholder("ph-name", app, app.taskGroups[0])
tgs := app.getTaskGroups()
holder := newPlaceholder("ph-name", app, tgs[0])
assert.Equal(t, holder.appID, appID)
assert.Equal(t, holder.taskGroupName, app.taskGroups[0].Name)
assert.Equal(t, holder.taskGroupName, tgs[0].Name)
assert.Equal(t, holder.pod.Spec.SchedulerName, constants.SchedulerName)
assert.Equal(t, holder.pod.Name, "ph-name")
assert.Equal(t, holder.pod.Namespace, namespace)
Expand All @@ -132,7 +133,7 @@ func TestNewPlaceholder(t *testing.T) {
"labelKey1": "labelKeyValue1",
})
assert.Equal(t, len(holder.pod.Annotations), 7, "unexpected number of annotations")
assert.Equal(t, holder.pod.Annotations[constants.AnnotationTaskGroupName], app.taskGroups[0].Name)
assert.Equal(t, holder.pod.Annotations[constants.AnnotationTaskGroupName], tgs[0].Name)
assert.Equal(t, holder.pod.Annotations[constants.AnnotationPlaceholderFlag], constants.True)
assert.Equal(t, holder.pod.Annotations["annotationKey0"], "annotationValue0")
assert.Equal(t, holder.pod.Annotations["annotationKey1"], "annotationValue1")
Expand Down Expand Up @@ -163,7 +164,8 @@ func TestNewPlaceholderWithNodeSelectors(t *testing.T) {
"bob", testGroups, map[string]string{constants.AppTagNamespace: namespace}, mockedSchedulerAPI)
app.setTaskGroups(taskGroups)

holder := newPlaceholder("ph-name", app, app.taskGroups[0])
tgs := app.getTaskGroups()
holder := newPlaceholder("ph-name", app, tgs[0])
assert.Equal(t, len(holder.pod.Spec.NodeSelector), 2)
assert.Equal(t, holder.pod.Spec.NodeSelector["nodeType"], "test")
assert.Equal(t, holder.pod.Spec.NodeSelector["nodeState"], "healthy")
Expand All @@ -178,7 +180,8 @@ func TestNewPlaceholderWithTolerations(t *testing.T) {
"bob", testGroups, map[string]string{constants.AppTagNamespace: namespace}, mockedSchedulerAPI)
app.setTaskGroups(taskGroups)

holder := newPlaceholder("ph-name", app, app.taskGroups[0])
tgs := app.getTaskGroups()
holder := newPlaceholder("ph-name", app, tgs[0])
assert.Equal(t, len(holder.pod.Spec.Tolerations), 1)
tlr := holder.pod.Spec.Tolerations[0]
assert.Equal(t, tlr.Key, "key1")
Expand All @@ -196,7 +199,8 @@ func TestNewPlaceholderWithAffinity(t *testing.T) {
"bob", testGroups, map[string]string{constants.AppTagNamespace: namespace}, mockedSchedulerAPI)
app.setTaskGroups(taskGroups)

holder := newPlaceholder("ph-name", app, app.taskGroups[0])
tgs := app.getTaskGroups()
holder := newPlaceholder("ph-name", app, tgs[0])
assert.Equal(t, len(holder.pod.Spec.Affinity.PodAffinity.RequiredDuringSchedulingIgnoredDuringExecution), 1)
term := holder.pod.Spec.Affinity.PodAffinity.RequiredDuringSchedulingIgnoredDuringExecution
assert.Equal(t, term[0].TopologyKey, "topologyKey")
Expand All @@ -215,14 +219,16 @@ func TestNewPlaceholderTaskGroupsDefinition(t *testing.T) {
app := NewApplication(appID, queue,
"bob", testGroups, map[string]string{constants.AppTagNamespace: namespace}, mockedSchedulerAPI)
app.setTaskGroups(taskGroups)
holder := newPlaceholder("ph-name", app, app.taskGroups[0])
tgs := app.getTaskGroups()
holder := newPlaceholder("ph-name", app, tgs[0])
assert.Equal(t, "", holder.pod.Annotations[constants.AnnotationTaskGroups])

app = NewApplication(appID, queue,
"bob", testGroups, map[string]string{constants.AppTagNamespace: namespace}, mockedSchedulerAPI)
app.setTaskGroups(taskGroups)
app.setTaskGroupsDefinition("taskGroupsDef")
holder = newPlaceholder("ph-name", app, app.taskGroups[0])
tgs = app.getTaskGroups()
holder = newPlaceholder("ph-name", app, tgs[0])
assert.Equal(t, "taskGroupsDef", holder.pod.Annotations[constants.AnnotationTaskGroups])
var priority *int32
assert.Equal(t, priority, holder.pod.Spec.Priority)
Expand All @@ -234,7 +240,9 @@ func TestNewPlaceholderExtendedResources(t *testing.T) {
app := NewApplication(appID, queue,
"bob", testGroups, map[string]string{constants.AppTagNamespace: namespace}, mockedSchedulerAPI)
app.setTaskGroups(taskGroups)
holder := newPlaceholder("ph-name", app, app.taskGroups[0])

tgs := app.getTaskGroups()
holder := newPlaceholder("ph-name", app, tgs[0])
assert.Equal(t, len(holder.pod.Spec.Containers[0].Resources.Requests), 5, "expected requests not found")
assert.Equal(t, len(holder.pod.Spec.Containers[0].Resources.Limits), 5, "expected limits not found")
assert.Equal(t, holder.pod.Spec.Containers[0].Resources.Limits[gpu], holder.pod.Spec.Containers[0].Resources.Requests[gpu], "gpu: expected same value for request and limit")
Expand Down Expand Up @@ -271,7 +279,8 @@ func TestNewPlaceholderWithPriorityClassName(t *testing.T) {
app.taskMap[taskID1] = task1
app.setOriginatingTask(task1)

holder := newPlaceholder("ph-name", app, app.taskGroups[0])
tgs := app.getTaskGroups()
holder := newPlaceholder("ph-name", app, tgs[0])
assert.Equal(t, len(holder.pod.Spec.Containers[0].Resources.Requests), 5, "expected requests not found")
assert.Equal(t, len(holder.pod.Spec.Containers[0].Resources.Limits), 5, "expected limits not found")
assert.Equal(t, holder.pod.Spec.Containers[0].Resources.Limits[gpu], holder.pod.Spec.Containers[0].Resources.Requests[gpu], "gpu: expected same value for request and limit")
Expand All @@ -287,7 +296,8 @@ func TestNewPlaceholderWithTopologySpreadConstraints(t *testing.T) {
"bob", testGroups, map[string]string{constants.AppTagNamespace: namespace}, mockedSchedulerAPI)
app.setTaskGroups(taskGroups)

holder := newPlaceholder("ph-name", app, app.taskGroups[0])
tgs := app.getTaskGroups()
holder := newPlaceholder("ph-name", app, tgs[0])
assert.Equal(t, len(holder.pod.Spec.TopologySpreadConstraints), 1)
assert.Equal(t, holder.pod.Spec.TopologySpreadConstraints[0].MaxSkew, int32(1))
assert.Equal(t, holder.pod.Spec.TopologySpreadConstraints[0].TopologyKey, v1.LabelTopologyZone)
Expand All @@ -297,3 +307,30 @@ func TestNewPlaceholderWithTopologySpreadConstraints(t *testing.T) {
"labelKey1": "labelKeyValue1",
})
}

func TestNewPlaceholderWithDuplicatedTaskGroup(t *testing.T) {
mockedSchedulerAPI := newMockSchedulerAPI()

// in this case, suppose pod1 triggers the creation of app.
pod1 := &v1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "gang-scheduling-job-app01-0",
UID: "UID-01",
},
}
app := NewApplication(appID, queue,
"bob", testGroups, map[string]string{constants.AppTagNamespace: namespace}, mockedSchedulerAPI)

var duplicatedTaskGroups = make([]TaskGroup, 0, 2)
duplicatedTaskGroups = append(duplicatedTaskGroups, taskGroups[0])
duplicatedTaskGroups = append(duplicatedTaskGroups, taskGroups[0])

app.checkTaskGroups(duplicatedTaskGroups, pod1)
app.setTaskGroups(duplicatedTaskGroups)
tgs := app.getTaskGroups()
assert.Equal(t, len(tgs), 1)
}
Loading