|
| 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 | + "testing" |
| 22 | + |
| 23 | + "go.opentelemetry.io/otel" |
| 24 | + "go.opentelemetry.io/otel/attribute" |
| 25 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 26 | + "go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 27 | + "go.opentelemetry.io/otel/trace" |
| 28 | + k8stypes "k8s.io/apimachinery/pkg/types" |
| 29 | + |
| 30 | + fwkdl "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/datalayer" |
| 31 | + fwkplugin "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/plugin" |
| 32 | + fwksched "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/scheduling" |
| 33 | +) |
| 34 | + |
| 35 | +func setupSpanRecorder(t *testing.T) *tracetest.SpanRecorder { |
| 36 | + t.Helper() |
| 37 | + recorder := tracetest.NewSpanRecorder() |
| 38 | + tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(recorder)) |
| 39 | + prev := otel.GetTracerProvider() |
| 40 | + otel.SetTracerProvider(tp) |
| 41 | + t.Cleanup(func() { otel.SetTracerProvider(prev) }) |
| 42 | + return recorder |
| 43 | +} |
| 44 | + |
| 45 | +func spansByName(spans []sdktrace.ReadOnlySpan, name string) []sdktrace.ReadOnlySpan { |
| 46 | + var out []sdktrace.ReadOnlySpan |
| 47 | + for _, s := range spans { |
| 48 | + if s.Name() == name { |
| 49 | + out = append(out, s) |
| 50 | + } |
| 51 | + } |
| 52 | + return out |
| 53 | +} |
| 54 | + |
| 55 | +func spanAttrs(span sdktrace.ReadOnlySpan) map[attribute.Key]attribute.Value { |
| 56 | + m := make(map[attribute.Key]attribute.Value) |
| 57 | + for _, kv := range span.Attributes() { |
| 58 | + m[kv.Key] = kv.Value |
| 59 | + } |
| 60 | + return m |
| 61 | +} |
| 62 | + |
| 63 | +func makeWeightedScores(names ...string) map[fwksched.Endpoint]float64 { |
| 64 | + scores := make(map[fwksched.Endpoint]float64, len(names)) |
| 65 | + for i, name := range names { |
| 66 | + ep := fwksched.NewEndpoint( |
| 67 | + &fwkdl.EndpointMetadata{NamespacedName: k8stypes.NamespacedName{Name: name}}, nil, nil) |
| 68 | + scores[ep] = float64(i + 1) |
| 69 | + } |
| 70 | + return scores |
| 71 | +} |
| 72 | + |
| 73 | +// stubPicker is a minimal Picker for tracing tests. |
| 74 | +type stubPicker struct { |
| 75 | + typedName fwkplugin.TypedName |
| 76 | + numPick int |
| 77 | + nilResult bool |
| 78 | + emitSpan bool |
| 79 | +} |
| 80 | + |
| 81 | +var _ fwksched.Picker = &stubPicker{} |
| 82 | + |
| 83 | +func (s *stubPicker) TypedName() fwkplugin.TypedName { return s.typedName } |
| 84 | + |
| 85 | +func (s *stubPicker) Pick(ctx context.Context, scored []*fwksched.ScoredEndpoint) *fwksched.ProfileRunResult { |
| 86 | + if s.emitSpan { |
| 87 | + _, child := otel.Tracer("test").Start(ctx, "child_picker_op") |
| 88 | + child.End() |
| 89 | + } |
| 90 | + if s.nilResult { |
| 91 | + return nil |
| 92 | + } |
| 93 | + n := s.numPick |
| 94 | + if n > len(scored) { |
| 95 | + n = len(scored) |
| 96 | + } |
| 97 | + targets := make([]fwksched.Endpoint, n) |
| 98 | + for i := 0; i < n; i++ { |
| 99 | + targets[i] = scored[i].Endpoint |
| 100 | + } |
| 101 | + return &fwksched.ProfileRunResult{TargetEndpoints: targets} |
| 102 | +} |
| 103 | + |
| 104 | +func TestPickerSpan_Basic(t *testing.T) { |
| 105 | + recorder := setupSpanRecorder(t) |
| 106 | + |
| 107 | + profile := NewSchedulerProfile().WithPicker(&stubPicker{ |
| 108 | + typedName: fwkplugin.TypedName{Type: "max-score-picker", Name: "default"}, |
| 109 | + numPick: 1, |
| 110 | + }) |
| 111 | + scores := makeWeightedScores("pod1", "pod2", "pod3") |
| 112 | + req := &fwksched.InferenceRequest{TargetModel: "llama-7b", RequestID: "req-123"} |
| 113 | + |
| 114 | + ctx, root := otel.Tracer("test").Start(context.Background(), "root") |
| 115 | + result := profile.runPickerPlugin(ctx, req, scores) |
| 116 | + root.End() |
| 117 | + |
| 118 | + if result == nil || len(result.TargetEndpoints) != 1 { |
| 119 | + t.Fatalf("expected 1 target endpoint, got %v", result) |
| 120 | + } |
| 121 | + |
| 122 | + spans := spansByName(recorder.Ended(), "pick_endpoints") |
| 123 | + if len(spans) != 1 { |
| 124 | + t.Fatalf("expected 1 pick_endpoints span, got %d", len(spans)) |
| 125 | + } |
| 126 | + |
| 127 | + span := spans[0] |
| 128 | + if span.SpanKind() != trace.SpanKindInternal { |
| 129 | + t.Errorf("span kind = %v, want Internal", span.SpanKind()) |
| 130 | + } |
| 131 | + if span.Parent().SpanID() != root.SpanContext().SpanID() { |
| 132 | + t.Errorf("span parent = %v, want root %v", span.Parent().SpanID(), root.SpanContext().SpanID()) |
| 133 | + } |
| 134 | + |
| 135 | + attrs := spanAttrs(span) |
| 136 | + if got := attrs["llm_d.epp.picker.candidate_endpoints"].AsInt64(); got != 3 { |
| 137 | + t.Errorf("candidate_endpoints = %d, want 3", got) |
| 138 | + } |
| 139 | + if got := attrs["llm_d.epp.picker.selected_endpoints"].AsInt64(); got != 1 { |
| 140 | + t.Errorf("selected_endpoints = %d, want 1", got) |
| 141 | + } |
| 142 | + if got := attrs["gen_ai.request.model"].AsString(); got != "llama-7b" { |
| 143 | + t.Errorf("gen_ai.request.model = %q, want %q", got, "llama-7b") |
| 144 | + } |
| 145 | + if got := attrs["gen_ai.request.id"].AsString(); got != "req-123" { |
| 146 | + t.Errorf("gen_ai.request.id = %q, want %q", got, "req-123") |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func TestPickerSpan_MultipleSelected(t *testing.T) { |
| 151 | + recorder := setupSpanRecorder(t) |
| 152 | + |
| 153 | + profile := NewSchedulerProfile().WithPicker(&stubPicker{ |
| 154 | + typedName: fwkplugin.TypedName{Type: "max-score-picker", Name: "multi"}, |
| 155 | + numPick: 2, |
| 156 | + }) |
| 157 | + scores := makeWeightedScores("pod1", "pod2", "pod3") |
| 158 | + |
| 159 | + ctx, root := otel.Tracer("test").Start(context.Background(), "root") |
| 160 | + result := profile.runPickerPlugin(ctx, &fwksched.InferenceRequest{TargetModel: "m1", RequestID: "r1"}, scores) |
| 161 | + root.End() |
| 162 | + |
| 163 | + if result == nil || len(result.TargetEndpoints) != 2 { |
| 164 | + t.Fatalf("expected 2 target endpoints, got %v", result) |
| 165 | + } |
| 166 | + |
| 167 | + spans := spansByName(recorder.Ended(), "pick_endpoints") |
| 168 | + if len(spans) != 1 { |
| 169 | + t.Fatalf("expected 1 span, got %d", len(spans)) |
| 170 | + } |
| 171 | + if got := spanAttrs(spans[0])["llm_d.epp.picker.selected_endpoints"].AsInt64(); got != 2 { |
| 172 | + t.Errorf("selected_endpoints = %d, want 2", got) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func TestPickerSpan_NilResult(t *testing.T) { |
| 177 | + recorder := setupSpanRecorder(t) |
| 178 | + |
| 179 | + profile := NewSchedulerProfile().WithPicker(&stubPicker{ |
| 180 | + typedName: fwkplugin.TypedName{Type: "noop", Name: "noop"}, |
| 181 | + nilResult: true, |
| 182 | + }) |
| 183 | + scores := makeWeightedScores("pod1", "pod2") |
| 184 | + |
| 185 | + ctx, root := otel.Tracer("test").Start(context.Background(), "root") |
| 186 | + result := profile.runPickerPlugin(ctx, &fwksched.InferenceRequest{RequestID: "r1"}, scores) |
| 187 | + root.End() |
| 188 | + |
| 189 | + if result != nil { |
| 190 | + t.Fatalf("expected nil result, got %v", result) |
| 191 | + } |
| 192 | + |
| 193 | + spans := spansByName(recorder.Ended(), "pick_endpoints") |
| 194 | + if len(spans) != 1 { |
| 195 | + t.Fatalf("expected 1 span even on nil result, got %d", len(spans)) |
| 196 | + } |
| 197 | + if got := spanAttrs(spans[0])["llm_d.epp.picker.selected_endpoints"].AsInt64(); got != 0 { |
| 198 | + t.Errorf("selected_endpoints = %d, want 0 on nil result", got) |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +func TestPickerSpan_OmitsEmptyGenAIFields(t *testing.T) { |
| 203 | + recorder := setupSpanRecorder(t) |
| 204 | + |
| 205 | + profile := NewSchedulerProfile().WithPicker(&stubPicker{ |
| 206 | + typedName: fwkplugin.TypedName{Type: "p", Name: "n"}, |
| 207 | + numPick: 1, |
| 208 | + }) |
| 209 | + scores := makeWeightedScores("pod1") |
| 210 | + |
| 211 | + ctx, root := otel.Tracer("test").Start(context.Background(), "root") |
| 212 | + profile.runPickerPlugin(ctx, &fwksched.InferenceRequest{}, scores) |
| 213 | + root.End() |
| 214 | + |
| 215 | + spans := spansByName(recorder.Ended(), "pick_endpoints") |
| 216 | + if len(spans) != 1 { |
| 217 | + t.Fatalf("expected 1 span, got %d", len(spans)) |
| 218 | + } |
| 219 | + attrs := spanAttrs(spans[0]) |
| 220 | + if _, ok := attrs["gen_ai.request.model"]; ok { |
| 221 | + t.Error("gen_ai.request.model should not be set when TargetModel is empty") |
| 222 | + } |
| 223 | + if _, ok := attrs["gen_ai.request.id"]; ok { |
| 224 | + t.Error("gen_ai.request.id should not be set when RequestID is empty") |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +func TestPickerSpan_NilRequest(t *testing.T) { |
| 229 | + recorder := setupSpanRecorder(t) |
| 230 | + |
| 231 | + profile := NewSchedulerProfile().WithPicker(&stubPicker{ |
| 232 | + typedName: fwkplugin.TypedName{Type: "p", Name: "n"}, |
| 233 | + numPick: 1, |
| 234 | + }) |
| 235 | + scores := makeWeightedScores("pod1") |
| 236 | + |
| 237 | + ctx, root := otel.Tracer("test").Start(context.Background(), "root") |
| 238 | + profile.runPickerPlugin(ctx, nil, scores) |
| 239 | + root.End() |
| 240 | + |
| 241 | + spans := spansByName(recorder.Ended(), "pick_endpoints") |
| 242 | + if len(spans) != 1 { |
| 243 | + t.Fatalf("expected 1 span, got %d", len(spans)) |
| 244 | + } |
| 245 | + attrs := spanAttrs(spans[0]) |
| 246 | + if _, ok := attrs["gen_ai.request.model"]; ok { |
| 247 | + t.Error("gen_ai.request.model should not be set when request is nil") |
| 248 | + } |
| 249 | + if _, ok := attrs["gen_ai.request.id"]; ok { |
| 250 | + t.Error("gen_ai.request.id should not be set when request is nil") |
| 251 | + } |
| 252 | + if got := attrs["llm_d.epp.picker.candidate_endpoints"].AsInt64(); got != 1 { |
| 253 | + t.Errorf("candidate_endpoints = %d, want 1", got) |
| 254 | + } |
| 255 | +} |
| 256 | + |
| 257 | +func TestPickerSpan_ChildSpanNests(t *testing.T) { |
| 258 | + recorder := setupSpanRecorder(t) |
| 259 | + |
| 260 | + profile := NewSchedulerProfile().WithPicker(&stubPicker{ |
| 261 | + typedName: fwkplugin.TypedName{Type: "nested", Name: "nested"}, |
| 262 | + numPick: 1, |
| 263 | + emitSpan: true, |
| 264 | + }) |
| 265 | + scores := makeWeightedScores("pod1") |
| 266 | + |
| 267 | + ctx, root := otel.Tracer("test").Start(context.Background(), "root") |
| 268 | + profile.runPickerPlugin(ctx, &fwksched.InferenceRequest{}, scores) |
| 269 | + root.End() |
| 270 | + |
| 271 | + pickerSpans := spansByName(recorder.Ended(), "pick_endpoints") |
| 272 | + childSpans := spansByName(recorder.Ended(), "child_picker_op") |
| 273 | + if len(pickerSpans) != 1 || len(childSpans) != 1 { |
| 274 | + t.Fatalf("expected 1 pick_endpoints and 1 child span, got %d and %d", len(pickerSpans), len(childSpans)) |
| 275 | + } |
| 276 | + if childSpans[0].Parent().SpanID() != pickerSpans[0].SpanContext().SpanID() { |
| 277 | + t.Errorf("child span parent = %v, want pick_endpoints span %v", |
| 278 | + childSpans[0].Parent().SpanID(), pickerSpans[0].SpanContext().SpanID()) |
| 279 | + } |
| 280 | +} |
0 commit comments