forked from llm-d/llm-d-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnector_nixlv2.go
More file actions
698 lines (624 loc) · 25.7 KB
/
Copy pathconnector_nixlv2.go
File metadata and controls
698 lines (624 loc) · 25.7 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
Copyright 2025 The llm-d 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 proxy
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strconv"
"sync"
"time"
"github.com/google/uuid"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"github.com/llm-d/llm-d-router/pkg/common/observability/tracing"
)
// tokenLimitMap returns the map holding the token-limit fields: sampling_params
// for the generate API (created if absent), or the request itself otherwise.
// The second return value reports whether an empty sampling_params map was
// synthesized; callers must drop it before dispatching downstream if it stays empty.
func tokenLimitMap(req map[string]any, apiType APIType) (map[string]any, bool) {
if apiType != APITypeGenerate {
return req, false
}
if sp, ok := req[requestFieldSamplingParams].(map[string]any); ok {
return sp, false
}
sp := map[string]any{}
req[requestFieldSamplingParams] = sp
return sp, true
}
func (s *Server) handleNIXLV2(w http.ResponseWriter, r *http.Request, prefillPodHostPort string, apiType APIType) {
tokenLimitFields := tokenLimitFieldsForAPIType(apiType)
s.logger.V(4).Info("running NIXL protocol V2", "url", prefillPodHostPort, "tokenLimitFields", tokenLimitFields)
original, completionRequest, ok := s.readJSONBody(r, w)
if !ok {
return
}
// Generate unique request UUID
uuid, err := uuid.NewUUID()
if err != nil {
if err := errorBadGateway(err, w); err != nil {
s.logger.Error(err, "failed to send error response to client")
}
return
}
uuidStr := uuid.String()
// Parallel-dispatch path synthesises decode's kv_transfer_params from config
// instead of the prefill response. The serial path below is unchanged when off.
if s.config.MoRIIOParallelDispatch && s.config.MoRIIOWriteMode {
// MoRI-IO requires transfer_id to carry the "tx" prefix for message routing.
transferID := "tx" + uuidStr
s.runNIXLProtocolV2WriteParallel(w, r, original, completionRequest, uuidStr, transferID, prefillPodHostPort)
return
}
// Prefill Stage
tracer := tracing.Tracer(tracerScope)
ctx := r.Context()
ctx, prefillSpan := tracer.Start(ctx, "prefill",
trace.WithSpanKind(trace.SpanKindInternal),
)
prefillSpan.SetAttributes(
attribute.String("llm_d.pd_proxy.request_id", uuidStr),
attribute.String("llm_d.pd_proxy.prefill_target", prefillPodHostPort),
attribute.String("llm_d.pd_proxy.connector", KVConnectorNIXLV2),
)
prefillStart := time.Now()
// 1. Prepare prefill request
preq := r.Clone(ctx)
preq.Header.Add(requestHeaderRequestID, uuidStr)
// Pin both legs to the same DP rank; the header is skipped for single-DP.
dpRank := pickDPRank(uuidStr, s.config.MoRIIODPSize)
if s.config.MoRIIODPSize > 1 {
preq.Header.Set(requestHeaderDataParallelRank, strconv.Itoa(dpRank))
}
// Save original values based on API type
streamValue, streamOk := completionRequest[requestFieldStream]
streamOptionsValue, streamOptionsOk := completionRequest[requestFieldStreamOptions]
// Save and override token limit fields for prefill
type savedField struct {
field string
val any
present bool
}
tokenMap, createdSamplingParams := tokenLimitMap(completionRequest, apiType)
var savedTokenValues [2]savedField
for i, field := range tokenLimitFields {
if v, ok := tokenMap[field]; ok {
savedTokenValues[i] = savedField{field: field, val: v, present: true}
} else {
savedTokenValues[i] = savedField{field: field}
}
}
// WRITE mode populates the destination fields the prefill engine needs for
// its RDMA Write; READ mode leaves them nil per the standard NIXLv2 contract.
if s.config.MoRIIOWriteMode {
// MoRI-IO requires transfer_id to carry the "tx" prefix for message routing.
transferID := "tx" + uuidStr
completionRequest[requestFieldKVTransferParams] = map[string]any{
requestFieldDoRemoteDecode: true,
requestFieldDoRemotePrefill: false,
requestFieldRemoteEngineID: nil,
requestFieldRemoteBlockIDs: nil,
requestFieldRemoteHost: s.config.MoRIIODecodePodIP,
requestFieldRemotePort: nil,
requestFieldRemoteNotifyPort: s.config.MoRIIODecodeNotifyPort,
requestFieldRemoteDPRank: dpRank,
requestFieldRemoteDPRankOverride: true,
requestFieldRemoteHandshakePort: s.config.MoRIIODecodeHandshakePort,
requestFieldTransferID: transferID,
"tp_size": s.config.MoRIIOTPSize,
"remote_dp_size": s.config.MoRIIODPSize,
}
// Wide-EP fan-out (prefill leg, serial path): remote_hosts must be the
// DECODE-side pod IPs so prefill handshakes the right pods.
if len(s.config.MoRIIODecodeHosts) > 0 {
pkv := completionRequest[requestFieldKVTransferParams].(map[string]any)
hosts := make([]any, len(s.config.MoRIIODecodeHosts))
for i, h := range s.config.MoRIIODecodeHosts {
hosts[i] = h
}
pkv["remote_hosts"] = hosts
if s.config.MoRIIODPSizeLocal > 0 {
pkv["remote_dp_size_local"] = s.config.MoRIIODPSizeLocal
}
}
} else {
completionRequest[requestFieldKVTransferParams] = map[string]any{
requestFieldDoRemoteDecode: true,
requestFieldDoRemotePrefill: false,
requestFieldRemoteEngineID: nil,
requestFieldRemoteBlockIDs: nil,
requestFieldRemoteHost: nil,
requestFieldRemotePort: nil,
}
}
completionRequest[requestFieldStream] = false
delete(completionRequest, requestFieldStreamOptions)
for _, field := range tokenLimitFields {
tokenMap[field] = 1
}
pbody, err := json.Marshal(completionRequest)
if err != nil {
if err := errorJSONInvalid(err, w); err != nil {
s.logger.Error(err, "failed to send error response to client")
}
return
}
preq.Body = io.NopCloser(bytes.NewReader(pbody))
preq.ContentLength = int64(len(pbody))
prefillHandler, err := s.prefillerProxyHandler(prefillPodHostPort)
if err != nil {
if err := errorBadGateway(err, w); err != nil {
s.logger.Error(err, "failed to send error response to client")
}
return
}
// 2. Forward request to prefiller
s.logger.V(4).Info("sending prefill request", "to", prefillPodHostPort)
s.logger.V(5).Info("Prefill request", "body", string(pbody))
// Retry on transient 5xx (502/503/504): these failures (e.g. connection
// reset → 502) are common when the prefill pod's accept queue overflows
// under load. Retrying the same host avoids expensive local prefill on
// decode. Non-transient errors (500/501) fail immediately.
var pw *bufferedResponseWriter
retryLoop:
for attempt := 0; ; attempt++ {
pw = &bufferedResponseWriter{}
preq.Body = io.NopCloser(bytes.NewReader(pbody))
preq.ContentLength = int64(len(pbody))
prefillHandler.ServeHTTP(pw, preq)
if !isHTTPError(pw.statusCode) {
break
}
if !isRetryableStatus(pw.statusCode) {
break
}
if attempt >= s.config.PrefillMaxRetries {
break
}
s.logger.Info("retrying prefill request",
"attempt", attempt+1,
"target", prefillPodHostPort,
"request_id", uuidStr,
"previous_code", pw.statusCode)
select {
case <-time.After(s.config.PrefillRetryBackoff):
case <-preq.Context().Done():
break retryLoop
}
}
prefillDuration := time.Since(prefillStart)
prefillSpan.SetAttributes(
attribute.Int("llm_d.pd_proxy.prefill.status_code", pw.statusCode),
attribute.Float64("llm_d.pd_proxy.prefill.duration_ms", float64(prefillDuration.Milliseconds())),
)
if isHTTPError(pw.statusCode) {
s.logger.Error(fmt.Errorf("prefill returned %d", pw.statusCode), "prefill request failed",
"request_id", uuidStr,
"body", pw.buffer.String())
prefillSpan.SetStatus(codes.Error, "prefill request failed")
prefillSpan.End()
for key, values := range pw.Header() {
for _, v := range values {
w.Header().Add(key, v)
}
}
w.WriteHeader(pw.statusCode)
if _, writeErr := w.Write(pw.bodyBytes()); writeErr != nil {
s.logger.Error(writeErr, "failed to send error response to client")
}
return
}
prefillSpan.End()
// Process response - extract p/d fields
var prefillerResponse map[string]any
if err := json.Unmarshal(pw.bodyBytes(), &prefillerResponse); err != nil {
if err := errorJSONInvalid(err, w); err != nil {
s.logger.Error(err, "failed to send error response to client")
}
return
}
// 3. Verify response
pKVTransferParams, ok := prefillerResponse[requestFieldKVTransferParams]
if !ok {
s.logger.Info("warning: missing 'kv_transfer_params' field in prefiller response")
}
pCachedTokens, hasPCachedTokens := extractCachedTokens(prefillerResponse)
if !hasPCachedTokens {
// vLLM returns prompt_tokens_details as null when cached_tokens is 0,
// so treat a missing prefiller cached_tokens value as zero.
pCachedTokens = 0
}
s.logger.V(5).Info("received prefiller response", requestFieldKVTransferParams, pKVTransferParams)
// Decode Stage
ctx, decodeSpan := tracer.Start(ctx, "decode",
trace.WithSpanKind(trace.SpanKindInternal),
)
defer decodeSpan.End()
decodeSpan.SetAttributes(
attribute.String("llm_d.pd_proxy.request_id", uuidStr),
attribute.String("llm_d.pd_proxy.connector", KVConnectorNIXLV2),
)
decodeStart := time.Now()
// 1. Prepare decode request
dreq := r.Clone(ctx)
dreq.Header.Add(requestHeaderRequestID, uuidStr)
// Stamp the same DP-rank pin on the decode leg.
if s.config.MoRIIODPSize > 1 {
dreq.Header.Set(
requestHeaderDataParallelRank,
strconv.Itoa(pickDPRank(uuidStr, s.config.MoRIIODPSize)),
)
}
delete(completionRequest, requestFieldStream)
streamingEnabled := false
if streamOk {
completionRequest[requestFieldStream] = streamValue
if streamBool, ok := streamValue.(bool); ok {
streamingEnabled = streamBool
}
}
decodeSpan.SetAttributes(attribute.Bool("llm_d.pd_proxy.decode.streaming", streamingEnabled))
if streamOptionsOk {
completionRequest[requestFieldStreamOptions] = streamOptionsValue
}
for i := range savedTokenValues[:len(tokenLimitFields)] {
sv := &savedTokenValues[i]
delete(tokenMap, sv.field)
if sv.present {
tokenMap[sv.field] = sv.val
}
}
// Drop the sampling_params map synthesized for prefill capping if it ended up
// empty, so the decode request matches the caller's original (which omitted it).
if createdSamplingParams && len(tokenMap) == 0 {
delete(completionRequest, requestFieldSamplingParams)
}
// WRITE mode: backfill the decode-side kv_transfer_params fields that
// vLLM's request_finished response does not echo back, sourcing the
// pod-local values from sidecar config.
if s.config.MoRIIOWriteMode {
if dKVParams, ok := pKVTransferParams.(map[string]any); ok {
if _, present := dKVParams[requestFieldTransferID]; !present {
// MoRI-IO requires transfer_id to carry the "tx" prefix.
dKVParams[requestFieldTransferID] = "tx" + uuidStr
}
if _, present := dKVParams[requestFieldRemoteNotifyPort]; !present {
dKVParams[requestFieldRemoteNotifyPort] = s.config.MoRIIODecodeNotifyPort
}
if _, present := dKVParams[requestFieldRemoteDPRank]; !present {
dKVParams[requestFieldRemoteDPRank] = pickDPRank(uuidStr, s.config.MoRIIODPSize)
dKVParams[requestFieldRemoteDPRankOverride] = true
}
if _, present := dKVParams[requestFieldRemoteHandshakePort]; !present {
dKVParams[requestFieldRemoteHandshakePort] = s.config.MoRIIODecodeHandshakePort
}
// Wide-EP fields for decode-side handshake loop
if _, present := dKVParams["remote_dp_size"]; !present {
dKVParams["remote_dp_size"] = s.config.MoRIIODPSize
}
// Wide-EP fan-out (decode leg, serial path): remote_hosts must be the
// PREFILL-side pod IPs so decode fans out handshakes across prefill pods.
if len(s.config.MoRIIORemoteHosts) > 0 {
if _, present := dKVParams["remote_hosts"]; !present {
hosts := make([]any, len(s.config.MoRIIORemoteHosts))
for i, h := range s.config.MoRIIORemoteHosts {
hosts[i] = h
}
dKVParams["remote_hosts"] = hosts
}
if s.config.MoRIIODPSizeLocal > 0 {
if _, present := dKVParams["remote_dp_size_local"]; !present {
dKVParams["remote_dp_size_local"] = s.config.MoRIIODPSizeLocal
}
}
}
}
}
delete(completionRequest, requestFieldECTransferParams)
completionRequest[requestFieldKVTransferParams] = pKVTransferParams
dbody, err := json.Marshal(completionRequest)
if err != nil {
if err := errorJSONInvalid(err, w); err != nil {
s.logger.Error(err, "failed to send error response to client")
}
return
}
dreq.Body = io.NopCloser(bytes.NewReader(dbody))
dreq.ContentLength = int64(len(dbody))
// 2. Forward to local decoder.
s.logger.V(5).Info("sending request to decoder", "body", string(dbody))
decodeWriter, finalizeDecodeWriter := newCachedTokensResponseWriterWithFinalize(w, pCachedTokens)
dataParallelUsed := s.forwardDataParallel && s.dataParallelHandler(decodeWriter, dreq)
decodeSpan.SetAttributes(attribute.Bool("llm_d.pd_proxy.decode.data_parallel", dataParallelUsed))
if !dataParallelUsed {
s.logger.V(4).Info("sending request to decoder", "to", s.config.DecoderURL.Host)
decodeSpan.SetAttributes(attribute.String("llm_d.pd_proxy.decode.target", s.config.DecoderURL.Host))
s.dispatchDecode(decodeWriter, dreq, completionRequest)
}
if err := finalizeDecodeWriter(); err != nil {
s.logger.Error(err, "failed to flush cached token response writer")
decodeSpan.SetStatus(codes.Error, "failed to flush cached token response writer")
return
}
decodeDuration := time.Since(decodeStart)
decodeSpan.SetAttributes(attribute.Float64("llm_d.pd_proxy.decode.duration_ms", float64(decodeDuration.Milliseconds())))
// Calculate end-to-end P/D timing metrics.
// True TTFT captures time from gateway request start to decode start, including
// gateway routing, scheduling, prefill, and coordination overhead that
// per-instance vLLM metrics miss.
if currentSpan := trace.SpanFromContext(ctx); currentSpan.SpanContext().IsValid() {
var totalDuration time.Duration
var trueTTFT time.Duration
if requestStartValue := ctx.Value(requestStartTimeKey); requestStartValue != nil {
if requestStart, ok := requestStartValue.(time.Time); ok {
totalDuration = time.Since(requestStart)
trueTTFT = decodeStart.Sub(requestStart)
}
}
coordinatorOverhead := decodeStart.Sub(prefillStart.Add(prefillDuration))
currentSpan.SetAttributes(
attribute.Float64("llm_d.pd_proxy.total_duration_ms", float64(totalDuration.Milliseconds())),
attribute.Float64("llm_d.pd_proxy.true_ttft_ms", float64(trueTTFT.Milliseconds())),
attribute.Float64("llm_d.pd_proxy.prefill_duration_ms", float64(prefillDuration.Milliseconds())),
attribute.Float64("llm_d.pd_proxy.decode_duration_ms", float64(decodeDuration.Milliseconds())),
attribute.Float64("llm_d.pd_proxy.coordinator_overhead_ms", float64(coordinatorOverhead.Milliseconds())),
)
}
}
// runNIXLProtocolV2WriteParallel is the MoRI-IO WRITE-mode concurrent-dispatch
// path: it builds both the prefill and decode bodies up front (synthesising
// decode's kv_transfer_params from config and prefillPodHostPort) and fires the
// two upstream calls in parallel so decode's block allocation overlaps prefill.
func (s *Server) runNIXLProtocolV2WriteParallel(
w http.ResponseWriter, r *http.Request, original []byte,
completionRequest map[string]any, uuidStr, transferID, prefillPodHostPort string,
) {
s.logger.V(4).Info("running NIXL protocol V2 (concurrent dispatch)",
"url", prefillPodHostPort, "request_id", uuidStr)
tracer := tracing.Tracer()
parentCtx := r.Context()
requestStartedAt := time.Now()
// Snapshot client fields before mutating completionRequest into the prefill
// body; they are restored when building the decode body.
streamValue, streamOk := completionRequest[requestFieldStream]
streamOptionsValue, streamOptionsOk := completionRequest[requestFieldStreamOptions]
maxTokensValue, maxTokensOk := completionRequest[requestFieldMaxTokens]
maxCompletionTokensValue, maxCompletionTokensOk := completionRequest[requestFieldMaxCompletionTokens]
maxOutputTokensValue, maxOutputTokensOk := completionRequest[requestFieldMaxOutputTokens]
// Pin both legs to the same DP rank (kv_transfer_params + HTTP header).
dpRank := pickDPRank(uuidStr, s.config.MoRIIODPSize)
// Build prefill body. remote_host points at the decode pod so prefill can
// RDMA-Write KV there; remote_dp_size gates the decode-side per-DP-rank
// handshake loop for Wide-EP.
completionRequest[requestFieldKVTransferParams] = map[string]any{
requestFieldDoRemoteDecode: true,
requestFieldDoRemotePrefill: false,
requestFieldRemoteEngineID: nil,
requestFieldRemoteBlockIDs: nil,
requestFieldRemoteHost: s.config.MoRIIODecodePodIP,
requestFieldRemotePort: nil,
requestFieldRemoteNotifyPort: s.config.MoRIIODecodeNotifyPort,
requestFieldRemoteDPRank: dpRank,
requestFieldRemoteDPRankOverride: true,
requestFieldRemoteHandshakePort: s.config.MoRIIODecodeHandshakePort,
requestFieldTransferID: transferID,
"tp_size": s.config.MoRIIOTPSize,
"remote_dp_size": s.config.MoRIIODPSize,
}
// Wide-EP fan-out (prefill leg): remote_hosts must be the DECODE-side pod
// IPs so prefill handshakes the right pods. Omitted when unset, falling back
// to the single-host remote_host path.
if len(s.config.MoRIIODecodeHosts) > 0 {
pkv := completionRequest[requestFieldKVTransferParams].(map[string]any)
hosts := make([]any, len(s.config.MoRIIODecodeHosts))
for i, h := range s.config.MoRIIODecodeHosts {
hosts[i] = h
}
pkv["remote_hosts"] = hosts
if s.config.MoRIIODPSizeLocal > 0 {
pkv["remote_dp_size_local"] = s.config.MoRIIODPSizeLocal
}
}
completionRequest[requestFieldStream] = false
delete(completionRequest, requestFieldStreamOptions)
completionRequest[requestFieldMaxTokens] = 1
completionRequest[requestFieldMaxCompletionTokens] = 1
completionRequest[requestFieldMaxOutputTokens] = 1
pbody, err := json.Marshal(completionRequest)
if err != nil {
if err := errorJSONInvalid(err, w); err != nil {
s.logger.Error(err, "failed to send error response to client (concurrent-dispatch marshal P)")
}
return
}
// ---------- Build decode body ----------
// Restore the client's streaming flags and max-token caps.
delete(completionRequest, requestFieldStream)
if streamOk {
completionRequest[requestFieldStream] = streamValue
}
if streamOptionsOk {
completionRequest[requestFieldStreamOptions] = streamOptionsValue
}
delete(completionRequest, requestFieldMaxTokens)
if maxTokensOk {
completionRequest[requestFieldMaxTokens] = maxTokensValue
}
delete(completionRequest, requestFieldMaxCompletionTokens)
if maxCompletionTokensOk {
completionRequest[requestFieldMaxCompletionTokens] = maxCompletionTokensValue
}
delete(completionRequest, requestFieldMaxOutputTokens)
if maxOutputTokensOk {
completionRequest[requestFieldMaxOutputTokens] = maxOutputTokensValue
}
// Synthesise decode-leg kv_transfer_params that the serial path would
// otherwise read from the prefill response. do_remote_prefill must be true:
// it gates the decode-side send_notify_block that prefill's RDMA Write waits on.
prefillHost, _, splitErr := net.SplitHostPort(prefillPodHostPort)
if splitErr != nil {
prefillHost = prefillPodHostPort
}
completionRequest[requestFieldKVTransferParams] = map[string]any{
requestFieldDoRemotePrefill: true,
requestFieldDoRemoteDecode: false,
requestFieldRemoteEngineID: fmt.Sprintf("%s:%d", prefillHost, s.config.MoRIIOPrefillHandshakePort),
// Empty (not nil) since decode allocates its own blocks in WRITE mode.
requestFieldRemoteBlockIDs: []any{},
requestFieldRemoteHost: prefillHost,
requestFieldRemotePort: s.config.MoRIIOPrefillHandshakePort,
requestFieldRemoteNotifyPort: s.config.MoRIIOPrefillNotifyPort,
requestFieldRemoteDPRank: dpRank,
requestFieldRemoteDPRankOverride: true,
requestFieldRemoteHandshakePort: s.config.MoRIIOPrefillHandshakePort,
requestFieldTransferID: transferID,
"tp_size": s.config.MoRIIOTPSize,
"remote_dp_size": s.config.MoRIIODPSize,
}
// Wide-EP fan-out (decode leg): the opposite host list, the PREFILL-side
// pod IPs. A multi-pod deployment must set both host flags.
if len(s.config.MoRIIORemoteHosts) > 0 {
dkv := completionRequest[requestFieldKVTransferParams].(map[string]any)
hosts := make([]any, len(s.config.MoRIIORemoteHosts))
for i, h := range s.config.MoRIIORemoteHosts {
hosts[i] = h
}
dkv["remote_hosts"] = hosts
if s.config.MoRIIODPSizeLocal > 0 {
dkv["remote_dp_size_local"] = s.config.MoRIIODPSizeLocal
}
}
dbody, err := json.Marshal(completionRequest)
if err != nil {
if err := errorJSONInvalid(err, w); err != nil {
s.logger.Error(err, "failed to send error response to client (concurrent-dispatch marshal D)")
}
return
}
// ---------- Fire prefill + decode in parallel ----------
prefillHandler, err := s.prefillerProxyHandler(prefillPodHostPort)
if err != nil {
if err := errorBadGateway(err, w); err != nil {
s.logger.Error(err, "failed to send error response to client (concurrent-dispatch P handler init)")
}
return
}
// Build cloned requests under separate contexts so each carries its own
// span lineage and either side can be observed/cancelled independently
// without affecting the other.
pCtx, prefillSpan := tracer.Start(parentCtx, "llm_d.pd_proxy.prefill",
trace.WithSpanKind(trace.SpanKindInternal),
)
prefillSpan.SetAttributes(
attribute.String("llm_d.pd_proxy.request_id", uuidStr),
attribute.String("llm_d.pd_proxy.prefill_target", prefillPodHostPort),
attribute.String("llm_d.pd_proxy.connector", "nixlv2"),
attribute.Bool("llm_d.pd_proxy.parallel_dispatch", true),
)
preq := r.Clone(pCtx)
preq.Header.Set(requestHeaderRequestID, uuidStr)
if s.config.MoRIIODPSize > 1 {
preq.Header.Set(requestHeaderDataParallelRank, strconv.Itoa(dpRank))
}
preq.Body = io.NopCloser(bytes.NewReader(pbody))
preq.ContentLength = int64(len(pbody))
dCtx, decodeSpan := tracer.Start(parentCtx, "llm_d.pd_proxy.decode",
trace.WithSpanKind(trace.SpanKindInternal),
)
defer decodeSpan.End()
decodeSpan.SetAttributes(
attribute.String("llm_d.pd_proxy.request_id", uuidStr),
attribute.String("llm_d.pd_proxy.connector", "nixlv2"),
attribute.Bool("llm_d.pd_proxy.parallel_dispatch", true),
)
dreq := r.Clone(dCtx)
dreq.Header.Set(requestHeaderRequestID, uuidStr)
if s.config.MoRIIODPSize > 1 {
dreq.Header.Set(requestHeaderDataParallelRank, strconv.Itoa(dpRank))
}
dreq.Body = io.NopCloser(bytes.NewReader(dbody))
dreq.ContentLength = int64(len(dbody))
s.logger.V(5).Info("concurrent-dispatch prefill request body", "body", string(pbody))
s.logger.V(5).Info("concurrent-dispatch decode request body", "body", string(dbody))
var wg sync.WaitGroup
wg.Add(2)
// Prefill goroutine: response body is discarded; we only observe the
// status code for telemetry / fallback decisions.
var prefillStatus int
var prefillBody string
prefillStartedAt := time.Now()
go func() {
defer wg.Done()
defer prefillSpan.End()
pw := &bufferedResponseWriter{}
prefillHandler.ServeHTTP(pw, preq)
prefillStatus = pw.statusCode
prefillBody = pw.buffer.String()
prefillSpan.SetAttributes(
attribute.Int("llm_d.pd_proxy.prefill.status_code", pw.statusCode),
attribute.Float64("llm_d.pd_proxy.prefill.duration_ms", float64(time.Since(prefillStartedAt).Milliseconds())),
)
if isHTTPError(pw.statusCode) {
prefillSpan.SetStatus(codes.Error, "prefill request failed")
s.logger.Error(nil, "concurrent-dispatch prefill returned error status",
"status", pw.statusCode, "request_id", uuidStr, "body", pw.buffer.String())
}
}()
// Decode goroutine: streams directly to the client's ResponseWriter.
// dataParallelHandler may steal the request and dispatch to another
// data-parallel replica; preserve that semantics.
decodeStartedAt := time.Now()
go func() {
defer wg.Done()
dataParallelUsed := s.forwardDataParallel && s.dataParallelHandler(w, dreq)
decodeSpan.SetAttributes(attribute.Bool("llm_d.pd_proxy.decode.data_parallel", dataParallelUsed))
if !dataParallelUsed {
decodeSpan.SetAttributes(attribute.String("llm_d.pd_proxy.decode.target", s.config.DecoderURL.Host))
s.decoderProxy.ServeHTTP(w, dreq)
}
decodeSpan.SetAttributes(attribute.Float64("llm_d.pd_proxy.decode.duration_ms", float64(time.Since(decodeStartedAt).Milliseconds())))
}()
wg.Wait()
// A failed prefill usually also hangs decode; log it so the cause is visible.
if isHTTPError(prefillStatus) {
s.logger.Info("concurrent-dispatch: prefill failed -- decode may have streamed an error or hung",
"request_id", uuidStr, "p_status", prefillStatus, "p_body_snippet", truncate(prefillBody, 256))
}
if currentSpan := trace.SpanFromContext(parentCtx); currentSpan.SpanContext().IsValid() {
var totalDuration time.Duration
if requestStartValue := parentCtx.Value(requestStartTimeKey); requestStartValue != nil {
if requestStart, ok := requestStartValue.(time.Time); ok {
totalDuration = time.Since(requestStart)
}
}
currentSpan.SetAttributes(
attribute.Float64("llm_d.pd_proxy.total_duration_ms", float64(totalDuration.Milliseconds())),
attribute.Float64("llm_d.pd_proxy.parallel_window_ms", float64(time.Since(requestStartedAt).Milliseconds())),
attribute.Bool("llm_d.pd_proxy.parallel_dispatch", true),
)
}
_ = original // kept for signature symmetry with the strictly-serial path
}
// truncate shortens s to at most n characters, appending "..." if truncated.
func truncate(s string, n int) string {
if len(s) <= n {
return s
}
return s[:n] + "..."
}