utils

package
v0.8.0-rc.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 42 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Copyright 2024 The Aibrix Team.

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.

Index

Constants

View Source
const (
	DefaultLLMEngine = "vllm"
)

https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken

View Source
const (
	NAMESPACE = "aibrix-system"
)

Variables

View Source
var (
	ReplicaSetDeploymentFinder = regexp.MustCompile(`^(.*)-\w+$`)     // Deployment-[random name]
	RayClusterFleetFinder      = regexp.MustCompile(`^(.*)-\w+-\w+$`) // RayClusterFleet-[random name]-[random name]
)
View Source
var (
	RayClusterIdentifier      string = "ray.io/is-ray-node"
	RayClusterIdentifierYes   string = "yes"
	RayClusterNodeType        string = "ray.io/node-type"
	ReyClusterFleetIdentifier string = getReyClusterFleetIdentifier()
)
View Source
var AllowedSchemas = []string{"s3://", "gcs://", "tos://", "huggingface://", "hf://", "/"}
View Source
var DeploymentIdentifier string = getDeploymentIdentifier()

Functions

func AddLabel

func AddLabel(labels map[string]string, labelKey, labelValue string) map[string]string

AddLabel returns a map with the given key and value added to the given map.

func CheckRedisHealth added in v0.5.0

func CheckRedisHealth(ctx context.Context, redisClient *redis.Client) error

func CheckUser

func CheckUser(ctx context.Context, u User, redisClient *redis.Client) bool

func CloneAndAddLabel

func CloneAndAddLabel(labels map[string]string, labelKey, labelValue string) map[string]string

Clones the given map and returns a new map with the given key and value added. Returns the given map, if labelKey is empty.

func CloneAndRemoveLabel

func CloneAndRemoveLabel(labels map[string]string, labelKey string) map[string]string

CloneAndRemoveLabel clones the given map and returns a new map with the given key removed. Returns the given map, if labelKey is empty.

func CloneSelectorAndAddLabel

func CloneSelectorAndAddLabel(selector *metav1.LabelSelector, labelKey, labelValue string) *metav1.LabelSelector

Use DeepCopy to clone the selector, or create new one if nil Returns the given selector, if labelKey is empty.

func CountReadyPods

func CountReadyPods(podList *v1.PodList) (int64, error)

func CountRoutablePods

func CountRoutablePods(pods []*v1.Pod) (cnt int)

CountRoutablePods filters and returns the number of pods that are routable. A pod is routable if it have a valid PodIP and not in terminating state.

func CryptoShuffle added in v0.6.0

func CryptoShuffle[T any](slice []T)

CryptoShuffle shuffles slice in place using crypto/rand. Every swap reads the OS entropy source, so it costs a getrandom syscall and several allocations per swap; use it where unpredictability matters, not on hot paths. See Shuffle.

func DelUser

func DelUser(ctx context.Context, u User, redisClient *redis.Client) error

func DeploymentNameFromPod added in v0.4.0

func DeploymentNameFromPod(pod *v1.Pod) string

DeploymentNameFromPod extracts the deployment name from the pod using two methods: 1. If the pod has a label with the key "app.kubernetes.io/name", its value is considered the deployment name. 2. If the pod has an owner reference of kind "ReplicaSet", the deployment name is extracted from the owner reference's name. Alternatively, if the pod is a ray cluster node, we check: 1. If the pod has a label with the key "orchestration.aibrix.ai/raycluster-fleet-name", its value is considered the deployment name. 2. "app.kubernetes.io/name" is discared for ray cluster node identifid by the label "ray.io/is-ray-node" 3. If the pod has an owner reference of kind "RayCluster", the deployment name is extracted from the owner reference's name.

func DetokenizeText

func DetokenizeText(tokenIds []int) (string, error)

func FilterActivePods

func FilterActivePods(pods []v1.Pod) []v1.Pod

FilterActivePods returns active pods.

func FilterPods

func FilterPods(pods []v1.Pod, filterFn filterPod) []v1.Pod

FilterPods returns replica sets that are filtered by filterFn (all returned ones should match filterFn).

func FilterPodsByLabel added in v0.5.0

func FilterPodsByLabel(pods []*v1.Pod, labelKey, labelValue string) []*v1.Pod

FilterPodsByLabel filters pods that have a specific label key-value pair

func FilterPodsByLabelSelector added in v0.6.0

func FilterPodsByLabelSelector(pods []*v1.Pod, labelSelector string) ([]*v1.Pod, error)

FilterPodsByLabelSelector filter pod by k8s labelSelector

func FilterReadyPod

func FilterReadyPod(pod *v1.Pod) bool

func FilterRoutablePods

func FilterRoutablePods(pods []*v1.Pod) []*v1.Pod

FilterRoutablePods filters and returns a list of pods that are routable. A pod is routable if it have a valid PodIP and not in terminating state.

func FilterRoutablePodsInPlace

func FilterRoutablePodsInPlace(pods []*v1.Pod) []*v1.Pod

FilterRoutablePodsInPlace filters a list of pods that are routable. A pod is routable if it have a valid PodIP and not in terminating state.

func GVKCheckExists added in v0.6.0

func GVKCheckExists(cfg *rest.Config, gvk schema.GroupVersionKind) (bool, error)

GVKCheckExists check if gvk is support

func GeneratePodKey

func GeneratePodKey(podNamespace, podName string) string

GeneratePodKey generates a key in the format "namespace/name" for a given pod. It concatenates rather than using fmt because routers call it for every candidate pod on the request path.

func GetLLMEngine added in v0.6.0

func GetLLMEngine(pod *v1.Pod, labelName string, defaultValue string) string

func GetModelPortForPod added in v0.4.0

func GetModelPortForPod(requestID string, pod *v1.Pod) int64

func GetPodCondition

func GetPodCondition(status *v1.PodStatus, conditionType v1.PodConditionType) (int, *v1.PodCondition)

GetPodCondition extracts the provided condition from the given status and returns that. Returns nil and -1 if the condition is not present, and the index of the located condition.

func GetPodConditionFromList

func GetPodConditionFromList(conditions []v1.PodCondition, conditionType v1.PodConditionType) (int, *v1.PodCondition)

GetPodConditionFromList extracts the provided condition from the given list of condition and returns the index of the condition and the condition. Returns -1 and nil if the condition is not present.

func GetPodEnv added in v0.6.0

func GetPodEnv(pod *v1.Pod, envName, defaultValue string) string

func GetPodListByLabelSelector

func GetPodListByLabelSelector(ctx context.Context, podLister client.Client, namespace string, selector labels.Selector) (*v1.PodList, error)

func GetPodReadyCondition

func GetPodReadyCondition(status v1.PodStatus) *v1.PodCondition

GetPodReadyCondition extracts the pod ready condition from the given status and returns that. Returns nil if the condition is not present.

func GetPortAnnotationOrDefault

func GetPortAnnotationOrDefault(annotations map[string]string, key string, defaultValue int) int

func GetPortsForPod added in v0.6.0

func GetPortsForPod(pod *v1.Pod) []int

GetPortsForPod returns all ports for a pod based on its configuration. For distributed data-parallel (DP) inference, a pod may expose multiple ports: - Base port from pod labels (e.g., "model-port") - Additional ports for each DP rank (base_port + rank_index) Returns nil if the pod doesn't have the required configuration.

func GetPositiveIntAnnotationOrDefault

func GetPositiveIntAnnotationOrDefault(annotations map[string]string, key string, defaultValue int) int

func GetRedisClient

func GetRedisClient() *redis.Client

func GetStringAnnotationOrDefault

func GetStringAnnotationOrDefault(annotations map[string]string, key string, defaultValue string) string

func HasVolume added in v0.6.0

func HasVolume(vols []v1.Volume, name string) bool

func HasVolumeMount added in v0.6.0

func HasVolumeMount(mounts []v1.VolumeMount, name, path string) bool

func IsDataParallelPod added in v0.6.0

func IsDataParallelPod(pod *v1.Pod) bool

IsDataParallelPod checks if the pod is configured for Distributed Data Parallel. It returns true if "data-parallel-size" environment variable is present and its value is greater than 1.

func IsPodActive added in v0.4.0

func IsPodActive(p *v1.Pod) bool

func IsPodDraining added in v0.8.0

func IsPodDraining(p *v1.Pod) bool

func IsPodPhaseTerminal added in v0.4.0

func IsPodPhaseTerminal(phase v1.PodPhase) bool

IsPodPhaseTerminal returns true if the pod's phase is terminal.

func IsPodReady

func IsPodReady(pod *v1.Pod) bool

IsPodReady returns true if a pod is ready; false otherwise.

func IsPodReadyConditionTrue

func IsPodReadyConditionTrue(status v1.PodStatus) bool

IsPodReadyConditionTrue returns true if a pod is ready; false otherwise.

func IsPodTerminal added in v0.4.0

func IsPodTerminal(pod *v1.Pod) bool

IsPodTerminal returns true if a pod is terminal, all containers are stopped and cannot ever regress.

func IsPodTerminating

func IsPodTerminating(p *v1.Pod) bool

IsPodTerminating check if pod is in terminating status via whether the deletion timestamp is set

func IsRayClusterAvailable

func IsRayClusterAvailable(cluster *rayclusterv1.RayCluster, minReadySeconds int32, now metav1.Time) bool

func IsRayClusterReady

func IsRayClusterReady(cluster *rayclusterv1.RayCluster) bool

IsRayClusterReady returns true if a RayCluster is ready; false otherwise.

func IsRayClusterStateReady

func IsRayClusterStateReady(status rayclusterv1.RayClusterStatus) bool

func LoadEnv

func LoadEnv(key, defaultValue string) string

LoadEnv loads an environment variable or returns a default value if not set.

func LoadEnvBool added in v0.4.0

func LoadEnvBool(key string, defaultValue bool) bool

LoadEnvBool loads a boolean environment variable or returns a default value if not set.

func LoadEnvDuration added in v0.4.0

func LoadEnvDuration(key string, defaultValue time.Duration) time.Duration

LoadEnvDuration loads a duration environment variable or returns a default value if not set. Like LoadEnvInt and LoadEnvFloat, it rejects non-positive values. The result is used as a period, TTL or timeout, where zero or a negative value does not mean "off": it either makes a time.After-based loop spin without waiting or silently skips work gated on a positive period.

func LoadEnvFloat

func LoadEnvFloat(key string, defaultValue float64) float64

func LoadEnvInt

func LoadEnvInt(key string, defaultValue int) int

func LoadEnvNonNegativeInt added in v0.8.0

func LoadEnvNonNegativeInt(key string, defaultValue int) int

LoadEnvNonNegativeInt loads an int environment variable that accepts 0 as a meaningful value, falling back to defaultValue when the variable is unset, empty, negative or unparseable.

It exists because LoadEnvInt cannot express an explicit 0: that helper treats every value <= 0 as invalid and silently falls back to its default. For some knobs 0 is a documented value in its own right - the off switch of the PD decode abort (AIBRIX_DECODE_ABORT_TIMEOUT) and the single-attempt setting of its retry (AIBRIX_DECODE_ABORT_RETRY_DELAY) - and an operator facing a misbehaving component must be able to switch it off from the environment without a new build. A negative or unparseable value is still refused, as there it means a typo rather than an intent.

LoadEnvInt keeps its positive-only behaviour: its other callers rely on it.

func LookupEnv

func LookupEnv(key string) (string, bool)

LookupEnv retrieves an environment variable and returns whether it exists. It returns the value and a boolean indicating its existence.

func ModelClaimBindingsFromPod added in v0.8.0

func ModelClaimBindingsFromPod(pod *v1.Pod) map[string]ModelClaimBinding

ModelClaimBindingsFromPod parses route.claim.model.aibrix.ai/* annotations on a warm runtime pod. State is additive: legacy annotations infer active from a positive port and activating from port 0.

func ModelClaimPortForPod added in v0.8.0

func ModelClaimPortForPod(pod *v1.Pod, modelName string) (int, bool)

ModelClaimPortForPod returns the serving port for a specific served model on a warm runtime pod. It is called on the routing hot path, so it avoids the map allocation of ModelClaimsFromPod: it pre-filters annotation values with a cheap substring check and only unmarshals the matching claim.

func ModelClaimsFromPod added in v0.8.0

func ModelClaimsFromPod(pod *v1.Pod) map[string]int

ModelClaimsFromPod is the routing compatibility view of ModelClaim bindings.

func ModelRouterName added in v0.8.0

func ModelRouterName(modelName string) string

ModelRouterName returns the deterministic HTTPRoute name used for a served model. Existing DNS-safe model names keep the historical <model>-router form.

func OTELEnabled added in v0.7.0

func OTELEnabled() bool

func ParsePodKey

func ParsePodKey(key string) (string, string, bool)

ParsePodKey parses a key in the format "namespace/podName". Returns (namespace, podName, success).

func PathWithoutQuery added in v0.8.0

func PathWithoutQuery(requestPath string) string

PathWithoutQuery strips the query string from an Envoy/HTTP :path value. HTTP/2 :path includes both path and query (RFC 7540), so exact/prefix matchers on the request path must cut on '?' before comparing.

func SelectRandomPod

func SelectRandomPod(pods []*v1.Pod, randomFn func(int) int) (*v1.Pod, error)

SelectRandomPod selects a random pod from the provided list, ensuring it's routable. It returns an error if no ready pods are available.

func SetConditionInList

func SetConditionInList(inputList []metav1.Condition, conditionType string, status metav1.ConditionStatus, reason, message string, args ...interface{}) []metav1.Condition

SetConditionInList sets the specific condition type on the given PodAutoscaler to the specified value with the given reason and message. The message and args are treated like a format string. The condition will be added if it is not present. The new list will be returned.

func SetUser

func SetUser(ctx context.Context, u User, redisClient *redis.Client) error

func Shuffle added in v0.8.0

func Shuffle[T any](slice []T)

Shuffle shuffles slice in place using math/rand/v2's process-wide generator, which is safe for concurrent use and neither allocates nor enters the kernel. It is the right choice for tie-breaking on request hot paths, where the order only needs to be unbiased, not unpredictable.

func TokenizeInputText

func TokenizeInputText(text string) ([]int, error)

func TrimMessage

func TrimMessage(message string) string

func ValidateArtifactURL

func ValidateArtifactURL(artifactURL string) error

ValidateArtifactURL checks if the ArtifactURL has a valid schema (s3://, gcs://, tos://, huggingface://, hf://, /)

func ValidateKVCacheBackend added in v0.4.0

func ValidateKVCacheBackend(kv *orchestrationv1alpha1.KVCache) error

ValidateKVCacheBackend validates that the backend specified in annotations is valid.

Types

type CustomizedRegistry

type CustomizedRegistry[V any, A comparable] struct {
	Registry[V]
	// contains filtered or unexported fields
}

CustomizedRegistry extends Registry to provide a customized array provider.

func NewRegistryWithArrayProvider

func NewRegistryWithArrayProvider[V any, A comparable](provider func([]V) A) *CustomizedRegistry[V, A]

func (*CustomizedRegistry[V, A]) Array

func (reg *CustomizedRegistry[V, A]) Array() (arr A)

func (*CustomizedRegistry[V, A]) Delete

func (reg *CustomizedRegistry[V, A]) Delete(key string)

func (*CustomizedRegistry[V, A]) Len

func (reg *CustomizedRegistry[V, A]) Len() int

func (*CustomizedRegistry[V, A]) Store

func (reg *CustomizedRegistry[V, A]) Store(key string, value V)

type Message

type Message struct {
	Content string `json:"content"`
	Role    string `json:"role"`
}

type ModelClaimBinding added in v0.8.0

type ModelClaimBinding struct {
	Model string
	Port  int
	State string
	// Claim is the ModelClaim the binding belongs to, named by the
	// annotation key.
	Claim string
	// WakeByRequest says that the controller wakes this engine when asked.
	// A request for it while it sleeps is then written on the pod as a wake
	// request, rather than sent to the runtime. An older controller does not
	// set it, and wakes nothing itself.
	WakeByRequest bool
	// Reason says more than State about why the model is not served, such as
	// WaitingForRoom while a wake waits for room, or Moving while the claim
	// moves to another pod. It is empty when the state says it all.
	Reason string
}

ModelClaimBinding is the runtime-observed route state carried by one warm pod annotation. Port 0 is known but non-routable.

type ModelClaimRoute added in v0.8.0

type ModelClaimRoute struct {
	Model         string `json:"model"`
	Port          int    `json:"port"`
	State         string `json:"state,omitempty"`
	WakeByRequest bool   `json:"wakeByRequest,omitempty"`
	Reason        string `json:"reason,omitempty"`
}

ModelClaimRoute is the value of a warm pod's annotation for one claim, as the controller writes it and the gateway reads it.

type PodArray

type PodArray struct {
	Pods []*v1.Pod
	// contains filtered or unexported fields
}

PodArray is a simple implementation of types.PodList indexed by deployment names

func (*PodArray) All

func (arr *PodArray) All() []*v1.Pod

func (*PodArray) Indexes

func (arr *PodArray) Indexes() []string

func (*PodArray) Len

func (arr *PodArray) Len() int

func (*PodArray) ListByIndex

func (arr *PodArray) ListByIndex(deploymentName string) []*v1.Pod

func (*PodArray) ListPortsForPod added in v0.6.0

func (arr *PodArray) ListPortsForPod() map[string][]int

ListPortsForPod returns each pod's ports keyed by pod key (namespace/name, see GeneratePodKey).

type Registry

type Registry[V any] struct {
	// contains filtered or unexported fields
}

Registry is a generic hash set focus on storing values with string keys. Array output is optimized by offering a cached copy. The cached copy is published as an immutable snapshot, so readers can load it without the lock: a snapshot is never modified once published, writers always replace it with a new one.

func NewRegistry

func NewRegistry[V any]() *Registry[V]

func (*Registry[V]) Array

func (reg *Registry[V]) Array() (arr []V)

func (*Registry[V]) Delete

func (reg *Registry[V]) Delete(key string)

func (*Registry[V]) Len

func (reg *Registry[V]) Len() int

func (*Registry[V]) Load

func (reg *Registry[V]) Load(key string) (value V, ok bool)

func (*Registry[V]) Store

func (reg *Registry[V]) Store(key string, value V)

type SyncMap

type SyncMap[K any, V any] struct {
	// contains filtered or unexported fields
}

func (*SyncMap[K, V]) CompareAndDelete

func (sm *SyncMap[K, V]) CompareAndDelete(key K, old V) (deleted bool)

func (*SyncMap[K, V]) CompareAndSwap

func (sm *SyncMap[K, V]) CompareAndSwap(key K, old V, new V) bool

func (*SyncMap[K, V]) Delete

func (sm *SyncMap[K, V]) Delete(key K)

func (*SyncMap[K, V]) Keys

func (sm *SyncMap[K, V]) Keys() []K

func (*SyncMap[K, V]) Len

func (sm *SyncMap[K, V]) Len() int

func (*SyncMap[K, V]) Load

func (sm *SyncMap[K, V]) Load(key K) (typedVal V, ok bool)

func (*SyncMap[K, V]) LoadAndDelete

func (sm *SyncMap[K, V]) LoadAndDelete(key K) (typedVal V, loaded bool)

func (*SyncMap[K, V]) LoadOrStore

func (sm *SyncMap[K, V]) LoadOrStore(key K, value V) (V, bool)

func (*SyncMap[K, V]) Range

func (sm *SyncMap[K, V]) Range(f func(key K, value V) bool)

func (*SyncMap[K, V]) Store

func (sm *SyncMap[K, V]) Store(key K, value V)

func (*SyncMap[K, V]) Swap

func (sm *SyncMap[K, V]) Swap(key K, value V) (V, bool)

func (*SyncMap[K, V]) Values

func (sm *SyncMap[K, V]) Values() []V

type Telemetry added in v0.7.0

type Telemetry struct {
	// contains filtered or unexported fields
}

func InitOpenTelemetry added in v0.7.0

func InitOpenTelemetry(serviceName string, protocol string) (*Telemetry, error)

func (*Telemetry) Shutdown added in v0.7.0

func (t *Telemetry) Shutdown()

type User

type User struct {
	Name string `json:"name" validate:"required"`
	Rpm  int64  `json:"rpm"`
	Tpm  int64  `json:"tpm"`
}

func GetUser

func GetUser(ctx context.Context, u User, redisClient *redis.Client) (User, error)

Directories

Path Synopsis
Package syncable defines the interface for state that can be synced across replicas (e.g.
Package syncable defines the interface for state that can be synced across replicas (e.g.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL