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

Implement Cohort cycle detection in hierarchy.Manager #3040

Merged
merged 1 commit into from
Sep 13, 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
1 change: 1 addition & 0 deletions pkg/cache/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var snapCmpOpts = []cmp.Option{
cmpopts.IgnoreUnexported(hierarchy.Cohort[*ClusterQueueSnapshot, *CohortSnapshot]{}),
cmpopts.IgnoreUnexported(hierarchy.ClusterQueue[*CohortSnapshot]{}),
cmpopts.IgnoreUnexported(hierarchy.Manager[*ClusterQueueSnapshot, *CohortSnapshot]{}),
cmpopts.IgnoreUnexported(hierarchy.CycleChecker{}),
cmpopts.IgnoreFields(metav1.Condition{}, "LastTransitionTime"),
}

Expand Down
42 changes: 42 additions & 0 deletions pkg/hierarchy/cycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package hierarchy

// cycleChecker checks for cycles in Cohorts, while memoizing the
// result.
type CycleChecker struct {
cycles map[string]bool
}

type CycleCheckable interface {
GetName() string
HasParent() bool
CCParent() CycleCheckable
}

func (c *CycleChecker) HasCycle(cohort CycleCheckable) bool {
if cycle, seen := c.cycles[cohort.GetName()]; seen {
return cycle
}
if !cohort.HasParent() {
c.cycles[cohort.GetName()] = false
return c.cycles[cohort.GetName()]
}
c.cycles[cohort.GetName()] = true
c.cycles[cohort.GetName()] = c.HasCycle(cohort.CCParent())
return c.cycles[cohort.GetName()]
}
8 changes: 8 additions & 0 deletions pkg/hierarchy/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Manager[CQ clusterQueueNode[C], C cohortNode[CQ, C]] struct {
Cohorts map[string]C
ClusterQueues map[string]CQ
cohortFactory func(string) C
CycleChecker CycleChecker
}

// NewManager creates a new Manager. A newCohort function must
Expand All @@ -32,6 +33,7 @@ func NewManager[CQ clusterQueueNode[C], C cohortNode[CQ, C]](newCohort func(stri
make(map[string]C),
make(map[string]CQ),
newCohort,
CycleChecker{make(map[string]bool)},
}
}

Expand Down Expand Up @@ -68,6 +70,7 @@ func (m *Manager[CQ, C]) AddCohort(cohortName string) {
}

func (m *Manager[CQ, C]) UpdateCohortEdge(name, parentName string) {
m.resetCycleChecker()
cohort := m.Cohorts[name]
m.detachCohortFromParent(cohort)
if parentName != "" {
Expand All @@ -78,6 +81,7 @@ func (m *Manager[CQ, C]) UpdateCohortEdge(name, parentName string) {
}

func (m *Manager[CQ, C]) DeleteCohort(name string) {
m.resetCycleChecker()
cohort, ok := m.Cohorts[name]
if !ok {
return
Expand Down Expand Up @@ -138,6 +142,10 @@ func (m *Manager[CQ, C]) cleanupCohort(cohort C) {
}
}

func (m *Manager[CQ, C]) resetCycleChecker() {
m.CycleChecker = CycleChecker{make(map[string]bool, len(m.Cohorts))}
}

type nodeBase interface {
GetName() string
comparable
Expand Down
123 changes: 123 additions & 0 deletions pkg/hierarchy/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,125 @@ func TestManager(t *testing.T) {
}
}

func TestCycles(t *testing.T) {
type M = Manager[*testClusterQueue, *testCohort]
cases := map[string]struct {
operations func(M)
wantCycles map[string]bool
}{
"no cycles": {
Copy link
Contributor

@mimowo mimowo Sep 13, 2024

Choose a reason for hiding this comment

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

Add a test case for a diamond structure.

    root
  /        \
left     right
  \          /
 shared child

should be no cycles, but I would like to confirm there is no bug here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can't represent this configuration, as a child can only have one parent

Copy link
Contributor

Choose a reason for hiding this comment

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

sure :)

operations: func(m M) {
m.AddCohort("root")
m.AddCohort("left")
m.AddCohort("right")
m.UpdateCohortEdge("left", "root")
m.UpdateCohortEdge("right", "root")
},
wantCycles: map[string]bool{
"root": false,
"left": false,
"right": false,
},
},
"self-cycle": {
operations: func(m M) {
m.AddCohort("root")
m.UpdateCohortEdge("root", "root")
},
wantCycles: map[string]bool{
"root": true,
},
},
"remove self-cycle": {
operations: func(m M) {
m.AddCohort("root")
m.UpdateCohortEdge("root", "root")
// we call HasCycle to test invalidation
m.CycleChecker.HasCycle(m.Cohorts["root"])
m.UpdateCohortEdge("root", "")
},
wantCycles: map[string]bool{
"root": false,
},
},
"cycle": {
operations: func(m M) {
m.AddCohort("cohort-a")
m.AddCohort("cohort-b")
m.UpdateCohortEdge("cohort-a", "cohort-b")
m.UpdateCohortEdge("cohort-b", "cohort-a")
},
wantCycles: map[string]bool{
"cohort-a": true,
"cohort-b": true,
},
},
"remove cycle via edge update": {
operations: func(m M) {
m.AddCohort("cohort-a")
m.AddCohort("cohort-b")
m.UpdateCohortEdge("cohort-a", "cohort-b")
m.UpdateCohortEdge("cohort-b", "cohort-a")

// we call HasCycle to test invalidation
m.CycleChecker.HasCycle(m.Cohorts["cohort-a"])

m.UpdateCohortEdge("cohort-a", "cohort-c")
},
wantCycles: map[string]bool{
"cohort-a": false,
"cohort-b": false,
"cohort-c": false,
},
},
"remove cycle via edge deletion": {
operations: func(m M) {
m.AddCohort("cohort-a")
m.AddCohort("cohort-b")
m.UpdateCohortEdge("cohort-a", "cohort-b")
m.UpdateCohortEdge("cohort-b", "cohort-a")
m.CycleChecker.HasCycle(m.Cohorts["cohort-a"])

m.UpdateCohortEdge("cohort-a", "")
},
wantCycles: map[string]bool{
"cohort-a": false,
"cohort-b": false,
},
},
"remove cycle via node deletion": {
operations: func(m M) {
m.AddCohort("cohort-a")
m.AddCohort("cohort-b")
m.UpdateCohortEdge("cohort-a", "cohort-b")
m.UpdateCohortEdge("cohort-b", "cohort-a")
m.CycleChecker.HasCycle(m.Cohorts["cohort-a"])
m.DeleteCohort("cohort-b")
},
wantCycles: map[string]bool{
"cohort-a": false,
"cohort-b": false,
},
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
mgr := NewManager(newCohort)
tc.operations(mgr)
for _, cohort := range mgr.Cohorts {
got := mgr.CycleChecker.HasCycle(cohort)
if got != tc.wantCycles[cohort.GetName()] {
t.Errorf("-want +got: %v %v", tc.wantCycles[cohort.GetName()], got)
}
}
if diff := cmp.Diff(mgr.CycleChecker.cycles, tc.wantCycles); diff != "" {
t.Errorf("-want +got: %v", diff)
}
})
}
}

type testCohort struct {
name string
Cohort[*testClusterQueue, *testCohort]
Expand All @@ -390,6 +509,10 @@ func (t *testCohort) GetName() string {
return t.name
}

func (t *testCohort) CCParent() CycleCheckable {
return t.Parent()
}

type testClusterQueue struct {
name string
ClusterQueue[*testCohort]
Expand Down
1 change: 1 addition & 0 deletions pkg/scheduler/preemption/preemption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var snapCmpOpts = []cmp.Option{
cmpopts.IgnoreUnexported(hierarchy.Cohort[*cache.ClusterQueueSnapshot, *cache.CohortSnapshot]{}),
cmpopts.IgnoreUnexported(hierarchy.ClusterQueue[*cache.CohortSnapshot]{}),
cmpopts.IgnoreUnexported(hierarchy.Manager[*cache.ClusterQueueSnapshot, *cache.CohortSnapshot]{}),
cmpopts.IgnoreUnexported(hierarchy.CycleChecker{}),
cmpopts.IgnoreFields(cache.ClusterQueueSnapshot{}, "AllocatableResourceGeneration"),
cmp.Transformer("Cohort.Members", func(s sets.Set[*cache.ClusterQueueSnapshot]) sets.Set[string] {
result := make(sets.Set[string], len(s))
Expand Down