forked from kubernetes-sigs/gateway-api-inference-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_graph.go
More file actions
202 lines (178 loc) · 6.08 KB
/
Copy pathdata_graph.go
File metadata and controls
202 lines (178 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
Copyright 2025 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 datalayer
import (
"errors"
"slices"
fwkfc "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/framework/interface/flowcontrol"
fwkrq "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/framework/interface/requestcontrol"
fwksch "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/framework/interface/scheduling"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/framework/interface/plugin"
)
// ValidateAndOrderDataDependencies validates that the data dependencies among the given plugins are acyclic
// and returns a topologically sorted order of plugin names based on their data dependencies.
// Further, it validates that the plugins are ordered in a way that respects the layer execution order.
func ValidateAndOrderDataDependencies(plugins []plugin.Plugin) ([]string, error) {
pluginMap := make(map[string]plugin.Plugin)
for _, p := range plugins {
pluginMap[p.TypedName().String()] = p
}
producers := make(map[string]plugin.ProducerPlugin)
consumers := make(map[string]plugin.ConsumerPlugin)
for name, p := range pluginMap {
if producer, ok := p.(plugin.ProducerPlugin); ok {
producers[name] = producer
}
if consumer, ok := p.(plugin.ConsumerPlugin); ok {
consumers[name] = consumer
}
}
dag, err := buildDAG(producers, consumers)
if err != nil {
return nil, err
}
// Topologically sort the DAG to determine the order of plugin execution.
pluginNames, err := topologicalSort(dag)
if err != nil {
return nil, err
}
return pluginNames, nil
}
// Define constants for layer execution order. Lower value means earlier execution.
const (
FlowControlLayer = 0
RequestControlLayer = 1
SchedulingLayer = 2
DefaultLayer = -1 // For plugins that don't fit into a known layer
)
func pluginToLayerExecutionOrder(plugin plugin.Plugin) int {
// Flow control plugins
if _, ok := plugin.(fwkfc.FairnessPolicy); ok {
return FlowControlLayer
}
if _, ok := plugin.(fwkfc.OrderingPolicy); ok {
return FlowControlLayer
}
// Request control plugins
if _, ok := plugin.(fwkrq.PrepareDataPlugin); ok {
return RequestControlLayer
}
if _, ok := plugin.(fwkrq.AdmissionPlugin); ok {
return RequestControlLayer
}
if _, ok := plugin.(fwkrq.PreRequest); ok {
return RequestControlLayer
}
if _, ok := plugin.(fwkrq.ResponseReceived); ok {
return RequestControlLayer
}
// Scheduling plugins
if _, ok := plugin.(fwksch.ProfileHandler); ok {
return SchedulingLayer
}
if _, ok := plugin.(fwksch.Filter); ok {
return SchedulingLayer
}
if _, ok := plugin.(fwksch.Scorer); ok {
return SchedulingLayer
}
if _, ok := plugin.(fwksch.Picker); ok {
return SchedulingLayer
}
// If the plugin doesn't match any known layer, return -1.
return DefaultLayer
}
// buildDAG builds a dependency graph among data preparation plugins based on their
// produced and consumed data keys.
func buildDAG(producers map[string]plugin.ProducerPlugin, consumers map[string]plugin.ConsumerPlugin) (map[string][]string, error) {
dag := make(map[string][]string)
// Create dependency graph as a DAG.
for _, producer := range producers {
dag[producer.TypedName().String()] = []string{}
}
for _, consumer := range consumers {
dag[consumer.TypedName().String()] = []string{}
}
for pName, producer := range producers {
for cName, consumer := range consumers {
if pName == cName {
continue
}
if producer.Produces() != nil && consumer.Consumes() != nil {
for producedKey, producedData := range producer.Produces() {
if consumedData, ok := consumer.Consumes()[producedKey]; ok {
// Check types are same. Reflection is avoided here for simplicity.
// TODO(#1985): Document this detail in IGW docs.
if producedData != consumedData {
return nil, errors.New("data type mismatch between produced and consumed data for key: " + producedKey)
}
if pluginToLayerExecutionOrder(producer) > pluginToLayerExecutionOrder(consumer) {
return nil, errors.New("invalid plugin layer execution order: producer " + pName + " needs to be executed before consumer " + cName)
}
// Consumer depends on producer, so add an edge from consumer to producer.
dag[cName] = append(dag[cName], pName)
break
}
}
}
}
}
return dag, nil
}
// TopologicalSort performs Kahn's Algorithm on a DAG.
// It returns the sorted order or an error if a cycle is detected.
func topologicalSort(graph map[string][]string) ([]string, error) {
// 1. Initialize in-degree map
inDegree := make(map[string]int)
// Ensure all nodes are present in the inDegree map, even those with no dependencies
for u, neighbors := range graph {
if _, ok := inDegree[u]; !ok {
inDegree[u] = 0
}
for _, v := range neighbors {
inDegree[v]++ // Increment in-degree for the destination node
}
}
// 2. Initialize the queue with nodes having 0 in-degree
var queue []string
for node, degree := range inDegree {
if degree == 0 {
queue = append(queue, node)
}
}
var result []string
// 3. Process the queue
for len(queue) > 0 {
// Dequeue
u := queue[0]
queue = queue[1:]
result = append(result, u)
// Decrease in-degree of neighbors
if neighbors, ok := graph[u]; ok {
for _, v := range neighbors {
inDegree[v]--
if inDegree[v] == 0 {
queue = append(queue, v)
}
}
}
}
// 4. Check for cycles
// If the result size != total nodes, there is a cycle
if len(result) != len(inDegree) {
return nil, errors.New("cycle detected: graph is not a DAG")
}
// Reverse to get the correct order since edges point from consumer to producer
slices.Reverse(result)
return result, nil
}