|
| 1 | +use super::{Plugin, SchedulerPlugin}; |
| 2 | +use crate::store::core::{RedisStore, StoreContext}; |
| 3 | +use anyhow::Error; |
| 4 | +use anyhow::Result; |
| 5 | +use log::warn; |
| 6 | +use redis::{Commands, Script}; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | +use shared::models::node::ComputeRequirements; |
| 9 | +use shared::models::task::Task; |
| 10 | +use std::{collections::BTreeSet, sync::Arc}; |
| 11 | +use std::{collections::HashSet, str::FromStr}; |
| 12 | + |
| 13 | +pub mod scheduler_impl; |
| 14 | +pub mod status_update_impl; |
| 15 | +#[cfg(test)] |
| 16 | +mod tests; |
| 17 | + |
| 18 | +const GROUP_KEY_PREFIX: &str = "node_group:"; |
| 19 | +const NODE_GROUP_MAP_KEY: &str = "node_to_group"; |
| 20 | +const GROUP_TASK_KEY_PREFIX: &str = "group_task:"; |
| 21 | + |
| 22 | +#[derive(Debug, Serialize, Deserialize, Clone)] |
| 23 | +pub struct NodeGroupConfiguration { |
| 24 | + name: String, |
| 25 | + min_group_size: usize, |
| 26 | + max_group_size: usize, |
| 27 | + #[serde(deserialize_with = "deserialize_compute_requirements")] |
| 28 | + compute_requirements: Option<ComputeRequirements>, |
| 29 | +} |
| 30 | + |
| 31 | +fn deserialize_compute_requirements<'de, D>( |
| 32 | + deserializer: D, |
| 33 | +) -> Result<Option<ComputeRequirements>, D::Error> |
| 34 | +where |
| 35 | + D: serde::Deserializer<'de>, |
| 36 | +{ |
| 37 | + let s: Option<String> = Option::deserialize(deserializer)?; |
| 38 | + match s { |
| 39 | + Some(s) => ComputeRequirements::from_str(&s) |
| 40 | + .map(Some) |
| 41 | + .map_err(serde::de::Error::custom), |
| 42 | + None => Ok(None), |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl NodeGroupConfiguration { |
| 47 | + pub fn is_valid(&self) -> bool { |
| 48 | + if self.max_group_size < self.min_group_size { |
| 49 | + return false; |
| 50 | + } |
| 51 | + true |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] |
| 56 | +pub struct NodeGroup { |
| 57 | + pub id: String, |
| 58 | + pub nodes: BTreeSet<String>, |
| 59 | + pub created_at: chrono::DateTime<chrono::Utc>, |
| 60 | + pub configuration_name: String, |
| 61 | +} |
| 62 | + |
| 63 | +#[derive(Clone)] |
| 64 | +pub struct NodeGroupsPlugin { |
| 65 | + configurations: Vec<NodeGroupConfiguration>, |
| 66 | + store: Arc<RedisStore>, |
| 67 | + store_context: Arc<StoreContext>, |
| 68 | +} |
| 69 | + |
| 70 | +impl NodeGroupsPlugin { |
| 71 | + pub fn new( |
| 72 | + configurations: Vec<NodeGroupConfiguration>, |
| 73 | + store: Arc<RedisStore>, |
| 74 | + store_context: Arc<StoreContext>, |
| 75 | + ) -> Self { |
| 76 | + let mut sorted_configs = configurations; |
| 77 | + |
| 78 | + // Check for duplicate configuration names |
| 79 | + let mut seen_names = HashSet::new(); |
| 80 | + for config in &sorted_configs { |
| 81 | + if !seen_names.insert(config.name.clone()) { |
| 82 | + panic!("Configuration names must be unique"); |
| 83 | + } |
| 84 | + if !config.is_valid() { |
| 85 | + panic!("Plugin configuration is invalid"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + sorted_configs.sort_by(|a, b| b.min_group_size.cmp(&a.min_group_size)); |
| 90 | + |
| 91 | + Self { |
| 92 | + configurations: sorted_configs, |
| 93 | + store, |
| 94 | + store_context, |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + fn generate_group_id() -> String { |
| 99 | + use rand::Rng; |
| 100 | + let mut rng = rand::rng(); |
| 101 | + format!("group_{}", rng.random::<u64>()) |
| 102 | + } |
| 103 | + |
| 104 | + fn get_group_key(group_id: &str) -> String { |
| 105 | + format!("{}{}", GROUP_KEY_PREFIX, group_id) |
| 106 | + } |
| 107 | + |
| 108 | + pub fn get_node_group(&self, node_addr: &str) -> Result<Option<NodeGroup>, Error> { |
| 109 | + let mut conn = self.store.client.get_connection()?; |
| 110 | + |
| 111 | + let group_id: Option<String> = conn.hget(NODE_GROUP_MAP_KEY, node_addr)?; |
| 112 | + if let Some(group_id) = group_id { |
| 113 | + let group_key = Self::get_group_key(&group_id); |
| 114 | + let group_data: Option<String> = conn.get(&group_key)?; |
| 115 | + if let Some(group_data) = group_data { |
| 116 | + return Ok(Some(serde_json::from_str(&group_data)?)); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + Ok(None) |
| 121 | + } |
| 122 | + |
| 123 | + fn get_current_group_task(&self, group_id: &str) -> Result<Option<Task>, Error> { |
| 124 | + let mut conn = self.store.client.get_connection()?; |
| 125 | + let task_key = format!("{}{}", GROUP_TASK_KEY_PREFIX, group_id); |
| 126 | + let task_id: Option<String> = conn.get(&task_key)?; |
| 127 | + |
| 128 | + if let Some(task_id) = task_id { |
| 129 | + if let Some(task) = self.store_context.task_store.get_task(&task_id) { |
| 130 | + return Ok(Some(task)); |
| 131 | + } |
| 132 | + |
| 133 | + warn!("Task id set but task not found"); |
| 134 | + let script = Script::new( |
| 135 | + r#" |
| 136 | + local task_key = KEYS[1] |
| 137 | + local expected_task_id = ARGV[1] |
| 138 | + |
| 139 | + local current_task_id = redis.call('GET', task_key) |
| 140 | + if current_task_id == expected_task_id then |
| 141 | + redis.call('DEL', task_key) |
| 142 | + return 1 |
| 143 | + else |
| 144 | + return 0 |
| 145 | + end |
| 146 | + "#, |
| 147 | + ); |
| 148 | + |
| 149 | + let _: () = script.key(&task_key).arg(task_id).invoke(&mut conn)?; |
| 150 | + } |
| 151 | + Ok(None) |
| 152 | + } |
| 153 | + |
| 154 | + fn assign_task_to_group(&self, group_id: &str, task_id: &str) -> Result<bool, Error> { |
| 155 | + let mut conn = self.store.client.get_connection()?; |
| 156 | + let task_key = format!("{}{}", GROUP_TASK_KEY_PREFIX, group_id); |
| 157 | + let result: bool = conn.set_nx::<_, _, bool>(&task_key, task_id)?; |
| 158 | + Ok(result) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl Plugin for NodeGroupsPlugin {} |
0 commit comments