Skip to content

Commit 0ce5cee

Browse files
atantawiopenshift-merge-robot
authored andcommitted
add and fix try and undo allocate testing
1 parent 19d60d2 commit 0ce5cee

File tree

8 files changed

+784
-41
lines changed

8 files changed

+784
-41
lines changed

pkg/quotaplugins/quota-forest/quota-manager/quota/core/consumer.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
Copyright 2022 The Multi-Cluster App Dispatcher Authors.
2+
Copyright 2022, 2023 The Multi-Cluster App Dispatcher Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -119,6 +119,13 @@ func (c *Consumer) IsAllocated() bool {
119119
return c.aNode != nil
120120
}
121121

122+
// ByID implements sort.Interface based on the ID field.
123+
type ByID []*Consumer
124+
125+
func (c ByID) Len() int { return len(c) }
126+
func (c ByID) Less(i, j int) bool { return c[i].id < c[j].id }
127+
func (c ByID) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
128+
122129
// String : a print out of the consumer
123130
func (c *Consumer) String() string {
124131
var b bytes.Buffer
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Copyright 2023 The Multi-Cluster App Dispatcher Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package core
18+
19+
import (
20+
"reflect"
21+
"sort"
22+
)
23+
24+
// EqualStateQuotaNodes : check if two quota nodes have similar allocation data
25+
func EqualStateQuotaNodes(qn1 *QuotaNode, qn2 *QuotaNode) bool {
26+
consumers1 := make([]*Consumer, len(qn1.GetConsumers()))
27+
copy(consumers1, qn1.GetConsumers())
28+
sort.Sort(ByID(consumers1))
29+
30+
consumers2 := make([]*Consumer, len(qn2.GetConsumers()))
31+
copy(consumers2, qn2.GetConsumers())
32+
sort.Sort(ByID(consumers2))
33+
34+
return reflect.DeepEqual(qn1.GetQuota(), qn2.GetQuota()) &&
35+
reflect.DeepEqual(qn1.GetAllocated(), qn2.GetAllocated()) &&
36+
reflect.DeepEqual(consumers1, consumers2)
37+
}
38+
39+
// EqualStateQuotaTrees : check if two quota trees have similar allocation data
40+
func EqualStateQuotaTrees(qt1 *QuotaTree, qt2 *QuotaTree) bool {
41+
nodeMap1 := qt1.GetNodes()
42+
nodeMap2 := qt2.GetNodes()
43+
if len(nodeMap1) != len(nodeMap2) {
44+
return false
45+
}
46+
for k, qn1 := range nodeMap1 {
47+
if qn2, exists := nodeMap2[k]; exists {
48+
if !EqualStateQuotaNodes(qn1, qn2) {
49+
return false
50+
}
51+
} else {
52+
return false
53+
}
54+
}
55+
return true
56+
}
57+
58+
// EqualStateControllers : check if two controllers have similar allocation state
59+
func EqualStateControllers(c1 *Controller, c2 *Controller) bool {
60+
pc1 := c1.GetPreemptedConsumers()
61+
sort.Strings(pc1)
62+
pc2 := c2.GetPreemptedConsumers()
63+
sort.Strings(pc2)
64+
65+
pca1 := c1.GetPreemptedConsumersArray()
66+
sort.Sort(ByID(pca1))
67+
pca2 := c2.GetPreemptedConsumersArray()
68+
sort.Sort(ByID(pca2))
69+
70+
return EqualStateQuotaTrees(c1.GetTree(), c2.GetTree()) &&
71+
reflect.DeepEqual(c1.GetConsumers(), c1.GetConsumers()) &&
72+
reflect.DeepEqual(pc1, pc2) &&
73+
reflect.DeepEqual(pca1, pca2)
74+
}
75+
76+
// EqualStateControllers : check if two forest controllers have similar allocation state
77+
func EqualStateForestControllers(fc1 *ForestController, fc2 *ForestController) bool {
78+
c1Map := fc1.GetControllers()
79+
c2Map := fc2.GetControllers()
80+
if len(c1Map) != len(c2Map) {
81+
return false
82+
}
83+
for k, c1 := range c1Map {
84+
if c2, exists := c2Map[k]; exists {
85+
if !EqualStateControllers(c1, c2) {
86+
return false
87+
}
88+
} else {
89+
return false
90+
}
91+
}
92+
return true
93+
}

pkg/quotaplugins/quota-forest/quota-manager/quota/core/quotanode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Multi-Cluster App Dispatcher Authors.
2+
Copyright 2022, 2023 The Multi-Cluster App Dispatcher Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.

pkg/quotaplugins/quota-forest/quota-manager/quota/core/quotatree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Multi-Cluster App Dispatcher Authors.
2+
Copyright 2022, 2023 The Multi-Cluster App Dispatcher Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.

pkg/quotaplugins/quota-forest/quota-manager/quota/core/treecontroller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Multi-Cluster App Dispatcher Authors.
2+
Copyright 2022, 2023 The Multi-Cluster App Dispatcher Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.

pkg/quotaplugins/quota-forest/quota-manager/quota/core/treecontroller_test.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package core
1818

1919
import (
20-
"reflect"
2120
"strings"
2221
"testing"
2322
"unsafe"
@@ -167,37 +166,3 @@ func createTestContoller(t *testing.T, treeName string) *Controller {
167166

168167
return controller
169168
}
170-
171-
// EqualStateQuotaNodes : check if two quota nodes have similar allocation data
172-
func EqualStateQuotaNodes(qn1 *QuotaNode, qn2 *QuotaNode) bool {
173-
return reflect.DeepEqual(qn1.GetQuota(), qn2.GetQuota()) &&
174-
reflect.DeepEqual(qn1.GetAllocated(), qn2.GetAllocated()) &&
175-
reflect.DeepEqual(qn1.GetConsumers(), qn2.GetConsumers())
176-
}
177-
178-
// EqualStateQuotaTrees : check if two quota trees have similar allocation data
179-
func EqualStateQuotaTrees(qt1 *QuotaTree, qt2 *QuotaTree) bool {
180-
nodeMap1 := qt1.GetNodes()
181-
nodeMap2 := qt2.GetNodes()
182-
if len(nodeMap1) != len(nodeMap2) {
183-
return false
184-
}
185-
for k, qn1 := range nodeMap1 {
186-
if qn2, exists := nodeMap2[k]; exists {
187-
if !EqualStateQuotaNodes(qn1, qn2) {
188-
return false
189-
}
190-
} else {
191-
return false
192-
}
193-
}
194-
return true
195-
}
196-
197-
// EqualStateControllers : check if two controllers have similar allocation state
198-
func EqualStateControllers(c1 *Controller, c2 *Controller) bool {
199-
return EqualStateQuotaTrees(c1.tree, c2.tree) &&
200-
reflect.DeepEqual(c1.consumers, c1.consumers) &&
201-
reflect.DeepEqual(c1.preemptedConsumers, c2.preemptedConsumers) &&
202-
reflect.DeepEqual(c1.preemptedConsumersArray, c2.preemptedConsumersArray)
203-
}

pkg/quotaplugins/quota-forest/quota-manager/quota/quotamanager.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022, 2203 The Multi-Cluster App Dispatcher Authors.
2+
Copyright 2022, 2023 The Multi-Cluster App Dispatcher Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -614,6 +614,25 @@ func (m *Manager) UpdateAll() (treeUnallocatedConsumerIDs map[string][]string,
614614
return treeUnallocatedConsumerIDs, treeResponse, forestUnallocatedConsumerIDs, forestResponse, err
615615
}
616616

617+
// GetTreeController : get the tree controller for a given tree
618+
func (m *Manager) GetTreeController(treeName string) *core.Controller {
619+
m.mutex.RLock()
620+
defer m.mutex.RUnlock()
621+
622+
if agent, exists := m.agents[treeName]; exists {
623+
return agent.controller
624+
}
625+
return nil
626+
}
627+
628+
// GetForestController : get the forest controller for a given forest
629+
func (m *Manager) GetForestController(forestName string) *core.ForestController {
630+
m.mutex.RLock()
631+
defer m.mutex.RUnlock()
632+
633+
return m.forests[forestName]
634+
}
635+
617636
// String : printout
618637
func (m *Manager) String() string {
619638

0 commit comments

Comments
 (0)