Skip to content
Open
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
21 changes: 14 additions & 7 deletions cmd/epp/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (r *Runner) Run(ctx context.Context) error {
}

// --- Setup Datastore ---
epf, err := r.setupMetricsCollection(setupLog, r.featureGates[datalayer.FeatureGate])
epf, err := r.setupMetricsCollection(setupLog, r.featureGates[datalayer.ExperimentalDatalayerFeatureGate])
if err != nil {
return err
}
Expand Down Expand Up @@ -376,7 +376,7 @@ func (r *Runner) Run(ctx context.Context) error {
MetricsStalenessThreshold: *metricsStalenessThreshold,
Director: director,
SaturationDetector: saturationDetector,
UseExperimentalDatalayerV2: r.featureGates[datalayer.FeatureGate], // pluggable data layer feature flag
UseExperimentalDatalayerV2: r.featureGates[datalayer.ExperimentalDatalayerFeatureGate], // pluggable data layer feature flag
}
if err := serverRunner.SetupWithManager(ctx, mgr); err != nil {
setupLog.Error(err, "Failed to setup EPP controllers")
Expand Down Expand Up @@ -467,8 +467,9 @@ func (r *Runner) parseConfigurationPhaseOne(ctx context.Context) (*configapi.End
}
}

loader.RegisterFeatureGate(datalayer.FeatureGate)
loader.RegisterFeatureGate(datalayer.ExperimentalDatalayerFeatureGate)
loader.RegisterFeatureGate(flowcontrol.FeatureGate)
loader.RegisterFeatureGate(datalayer.PrepareDataPluginsFeatureGate)

r.registerInTreePlugins()

Expand Down Expand Up @@ -508,9 +509,15 @@ func (r *Runner) parseConfigurationPhaseTwo(ctx context.Context, rawConfig *conf

// Add requestControl plugins
r.requestControlConfig.AddPlugins(handle.GetAllPlugins()...)
// Sort prepare data plugins in DAG order (topological sort). Also check prepare data plugins for cycles.
if r.requestControlConfig.PrepareDataPluginGraph() != nil {
return nil, errors.New("failed to load the configuration - prepare data plugins have cyclic dependencies")

// TODO(#1970): Remove feature gate check once prepare data plugins are stable.
if r.featureGates[datalayer.PrepareDataPluginsFeatureGate] {
// Sort prepare data plugins in DAG order (topological sort). Also check prepare data plugins for cycles.
if r.requestControlConfig.PrepareDataPluginGraph() != nil {
return nil, errors.New("failed to load the configuration - prepare data plugins have cyclic dependencies")
}
} else {
r.requestControlConfig.WithPrepareDataPlugins()
}

// Handler deprecated configuration options
Expand All @@ -533,7 +540,7 @@ func (r *Runner) deprecatedConfigurationHelper(cfg *config.Config, logger logr.L

if _, ok := os.LookupEnv(enableExperimentalDatalayerV2); ok {
logger.Info("Enabling the experimental Data Layer V2 using environment variables is deprecated and will be removed in next version")
r.featureGates[datalayer.FeatureGate] = env.GetEnvBool(enableExperimentalDatalayerV2, false, logger)
r.featureGates[datalayer.ExperimentalDatalayerFeatureGate] = env.GetEnvBool(enableExperimentalDatalayerV2, false, logger)
}
if _, ok := os.LookupEnv(enableExperimentalFlowControlLayer); ok {
logger.Info("Enabling the experimental Flow Control layer using environment variables is deprecated and will be removed in next version")
Expand Down
6 changes: 3 additions & 3 deletions pkg/epp/config/loader/configloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestLoadRawConfiguration(t *testing.T) {
t.Parallel()

// Register known feature gates for validation.
RegisterFeatureGate(datalayer.FeatureGate)
RegisterFeatureGate(datalayer.ExperimentalDatalayerFeatureGate)

tests := []struct {
name string
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestLoadRawConfiguration(t *testing.T) {
},
},
},
FeatureGates: configapi.FeatureGates{datalayer.FeatureGate},
FeatureGates: configapi.FeatureGates{datalayer.ExperimentalDatalayerFeatureGate},
SaturationDetector: &configapi.SaturationDetector{
QueueDepthThreshold: 10,
KVCacheUtilThreshold: 0.8,
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestInstantiateAndConfigure(t *testing.T) {
// Not parallel because it modifies global plugin registry.
registerTestPlugins(t)

RegisterFeatureGate(datalayer.FeatureGate)
RegisterFeatureGate(datalayer.ExperimentalDatalayerFeatureGate)

tests := []struct {
name string
Expand Down
3 changes: 2 additions & 1 deletion pkg/epp/datalayer/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (
)

const (
FeatureGate = "dataLayer"
ExperimentalDatalayerFeatureGate = "dataLayer"
PrepareDataPluginsFeatureGate = "prepareDataPlugins"
)

// PoolInfo represents the DataStore information needed for endpoints.
Expand Down
52 changes: 52 additions & 0 deletions pkg/epp/datalayer/plugins/approximateprefix/data_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
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 approximateprefix

import (
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datalayer"
)

const (
PrefixCacheMatchInfoKey = "PrefixCacheMatchInfoKey"
)

type PrefixCacheMatchInfo struct {
matchLength int
totalBlocks int
}

func NewPrefixCacheMatchInfo(matchLen int, blockHashLen int) *PrefixCacheMatchInfo {
return &PrefixCacheMatchInfo{
matchLength: matchLen,
totalBlocks: blockHashLen,
}
}

func (p *PrefixCacheMatchInfo) MatchLength() int {
return p.matchLength
}

func (p *PrefixCacheMatchInfo) TotalLength() int {
return p.totalBlocks
}

func (p *PrefixCacheMatchInfo) Clone() datalayer.Cloneable {
return &PrefixCacheMatchInfo{
matchLength: p.matchLength,
totalBlocks: p.totalBlocks,
}
}
3 changes: 3 additions & 0 deletions pkg/epp/requestcontrol/director.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ func (d *Director) runPreRequestPlugins(ctx context.Context, request *scheduling

func (d *Director) runPrepareDataPlugins(ctx context.Context,
request *schedulingtypes.LLMRequest, pods []schedulingtypes.Pod) error {
if len(d.requestControlPlugins.prepareDataPlugins) == 0 {
return nil
}
return prepareDataPluginsWithTimeout(prepareDataTimeout, d.requestControlPlugins.prepareDataPlugins, ctx, request, pods)
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/epp/requestcontrol/request_control_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ func (c *Config) AddPlugins(pluginObjects ...plugins.Plugin) {

// PrepareDataPluginGraph creates data dependency graph and sorts the plugins in topological order.
// If a cycle is detected, it returns an error.
// TODO(#1970): Validate data dependency graph even when prepare data plugins are not in use.
func (c *Config) PrepareDataPluginGraph() error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we create an issue that we should create a dag, even when the prepare data plugins are not in use? we want to make sure that all data consumers have a corresponding consumer, and they are not cyclical. It's not scoped to just prepare data

if len(c.prepareDataPlugins) == 0 {
return nil
}
dag := buildDAG(c.prepareDataPlugins)
plugins, err := sortPlugins(dag, c.prepareDataPlugins)
if err != nil {
Expand Down
25 changes: 25 additions & 0 deletions pkg/epp/scheduling/framework/plugins/multi/prefix/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
k8stypes "k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/log"

"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datalayer/plugins/approximateprefix"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/metrics"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/requestcontrol"
Expand Down Expand Up @@ -206,6 +207,30 @@ func (p *Plugin) WithName(name string) *Plugin {
return p
}

func (p *Plugin) Produces() map[string]any {
return map[string]any{approximateprefix.PrefixCacheMatchInfoKey: approximateprefix.PrefixCacheMatchInfo{}}
}

func (p *Plugin) Consumes() map[string]any {
return map[string]any{}
}

// PrepareRequestData hashes prompt, finds longest prefix match and stores it in pod as attribute.
func (p *Plugin) PrepareRequestData(ctx context.Context, request *types.LLMRequest, pods []types.Pod) error {
hashes := hashPrompt(ctx, request, getBlockSize(pods, p.config), p.config.MaxPrefixBlocksToMatch)
state := &SchedulingContextState{
PrefixHashes: hashes,
PrefixCacheServers: p.matchLongestPrefix(ctx, hashes),
}
total := len(state.PrefixHashes)

for _, pod := range pods {
matchLen := state.PrefixCacheServers[ServerID(pod.GetPod().NamespacedName)]
pod.Put(approximateprefix.PrefixCacheMatchInfoKey, approximateprefix.NewPrefixCacheMatchInfo(matchLen, total))
}
return nil
}

// Score returns the scoring result for the given list of pods based on context.
func (p *Plugin) Score(ctx context.Context, cycleState *types.CycleState, request *types.LLMRequest, pods []types.Pod) map[types.Pod]float64 {
// pre score step, hashing prompt and find longest prefix match.
Expand Down
67 changes: 67 additions & 0 deletions pkg/epp/scheduling/framework/plugins/multi/prefix/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ import (

"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend"
backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datalayer"
dplugins "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datalayer/plugins/approximateprefix"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/requestcontrol"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types"
)

// static check to ensure Plugin implements the PrepareDataPlugin interface.
var _ requestcontrol.PrepareDataPlugin = &Plugin{}

func TestPrefixPluginCompletion(t *testing.T) {
config := Config{
BlockSize: 4,
Expand Down Expand Up @@ -571,6 +577,67 @@ func randomPrompt(n int) string {
return sb.String()
}

func TestPrepareRequestData(t *testing.T) {
config := Config{
BlockSize: 4,
MaxPrefixBlocksToMatch: DefaultMaxPrefixBlocks,
LRUCapacityPerServer: DefaultLRUCapacityPerServer,
}
plugin := New(context.Background(), config)

pod1 := &types.PodMetrics{Pod: &backend.Pod{NamespacedName: k8stypes.NamespacedName{Name: "pod1"}}, MetricsState: backendmetrics.NewMetricsState(), AttributeMap: datalayer.NewAttributes()}
pod2 := &types.PodMetrics{Pod: &backend.Pod{NamespacedName: k8stypes.NamespacedName{Name: "pod2"}}, MetricsState: backendmetrics.NewMetricsState(), AttributeMap: datalayer.NewAttributes()}
pods := []types.Pod{pod1, pod2}

// First request to populate cache.
req1 := &types.LLMRequest{
RequestId: uuid.NewString(),
TargetModel: "test-model1",
Body: &types.LLMRequestBody{
Completions: &types.CompletionsRequest{
Prompt: "aaaabbbb",
},
},
}
_ = plugin.Score(context.Background(), types.NewCycleState(), req1, pods)
schedulingResult := &types.SchedulingResult{
PrimaryProfileName: "default",
ProfileResults: map[string]*types.ProfileRunResult{
"default": {TargetPods: []types.Pod{pod1}},
},
}
plugin.PreRequest(context.Background(), req1, schedulingResult)
plugin.wg.Wait()

// Second request that shares a prefix.
req2 := &types.LLMRequest{
RequestId: uuid.NewString(),
TargetModel: "test-model1",
Body: &types.LLMRequestBody{
Completions: &types.CompletionsRequest{
Prompt: "aaaacccc",
},
},
}

err := plugin.PrepareRequestData(context.Background(), req2, pods)
assert.NoError(t, err)

// Verify pod1 has the correct prefix match info
info1, ok := pod1.Get(dplugins.PrefixCacheMatchInfoKey)
assert.True(t, ok)
prefixInfo1 := info1.(*dplugins.PrefixCacheMatchInfo)
assert.Equal(t, 1, prefixInfo1.MatchLength()) // "aaaa" matches
assert.Equal(t, 2, prefixInfo1.TotalLength()) // "aaaacccc" -> 2 blocks

// Verify pod2 has no match info
info2, ok := pod2.Get(dplugins.PrefixCacheMatchInfoKey)
assert.True(t, ok)
prefixInfo2 := info2.(*dplugins.PrefixCacheMatchInfo)
assert.Equal(t, 0, prefixInfo2.MatchLength()) // No match for pod2
assert.Equal(t, 2, prefixInfo2.TotalLength())
}

// BenchmarkPrefixPluginChatCompletionsStress is a stress test for chat completions with varying message counts and lengths
func BenchmarkPrefixPluginChatCompletionsStress(b *testing.B) {
blockSize := 8
Expand Down