Skip to content

Commit

Permalink
feat(schedule): add straw2 algo
Browse files Browse the repository at this point in the history
Signed-off-by: NaturalSelect <[email protected]>
  • Loading branch information
NaturalSelect committed Dec 15, 2023
1 parent 845b3c9 commit f809aeb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
qp2p = "0.36.4" #{ path = "qp2p" }
qp2p = "0.36.4" #{ path = "qp2p" }
tokio = { version = "1.32.0", features = ["full"] }
thiserror = "1.0.50"
async-trait = "0.1.74"
Expand Down Expand Up @@ -39,6 +39,7 @@ wasmedge-sdk = { version = "0.10.1" }
async-channel = "2.1.0"
sysinfo = "0.29.10"
ssh2 = "0.9.4"
rand = "0.8.5"

# slog-envlogger = { version = "2.1.0", optional = true }

Expand Down
39 changes: 39 additions & 0 deletions src/schedule/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Redirect, Response},
};
use rand::{distributions::Uniform, prelude::Distribution, thread_rng};
use ws_derive::LogicalModule;
// use

use super::{executor::Executor, http_handler::RequestHandler};
use crate::{
Expand All @@ -24,6 +26,43 @@ pub struct ScheMaster {
// view: ScheMasterView,
}

trait NodeWeighteFetcher: Send + Sync + 'static {
// NOTE: get weight return node weight
// larger is better
fn get_node_weight(&self, id: NodeID) -> f64;
}

struct StrawNodeSelector {
weight_fetcher: Box<dyn NodeWeighteFetcher>,
}

impl StrawNodeSelector {
fn new(weight_fetcher: Box<dyn NodeWeighteFetcher>) -> Self {
Self { weight_fetcher }
}
}

// NOTE: Straw2 algorithm
impl NodeSelector for StrawNodeSelector {
fn select_node(&self, all_node_cnt: usize, _fn_name: &str) -> NodeID {
// NOTE: 1 is an impossible value for straw
let mut max_straw: f64 = 1.0;
let mut node_id: NodeID = 1;
let range = Uniform::new(0.0, 65536.0);
// NOTE: node id is [1,all_node_cnt]
for i in 1..all_node_cnt + 1 {
let weight = self.weight_fetcher.get_node_weight(i as NodeID);
let mut straw: f64 = range.sample(&mut thread_rng());
straw = (straw / 65536.0).ln() / weight;
if (max_straw - 1.0).abs() < 0.000001 || max_straw < straw {
max_straw = straw;
node_id = i as NodeID;
}
}
return node_id;
}
}

trait NodeSelector: Send + Sync + 'static {
fn select_node(&self, all_node_cnt: usize, fn_name: &str) -> NodeID;
}
Expand Down

0 comments on commit f809aeb

Please sign in to comment.