forked from llm-d/llm-d-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnector_p2p.go
More file actions
288 lines (253 loc) · 11.1 KB
/
Copy pathconnector_p2p.go
File metadata and controls
288 lines (253 loc) · 11.1 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
/*
Copyright 2026 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 (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
logging "github.com/llm-d/llm-d-router/pkg/common/observability/logging"
"github.com/llm-d/llm-d-router/pkg/common/observability/tracing"
reqcommon "github.com/llm-d/llm-d-router/pkg/common/request"
)
// handleP2P implements the vLLM OffloadingConnector P2P orchestration contract. The
// prefiller stores KV under a kv_request_id with no peer address; the decoder
// pulls it using the prefiller's OffloadingConnector P2P tier host/port. Both legs are
// dispatched concurrently: the connector parks any KV blocks stored before the
// decoder's fetch binds the session, so ordering between the legs is safe.
func (s *Server) handleP2P(w http.ResponseWriter, r *http.Request, prefillPodHostPort, kvCacheSource string) {
body, err := io.ReadAll(r.Body)
if err != nil {
if err := errorJSONInvalid(fmt.Errorf("failed to read request body: %w", err), w); err != nil {
s.logger.Error(err, "failed to send error response to client")
}
return
}
var requestData map[string]any
if err := json.Unmarshal(body, &requestData); err != nil {
if err := errorJSONInvalid(err, w); err != nil {
s.logger.Error(err, "failed to send error response to client")
}
return
}
kvRequestID := newUUID()
s.logger.Info("running P2P protocol",
"prefill_host", extractHost(prefillPodHostPort),
"kv_request_id", kvRequestID,
"p2p_connector_port", s.config.P2PConnectorPort)
// Prefill leg: store KV under kv_request_id, no peer address. Capped to a
// single output token so the prefiller returns as soon as KV is stored.
prefillData := make(map[string]any, len(requestData)+1)
for k, v := range requestData {
prefillData[k] = v
}
prefillKVParams := map[string]any{
requestFieldP2PDecodeParams: map[string]any{
requestFieldKVRequestID: kvRequestID,
},
}
s.addP2PPullToPrefill(prefillKVParams, kvCacheSource, prefillPodHostPort)
prefillData[requestFieldKVTransferParams] = prefillKVParams
reqcommon.PrimeSingleTokenRequest(prefillData, requestData)
prefillBody, err := json.Marshal(prefillData)
if err != nil {
if err := errorJSONInvalid(err, w); err != nil {
s.logger.Error(err, "failed to send error response to client")
}
return
}
if v := s.logger.V(logging.TRACE); v.Enabled() {
v.Info("prefill request body", "body", string(prefillBody))
}
// Decode leg: pull KV from the prefiller's OffloadingConnector P2P tier. Original body
// (streaming, token limits) is preserved.
decodeData := make(map[string]any, len(requestData)+1)
for k, v := range requestData {
decodeData[k] = v
}
decodeData[requestFieldKVTransferParams] = map[string]any{
requestFieldP2PPrefillParams: map[string]any{
requestFieldKVRequestID: kvRequestID,
requestFieldRemoteHost: extractHost(prefillPodHostPort),
requestFieldRemotePort: s.config.P2PConnectorPort,
},
}
decodeBody, err := json.Marshal(decodeData)
if err != nil {
if err := errorJSONInvalid(err, w); err != nil {
s.logger.Error(err, "failed to send error response to client")
}
return
}
if v := s.logger.V(logging.TRACE); v.Enabled() {
v.Info("decode request body", "body", string(decodeBody))
}
s.handleP2PConcurrentRequests(w, r, prefillBody, decodeBody, prefillPodHostPort)
}
func (s *Server) handleP2PConcurrentRequests(w http.ResponseWriter, r *http.Request, prefillBody, decodeBody []byte, prefillHost string) {
tracer := tracing.Tracer(tracerScope)
ctx := r.Context()
// WithoutCancel for prefill so it isn't aborted when the decode response finishes first.
prefillReq := cloneRequestWithBody(context.WithoutCancel(ctx), r, prefillBody)
decodeReq := cloneRequestWithBody(ctx, r, decodeBody)
// Prefill runs in a goroutine: only stores KV, response is discarded.
// Decode runs on the main thread: writes the actual response back via w.
ctx, prefillSpan := tracer.Start(ctx, "llm_d.pd_proxy.prefill",
trace.WithSpanKind(trace.SpanKindInternal),
)
prefillSpan.SetAttributes(
attribute.String("llm_d.pd_proxy.prefill_target", prefillHost),
attribute.String("llm_d.pd_proxy.connector", KVConnectorOffloading),
attribute.Bool("llm_d.pd_proxy.prefill.async", true),
)
prefillStart := time.Now()
prefillHandler, err := s.prefillerProxyHandler(prefillHost)
if err != nil {
prefillSpan.SetStatus(codes.Error, "failed to create prefill handler")
prefillSpan.End()
if err := errorBadGateway(err, w); err != nil {
s.logger.Error(err, "failed to send error response to client")
}
return
}
go func() {
defer prefillSpan.End()
defer func() {
if rec := recover(); rec != nil && rec != http.ErrAbortHandler {
s.logger.Error(fmt.Errorf("panic: %v", rec), "panic in prefill request")
}
}()
pw := &bufferedResponseWriter{}
prefillHandler.ServeHTTP(pw, prefillReq)
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) {
prefillSpan.SetStatus(codes.Error, "prefill request failed")
}
s.logger.V(logging.DEBUG).Info("p2p prefill request completed", "status", pw.statusCode)
}()
// Decode Stage
ctx, decodeSpan := tracer.Start(ctx, "llm_d.pd_proxy.decode",
trace.WithSpanKind(trace.SpanKindInternal),
)
defer decodeSpan.End()
decodeSpan.SetAttributes(
attribute.String("llm_d.pd_proxy.connector", KVConnectorOffloading),
attribute.Bool("llm_d.pd_proxy.decode.concurrent_with_prefill", true),
)
decodeStart := time.Now()
decodeReq = decodeReq.WithContext(ctx)
s.decoderProxy.ServeHTTP(w, decodeReq)
decodeDuration := time.Since(decodeStart)
decodeSpan.SetAttributes(
attribute.Float64("llm_d.pd_proxy.decode.duration_ms", float64(decodeDuration.Milliseconds())),
attribute.String("llm_d.pd_proxy.decode.target", s.config.DecoderURL.Host),
)
// End-to-end P/D timing. True TTFT captures time from gateway request start
// to decode start; prefill duration is tracked in the async prefill span.
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)
}
}
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.decode_duration_ms", float64(decodeDuration.Milliseconds())),
attribute.Bool("llm_d.pd_proxy.concurrent_pd", true),
)
}
}
// p2pPullAvailable reports whether this deployment can pull cached prefix over
// the OffloadingConnector P2P tier. That tier is the PD connector itself when
// KVConnector is offloading, or is composed alongside NIXL via MultiConnector
// (declared with --enable-p2p-pull) when the PD connector is NIXLv2. On any
// other connector --enable-p2p-pull has no effect, since no MultiConnector
// routes the p2p params to an OffloadingConnector.
func (s *Server) p2pPullAvailable() bool {
return s.config.KVConnector == KVConnectorOffloading ||
(s.config.EnableP2PPull && s.config.KVConnector == KVConnectorNIXLV2)
}
// addP2PPullToPrefill adds the OffloadingConnector p2p pull block to a prefill
// leg's kv_transfer_params so the prefiller pulls cached prefix from
// kvCacheSource while keeping its own computed blocks available for the
// decoder. It is a no-op when no source is set or the source resolves to the
// prefiller itself, since there is nothing to pull from oneself. The p2p key
// composes with NIXL params: vLLM's MultiConnector routes it to the
// OffloadingConnector and the NIXL fields to the NixlConnector.
func (s *Server) addP2PPullToPrefill(prefillKVParams map[string]any, kvCacheSource, prefillPodHostPort string) {
if kvCacheSource != "" && extractHost(kvCacheSource) != extractHost(prefillPodHostPort) {
prefillKVParams[requestFieldP2PParams] = s.p2pSourceParams(kvCacheSource)
}
}
// p2pSourceParams builds the kv_transfer_params.remote_kv_source block for a
// pull from sourceHostPort's OffloadingConnector P2P tier. The kv_request_id is its
// own fresh UUID: in P2P mode it is consumer-side only.
func (s *Server) p2pSourceParams(sourceHostPort string) map[string]any {
return map[string]any{
requestFieldKVRequestID: newUUID(),
requestFieldRemoteHost: extractHost(sourceHostPort),
requestFieldRemotePort: s.config.P2PConnectorPort,
}
}
// decodeWithP2PSource serves a decoder-only request through the local vLLM
// with kv_transfer_params.remote_kv_source injected, so the engine looks up and pulls
// cached prefix blocks from the peer at sourceHostPort instead of recomputing
// them. It replaces any client-supplied kv_transfer_params (the sidecar owns
// that field) and routes through dispatchDecode so chunked decode still
// applies. When sourceHostPort resolves to this pod, injecting would tell the
// engine to pull the prefix it is already computing, so it decodes normally.
func (s *Server) decodeWithP2PSource(w http.ResponseWriter, r *http.Request, sourceHostPort string) {
raw, requestData, ok := s.readJSONBody(r, w)
if !ok {
return
}
if extractHost(sourceHostPort) == os.Getenv("POD_IP") {
s.logger.V(logging.DEBUG).Info("KV cache source is the local pod, skipping p2p injection",
"source", sourceHostPort)
s.dispatchDecode(w, cloneRequestWithBody(r.Context(), r, raw), requestData)
return
}
p2pParams := s.p2pSourceParams(sourceHostPort)
// Rebuild kv_transfer_params from scratch: the sidecar owns this field, so
// client-supplied keys are dropped rather than forwarded to vLLM.
requestData[requestFieldKVTransferParams] = map[string]any{requestFieldP2PParams: p2pParams}
s.logger.Info("running P2P source protocol",
"source_host", extractHost(sourceHostPort),
"kv_request_id", p2pParams[requestFieldKVRequestID],
"p2p_connector_port", s.config.P2PConnectorPort)
newBody, err := json.Marshal(requestData)
if err != nil {
if err := errorJSONInvalid(err, w); err != nil {
s.logger.Error(err, "failed to send error response to client")
}
return
}
if v := s.logger.V(logging.TRACE); v.Enabled() {
v.Info("decoder request body with p2p source", "body", string(newBody))
}
s.dispatchDecode(w, cloneRequestWithBody(r.Context(), r, newBody), requestData)
}