Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

balancer: unload duplicate rooms #1131

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/harness-tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use test_context::test_context;

mod connection;
mod routing;
mod state;

#[test_context(TestRunner)]
#[tokio::test]
Expand Down
50 changes: 50 additions & 0 deletions crates/harness-tests/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! Tests for how the balancer handles room state in the context of managing rooms on monoliths.

use std::time::Duration;

use harness::{Client, Monolith, TestRunner};
use ott_balancer_protocol::monolith::{B2MUnload, MsgB2M};
use test_context::test_context;

#[test_context(TestRunner)]
#[tokio::test]
async fn should_unload_duplicate_rooms(ctx: &TestRunner) {
let mut m1 = Monolith::new(ctx).await.unwrap();
let mut m2 = Monolith::new(ctx).await.unwrap();

m1.show().await;
m2.show().await;

m1.load_room("foo").await;
m2.load_room("foo").await;

m2.wait_recv().await;

let recv = m2.collect_recv();
assert_eq!(recv.len(), 1);
assert!(matches!(recv[0], MsgB2M::Unload(B2MUnload { .. })));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in general what does the exclamation point after certain functions mean?
Couple questions for this line:

  • what's stored in recv[0]
  • What does the { .. } statement do

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

#[test_context(TestRunner)]
#[tokio::test]
async fn should_unload_duplicate_rooms_and_route_correctly(ctx: &TestRunner) {
let mut m1 = Monolith::new(ctx).await.unwrap();
let mut m2 = Monolith::new(ctx).await.unwrap();

m1.show().await;
m2.show().await;

m1.load_room("foo").await;
m2.load_room("foo").await;

tokio::time::timeout(Duration::from_millis(100), m2.wait_recv())
.await
.expect("timed out waiting for unload");

let mut c = Client::new(ctx).unwrap();
c.join("foo").await;

tokio::time::timeout(Duration::from_millis(100), m1.wait_recv())
.await
.expect("timed out waiting for client join");
}
15 changes: 13 additions & 2 deletions crates/ott-balancer-bin/src/balancer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{collections::HashMap, sync::Arc};

use ott_balancer_protocol::monolith::{B2MClientMsg, B2MJoin, B2MLeave, MsgM2B, RoomMetadata};
use ott_balancer_protocol::monolith::{
B2MClientMsg, B2MJoin, B2MLeave, B2MUnload, MsgM2B, RoomMetadata,
};
use ott_balancer_protocol::*;
use rand::seq::IteratorRandom;
use serde_json::value::RawValue;
Expand Down Expand Up @@ -343,12 +345,15 @@ impl BalancerContext {
match locator.load_epoch().cmp(&load_epoch) {
std::cmp::Ordering::Less => {
// we already have an older version of this room
self.unload_room(monolith_id, metadata.name.clone()).await?;
return Err(anyhow::anyhow!("room already loaded"));
}
std::cmp::Ordering::Greater => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are rooms ordered by id?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this code compares the load_epoch of the room. Only the room with the lowest load_epoch should be loaded, as specified in our spec doc.

// we have an newer version of this room, remove it
self.remove_room(&metadata.name, locator.monolith_id())
self.unload_room(locator.monolith_id(), metadata.name.clone())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

difference between unload_room() and remove_room()?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unload_room sends a message to the monolith that the room should be unloaded

remove_room modifies BalancerContext to remove the room from the balancer's state but doesn't send a message.

.await?;
// self.remove_room(&metadata.name, locator.monolith_id())
// .await?;
}
_ => {}
}
Expand Down Expand Up @@ -414,6 +419,12 @@ impl BalancerContext {
.ok_or(anyhow::anyhow!("no monoliths available"))?;
Ok(selected)
}

pub async fn unload_room(&self, monolith: MonolithId, room: RoomName) -> anyhow::Result<()> {
let monolith = self.monoliths.get(&monolith).unwrap();
monolith.send(B2MUnload { room }).await?;
Ok(())
}
}

pub async fn join_client(
Expand Down
6 changes: 6 additions & 0 deletions crates/ott-balancer-protocol/src/monolith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ impl From<B2MLoad> for MsgB2M {
}
}

impl From<B2MUnload> for MsgB2M {
fn from(val: B2MUnload) -> Self {
Self::Unload(val)
}
}

impl From<B2MJoin> for MsgB2M {
fn from(val: B2MJoin) -> Self {
Self::Join(val)
Expand Down
Loading