Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Change balancing method to greedy (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
feymartynov committed Oct 6, 2020
1 parent 73d2f7d commit f311681
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/app/endpoint/rtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl RequestHandler for ConnectHandler {

// There are 3 cases:
// 1. Connecting as writer for the first time. There's no recording in that case.
// Select the least loaded backend that is capable to host the room's reservation.
// Select the most loaded backend that is capable to host the room's reservation.
// 2. Connecting as reader with existing recording. Choose the backend of the active
// recording because Janus doesn't support clustering and it must be the same server
// that the writer is connected to.
Expand All @@ -311,7 +311,7 @@ impl RequestHandler for ConnectHandler {
.execute(&conn)?
.ok_or("no backend found for stream")
.status(ResponseStatus::UNPROCESSABLE_ENTITY)?,
None => db::janus_backend::least_loaded(room.id(), &conn)?
None => db::janus_backend::most_loaded(room.id(), &conn)?
.ok_or("no available backends")
.status(ResponseStatus::SERVICE_UNAVAILABLE)?,
};
Expand Down Expand Up @@ -730,8 +730,7 @@ mod test {
shared_helpers::insert_agent(&conn, s1a1.agent_id(), rtc1.room_id());

// The second backend has 1 stream with 2 agents but it's not started
// so it doesn't make any load on the backend and should be selected
// by the balancer.
// so it doesn't make any load on the backend.
let rtc2 = shared_helpers::insert_rtc(&conn);

factory::Recording::new()
Expand All @@ -753,7 +752,7 @@ mod test {
// The new rtc for which we will balance the stream.
let rtc3 = shared_helpers::insert_rtc(&conn);

(rtc3, backend2)
(rtc3, backend1)
})
.unwrap();

Expand Down
10 changes: 5 additions & 5 deletions src/db/janus_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ impl<'a> DeleteQuery<'a> {

////////////////////////////////////////////////////////////////////////////////

// Returns the least loaded backend capable to host the room with its reserve considering:
// Returns the most loaded backend capable to host the room with its reserve considering:
// - room opening period;
// - actual number of online agents;
// - optional backend capacity;
// - optional room reserve.
const LEAST_LOADED_SQL: &str = r#"
const MOST_LOADED_SQL: &str = r#"
WITH
room_load AS (
SELECT
Expand Down Expand Up @@ -239,15 +239,15 @@ const LEAST_LOADED_SQL: &str = r#"
ON 1 = 1
WHERE r2.id = $1
AND COALESCE(jb.balancer_capacity, jb.capacity, 2147483647) - COALESCE(jbl.load, 0) > COALESCE(r2.reserve, 0)
ORDER BY COALESCE(jb.balancer_capacity, jb.capacity, 2147483647) - COALESCE(jbl.load, 0) DESC
ORDER BY COALESCE(jbl.load, 0) DESC
LIMIT 1
"#;

pub(crate) fn least_loaded(room_id: Uuid, conn: &PgConnection) -> Result<Option<Object>, Error> {
pub(crate) fn most_loaded(room_id: Uuid, conn: &PgConnection) -> Result<Option<Object>, Error> {
use diesel::prelude::*;
use diesel::sql_types::Uuid;

diesel::sql_query(LEAST_LOADED_SQL)
diesel::sql_query(MOST_LOADED_SQL)
.bind::<Uuid, _>(room_id)
.get_result(conn)
.optional()
Expand Down

0 comments on commit f311681

Please sign in to comment.