forked from llm-d/llm-d-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnector_mooncake.go
More file actions
283 lines (242 loc) · 9.79 KB
/
Copy pathconnector_mooncake.go
File metadata and controls
283 lines (242 loc) · 9.79 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
/*
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 (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"time"
"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/logging"
"github.com/llm-d/llm-d-router/pkg/common/observability/tracing"
reqcommon "github.com/llm-d/llm-d-router/pkg/common/request"
)
const mooncakeBootstrapTimeout = 5 * time.Second // set to same value as the other timeout on vllm
const mooncakeDataParallelRankHeader = "X-data-parallel-rank" // to send rank id in header to prefill
func (s *Server) handleMooncake(w http.ResponseWriter, r *http.Request, prefillPodHostPort string) {
s.logger.V(logging.DEBUG).Info("running Mooncake protocol", "url", prefillPodHostPort)
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
}
bootstrapAddr := fmt.Sprintf("http://%s:%d", extractHost(prefillPodHostPort), s.config.MooncakeBootstrapPort)
engineMap, err := s.getMooncakeEngineMap(r.Context(), prefillPodHostPort, bootstrapAddr)
if err != nil {
s.logger.Error(err, "failed to query mooncake engine ID", "bootstrap_addr", bootstrapAddr)
if err := errorBadGateway(err, w); err != nil {
s.logger.Error(err, "failed to send error response to client")
}
return
}
// golang map randomnize key(rankid) order to spread prefill load for multi-ranks.
var dpRank, engineID string
for dpRank, engineID = range engineMap {
break
}
transferID := "xfer-" + newUUID()
s.logger.V(logging.TRACE).Info("mooncake protocol info",
"transfer_id", transferID,
"bootstrap_addr", bootstrapAddr,
"dp_rank", dpRank,
"engine_id", engineID)
// Build prefill request body
prefillData := make(map[string]any)
for k, v := range requestData {
prefillData[k] = v
}
prefillData[requestFieldKVTransferParams] = map[string]any{
requestFieldDoRemotePrefill: false,
requestFieldDoRemoteDecode: true,
requestFieldTransferID: transferID,
}
// update fields from original body; return asap.
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
}
// Guarded: stringifying the body allocates a copy per request even when
// TRACE is disabled.
if trace := s.logger.V(logging.TRACE); trace.Enabled() {
trace.Info("Prefill request", "body", string(prefillBody))
}
// Build decode request body
decodeData := make(map[string]any)
for k, v := range requestData {
decodeData[k] = v
}
decodeData[requestFieldKVTransferParams] = map[string]any{
requestFieldDoRemotePrefill: true,
requestFieldDoRemoteDecode: false,
requestFieldTransferID: transferID,
requestFieldRemoteBootstrapAddr: bootstrapAddr,
requestFieldRemoteEngineID: engineID,
}
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 trace := s.logger.V(logging.TRACE); trace.Enabled() {
trace.Info("Decode request", "body", string(decodeBody))
}
s.handleMooncakeConcurrentRequests(w, r, prefillBody, decodeBody, prefillPodHostPort, dpRank)
}
// getMooncakeEngineMap returns the dp_rank -> engine_id mapping for the given prefill, querying the bootstrap server on first use and caching it.
func (s *Server) getMooncakeEngineMap(ctx context.Context, prefillHostPort, bootstrapAddr string) (map[string]string, error) {
if m, ok := s.mooncakeEngineIDs.Get(prefillHostPort); ok {
return m, nil
}
ctx, cancel := context.WithTimeout(ctx, mooncakeBootstrapTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, bootstrapAddr+"/query", nil)
if err != nil {
return nil, fmt.Errorf("failed to get mooncake bootstrap request: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to query bootstrap endpoint: %w", err)
}
defer resp.Body.Close() //nolint:errcheck
if isHTTPError(resp.StatusCode) {
return nil, fmt.Errorf("bootstrap endpoint returned status %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read bootstrap response: %w", err)
}
// response format: {"0": {"engine_id": "...", ...}, "1": {...}}
// key is dp_ranks; each rank has its own engine_id.
var data map[string]map[string]any
if err := json.Unmarshal(body, &data); err != nil {
return nil, fmt.Errorf("failed to parse bootstrap response: %w", err)
}
engineMap := make(map[string]string, len(data))
for dpRank, entry := range data {
if id, ok := entry["engine_id"].(string); ok {
engineMap[dpRank] = id
}
}
if len(engineMap) == 0 {
return nil, errors.New("engine_id not found in bootstrap response")
}
// add the full map into LRU
s.mooncakeEngineIDs.Add(prefillHostPort, engineMap)
return engineMap, nil
}
func (s *Server) handleMooncakeConcurrentRequests(w http.ResponseWriter, r *http.Request, prefillBody, decodeBody []byte, prefillHost, dpRank 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)
// Route prefill to the same DP rank whose engine_id was given to decode, so the
// KV it produces lands on the engine decode pulls from. No-op for a single rank.
prefillReq.Header.Set(mooncakeDataParallelRankHeader, dpRank)
// Prefill runs in a goroutine: only populates KV cache, response is discarded.
// Decode runs on the main thread: writes the actual response back to the client 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", KVConnectorMooncake),
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")
}
}()
// buffered writer captures response for status check only, not sent to client
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.TRACE).Info("mooncake 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", KVConnectorMooncake),
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),
)
// Calculate end-to-end P/D timing metrics for concurrent P/D.
// True TTFT captures time from gateway request start to decode start.
// In Mooncake's concurrent mode, 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),
)
}
}