Documentation
¶
Index ¶
- Constants
- Variables
- func GetDisaggRequestID(machineID int64) int64
- func NewPDRequestID(requestID string) string
- func Register(h EngineHandler)
- func SetConnectorTypeFunc(fn func() string)
- func ValidEngineNames() []string
- func ValidateRequest(body []byte, h EngineHandler) error
- func ValidateSGLangRequest(body []byte) error
- func ValidateTRTMachineID(machineID int64) error
- type AsyncDispatchPolicy
- type DefaultHandler
- func (h *DefaultHandler) AugmentPrefillRequest(_ *types.RoutingContext, _ *v1.Pod, body []byte) ([]byte, error)
- func (h *DefaultHandler) ControlledFields() []string
- func (h *DefaultHandler) IsAsync() bool
- func (h *DefaultHandler) MergePrefillResponse(_ *types.RoutingContext, _ []byte, _ *v1.Pod) error
- func (h *DefaultHandler) Name() string
- type EngineHandler
- type InvalidRequestError
- type SGLangHandler
- func (h *SGLangHandler) AugmentPrefillRequest(routingCtx *types.RoutingContext, pod *v1.Pod, body []byte) ([]byte, error)
- func (h *SGLangHandler) ControlledFields() []string
- func (h *SGLangHandler) IsAsync() bool
- func (h *SGLangHandler) MergePrefillResponse(_ *types.RoutingContext, _ []byte, _ *v1.Pod) error
- func (h *SGLangHandler) Name() string
- type TRTLLMHandler
- func (h *TRTLLMHandler) AsyncDispatch() AsyncDispatchPolicy
- func (h *TRTLLMHandler) AugmentPrefillRequest(routingCtx *types.RoutingContext, pod *v1.Pod, body []byte) ([]byte, error)
- func (h *TRTLLMHandler) ControlledFields() []string
- func (h *TRTLLMHandler) IsAsync() bool
- func (h *TRTLLMHandler) MergePrefillResponse(routingCtx *types.RoutingContext, prefillResponse []byte, prefillPod *v1.Pod) error
- func (h *TRTLLMHandler) Name() string
- type TRTServerInfo
- type TRTServerInfoCache
- type TRTServerInfoProvider
- type VLLMHandler
- func (h *VLLMHandler) AugmentPrefillRequest(routingCtx *types.RoutingContext, pod *v1.Pod, body []byte) ([]byte, error)
- func (h *VLLMHandler) ControlledFields() []string
- func (h *VLLMHandler) IsAsync() bool
- func (h *VLLMHandler) MergePrefillResponse(routingCtx *types.RoutingContext, prefillResponse []byte, pod *v1.Pod) error
- func (h *VLLMHandler) Name() string
Constants ¶
const ( // TRTMachineIDBits is the width of the machine-ID field in snowflake disagg request IDs. TRTMachineIDBits = 10 // TRTMinGlobalID is the minimum global disagg ID; values below this are treated as local by TRT-LLM. TRTMinGlobalID = int64(1) << 42 )
Snowflake-style disagg request ID constants for TensorRT-LLM PD routing. Layout: [timestamp(41b)][machineID(10b)][counter(12b)] The modulo rotation guarantees result >= TRTMinGlobalID so TRT-LLM's executor treats it as a global (cross-worker) disagg ID rather than a local one.
const ( TRTContextFirst = "context_first" TRTGenerationFirst = "generation_first" )
Variables ¶
var DefaultAsyncDispatchPolicy = AsyncDispatchPolicy{AbortDecode: true}
DefaultAsyncDispatchPolicy is the contract of an async engine that does not implement AsyncDispatchHandler: SGLang today.
Functions ¶
func GetDisaggRequestID ¶
GetDisaggRequestID generates a snowflake-style ID shared between a prefill and its decode request so TRT-LLM can correlate the KV-cache entry.
func NewPDRequestID ¶ added in v0.8.0
NewPDRequestID derives the engine-visible rid of one PD attempt from the gateway request id. Fixed-width suffix; see the comment above.
func Register ¶
func Register(h EngineHandler)
Register adds h to the global engine registry. Called from init() in each engine file.
func SetConnectorTypeFunc ¶
func SetConnectorTypeFunc(fn func() string)
SetConnectorTypeFunc installs a function that returns the current global KV connector type. Call this once at startup with a closure over the routingalgorithms package-level var.
func ValidEngineNames ¶
func ValidEngineNames() []string
ValidEngineNames returns the sorted list of registered engine names.
func ValidateRequest ¶ added in v0.8.0
func ValidateRequest(body []byte, h EngineHandler) error
ValidateRequest checks that body is a JSON object that does not repeat any gateway-controlled top-level key (pd.CommonControlledFields plus h.ControlledFields()). It returns *InvalidRequestError so the gateway can map the failure to HTTP 400. Route() calls it for every engine before pod selection, so a malformed request can neither pollute selection state nor reach a prefill pod.
func ValidateSGLangRequest ¶ added in v0.8.0
ValidateSGLangRequest checks the request body for conditions that would make SGLang PD routing unsafe or ambiguous. It returns *InvalidRequestError when the request contains duplicate top-level controlled fields or is not a valid JSON object, so the gateway layer can map the error to HTTP 400.
It is the SGLang-specific form of ValidateRequest and is kept for callers that validate outside Route().
func ValidateTRTMachineID ¶
ValidateTRTMachineID returns an error if machineID does not fit in TRTMachineIDBits bits.
Types ¶
type AsyncDispatchPolicy ¶ added in v0.8.0
type AsyncDispatchPolicy struct {
// KeepClientCancel keeps the client's cancellation and deadline on the
// detached prefill call instead of detaching it. TRT-LLM generation-first
// needs it: the engine cancels its inference promise when the HTTP client
// disconnects, so the gateway must let that disconnect through.
KeepClientCancel bool
// AbortDecode sends the engine's native decode abort (SGLang's
// /abort_request) when the prefill leg fails. TRT-LLM has no such endpoint:
// it relies on the gateway failing the Envoy stream instead.
AbortDecode bool
// ResetAfterHeaders resets the client's decode stream when a terminal
// prefill failure is observed after the decode pod started responding.
// TRT-LLM generation-first can send SSE headers before the KV cache is
// available, so headers are not proof that decode is making progress.
ResetAfterHeaders bool
}
AsyncDispatchPolicy describes the parts of the async prefill contract that differ per engine. It exists because IsAsync alone used to mean one contract (detach from the client, abort the decode leg natively, leave an already-streaming response alone), and engines that only share the "fire-and-forget" half of it had to be told apart by their name at every dispatch site. The mode lives on the handler that already knows it, and the executor and the gateway read it once.
func AsyncDispatchPolicyFor ¶ added in v0.8.0
func AsyncDispatchPolicyFor(h EngineHandler) AsyncDispatchPolicy
AsyncDispatchPolicyFor returns the async dispatch policy of h. Handlers that do not implement AsyncDispatchHandler get DefaultAsyncDispatchPolicy, which keeps their existing behavior without a name-based special case at the call site.
type DefaultHandler ¶
type DefaultHandler struct{}
DefaultHandler is a no-op handler used for unknown or future engines. It performs a synchronous prefill with no request augmentation or response processing, which matches the original "default" branch behaviour.
func (*DefaultHandler) AugmentPrefillRequest ¶
func (h *DefaultHandler) AugmentPrefillRequest(_ *types.RoutingContext, _ *v1.Pod, body []byte) ([]byte, error)
func (*DefaultHandler) ControlledFields ¶ added in v0.8.0
func (h *DefaultHandler) ControlledFields() []string
func (*DefaultHandler) IsAsync ¶
func (h *DefaultHandler) IsAsync() bool
func (*DefaultHandler) MergePrefillResponse ¶
func (h *DefaultHandler) MergePrefillResponse(_ *types.RoutingContext, _ []byte, _ *v1.Pod) error
func (*DefaultHandler) Name ¶
func (h *DefaultHandler) Name() string
type EngineHandler ¶
type EngineHandler interface {
// Name returns the engine identifier (e.g. "vllm", "sglang", "trtllm").
Name() string
// IsAsync returns true when the engine's prefill handshake is
// self-coordinating and should be fired in a goroutine (e.g. SGLang).
IsAsync() bool
// AugmentPrefillRequest returns the prefill request body derived from
// body, which is the client request already validated as a JSON object.
// The common prefill constraints (max_tokens=1, stream=false, …) are
// applied by the caller afterwards. Handlers that must also change the
// decode body (e.g. SGLang bootstrap fields) update routingCtx.ReqBody;
// on error routingCtx.ReqBody must be left unchanged.
AugmentPrefillRequest(routingCtx *types.RoutingContext, pod *v1.Pod, body []byte) ([]byte, error)
// MergePrefillResponse injects engine-specific data from the raw prefill
// response into routingCtx.ReqBody before the decode pod receives it.
MergePrefillResponse(routingCtx *types.RoutingContext, prefillResponse []byte, pod *v1.Pod) error
// ControlledFields returns the top-level keys, in addition to
// pd.CommonControlledFields, that AugmentPrefillRequest or
// MergePrefillResponse may write. ValidateRequest rejects client bodies
// that contain any of them more than once.
ControlledFields() []string
}
EngineHandler encapsulates engine-specific behaviour for PD-disaggregated prefill: how to derive the outbound prefill request body and how to merge the prefill response back into the decode request body.
Both methods operate on raw JSON bytes and must only add, replace or delete top-level keys (or keys inside the small gateway-owned objects such as kv_transfer_params / disaggregated_params). They must never round-trip the body through map[string]any: that re-serialises nested objects (messages, tools, …) in random key order, which changes prompt_token_ids between the prefill and decode requests and across repeated requests, defeating KV-cache prefix reuse.
func Resolve ¶
func Resolve(name string) EngineHandler
Resolve returns the registered EngineHandler for name. Returns DefaultHandler for unknown engines so the caller always gets a valid handler without needing to check for nil.
type InvalidRequestError ¶ added in v0.8.0
type InvalidRequestError struct {
Message string
}
InvalidRequestError represents a client-side error (malformed request body) that should result in an HTTP 400 response rather than a 5xx. Gateway layers should use errors.As to detect this type and map it to BadRequest.
func (*InvalidRequestError) Error ¶ added in v0.8.0
func (e *InvalidRequestError) Error() string
type SGLangHandler ¶
type SGLangHandler struct{}
SGLangHandler implements EngineHandler for SGLang. SGLang uses a bootstrap handshake (bootstrap_host/port/room) to coordinate KV transfer between prefill and decode pods out-of-band, so the prefill fires asynchronously (IsAsync returns true).
func (*SGLangHandler) AugmentPrefillRequest ¶
func (h *SGLangHandler) AugmentPrefillRequest( routingCtx *types.RoutingContext, pod *v1.Pod, body []byte, ) ([]byte, error)
AugmentPrefillRequest overwrites bootstrap_host, bootstrap_port, a random bootstrap_room and the gateway-owned rid on the client body with sjson, so that every nested field (messages, tools, …) is preserved at the byte level. The resulting decode body is stored in routingCtx.ReqBody and returned as the prefill base body; the caller applies the common prefill constraints on top of it, so both legs carry the same rid. On error routingCtx.ReqBody is left unchanged.
ValidateRequest is already called in Route() before this method is invoked, so duplicate controlled fields have been rejected by then.
func (*SGLangHandler) ControlledFields ¶ added in v0.8.0
func (h *SGLangHandler) ControlledFields() []string
ControlledFields returns the SGLang bootstrap keys; the common prefill control keys are added by ValidateRequest.
func (*SGLangHandler) IsAsync ¶
func (h *SGLangHandler) IsAsync() bool
func (*SGLangHandler) MergePrefillResponse ¶
func (h *SGLangHandler) MergePrefillResponse( _ *types.RoutingContext, _ []byte, _ *v1.Pod, ) error
MergePrefillResponse is a no-op for SGLang: the bootstrap handshake is self-coordinating, so no data from the prefill response needs to be injected into the decode request.
func (*SGLangHandler) Name ¶
func (h *SGLangHandler) Name() string
type TRTLLMHandler ¶
type TRTLLMHandler struct {
// contains filtered or unexported fields
}
TRTLLMHandler implements EngineHandler for TensorRT-LLM. Configuration is immutable and router-scoped; the zero value retains context-first behavior.
func NewTRTLLMHandler ¶ added in v0.8.0
func NewTRTLLMHandler(scheduleStyle string, serverInfo TRTServerInfoProvider) (*TRTLLMHandler, error)
NewTRTLLMHandler validates the configured schedule before any request is sent. The registry's default handler is never mutated by a router's configuration.
func (*TRTLLMHandler) AsyncDispatch ¶ added in v0.8.0
func (h *TRTLLMHandler) AsyncDispatch() AsyncDispatchPolicy
AsyncDispatch returns the generation-first dispatch contract; context-first keeps the default one and never dispatches a detached prefill leg. TRT-LLM generation-first keeps the client's cancellation (the engine aborts its promise on disconnect), has no native decode abort, and resets the decode stream on a terminal prefill failure even after response headers.
func (*TRTLLMHandler) AugmentPrefillRequest ¶
func (h *TRTLLMHandler) AugmentPrefillRequest( routingCtx *types.RoutingContext, pod *v1.Pod, body []byte, ) ([]byte, error)
AugmentPrefillRequest replaces disaggregated_params with request_type="context_only" and a unique disagg_request_id generated from the machine snowflake ID, in a single top-level edit. The ID is written as an integer literal, so it never goes through float64 and keeps full int64 precision.
func (*TRTLLMHandler) ControlledFields ¶ added in v0.8.0
func (h *TRTLLMHandler) ControlledFields() []string
func (*TRTLLMHandler) IsAsync ¶
func (h *TRTLLMHandler) IsAsync() bool
func (*TRTLLMHandler) MergePrefillResponse ¶
func (h *TRTLLMHandler) MergePrefillResponse( routingCtx *types.RoutingContext, prefillResponse []byte, prefillPod *v1.Pod, ) error
MergePrefillResponse injects TensorRT-LLM disaggregated_params from the prefill response into routingCtx.ReqBody so the decode worker can resume generation from the pre-filled KV cache.
disaggregated_params is looked up at the top level first, then under choices[0]. Its fragment is copied verbatim from the response with request_type overridden to "generation_only", so large integer fields such as disagg_request_id / ctx_request_id keep their exact value. When the response includes prompt_token_ids they are routed into the decode body by request path: "prompt" for /v1/completions, "prompt_token_ids" for /v1/chat/completions.
func (*TRTLLMHandler) Name ¶
func (h *TRTLLMHandler) Name() string
type TRTServerInfo ¶ added in v0.8.0
type TRTServerInfo struct {
ContextInfoEndpoint string `json:"ctx_info_endpoint"`
ContextDPRank int `json:"ctx_dp_rank"`
EncodedOpaqueState string `json:"encoded_opaque_state,omitempty"`
}
TRTServerInfo is the worker-scoped metadata used to bootstrap a generation request without waiting for the context response. DPRank is attention DP, not tensor parallel rank or a Kubernetes replica index.
type TRTServerInfoCache ¶ added in v0.8.0
type TRTServerInfoCache struct {
// contains filtered or unexported fields
}
TRTServerInfoCache loads workers lazily, since pods need not exist when the router is constructed. Entries expire one at a time on lookup and refresh; no background worker or informer lifecycle is required. Concurrent misses for one incarnation are coalesced, and a canceled waiter cannot cancel another request's lookup.
func NewTRTServerInfoCache ¶ added in v0.8.0
func NewTRTServerInfoCache(client *http.Client) *TRTServerInfoCache
func (*TRTServerInfoCache) Get ¶ added in v0.8.0
func (c *TRTServerInfoCache) Get(ctx context.Context, pod *v1.Pod) (TRTServerInfo, error)
type TRTServerInfoProvider ¶ added in v0.8.0
TRTServerInfoProvider resolves metadata for the exact selected context worker. Implementations must be safe for concurrent requests.
type VLLMHandler ¶
type VLLMHandler struct{}
VLLMHandler implements EngineHandler for vLLM. KV-transfer augmentation is fully delegated to the KVTransferAgent resolved for the prefill pod, so adding a new connector type (e.g. Mooncake) requires no changes here.
func (*VLLMHandler) AugmentPrefillRequest ¶
func (h *VLLMHandler) AugmentPrefillRequest( routingCtx *types.RoutingContext, pod *v1.Pod, body []byte, ) ([]byte, error)
func (*VLLMHandler) ControlledFields ¶ added in v0.8.0
func (h *VLLMHandler) ControlledFields() []string
ControlledFields returns the union of every registered KV transfer agent's controlled fields. The agent is only resolved per prefill pod after pod selection, while validation runs before it, so the union is the set that is safe to reject regardless of which connector the request ends up on.
func (*VLLMHandler) IsAsync ¶
func (h *VLLMHandler) IsAsync() bool
func (*VLLMHandler) MergePrefillResponse ¶
func (h *VLLMHandler) MergePrefillResponse( routingCtx *types.RoutingContext, prefillResponse []byte, pod *v1.Pod, ) error
func (*VLLMHandler) Name ¶
func (h *VLLMHandler) Name() string