generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 195
Execute prepare data plugins in topological order of data dependencies #1878
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
Open
rahulgurnani
wants to merge
5
commits into
kubernetes-sigs:main
Choose a base branch
from
rahulgurnani:parallel-plugins2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+453
−14
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
66bf69f
Parallelize execution of prepare data plugins as a DAG. Also detect d…
rahulgurnani 02dfa48
Use buffered channel based approach to synchronize go routines. Also …
rahulgurnani d89c4cd
Fix runner after rebase
rahulgurnani d225c3c
Cache DAG to avoid recomputation
rahulgurnani db79ebf
Make plugin execution sequential in topologically sorted order
rahulgurnani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| 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 requestcontrol | ||
|
|
||
| import "errors" | ||
|
|
||
| // buildDAG builds a dependency graph among data preparation plugins based on their | ||
| // produced and consumed data keys. | ||
| func buildDAG(plugins []PrepareDataPlugin) map[string][]string { | ||
| dag := make(map[string][]string) | ||
| for _, plugin := range plugins { | ||
| dag[plugin.TypedName().String()] = []string{} | ||
| } | ||
| // Create dependency graph as a DAG. | ||
| for i := range plugins { | ||
| for j := range plugins { | ||
| if i == j { | ||
| continue | ||
| } | ||
| // Check whether plugin[i] produces something consumed by plugin[j]. In that case, j depends on i. | ||
| if plugins[i].Produces() != nil && plugins[j].Consumes() != nil { | ||
| // For all the keys produced by plugin i, check if plugin j consumes any of them. | ||
| // If yes, then j depends on i. | ||
| for producedKey := range plugins[i].Produces() { | ||
| // If plugin j consumes the produced key, then j depends on i. We can break after the first match. | ||
| if _, ok := plugins[j].Consumes()[producedKey]; ok { | ||
| iPluginName := plugins[i].TypedName().String() | ||
| jPluginName := plugins[j].TypedName().String() | ||
| dag[jPluginName] = append(dag[jPluginName], iPluginName) | ||
| break | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return dag | ||
| } | ||
|
|
||
| // prepareDataGraph builds a DAG of data preparation plugins and checks for cycles. | ||
| // If there is a cycle, it returns an error. | ||
| func prepareDataGraph(plugins []PrepareDataPlugin) (map[string][]string, error) { | ||
| dag := buildDAG(plugins) | ||
|
|
||
| // Check for cycles in the DAG. | ||
| if cycleExistsInDAG(dag) { | ||
| return nil, errors.New("cycle detected in data preparation plugin dependencies") | ||
| } | ||
|
|
||
| return dag, nil | ||
| } | ||
|
|
||
| // cycleExistsInDAG checks if there are cycles in the given directed graph represented as an adjacency list. | ||
| func cycleExistsInDAG(dag map[string][]string) bool { | ||
| visited := make(map[string]bool) | ||
| recStack := make(map[string]bool) | ||
|
|
||
| var dfs func(string) bool | ||
| dfs = func(node string) bool { | ||
| if recStack[node] { | ||
| return true // Cycle detected | ||
| } | ||
| if visited[node] { | ||
| return false | ||
| } | ||
| visited[node] = true | ||
| recStack[node] = true | ||
|
|
||
| for _, neighbor := range dag[node] { | ||
| if dfs(neighbor) { | ||
| return true | ||
| } | ||
| } | ||
| recStack[node] = false | ||
| return false | ||
| } | ||
|
|
||
| for pluginName := range dag { | ||
| if !visited[pluginName] { | ||
| if dfs(pluginName) { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
| return false | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /* | ||
| 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 requestcontrol | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/stretchr/testify/assert" | ||
| "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins" | ||
| "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" | ||
| ) | ||
|
|
||
| type mockPrepareRequestDataP struct { | ||
| name string | ||
| produces map[string]any | ||
| consumes map[string]any | ||
| } | ||
|
|
||
| func (m *mockPrepareRequestDataP) TypedName() plugins.TypedName { | ||
| return plugins.TypedName{Name: m.name, Type: "mock"} | ||
| } | ||
|
|
||
| func (m *mockPrepareRequestDataP) Produces() map[string]any { | ||
| return m.produces | ||
| } | ||
|
|
||
| func (m *mockPrepareRequestDataP) Consumes() map[string]any { | ||
| return m.consumes | ||
| } | ||
|
|
||
| func (m *mockPrepareRequestDataP) PrepareRequestData(ctx context.Context, request *types.LLMRequest, pods []types.Pod) error { | ||
| pods[0].Put(mockProducedDataKey, mockProducedDataType{value: 42}) | ||
| return nil | ||
| } | ||
|
|
||
| func TestPrepareDataGraph(t *testing.T) { | ||
| pluginA := &mockPrepareRequestDataP{name: "A", produces: map[string]any{"keyA": nil}} | ||
| pluginB := &mockPrepareRequestDataP{name: "B", consumes: map[string]any{"keyA": nil}, produces: map[string]any{"keyB": nil}} | ||
| pluginC := &mockPrepareRequestDataP{name: "C", consumes: map[string]any{"keyB": nil}} | ||
| pluginD := &mockPrepareRequestDataP{name: "D", consumes: map[string]any{"keyA": nil}} | ||
| pluginE := &mockPrepareRequestDataP{name: "E"} // No dependencies | ||
|
|
||
| // Cycle plugins | ||
| pluginX := &mockPrepareRequestDataP{name: "X", produces: map[string]any{"keyX": nil}, consumes: map[string]any{"keyY": nil}} | ||
| pluginY := &mockPrepareRequestDataP{name: "Y", produces: map[string]any{"keyY": nil}, consumes: map[string]any{"keyX": nil}} | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| plugins []PrepareDataPlugin | ||
| expectedDAG map[string][]string | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "No plugins", | ||
| plugins: []PrepareDataPlugin{}, | ||
| expectedDAG: map[string][]string{}, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "Plugins with no dependencies", | ||
| plugins: []PrepareDataPlugin{pluginA, pluginE}, | ||
| expectedDAG: map[string][]string{ | ||
| "A/mock": {}, | ||
| "E/mock": {}, | ||
| }, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "Simple linear dependency (A -> B -> C)", | ||
| plugins: []PrepareDataPlugin{pluginA, pluginB, pluginC}, | ||
| expectedDAG: map[string][]string{ | ||
| "A/mock": {}, | ||
| "B/mock": {"A/mock"}, | ||
| "C/mock": {"B/mock"}, | ||
| }, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "DAG with multiple dependencies (A -> B, A -> D)", | ||
| plugins: []PrepareDataPlugin{pluginA, pluginB, pluginD, pluginE}, | ||
| expectedDAG: map[string][]string{ | ||
| "A/mock": {}, | ||
| "B/mock": {"A/mock"}, | ||
| "D/mock": {"A/mock"}, | ||
| "E/mock": {}, | ||
| }, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "Graph with a cycle (X -> Y, Y -> X)", | ||
| plugins: []PrepareDataPlugin{pluginX, pluginY}, | ||
| expectedDAG: nil, | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "Complex graph with a cycle", | ||
| plugins: []PrepareDataPlugin{pluginA, pluginB, pluginX, pluginY}, | ||
| expectedDAG: nil, | ||
| expectError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| dag, err := prepareDataGraph(tc.plugins) | ||
|
|
||
| if tc.expectError { | ||
| assert.Error(t, err) | ||
| assert.Nil(t, dag) | ||
| assert.Contains(t, err.Error(), "cycle detected") | ||
| } else { | ||
| assert.NoError(t, err) | ||
|
|
||
| // Normalize the slices in the maps for consistent comparison | ||
| normalizedDAG := make(map[string][]string) | ||
| for k, v := range dag { | ||
| normalizedDAG[k] = v | ||
| } | ||
| normalizedExpectedDAG := make(map[string][]string) | ||
| for k, v := range tc.expectedDAG { | ||
| normalizedExpectedDAG[k] = v | ||
| } | ||
|
|
||
| if diff := cmp.Diff(normalizedExpectedDAG, normalizedDAG); diff != "" { | ||
| t.Errorf("prepareDataGraph() mismatch (-want +got):\n%s", diff) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.