Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
change
sglang
Commits
36bfddec
Unverified
Commit
36bfddec
authored
Aug 09, 2025
by
Tony Lu
Committed by
GitHub
Aug 08, 2025
Browse files
[router] add metrics for worker and policy (#8971)
Signed-off-by:
Tony Lu
<
tonyluj@gmail.com
>
parent
91e2f902
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
13 additions
and
0 deletions
+13
-0
sgl-router/src/core/worker.rs
sgl-router/src/core/worker.rs
+2
-0
sgl-router/src/policies/cache_aware.rs
sgl-router/src/policies/cache_aware.rs
+1
-0
sgl-router/src/policies/power_of_two.rs
sgl-router/src/policies/power_of_two.rs
+1
-0
sgl-router/src/policies/random.rs
sgl-router/src/policies/random.rs
+5
-0
sgl-router/src/policies/round_robin.rs
sgl-router/src/policies/round_robin.rs
+4
-0
No files found.
sgl-router/src/core/worker.rs
View file @
36bfddec
use
super
::{
CircuitBreaker
,
CircuitBreakerConfig
,
WorkerError
,
WorkerResult
};
use
super
::{
CircuitBreaker
,
CircuitBreakerConfig
,
WorkerError
,
WorkerResult
};
use
crate
::
metrics
::
RouterMetrics
;
use
async_trait
::
async_trait
;
use
async_trait
::
async_trait
;
use
futures
;
use
futures
;
use
serde_json
;
use
serde_json
;
...
@@ -259,6 +260,7 @@ impl Worker for BasicWorker {
...
@@ -259,6 +260,7 @@ impl Worker for BasicWorker {
fn
set_healthy
(
&
self
,
healthy
:
bool
)
{
fn
set_healthy
(
&
self
,
healthy
:
bool
)
{
self
.healthy
.store
(
healthy
,
Ordering
::
Release
);
self
.healthy
.store
(
healthy
,
Ordering
::
Release
);
RouterMetrics
::
set_worker_health
(
self
.url
(),
healthy
);
}
}
async
fn
check_health_async
(
&
self
)
->
WorkerResult
<
()
>
{
async
fn
check_health_async
(
&
self
)
->
WorkerResult
<
()
>
{
...
...
sgl-router/src/policies/cache_aware.rs
View file @
36bfddec
...
@@ -181,6 +181,7 @@ impl LoadBalancingPolicy for CacheAwarePolicy {
...
@@ -181,6 +181,7 @@ impl LoadBalancingPolicy for CacheAwarePolicy {
// Increment processed counter
// Increment processed counter
workers
[
min_load_idx
]
.increment_processed
();
workers
[
min_load_idx
]
.increment_processed
();
RouterMetrics
::
record_processed_request
(
workers
[
min_load_idx
]
.url
());
RouterMetrics
::
record_processed_request
(
workers
[
min_load_idx
]
.url
());
RouterMetrics
::
record_policy_decision
(
self
.name
(),
workers
[
min_load_idx
]
.url
());
return
Some
(
min_load_idx
);
return
Some
(
min_load_idx
);
}
}
...
...
sgl-router/src/policies/power_of_two.rs
View file @
36bfddec
...
@@ -90,6 +90,7 @@ impl LoadBalancingPolicy for PowerOfTwoPolicy {
...
@@ -90,6 +90,7 @@ impl LoadBalancingPolicy for PowerOfTwoPolicy {
// Increment processed counter
// Increment processed counter
workers
[
selected_idx
]
.increment_processed
();
workers
[
selected_idx
]
.increment_processed
();
RouterMetrics
::
record_processed_request
(
workers
[
selected_idx
]
.url
());
RouterMetrics
::
record_processed_request
(
workers
[
selected_idx
]
.url
());
RouterMetrics
::
record_policy_decision
(
self
.name
(),
workers
[
selected_idx
]
.url
());
Some
(
selected_idx
)
Some
(
selected_idx
)
}
}
...
...
sgl-router/src/policies/random.rs
View file @
36bfddec
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
use
super
::{
get_healthy_worker_indices
,
LoadBalancingPolicy
};
use
super
::{
get_healthy_worker_indices
,
LoadBalancingPolicy
};
use
crate
::
core
::
Worker
;
use
crate
::
core
::
Worker
;
use
crate
::
metrics
::
RouterMetrics
;
use
rand
::
Rng
;
use
rand
::
Rng
;
/// Random selection policy
/// Random selection policy
...
@@ -30,6 +31,10 @@ impl LoadBalancingPolicy for RandomPolicy {
...
@@ -30,6 +31,10 @@ impl LoadBalancingPolicy for RandomPolicy {
let
mut
rng
=
rand
::
thread_rng
();
let
mut
rng
=
rand
::
thread_rng
();
let
random_idx
=
rng
.gen_range
(
0
..
healthy_indices
.len
());
let
random_idx
=
rng
.gen_range
(
0
..
healthy_indices
.len
());
let
worker
=
workers
[
healthy_indices
[
random_idx
]]
.url
();
RouterMetrics
::
record_processed_request
(
worker
);
RouterMetrics
::
record_policy_decision
(
self
.name
(),
worker
);
Some
(
healthy_indices
[
random_idx
])
Some
(
healthy_indices
[
random_idx
])
}
}
...
...
sgl-router/src/policies/round_robin.rs
View file @
36bfddec
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
use
super
::{
get_healthy_worker_indices
,
LoadBalancingPolicy
};
use
super
::{
get_healthy_worker_indices
,
LoadBalancingPolicy
};
use
crate
::
core
::
Worker
;
use
crate
::
core
::
Worker
;
use
crate
::
metrics
::
RouterMetrics
;
use
std
::
sync
::
atomic
::{
AtomicUsize
,
Ordering
};
use
std
::
sync
::
atomic
::{
AtomicUsize
,
Ordering
};
/// Round-robin selection policy
/// Round-robin selection policy
...
@@ -35,7 +36,10 @@ impl LoadBalancingPolicy for RoundRobinPolicy {
...
@@ -35,7 +36,10 @@ impl LoadBalancingPolicy for RoundRobinPolicy {
// Get and increment counter atomically
// Get and increment counter atomically
let
count
=
self
.counter
.fetch_add
(
1
,
Ordering
::
Relaxed
);
let
count
=
self
.counter
.fetch_add
(
1
,
Ordering
::
Relaxed
);
let
selected_idx
=
count
%
healthy_indices
.len
();
let
selected_idx
=
count
%
healthy_indices
.len
();
let
worker
=
workers
[
healthy_indices
[
selected_idx
]]
.url
();
RouterMetrics
::
record_processed_request
(
worker
);
RouterMetrics
::
record_policy_decision
(
self
.name
(),
worker
);
Some
(
healthy_indices
[
selected_idx
])
Some
(
healthy_indices
[
selected_idx
])
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment