|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes 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 scheduling |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "reflect" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "go.opentelemetry.io/otel" |
| 25 | + "go.opentelemetry.io/otel/attribute" |
| 26 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 27 | + "go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 28 | + "go.opentelemetry.io/otel/trace" |
| 29 | + k8stypes "k8s.io/apimachinery/pkg/types" |
| 30 | + |
| 31 | + fwkdl "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/datalayer" |
| 32 | + fwkplugin "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/plugin" |
| 33 | + fwksched "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/scheduling" |
| 34 | +) |
| 35 | + |
| 36 | +// setupPickerSpanRecorder installs an in-memory span recorder as the global |
| 37 | +// tracer provider and returns it, restoring the previous provider on cleanup. |
| 38 | +func setupPickerSpanRecorder(t *testing.T) *tracetest.SpanRecorder { |
| 39 | + t.Helper() |
| 40 | + recorder := tracetest.NewSpanRecorder() |
| 41 | + tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(recorder)) |
| 42 | + origTP := otel.GetTracerProvider() |
| 43 | + otel.SetTracerProvider(tp) |
| 44 | + t.Cleanup(func() { otel.SetTracerProvider(origTP) }) |
| 45 | + return recorder |
| 46 | +} |
| 47 | + |
| 48 | +func findPickerSpans(spans []sdktrace.ReadOnlySpan, name string) []sdktrace.ReadOnlySpan { |
| 49 | + var out []sdktrace.ReadOnlySpan |
| 50 | + for _, s := range spans { |
| 51 | + if s.Name() == name { |
| 52 | + out = append(out, s) |
| 53 | + } |
| 54 | + } |
| 55 | + return out |
| 56 | +} |
| 57 | + |
| 58 | +func pickerSpanAttributes(span sdktrace.ReadOnlySpan) map[attribute.Key]attribute.Value { |
| 59 | + attrs := make(map[attribute.Key]attribute.Value) |
| 60 | + for _, kv := range span.Attributes() { |
| 61 | + attrs[kv.Key] = kv.Value |
| 62 | + } |
| 63 | + return attrs |
| 64 | +} |
| 65 | + |
| 66 | +func newWeightedScores(names ...string) map[fwksched.Endpoint]float64 { |
| 67 | + scores := make(map[fwksched.Endpoint]float64, len(names)) |
| 68 | + for i, name := range names { |
| 69 | + endpoint := fwksched.NewEndpoint( |
| 70 | + &fwkdl.EndpointMetadata{NamespacedName: k8stypes.NamespacedName{Name: name}}, nil, nil) |
| 71 | + scores[endpoint] = float64(i) |
| 72 | + } |
| 73 | + return scores |
| 74 | +} |
| 75 | + |
| 76 | +// fakePicker returns the first selected candidates as targets, or a nil result |
| 77 | +// when nilResult is set, and optionally emits a child span from its context. |
| 78 | +type fakePicker struct { |
| 79 | + typedName fwkplugin.TypedName |
| 80 | + selected int |
| 81 | + nilResult bool |
| 82 | + emitChild bool |
| 83 | +} |
| 84 | + |
| 85 | +var _ fwksched.Picker = &fakePicker{} |
| 86 | + |
| 87 | +func (f *fakePicker) TypedName() fwkplugin.TypedName { return f.typedName } |
| 88 | + |
| 89 | +func (f *fakePicker) Pick(ctx context.Context, scoredPods []*fwksched.ScoredEndpoint) *fwksched.ProfileRunResult { |
| 90 | + if f.emitChild { |
| 91 | + _, span := otel.Tracer("test").Start(ctx, "inner_picker_span") |
| 92 | + span.End() |
| 93 | + } |
| 94 | + if f.nilResult { |
| 95 | + return nil |
| 96 | + } |
| 97 | + targets := make([]fwksched.Endpoint, 0, f.selected) |
| 98 | + for i := 0; i < f.selected && i < len(scoredPods); i++ { |
| 99 | + targets = append(targets, scoredPods[i].Endpoint) |
| 100 | + } |
| 101 | + return &fwksched.ProfileRunResult{TargetEndpoints: targets} |
| 102 | +} |
| 103 | + |
| 104 | +func TestRunPickerPluginSingleSpan(t *testing.T) { |
| 105 | + recorder := setupPickerSpanRecorder(t) |
| 106 | + |
| 107 | + picker := &fakePicker{typedName: fwkplugin.TypedName{Type: "max-score", Name: "instance-a"}, selected: 1} |
| 108 | + profile := NewSchedulerProfile().WithPicker(picker) |
| 109 | + scores := newWeightedScores("pod1", "pod2", "pod3") |
| 110 | + |
| 111 | + ctx, root := otel.Tracer("test").Start(context.Background(), "root") |
| 112 | + result := profile.runPickerPlugin(ctx, &fwksched.InferenceRequest{TargetModel: "m1", RequestID: "r1"}, scores) |
| 113 | + root.End() |
| 114 | + |
| 115 | + if result == nil || len(result.TargetEndpoints) != 1 { |
| 116 | + t.Fatalf("runPickerPlugin returned %v, want 1 target", result) |
| 117 | + } |
| 118 | + |
| 119 | + spans := findPickerSpans(recorder.Ended(), "pick_endpoints") |
| 120 | + if len(spans) != 1 { |
| 121 | + t.Fatalf("got %d pick_endpoints spans, want 1", len(spans)) |
| 122 | + } |
| 123 | + span := spans[0] |
| 124 | + if span.SpanKind() != trace.SpanKindInternal { |
| 125 | + t.Errorf("span kind = %v, want Internal", span.SpanKind()) |
| 126 | + } |
| 127 | + if span.Parent().SpanID() != root.SpanContext().SpanID() { |
| 128 | + t.Errorf("parent span ID = %v, want root %v", span.Parent().SpanID(), root.SpanContext().SpanID()) |
| 129 | + } |
| 130 | + |
| 131 | + attrs := pickerSpanAttributes(span) |
| 132 | + for _, tc := range []struct { |
| 133 | + key attribute.Key |
| 134 | + want string |
| 135 | + }{ |
| 136 | + {"gen_ai.request.model", "m1"}, |
| 137 | + {"gen_ai.request.id", "r1"}, |
| 138 | + } { |
| 139 | + if got := attrs[tc.key].AsString(); got != tc.want { |
| 140 | + t.Errorf("%s = %q, want %q", tc.key, got, tc.want) |
| 141 | + } |
| 142 | + } |
| 143 | + if got := attrs["llm_d.epp.picker.candidate_endpoints"].AsInt64(); got != 3 { |
| 144 | + t.Errorf("candidate_endpoints = %d, want 3", got) |
| 145 | + } |
| 146 | + // newWeightedScores assigns score i to the i-th name, so the span records |
| 147 | + // candidates highest score first. |
| 148 | + if got, want := attrs["llm_d.epp.picker.top_endpoints"].AsStringSlice(), []string{"/pod3", "/pod2", "/pod1"}; !reflect.DeepEqual(got, want) { |
| 149 | + t.Errorf("top_endpoints = %v, want %v", got, want) |
| 150 | + } |
| 151 | + if got, want := attrs["llm_d.epp.picker.top_scores"].AsFloat64Slice(), []float64{2, 1, 0}; !reflect.DeepEqual(got, want) { |
| 152 | + t.Errorf("top_scores = %v, want %v", got, want) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestRunPickerPluginTopScoresCapped(t *testing.T) { |
| 157 | + recorder := setupPickerSpanRecorder(t) |
| 158 | + |
| 159 | + picker := &fakePicker{typedName: fwkplugin.TypedName{Type: "max-score", Name: "multi"}, selected: 2} |
| 160 | + profile := NewSchedulerProfile().WithPicker(picker) |
| 161 | + // Seven candidates (scores 0..6) exceed the cap of five. |
| 162 | + scores := newWeightedScores("pod1", "pod2", "pod3", "pod4", "pod5", "pod6", "pod7") |
| 163 | + |
| 164 | + ctx, root := otel.Tracer("test").Start(context.Background(), "root") |
| 165 | + profile.runPickerPlugin(ctx, &fwksched.InferenceRequest{}, scores) |
| 166 | + root.End() |
| 167 | + |
| 168 | + spans := findPickerSpans(recorder.Ended(), "pick_endpoints") |
| 169 | + if len(spans) != 1 { |
| 170 | + t.Fatalf("got %d pick_endpoints spans, want 1", len(spans)) |
| 171 | + } |
| 172 | + attrs := pickerSpanAttributes(spans[0]) |
| 173 | + if got, want := attrs["llm_d.epp.picker.top_endpoints"].AsStringSlice(), []string{"/pod7", "/pod6", "/pod5", "/pod4", "/pod3"}; !reflect.DeepEqual(got, want) { |
| 174 | + t.Errorf("top_endpoints = %v, want the 5 highest-scoring %v", got, want) |
| 175 | + } |
| 176 | + if got, want := attrs["llm_d.epp.picker.top_scores"].AsFloat64Slice(), []float64{6, 5, 4, 3, 2}; !reflect.DeepEqual(got, want) { |
| 177 | + t.Errorf("top_scores = %v, want %v", got, want) |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +func TestRunPickerPluginNilResult(t *testing.T) { |
| 182 | + recorder := setupPickerSpanRecorder(t) |
| 183 | + |
| 184 | + picker := &fakePicker{typedName: fwkplugin.TypedName{Type: "noop-picker", Name: "noop"}, nilResult: true} |
| 185 | + profile := NewSchedulerProfile().WithPicker(picker) |
| 186 | + scores := newWeightedScores("pod1", "pod2") |
| 187 | + |
| 188 | + ctx, root := otel.Tracer("test").Start(context.Background(), "root") |
| 189 | + result := profile.runPickerPlugin(ctx, &fwksched.InferenceRequest{}, scores) |
| 190 | + root.End() |
| 191 | + |
| 192 | + if result != nil { |
| 193 | + t.Fatalf("runPickerPlugin returned %v, want nil", result) |
| 194 | + } |
| 195 | + spans := findPickerSpans(recorder.Ended(), "pick_endpoints") |
| 196 | + if len(spans) != 1 { |
| 197 | + t.Fatalf("got %d pick_endpoints spans, want 1 (span must end on nil result)", len(spans)) |
| 198 | + } |
| 199 | + // Scores are captured from the candidates before Pick, so they are recorded |
| 200 | + // even when the picker selects nothing. |
| 201 | + if got, want := pickerSpanAttributes(spans[0])["llm_d.epp.picker.top_scores"].AsFloat64Slice(), []float64{1, 0}; !reflect.DeepEqual(got, want) { |
| 202 | + t.Errorf("top_scores = %v, want %v", got, want) |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +func TestRunPickerPluginNestsDelegateSpan(t *testing.T) { |
| 207 | + recorder := setupPickerSpanRecorder(t) |
| 208 | + |
| 209 | + picker := &fakePicker{typedName: fwkplugin.TypedName{Type: "child", Name: "c"}, selected: 1, emitChild: true} |
| 210 | + profile := NewSchedulerProfile().WithPicker(picker) |
| 211 | + scores := newWeightedScores("pod1") |
| 212 | + |
| 213 | + ctx, root := otel.Tracer("test").Start(context.Background(), "root") |
| 214 | + profile.runPickerPlugin(ctx, &fwksched.InferenceRequest{}, scores) |
| 215 | + root.End() |
| 216 | + |
| 217 | + outer := findPickerSpans(recorder.Ended(), "pick_endpoints") |
| 218 | + inner := findPickerSpans(recorder.Ended(), "inner_picker_span") |
| 219 | + if len(outer) != 1 || len(inner) != 1 { |
| 220 | + t.Fatalf("got %d pick_endpoints and %d inner spans, want 1 each", len(outer), len(inner)) |
| 221 | + } |
| 222 | + if inner[0].Parent().SpanID() != outer[0].SpanContext().SpanID() { |
| 223 | + t.Errorf("inner span parent = %v, want pick_endpoints span %v", |
| 224 | + inner[0].Parent().SpanID(), outer[0].SpanContext().SpanID()) |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +func TestRunPickerPluginOmitsEmptyGenAI(t *testing.T) { |
| 229 | + recorder := setupPickerSpanRecorder(t) |
| 230 | + |
| 231 | + picker := &fakePicker{typedName: fwkplugin.TypedName{Type: "p", Name: "n"}, selected: 1} |
| 232 | + profile := NewSchedulerProfile().WithPicker(picker) |
| 233 | + scores := newWeightedScores("pod1") |
| 234 | + |
| 235 | + ctx, root := otel.Tracer("test").Start(context.Background(), "root") |
| 236 | + profile.runPickerPlugin(ctx, &fwksched.InferenceRequest{}, scores) |
| 237 | + root.End() |
| 238 | + |
| 239 | + spans := findPickerSpans(recorder.Ended(), "pick_endpoints") |
| 240 | + if len(spans) != 1 { |
| 241 | + t.Fatalf("got %d pick_endpoints spans, want 1", len(spans)) |
| 242 | + } |
| 243 | + attrs := pickerSpanAttributes(spans[0]) |
| 244 | + if _, ok := attrs["gen_ai.request.model"]; ok { |
| 245 | + t.Error("gen_ai.request.model set for empty TargetModel") |
| 246 | + } |
| 247 | + if _, ok := attrs["gen_ai.request.id"]; ok { |
| 248 | + t.Error("gen_ai.request.id set for empty RequestID") |
| 249 | + } |
| 250 | +} |
0 commit comments