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 9, 2023
1 parent 8d1bf94 commit 24be4e1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
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.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ wasmedge-sdk = { version = "0.10.1" }
async-channel = "2.1.0"
sysinfo = "0.29.10"
ssh2 = "0.9.4"
rand = "0.8.5"
numeric = "0.1.4"

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

Expand Down
38 changes: 38 additions & 0 deletions src/schedule/master.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::{collections::hash_map::DefaultHasher, hash::Hasher};

use async_raft::NodeId;
use async_trait::async_trait;
use axum::{
http::StatusCode,
response::{IntoResponse, Redirect, Response},
};
use bincode::Options;
use ws_derive::LogicalModule;
// use

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

trait NodeWeighteFetcher {
// NOTE: get weight return node weight
// larger is better
fn get_node_weight(id:NodeId) -> f64;
}

struct StrawNodeSelector {
weight_fetcher:NodeWeighteFetcher,
rng:rand::Rng
}

impl StrawNodeSelector {
fn new(weight_fetcher: NodeWeighteFetcher) -> Self { Self { weight_fetcher,rng:rand::thread_rng() } }
}

// NOTE: Straw2 algorithm
impl NodeSelector for StrawNodeSelector {
fn select_node(&self, all_node_cnt: usize, fn_name: &str) -> NodeID {
// NOTE: node id is [1,all_node_cnt]
let mut maxStraw:f64 = -1;
let mut nodeId:NodeID = 1;
let range = Uniform::new(0, 65536);
for i in 1..all_node_cnt+1 {
let weight = self.weight_fetcher.get_node_weight(i);
let mut straw:f64 = range.sample(&mut self.rng);
straw = (straw / 65536).ln() / weight;
if maxStraw == -1 || maxStraw < straw {
maxStraw = straw;
nodeId = i;
}
}
return nodeId;
}
}

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

0 comments on commit 24be4e1

Please sign in to comment.