You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iggy.apache.org by gk...@apache.org on 2025/05/17 16:25:13 UTC

(iggy) branch io_uring_tpc updated (102eb2e3 -> 47d86c1c)

This is an automated email from the ASF dual-hosted git repository.

gkoszyk pushed a change to branch io_uring_tpc
in repository https://gitbox.apache.org/repos/asf/iggy.git


 discard 102eb2e3 begin shard init
 discard 3dd41328 feat(io_uring): load server_config (#1798)
 discard 03520660 feat(io_uring): bootstrap the main function (#1788)
 discard f930cd3b initial setup and migration of shards
     add d53a6c17 feat(server): verify the format of server_addr (#1770)
     new 6685eacf initial setup and migration of shards
     new d50f2877 feat(io_uring): bootstrap the main function (#1788)
     new db7eb716 feat(io_uring): load server_config (#1798)
     new 47d86c1c begin shard init

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (102eb2e3)
            \
             N -- N -- N   refs/heads/io_uring_tpc (47d86c1c)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 core/common/src/error/iggy_error.rs                |  2 +
 .../tcp_config/tcp_client_config_builder.rs        | 83 +++++++++++++++++++++-
 core/sdk/src/clients/client_builder.rs             |  2 +-
 3 files changed, 83 insertions(+), 4 deletions(-)


(iggy) 04/04: begin shard init

Posted by gk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gkoszyk pushed a commit to branch io_uring_tpc
in repository https://gitbox.apache.org/repos/asf/iggy.git

commit 47d86c1c3fdd5100a6deb71fe6ffb2222d7f1c1b
Author: numinex <g....@gmail.com>
AuthorDate: Sat May 17 14:00:52 2025 +0200

    begin shard init
---
 core/server/Cargo.toml             |   2 +-
 core/server/src/bootstrap.rs       | 131 +++++++++++++++++++++++++++++++++++--
 core/server/src/main.rs            |  33 +++++-----
 core/server/src/shard/builder.rs   |   2 +-
 core/server/src/shard/mod.rs       |  28 +++++++-
 core/server/src/shard/namespace.rs |   1 +
 core/server/src/state/file.rs      |  13 +---
 7 files changed, 170 insertions(+), 40 deletions(-)

diff --git a/core/server/Cargo.toml b/core/server/Cargo.toml
index 63731abb..fba3fcd9 100644
--- a/core/server/Cargo.toml
+++ b/core/server/Cargo.toml
@@ -61,7 +61,7 @@ jsonwebtoken = "9.3.1"
 lending-iterator = "0.1.7"
 mimalloc = { workspace = true, optional = true }
 moka = { version = "0.12.10", features = ["future"] }
-monoio = "0.2.4"
+monoio = { version = "0.2.4", features = ["mkdirat", "unlinkat"] }
 nix = { version = "0.30", features = ["fs"] }
 once_cell = "1.21.3"
 openssl = { workspace = true }
diff --git a/core/server/src/bootstrap.rs b/core/server/src/bootstrap.rs
index fd6a5aa0..d51096a2 100644
--- a/core/server/src/bootstrap.rs
+++ b/core/server/src/bootstrap.rs
@@ -1,7 +1,24 @@
-use monoio::{Buildable, Driver, Runtime};
+use iggy_common::{
+    defaults::{
+        DEFAULT_ROOT_PASSWORD, DEFAULT_ROOT_USERNAME, MAX_PASSWORD_LENGTH, MAX_USERNAME_LENGTH,
+        MIN_PASSWORD_LENGTH, MIN_USERNAME_LENGTH,
+    },
+    IggyError,
+};
+use monoio::{
+    fs::create_dir_all,
+    Buildable, Driver, Runtime,
+};
+use tracing::info;
 
-use crate::shard::{connector::ShardConnector, frame::ShardFrame};
-use std::ops::Range;
+use crate::{
+    configs::{config_provider::ConfigProviderKind, server::ServerConfig, system::SystemConfig},
+    server_error::ServerError,
+    shard::{connector::ShardConnector, frame::ShardFrame},
+    streaming::users::user::User,
+    IGGY_ROOT_PASSWORD_ENV, IGGY_ROOT_USERNAME_ENV,
+};
+use std::{env, fs::remove_dir_all, ops::Range, path::Path};
 
 pub fn create_shard_connections(shards_set: Range<usize>) -> Vec<ShardConnector<ShardFrame>> {
     let shards_count = shards_set.len();
@@ -13,12 +30,112 @@ pub fn create_shard_connections(shards_set: Range<usize>) -> Vec<ShardConnector<
     connectors
 }
 
-pub async fn create_directories() {
-    todo!();
+pub async fn load_config(
+    config_provider: &ConfigProviderKind,
+) -> Result<ServerConfig, ServerError> {
+    let config = ServerConfig::load(config_provider).await?;
+    Ok(config)
 }
 
-pub async fn create_root_user() {
-    todo!();
+pub async fn create_directories(config: &SystemConfig) -> Result<(), IggyError> {
+    let system_path = config.get_system_path();
+    if !Path::new(&system_path).exists() && create_dir_all(&system_path).await.is_err() {
+        return Err(IggyError::CannotCreateBaseDirectory(system_path));
+    }
+
+    let state_path = config.get_state_path();
+    if !Path::new(&state_path).exists() && create_dir_all(&state_path).await.is_err() {
+        return Err(IggyError::CannotCreateStateDirectory(state_path));
+    }
+
+    let streams_path = config.get_streams_path();
+    if !Path::new(&streams_path).exists() && create_dir_all(&streams_path).await.is_err() {
+        return Err(IggyError::CannotCreateStreamsDirectory(streams_path));
+    }
+
+    let runtime_path = config.get_runtime_path();
+    // TODO: Change remove_dir_all to async version, once we implement the dir walk using monoio `remove_dir` method.
+    if Path::new(&runtime_path).exists() && remove_dir_all(&runtime_path).is_err() {
+        return Err(IggyError::CannotRemoveRuntimeDirectory(runtime_path));
+    }
+
+    if create_dir_all(&runtime_path).await.is_err() {
+        return Err(IggyError::CannotCreateRuntimeDirectory(runtime_path));
+    }
+
+    info!(
+        "Initializing system, data will be stored at: {}",
+        config.get_system_path()
+    );
+    Ok(())
+
+    // TODO: Move this to individual shard level
+    /*
+    let state_entries = self.state.init().await.with_error_context(|error| {
+        format!("{COMPONENT} (error: {error}) - failed to initialize state entries")
+    })?;
+    let system_state = SystemState::init(state_entries)
+        .await
+        .with_error_context(|error| {
+            format!("{COMPONENT} (error: {error}) - failed to initialize system state")
+        })?;
+    let now = Instant::now();
+    self.load_version().await.with_error_context(|error| {
+        format!("{COMPONENT} (error: {error}) - failed to load version")
+    })?;
+    self.load_users(system_state.users.into_values().collect())
+        .await
+        .with_error_context(|error| {
+            format!("{COMPONENT} (error: {error}) - failed to load users")
+        })?;
+    self.load_streams(system_state.streams.into_values().collect())
+        .await
+        .with_error_context(|error| {
+            format!("{COMPONENT} (error: {error}) - failed to load streams")
+        })?;
+    if let Some(archiver) = self.archiver.as_ref() {
+        archiver
+            .init()
+            .await
+            .expect("Failed to initialize archiver");
+    }
+    info!("Initialized system in {} ms.", now.elapsed().as_millis());
+    Ok(())
+    */
+}
+
+pub fn create_root_user() -> User {
+    info!("Creating root user...");
+    let username = env::var(IGGY_ROOT_USERNAME_ENV);
+    let password = env::var(IGGY_ROOT_PASSWORD_ENV);
+    if (username.is_ok() && password.is_err()) || (username.is_err() && password.is_ok()) {
+        panic!("When providing the custom root user credentials, both username and password must be set.");
+    }
+    if username.is_ok() && password.is_ok() {
+        info!("Using the custom root user credentials.");
+    } else {
+        info!("Using the default root user credentials.");
+    }
+
+    let username = username.unwrap_or(DEFAULT_ROOT_USERNAME.to_string());
+    let password = password.unwrap_or(DEFAULT_ROOT_PASSWORD.to_string());
+    if username.is_empty() || password.is_empty() {
+        panic!("Root user credentials are not set.");
+    }
+    if username.len() < MIN_USERNAME_LENGTH {
+        panic!("Root username is too short.");
+    }
+    if username.len() > MAX_USERNAME_LENGTH {
+        panic!("Root username is too long.");
+    }
+    if password.len() < MIN_PASSWORD_LENGTH {
+        panic!("Root password is too short.");
+    }
+    if password.len() > MAX_PASSWORD_LENGTH {
+        panic!("Root password is too long.");
+    }
+    let user = User::root(&username, &password);
+    user
 }
 
 pub fn create_default_executor<D>() -> Runtime<D>
diff --git a/core/server/src/main.rs b/core/server/src/main.rs
index a5bbbe48..cd79cedc 100644
--- a/core/server/src/main.rs
+++ b/core/server/src/main.rs
@@ -21,11 +21,13 @@ use std::thread::available_parallelism;
 use anyhow::Result;
 use clap::Parser;
 use dotenvy::dotenv;
+use error_set::ErrContext;
 use figlet_rs::FIGfont;
 use monoio::Buildable;
 use server::args::Args;
 use server::bootstrap::{
     create_default_executor, create_directories, create_root_user, create_shard_connections,
+    load_config,
 };
 use server::channels::commands::archive_state::ArchiveStateExecutor;
 use server::channels::commands::clean_personal_access_tokens::CleanPersonalAccessTokensExecutor;
@@ -73,17 +75,12 @@ fn main() -> Result<(), ServerError> {
     }
 
     let args = Args::parse();
+    // TODO: I think we could get rid of config provider, since we support only TOML
+    // as config provider.
     let config_provider = config_provider::resolve(&args.config_provider)?;
     let config = std::thread::scope(|scope| {
         let config = scope
             .spawn(move || {
-                async fn load_config(
-                    config_provider: &ConfigProviderKind,
-                ) -> Result<ServerConfig, ServerError> {
-                    let config = ServerConfig::load(config_provider).await?;
-                    Ok(config)
-                }
-
                 let mut rt = create_default_executor::<monoio::IoUringDriver>();
                 rt.block_on(load_config(&config_provider))
             })
@@ -93,7 +90,7 @@ fn main() -> Result<(), ServerError> {
     })?;
 
     // Create directories and root user.
-    // Remove `local_data` directory if run with `--fresh` flag. 
+    // Remove `local_data` directory if run with `--fresh` flag.
     std::thread::scope(|scope| {
         scope
             .spawn(|| {
@@ -115,14 +112,15 @@ fn main() -> Result<(), ServerError> {
                         }
                     }
 
-                    // Create directories and root user
-                    create_directories().await;
-                    create_root_user().await;
-                });
+                    // Create directories. 
+                    create_directories(&config.system).await?;
+                    Ok::<(), ServerError>(())
+                })
             })
             .join()
-            .expect("Failed to create directories and root user");
-    });
+            .expect("Failed to create directories and root user")
+    })
+    .with_error_context(|err| format!("Failed to create directories, err: {err}"))?;
 
     // Initialize logging
     let mut logging = Logging::new(config.telemetry.clone());
@@ -153,20 +151,21 @@ fn main() -> Result<(), ServerError> {
                 // let urb = io_uring::IoUring::builder();
                 // TODO: Shall we make the size of ring be configureable ?
                 let mut rt = monoio::RuntimeBuilder::<monoio::IoUringDriver>::new()
-                    //.uring_builder(urb.setup_coop_taskrun())
+                    //.uring_builder(urb.setup_coop_taskrun()) // broken shit.
                     .with_entries(1024) // Default size
                     .enable_timer()
                     .build()
                     .expect(format!("Failed to build monoio runtime for shard-{id}").as_str());
                 rt.block_on(async move {
                     let builder = IggyShard::builder();
-                    let shard = builder
+                    let mut shard = builder
                         .id(id)
                         .connections(connections)
                         .server_config(server_config)
-                        .build_and_init()
+                        .build()
                         .await;
 
+                    shard.init().await;
                     shard.assert_init();
                 })
             })
diff --git a/core/server/src/shard/builder.rs b/core/server/src/shard/builder.rs
index 750e2b71..1a36a8fe 100644
--- a/core/server/src/shard/builder.rs
+++ b/core/server/src/shard/builder.rs
@@ -43,7 +43,7 @@ impl IggyShardBuilder {
         self
     }
 
-    pub async fn build_and_init(self) -> IggyShard {
+    pub async fn build(self) -> IggyShard {
         let id = self.id.unwrap();
         let config = self.config.unwrap();
         let connections = self.connections.unwrap();
diff --git a/core/server/src/shard/mod.rs b/core/server/src/shard/mod.rs
index dcb089d9..5373b766 100644
--- a/core/server/src/shard/mod.rs
+++ b/core/server/src/shard/mod.rs
@@ -15,6 +15,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
 pub mod builder;
 pub mod connector;
 pub mod frame;
@@ -27,7 +28,7 @@ use frame::ShardFrame;
 use namespace::IggyNamespace;
 use std::cell::{Cell, RefCell};
 
-use crate::configs::server::ServerConfig;
+use crate::{bootstrap::create_root_user, configs::server::ServerConfig};
 pub(crate) struct Shard {
     id: u16,
     connection: ShardConnector<ShardFrame>,
@@ -49,7 +50,7 @@ struct ShardInfo {
 pub struct IggyShard {
     pub id: u16,
     shards: Vec<Shard>,
-    //shards_table: RefCell<HashMap<IggyNamespace, ShardInfo>>,
+    shards_table: RefCell<HashMap<IggyNamespace, ShardInfo>>,
 
     //pub(crate) permissioner: RefCell<Permissioner>,
     //pub(crate) streams: RwLock<HashMap<u32, Stream>>,
@@ -84,6 +85,7 @@ impl IggyShard {
         Self {
             id,
             shards,
+            shards_table: Default::default(),
             config,
             stop_receiver,
             stop_sender,
@@ -91,5 +93,27 @@ impl IggyShard {
         }
     }
 
+    pub async fn init(&mut self) {
+        let user = create_root_user();
+        self.load_state().await;
+        self.load_users().await;
+        // Add default root user.
+        todo!();
+        self.load_streams().await;
+
+    }
+
+    async fn load_state(&self) {
+        todo!()
+    }
+
+    async fn load_users(&self) {
+        todo!()
+    }
+
+    async fn load_streams(&self) {
+        todo!()
+    }
+
     pub fn assert_init(&self) {}
 }
diff --git a/core/server/src/shard/namespace.rs b/core/server/src/shard/namespace.rs
index 4b972125..d9aedfb7 100644
--- a/core/server/src/shard/namespace.rs
+++ b/core/server/src/shard/namespace.rs
@@ -15,6 +15,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
 #[derive(Debug, Clone, Eq, PartialEq, Hash)]
 pub struct IggyNamespace {
     pub(crate) stream_id: u32,
diff --git a/core/server/src/state/file.rs b/core/server/src/state/file.rs
index c9e77977..f162ed75 100644
--- a/core/server/src/state/file.rs
+++ b/core/server/src/state/file.rs
@@ -84,18 +84,7 @@ impl FileState {
 
 impl State for FileState {
     async fn init(&self) -> Result<Vec<StateEntry>, IggyError> {
-        if !Path::new(&self.path).exists() {
-            info!("State file does not exist, creating a new one");
-            self.persister
-                .overwrite(&self.path, &[])
-                .await
-                .with_error_context(|error| {
-                    format!(
-                        "{COMPONENT} (error: {error}) - failed to overwrite state file, path: {}",
-                        self.path
-                    )
-                })?;
-        }
+        assert!(Path::new(&self.path).exists());
 
         let entries = self.load_entries().await.with_error_context(|error| {
             format!("{COMPONENT} (error: {error}) - failed to load entries")


(iggy) 02/04: feat(io_uring): bootstrap the main function (#1788)

Posted by gk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gkoszyk pushed a commit to branch io_uring_tpc
in repository https://gitbox.apache.org/repos/asf/iggy.git

commit d50f2877d32ac1e7797fac6f3f5c99796d176ca6
Author: Grzegorz Koszyk <11...@users.noreply.github.com>
AuthorDate: Wed May 14 20:38:55 2025 +0200

    feat(io_uring): bootstrap the main function (#1788)
---
 Cargo.lock                         |  1 +
 core/server/Cargo.toml             |  1 +
 core/server/src/bootstrap.rs       | 20 ++++++++++
 core/server/src/lib.rs             |  4 +-
 core/server/src/main.rs            | 80 ++++++++++++++++++++++++++++++++++++--
 core/server/src/shard/builder.rs   | 73 ++++++++++++++++++++++++++++++++++
 core/server/src/shard/connector.rs |  2 +
 core/server/src/shard/frame.rs     |  3 +-
 core/server/src/shard/mod.rs       | 79 +++++++++++++++++++++++++++----------
 9 files changed, 234 insertions(+), 29 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index e3a0fa12..de8beee8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6252,6 +6252,7 @@ dependencies = [
  "futures",
  "human-repr",
  "iggy_common",
+ "io-uring",
  "jsonwebtoken",
  "lending-iterator",
  "mimalloc",
diff --git a/core/server/Cargo.toml b/core/server/Cargo.toml
index 5323997c..63731abb 100644
--- a/core/server/Cargo.toml
+++ b/core/server/Cargo.toml
@@ -56,6 +56,7 @@ flume = { workspace = true }
 futures = { workspace = true }
 human-repr = { workspace = true }
 iggy_common = { workspace = true }
+io-uring = "0.6"
 jsonwebtoken = "9.3.1"
 lending-iterator = "0.1.7"
 mimalloc = { workspace = true, optional = true }
diff --git a/core/server/src/bootstrap.rs b/core/server/src/bootstrap.rs
index e69de29b..8eacf223 100644
--- a/core/server/src/bootstrap.rs
+++ b/core/server/src/bootstrap.rs
@@ -0,0 +1,20 @@
+use crate::shard::{connector::ShardConnector, frame::ShardFrame};
+use std::ops::Range;
+
+pub fn create_shard_connections(shards_set: Range<usize>) -> Vec<ShardConnector<ShardFrame>> {
+    let shards_count = shards_set.len();
+    let connectors = shards_set
+        .into_iter()
+        .map(|id| ShardConnector::new(id as u16, shards_count))
+        .collect();
+
+    connectors
+}
+
+pub async fn create_directories() {
+
+}
+
+pub async fn create_root_user() {
+
+}
diff --git a/core/server/src/lib.rs b/core/server/src/lib.rs
index 572301e0..e64837da 100644
--- a/core/server/src/lib.rs
+++ b/core/server/src/lib.rs
@@ -26,18 +26,18 @@ static GLOBAL: MiMalloc = MiMalloc;
 #[cfg(windows)]
 compile_error!("iggy-server doesn't support windows.");
 
-mod bootstrap;
 pub mod archiver;
 pub mod args;
 pub mod binary;
+pub mod bootstrap;
 pub mod channels;
 pub(crate) mod compat;
 pub mod configs;
-pub mod shard;
 pub mod http;
 pub mod log;
 pub mod quic;
 pub mod server_error;
+pub mod shard;
 pub mod state;
 pub mod streaming;
 pub mod tcp;
diff --git a/core/server/src/main.rs b/core/server/src/main.rs
index d9d9d753..bb92796d 100644
--- a/core/server/src/main.rs
+++ b/core/server/src/main.rs
@@ -16,11 +16,15 @@
  * under the License.
  */
 
+use std::thread::available_parallelism;
+
 use anyhow::Result;
 use clap::Parser;
 use dotenvy::dotenv;
 use figlet_rs::FIGfont;
+use monoio::Buildable;
 use server::args::Args;
+use server::bootstrap::{create_directories, create_root_user, create_shard_connections};
 use server::channels::commands::archive_state::ArchiveStateExecutor;
 use server::channels::commands::clean_personal_access_tokens::CleanPersonalAccessTokensExecutor;
 use server::channels::commands::maintain_messages::MaintainMessagesExecutor;
@@ -37,15 +41,15 @@ use server::log::logger::Logging;
 use server::log::tokio_console::Logging;
 use server::quic::quic_server;
 use server::server_error::ServerError;
+use server::shard::IggyShard;
 use server::streaming::systems::system::{SharedSystem, System};
 use server::streaming::utils::MemoryPool;
 use server::tcp::tcp_server;
 use tokio::time::Instant;
 use tracing::{info, instrument};
 
-#[tokio::main]
 #[instrument(skip_all, name = "trace_start_server")]
-async fn main() -> Result<(), ServerError> {
+fn main() -> Result<(), ServerError> {
     let startup_timestamp = Instant::now();
     let standard_font = FIGfont::standard().unwrap();
     let figure = standard_font.convert("Iggy Server");
@@ -68,6 +72,24 @@ async fn main() -> Result<(), ServerError> {
 
     let args = Args::parse();
     let config_provider = config_provider::resolve(&args.config_provider)?;
+    let config = ServerConfig::default();
+    //TODO: Load config.
+    /*
+    let xd = std::thread::scope(|scope| {
+        let config = scope.spawn(move || {
+            let mut rt = monoio::RuntimeBuilder::<monoio::IoUringDriver>::new().build().unwrap();
+            let config: Result<ServerConfig, ServerError> = rt.block_on(async {
+                let config = ServerConfig::load(&config_provider).await?;
+                create_directories().await;
+                create_root_user().await;
+                Ok(config)
+            });
+            config
+        }).join().unwrap();
+        config
+    });
+    */
+    /*
     let config = ServerConfig::load(&config_provider).await?;
     if args.fresh {
         let system_path = config.system.get_system_path();
@@ -83,9 +105,56 @@ async fn main() -> Result<(), ServerError> {
     }
     let mut logging = Logging::new(config.telemetry.clone());
     logging.early_init();
+    */
+    // TODO: Make this configurable from config as a range
+    // for example this instance of Iggy will use cores from 0..4
+    let available_cpus = available_parallelism()
+        .expect("Failed to get num of cores")
+        .into();
+    let shards_count = available_cpus;
+    let shards_set = 0..shards_count;
+
+    let connections = create_shard_connections(shards_set.clone());
+
+    for shard_id in shards_set {
+        let id = shard_id as u16;
+        let connections = connections.clone();
+        let server_config = config.clone();
+        std::thread::Builder::new()
+            .name(format!("shard-{id}"))
+            .spawn(move || {
+                monoio::utils::bind_to_cpu_set(Some(shard_id))
+                    .expect(format!("Failed to set CPU affinity for shard-{id}").as_str());
 
+                // TODO: Figure out what else we could tweak there
+                // We for sure want to disable the userspace interrupts on new cq entry (set_coop_taskrun)
+                // let urb = io_uring::IoUring::builder();
+                // TODO: Shall we make the size of ring be configureable ?
+                let mut rt = monoio::RuntimeBuilder::<monoio::IoUringDriver>::new()
+                    //.uring_builder(urb.setup_coop_taskrun())
+                    .with_entries(1024) // Default size
+                    .enable_timer()
+                    .build()
+                    .expect(format!("Failed to build monoio runtime for shard-{id}").as_str());
+                rt.block_on(async move {
+                    let builder = IggyShard::builder();
+                    let shard = builder
+                        .id(id)
+                        .connections(connections)
+                        .server_config(server_config)
+                        .build_and_init()
+                        .await;
+
+                    shard.assert_init();
+                })
+            })
+            .expect(format!("Failed to spawn thread for shard-{id}").as_str())
+            .join()
+            .expect(format!("Failed to join thread for shard-{id}").as_str());
+    }
     // From this point on, we can use tracing macros to log messages.
 
+    /*
     logging.late_init(config.system.get_system_path(), &config.system.logging)?;
 
     #[cfg(feature = "disable-mimalloc")]
@@ -102,13 +171,15 @@ async fn main() -> Result<(), ServerError> {
         config.data_maintenance.clone(),
         config.personal_access_token.clone(),
     ));
+    */
 
     // Workaround to ensure that the statistics are initialized before the server
     // loads streams and starts accepting connections. This is necessary to
     // have the correct statistics when the server starts.
-    system.write().await.get_stats().await?;
-    system.write().await.init().await?;
+    //system.write().await.get_stats().await?;
+    //system.write().await.init().await?;
 
+    /*
     let _command_handler = BackgroundServerCommandHandler::new(system.clone(), &config)
         .install_handler(SaveMessagesExecutor)
         .install_handler(MaintainMessagesExecutor)
@@ -184,5 +255,6 @@ async fn main() -> Result<(), ServerError> {
         "Iggy server has shutdown successfully. Shutdown took {} ms.",
         elapsed_time.as_millis()
     );
+    */
     Ok(())
 }
diff --git a/core/server/src/shard/builder.rs b/core/server/src/shard/builder.rs
new file mode 100644
index 00000000..08a02e54
--- /dev/null
+++ b/core/server/src/shard/builder.rs
@@ -0,0 +1,73 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use crate::{configs::server::ServerConfig, shard::Shard};
+
+use super::{connector::ShardConnector, frame::ShardFrame, IggyShard};
+
+#[derive(Default)]
+pub struct IggyShardBuilder {
+    id: Option<u16>,
+    connections: Option<Vec<ShardConnector<ShardFrame>>>,
+    config: Option<ServerConfig>,
+}
+
+impl IggyShardBuilder {
+    pub fn id(mut self, id: u16) -> Self {
+        self.id = Some(id);
+        self
+    }
+
+    pub fn connections(mut self, connections: Vec<ShardConnector<ShardFrame>>) -> Self {
+        self.connections = Some(connections);
+        self
+    }
+
+    pub fn server_config(mut self, config: ServerConfig) -> Self {
+        self.config = Some(config);
+        self
+    }
+
+    pub async fn build_and_init(self) -> IggyShard {
+        let id = self.id.unwrap();
+        let config = self.config.unwrap();
+        let connections = self.connections.unwrap();
+        let (stop_sender, stop_receiver, receiver) = connections
+            .iter()
+            .filter(|c| c.id == id)
+            .map(|c| {
+                (
+                    c.stop_sender.clone(),
+                    c.stop_receiver.clone(),
+                    c.receiver.clone(),
+                )
+            })
+            .next()
+            .expect("Failed to find connection with the specified ID");
+        let shards = connections.into_iter().map(Shard::new).collect();
+
+        IggyShard::new(
+            id,
+            shards,
+            config,
+            stop_receiver,
+            stop_sender,
+            receiver,
+        )
+    }
+}
diff --git a/core/server/src/shard/connector.rs b/core/server/src/shard/connector.rs
index 349218ba..1d762d68 100644
--- a/core/server/src/shard/connector.rs
+++ b/core/server/src/shard/connector.rs
@@ -54,6 +54,8 @@ impl<T: Clone> ShardConnector<T> {
     }
 }
 
+// TODO: I think those Arcs can be replaced with 'static lifetimes...
+// Those shards will live for the entire duration of the application.
 #[derive(Clone)]
 pub struct Receiver<T> {
     channel: Arc<ShardedChannel<T>>,
diff --git a/core/server/src/shard/frame.rs b/core/server/src/shard/frame.rs
index b1524ad6..a2c1220f 100644
--- a/core/server/src/shard/frame.rs
+++ b/core/server/src/shard/frame.rs
@@ -27,8 +27,7 @@ pub enum ShardMessage {
 }
 
 #[derive(Debug, Clone)]
-pub enum ShardEvent {
-}
+pub enum ShardEvent {}
 
 #[derive(Debug)]
 pub enum ShardResponse {
diff --git a/core/server/src/shard/mod.rs b/core/server/src/shard/mod.rs
index 6ccbcab3..bffaca45 100644
--- a/core/server/src/shard/mod.rs
+++ b/core/server/src/shard/mod.rs
@@ -15,22 +15,33 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-mod connector;
-mod namespace;
-mod frame;
+pub mod builder;
+pub mod connector;
+pub mod frame;
+pub mod namespace;
 
-
-
-use std::cell::RefCell;
 use ahash::HashMap;
-use connector::ShardConnector;
+use builder::IggyShardBuilder;
+use connector::{Receiver, ShardConnector, StopReceiver, StopSender};
 use frame::ShardFrame;
 use namespace::IggyNamespace;
-struct Shard {
+use std::cell::{Cell, RefCell};
+
+use crate::configs::server::ServerConfig;
+pub(crate) struct Shard {
     id: u16,
     connection: ShardConnector<ShardFrame>,
 }
 
+impl Shard {
+    pub fn new(connection: ShardConnector<ShardFrame>) -> Self {
+        Self {
+            id: connection.id,
+            connection,
+        }
+    }
+}
+
 struct ShardInfo {
     id: u16,
 }
@@ -38,22 +49,48 @@ struct ShardInfo {
 pub struct IggyShard {
     pub id: u16,
     shards: Vec<Shard>,
-    shards_table: RefCell<HashMap<IggyNamespace, ShardInfo>>,
+    //shards_table: RefCell<HashMap<IggyNamespace, ShardInfo>>,
 
-    pub(crate) permissioner: RefCell<Permissioner>,
-    pub(crate) storage: Rc<SystemStorage>,
-    pub(crate) streams: RwLock<HashMap<u32, Stream>>,
-    pub(crate) streams_ids: RefCell<HashMap<String, u32>>,
-    pub(crate) users: RefCell<HashMap<UserId, User>>,
+    //pub(crate) permissioner: RefCell<Permissioner>,
+    //pub(crate) streams: RwLock<HashMap<u32, Stream>>,
+    //pub(crate) streams_ids: RefCell<HashMap<String, u32>>,
+    //pub(crate) users: RefCell<HashMap<UserId, User>>,
 
     // TODO - get rid of this dynamic dispatch.
-    pub(crate) state: Rc<FileState>,
-    pub(crate) encryptor: Option<Rc<dyn Encryptor>>,
-    pub(crate) config: ServerConfig,
-    pub(crate) client_manager: RefCell<ClientManager>,
-    pub(crate) active_sessions: RefCell<Vec<Session>>,
-    pub(crate) metrics: Metrics,
+    //pub(crate) state: Rc<FileState>,
+    //pub(crate) encryptor: Option<Rc<dyn Encryptor>>,
+    config: ServerConfig,
+    //pub(crate) client_manager: RefCell<ClientManager>,
+    //pub(crate) active_sessions: RefCell<Vec<Session>>,
+    //pub(crate) metrics: Metrics,
     pub message_receiver: Cell<Option<Receiver<ShardFrame>>>,
     stop_receiver: StopReceiver,
     stop_sender: StopSender,
-}
\ No newline at end of file
+}
+
+impl IggyShard {
+    pub fn builder() -> IggyShardBuilder {
+        Default::default()
+    }
+
+    pub(crate) fn new(
+        id: u16,
+        shards: Vec<Shard>,
+        config: ServerConfig,
+        stop_receiver: StopReceiver,
+        stop_sender: StopSender,
+        shard_messages_receiver: Receiver<ShardFrame>,
+    ) -> Self {
+        Self {
+            id,
+            shards,
+            config,
+            stop_receiver,
+            stop_sender,
+            message_receiver: Cell::new(Some(shard_messages_receiver)),
+        }
+    }
+
+    pub fn assert_init(&self) {
+    }
+}


(iggy) 01/04: initial setup and migration of shards

Posted by gk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gkoszyk pushed a commit to branch io_uring_tpc
in repository https://gitbox.apache.org/repos/asf/iggy.git

commit 6685eacff0347855ce12775f5e5692e24469b04e
Author: numinex <g....@gmail.com>
AuthorDate: Sun May 11 17:17:51 2025 +0200

    initial setup and migration of shards
---
 Cargo.lock                                     | 114 +++++++++++++++++++-
 core/server/Cargo.toml                         |   2 +
 core/server/src/bootstrap.rs                   |   0
 core/server/src/lib.rs                         |   2 +
 core/server/src/shard/connector.rs             | 140 +++++++++++++++++++++++++
 core/server/src/shard/frame.rs                 |  68 ++++++++++++
 core/server/src/shard/mod.rs                   |  59 +++++++++++
 core/server/src/{lib.rs => shard/namespace.rs} |  48 +++------
 8 files changed, 397 insertions(+), 36 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 96dee5e0..e3a0fa12 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -142,7 +142,7 @@ dependencies = [
  "actix-utils",
  "futures-core",
  "futures-util",
- "mio",
+ "mio 1.0.3",
  "socket2",
  "tokio",
  "tracing",
@@ -615,6 +615,17 @@ dependencies = [
  "url",
 ]
 
+[[package]]
+name = "auto-const-array"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fd73835ad7deb4bd2b389e6f10333b143f025d607c55ca04c66a0bcc6bb2fc6d"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.101",
+]
+
 [[package]]
 name = "autocfg"
 version = "1.4.0"
@@ -2459,6 +2470,15 @@ dependencies = [
  "slab",
 ]
 
+[[package]]
+name = "fxhash"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c"
+dependencies = [
+ "byteorder",
+]
+
 [[package]]
 name = "generator"
 version = "0.8.4"
@@ -3697,6 +3717,16 @@ dependencies = [
  "zip",
 ]
 
+[[package]]
+name = "io-uring"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "595a0399f411a508feb2ec1e970a4a30c249351e30208960d58298de8660b0e5"
+dependencies = [
+ "bitflags 1.3.2",
+ "libc",
+]
+
 [[package]]
 name = "ipnet"
 version = "2.11.0"
@@ -4146,6 +4176,15 @@ version = "2.7.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
 
+[[package]]
+name = "memoffset"
+version = "0.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4"
+dependencies = [
+ "autocfg",
+]
+
 [[package]]
 name = "mimalloc"
 version = "0.1.46"
@@ -4195,6 +4234,18 @@ dependencies = [
  "adler2",
 ]
 
+[[package]]
+name = "mio"
+version = "0.8.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
+dependencies = [
+ "libc",
+ "log",
+ "wasi 0.11.0+wasi-snapshot-preview1",
+ "windows-sys 0.48.0",
+]
+
 [[package]]
 name = "mio"
 version = "1.0.3"
@@ -4255,6 +4306,37 @@ dependencies = [
  "uuid",
 ]
 
+[[package]]
+name = "monoio"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3bd0f8bcde87b1949f95338b547543fcab187bc7e7a5024247e359a5e828ba6a"
+dependencies = [
+ "auto-const-array",
+ "bytes",
+ "fxhash",
+ "io-uring",
+ "libc",
+ "memchr",
+ "mio 0.8.11",
+ "monoio-macros",
+ "nix 0.26.4",
+ "pin-project-lite",
+ "socket2",
+ "windows-sys 0.48.0",
+]
+
+[[package]]
+name = "monoio-macros"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "176a5f5e69613d9e88337cf2a65e11135332b4efbcc628404a7c555e4452084c"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.101",
+]
+
 [[package]]
 name = "nanorand"
 version = "0.7.0"
@@ -4287,6 +4369,19 @@ version = "6.6.666"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "cf5a574dadd7941adeaa71823ecba5e28331b8313fb2e1c6a5c7e5981ea53ad6"
 
+[[package]]
+name = "nix"
+version = "0.26.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b"
+dependencies = [
+ "bitflags 1.3.2",
+ "cfg-if",
+ "libc",
+ "memoffset",
+ "pin-utils",
+]
+
 [[package]]
 name = "nix"
 version = "0.30.1"
@@ -4340,7 +4435,7 @@ dependencies = [
  "kqueue",
  "libc",
  "log",
- "mio",
+ "mio 1.0.3",
  "notify-types",
  "walkdir",
  "windows-sys 0.59.0",
@@ -6162,7 +6257,8 @@ dependencies = [
  "mimalloc",
  "mockall",
  "moka",
- "nix",
+ "monoio",
+ "nix 0.30.1",
  "once_cell",
  "openssl",
  "opentelemetry",
@@ -6181,6 +6277,7 @@ dependencies = [
  "serde",
  "serde_with",
  "serial_test",
+ "sharded_queue",
  "static-toml",
  "strum",
  "sysinfo 0.35.0",
@@ -6232,6 +6329,15 @@ dependencies = [
  "lazy_static",
 ]
 
+[[package]]
+name = "sharded_queue"
+version = "2.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a3499be28bd82560e75ad10457698607ba3cc389175ab47ac93279834ce1fab4"
+dependencies = [
+ "crossbeam-utils",
+]
+
 [[package]]
 name = "shlex"
 version = "1.3.0"
@@ -6671,7 +6777,7 @@ dependencies = [
  "backtrace",
  "bytes",
  "libc",
- "mio",
+ "mio 1.0.3",
  "parking_lot 0.12.3",
  "pin-project-lite",
  "signal-hook-registry",
diff --git a/core/server/Cargo.toml b/core/server/Cargo.toml
index e507ed47..5323997c 100644
--- a/core/server/Cargo.toml
+++ b/core/server/Cargo.toml
@@ -60,6 +60,7 @@ jsonwebtoken = "9.3.1"
 lending-iterator = "0.1.7"
 mimalloc = { workspace = true, optional = true }
 moka = { version = "0.12.10", features = ["future"] }
+monoio = "0.2.4"
 nix = { version = "0.30", features = ["fs"] }
 once_cell = "1.21.3"
 openssl = { workspace = true }
@@ -95,6 +96,7 @@ rustls-pemfile = "2.2.0"
 serde = { workspace = true }
 serde_with = { workspace = true }
 static-toml = "1.3.0"
+sharded_queue = "2.0.1"
 strum = { workspace = true }
 sysinfo = { workspace = true }
 tempfile = { workspace = true }
diff --git a/core/server/src/bootstrap.rs b/core/server/src/bootstrap.rs
new file mode 100644
index 00000000..e69de29b
diff --git a/core/server/src/lib.rs b/core/server/src/lib.rs
index 91d17586..572301e0 100644
--- a/core/server/src/lib.rs
+++ b/core/server/src/lib.rs
@@ -26,12 +26,14 @@ static GLOBAL: MiMalloc = MiMalloc;
 #[cfg(windows)]
 compile_error!("iggy-server doesn't support windows.");
 
+mod bootstrap;
 pub mod archiver;
 pub mod args;
 pub mod binary;
 pub mod channels;
 pub(crate) mod compat;
 pub mod configs;
+pub mod shard;
 pub mod http;
 pub mod log;
 pub mod quic;
diff --git a/core/server/src/shard/connector.rs b/core/server/src/shard/connector.rs
new file mode 100644
index 00000000..349218ba
--- /dev/null
+++ b/core/server/src/shard/connector.rs
@@ -0,0 +1,140 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+use futures::{task::AtomicWaker, Stream};
+use sharded_queue::ShardedQueue;
+use std::{
+    sync::{atomic::AtomicUsize, Arc},
+    task::Poll,
+};
+
+pub type StopSender = flume::Sender<()>;
+pub type StopReceiver = flume::Receiver<()>;
+
+#[derive(Clone)]
+pub struct ShardConnector<T> {
+    pub id: u16,
+    pub sender: Sender<T>,
+    pub receiver: Receiver<T>,
+    pub stop_receiver: StopReceiver,
+    pub stop_sender: StopSender,
+}
+
+// TODO(numinex) - replace flume with async_channel
+impl<T: Clone> ShardConnector<T> {
+    pub fn new(id: u16, max_concurrent_thread_count: usize) -> Self {
+        let channel = Arc::new(ShardedChannel::new(max_concurrent_thread_count));
+        let (sender, receiver) = channel.unbounded();
+        let (stop_sender, stop_receiver) = flume::bounded(1);
+        Self {
+            id,
+            receiver,
+            sender,
+            stop_receiver,
+            stop_sender,
+        }
+    }
+
+    pub fn send(&self, data: T) {
+        self.sender.send(data);
+    }
+}
+
+#[derive(Clone)]
+pub struct Receiver<T> {
+    channel: Arc<ShardedChannel<T>>,
+}
+
+#[derive(Clone)]
+pub struct Sender<T> {
+    channel: Arc<ShardedChannel<T>>,
+}
+
+impl<T> Sender<T> {
+    pub fn send(&self, data: T) {
+        self.channel
+            .task_queue
+            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
+        self.channel.queue.push_back(data);
+        self.channel.waker.wake();
+    }
+}
+
+pub struct ShardedChannel<T> {
+    queue: ShardedQueue<T>,
+    task_queue: AtomicUsize,
+    waker: AtomicWaker,
+}
+
+impl<T> ShardedChannel<T> {
+    pub fn new(max_concurrent_thread_count: usize) -> Self {
+        let waker = AtomicWaker::new();
+
+        Self {
+            queue: ShardedQueue::new(max_concurrent_thread_count),
+            task_queue: AtomicUsize::new(0),
+            waker,
+        }
+    }
+}
+
+pub trait ShardedChannelsSplit<T: Clone> {
+    fn unbounded(&self) -> (Sender<T>, Receiver<T>);
+
+    fn sender(&self) -> Sender<T>;
+}
+
+impl<T: Clone> ShardedChannelsSplit<T> for Arc<ShardedChannel<T>> {
+    fn unbounded(&self) -> (Sender<T>, Receiver<T>) {
+        let tx = self.sender();
+        let rx = Receiver {
+            channel: Arc::clone(self),
+        };
+
+        (tx, rx)
+    }
+
+    fn sender(&self) -> Sender<T> {
+        Sender {
+            channel: Arc::clone(self),
+        }
+    }
+}
+
+impl<T> Stream for Receiver<T> {
+    type Item = T;
+    fn poll_next(
+        self: std::pin::Pin<&mut Self>,
+        cx: &mut std::task::Context<'_>,
+    ) -> std::task::Poll<Option<Self::Item>> {
+        let old = self
+            .channel
+            .task_queue
+            .load(std::sync::atomic::Ordering::Relaxed);
+        if old == 0 {
+            self.channel.waker.register(cx.waker());
+            return Poll::Pending;
+        }
+
+        assert!(old > 0);
+        self.channel
+            .task_queue
+            .fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
+        let item = self.channel.queue.pop_front_or_spin_wait_item();
+        Poll::Ready(Some(item))
+    }
+}
diff --git a/core/server/src/shard/frame.rs b/core/server/src/shard/frame.rs
new file mode 100644
index 00000000..b1524ad6
--- /dev/null
+++ b/core/server/src/shard/frame.rs
@@ -0,0 +1,68 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+use bytes::Bytes;
+use flume::Sender;
+use iggy_common::IggyError;
+
+#[derive(Debug, Clone)]
+pub enum ShardMessage {
+    //TODO: Fixme
+    //Command(ServerCommand),
+    Event(ShardEvent),
+}
+
+#[derive(Debug, Clone)]
+pub enum ShardEvent {
+}
+
+#[derive(Debug)]
+pub enum ShardResponse {
+    BinaryResponse(Bytes),
+    ErrorResponse(IggyError),
+}
+
+#[derive(Debug, Clone)]
+pub struct ShardFrame {
+    pub client_id: u32,
+    pub message: ShardMessage,
+    pub response_sender: Option<Sender<ShardResponse>>,
+}
+
+impl ShardFrame {
+    pub fn new(
+        client_id: u32,
+        message: ShardMessage,
+        response_sender: Option<Sender<ShardResponse>>,
+    ) -> Self {
+        Self {
+            client_id,
+            message,
+            response_sender,
+        }
+    }
+}
+
+#[macro_export]
+macro_rules! handle_response {
+    ($sender:expr, $response:expr) => {
+        match $response {
+            ShardResponse::BinaryResponse(payload) => $sender.send_ok_response(&payload).await?,
+            ShardResponse::ErrorResponse(err) => $sender.send_error_response(err).await?,
+        }
+    };
+}
diff --git a/core/server/src/shard/mod.rs b/core/server/src/shard/mod.rs
new file mode 100644
index 00000000..6ccbcab3
--- /dev/null
+++ b/core/server/src/shard/mod.rs
@@ -0,0 +1,59 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+mod connector;
+mod namespace;
+mod frame;
+
+
+
+use std::cell::RefCell;
+use ahash::HashMap;
+use connector::ShardConnector;
+use frame::ShardFrame;
+use namespace::IggyNamespace;
+struct Shard {
+    id: u16,
+    connection: ShardConnector<ShardFrame>,
+}
+
+struct ShardInfo {
+    id: u16,
+}
+
+pub struct IggyShard {
+    pub id: u16,
+    shards: Vec<Shard>,
+    shards_table: RefCell<HashMap<IggyNamespace, ShardInfo>>,
+
+    pub(crate) permissioner: RefCell<Permissioner>,
+    pub(crate) storage: Rc<SystemStorage>,
+    pub(crate) streams: RwLock<HashMap<u32, Stream>>,
+    pub(crate) streams_ids: RefCell<HashMap<String, u32>>,
+    pub(crate) users: RefCell<HashMap<UserId, User>>,
+
+    // TODO - get rid of this dynamic dispatch.
+    pub(crate) state: Rc<FileState>,
+    pub(crate) encryptor: Option<Rc<dyn Encryptor>>,
+    pub(crate) config: ServerConfig,
+    pub(crate) client_manager: RefCell<ClientManager>,
+    pub(crate) active_sessions: RefCell<Vec<Session>>,
+    pub(crate) metrics: Metrics,
+    pub message_receiver: Cell<Option<Receiver<ShardFrame>>>,
+    stop_receiver: StopReceiver,
+    stop_sender: StopSender,
+}
\ No newline at end of file
diff --git a/core/server/src/lib.rs b/core/server/src/shard/namespace.rs
similarity index 51%
copy from core/server/src/lib.rs
copy to core/server/src/shard/namespace.rs
index 91d17586..4b972125 100644
--- a/core/server/src/lib.rs
+++ b/core/server/src/shard/namespace.rs
@@ -15,39 +15,23 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+#[derive(Debug, Clone, Eq, PartialEq, Hash)]
+pub struct IggyNamespace {
+    pub(crate) stream_id: u32,
+    pub(crate) topic_id: u32,
+    pub(crate) partition_id: u32,
+}
 
-#[cfg(not(feature = "disable-mimalloc"))]
-use mimalloc::MiMalloc;
-
-#[cfg(not(feature = "disable-mimalloc"))]
-#[global_allocator]
-static GLOBAL: MiMalloc = MiMalloc;
-
-#[cfg(windows)]
-compile_error!("iggy-server doesn't support windows.");
-
-pub mod archiver;
-pub mod args;
-pub mod binary;
-pub mod channels;
-pub(crate) mod compat;
-pub mod configs;
-pub mod http;
-pub mod log;
-pub mod quic;
-pub mod server_error;
-pub mod state;
-pub mod streaming;
-pub mod tcp;
-pub mod versioning;
-
-const VERSION: &str = env!("CARGO_PKG_VERSION");
-const IGGY_ROOT_USERNAME_ENV: &str = "IGGY_ROOT_USERNAME";
-const IGGY_ROOT_PASSWORD_ENV: &str = "IGGY_ROOT_PASSWORD";
+impl IggyNamespace {
+    pub fn new(stream_id: u32, topic_id: u32, partition_id: u32) -> Self {
+        Self {
+            stream_id,
+            topic_id,
+            partition_id,
+        }
+    }
 
-pub(crate) fn map_toggle_str<'a>(enabled: bool) -> &'a str {
-    match enabled {
-        true => "enabled",
-        false => "disabled",
+    pub fn generate_hash(&self) -> u32 {
+        todo!();
     }
 }


(iggy) 03/04: feat(io_uring): load server_config (#1798)

Posted by gk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gkoszyk pushed a commit to branch io_uring_tpc
in repository https://gitbox.apache.org/repos/asf/iggy.git

commit db7eb71665e7b73e85dcdf2f8c41d86cee2c04fc
Author: Grzegorz Koszyk <11...@users.noreply.github.com>
AuthorDate: Fri May 16 21:37:23 2025 +0200

    feat(io_uring): load server_config (#1798)
---
 core/server/src/bootstrap.rs     | 13 +++++-
 core/server/src/main.rs          | 90 +++++++++++++++++++++++++---------------
 core/server/src/shard/builder.rs |  9 +---
 core/server/src/shard/mod.rs     |  3 +-
 4 files changed, 70 insertions(+), 45 deletions(-)

diff --git a/core/server/src/bootstrap.rs b/core/server/src/bootstrap.rs
index 8eacf223..fd6a5aa0 100644
--- a/core/server/src/bootstrap.rs
+++ b/core/server/src/bootstrap.rs
@@ -1,3 +1,5 @@
+use monoio::{Buildable, Driver, Runtime};
+
 use crate::shard::{connector::ShardConnector, frame::ShardFrame};
 use std::ops::Range;
 
@@ -12,9 +14,18 @@ pub fn create_shard_connections(shards_set: Range<usize>) -> Vec<ShardConnector<
 }
 
 pub async fn create_directories() {
-
+    todo!();
 }
 
 pub async fn create_root_user() {
+    todo!();
+}
 
+pub fn create_default_executor<D>() -> Runtime<D>
+where
+    D: Driver + Buildable,
+{
+    let builder = monoio::RuntimeBuilder::<D>::new();
+    let rt = Buildable::build(builder).expect("Failed to create default runtime");
+    rt
 }
diff --git a/core/server/src/main.rs b/core/server/src/main.rs
index bb92796d..a5bbbe48 100644
--- a/core/server/src/main.rs
+++ b/core/server/src/main.rs
@@ -24,7 +24,9 @@ use dotenvy::dotenv;
 use figlet_rs::FIGfont;
 use monoio::Buildable;
 use server::args::Args;
-use server::bootstrap::{create_directories, create_root_user, create_shard_connections};
+use server::bootstrap::{
+    create_default_executor, create_directories, create_root_user, create_shard_connections,
+};
 use server::channels::commands::archive_state::ArchiveStateExecutor;
 use server::channels::commands::clean_personal_access_tokens::CleanPersonalAccessTokensExecutor;
 use server::channels::commands::maintain_messages::MaintainMessagesExecutor;
@@ -32,7 +34,7 @@ use server::channels::commands::print_sysinfo::SysInfoPrintExecutor;
 use server::channels::commands::save_messages::SaveMessagesExecutor;
 use server::channels::commands::verify_heartbeats::VerifyHeartbeatsExecutor;
 use server::channels::handler::BackgroundServerCommandHandler;
-use server::configs::config_provider;
+use server::configs::config_provider::{self, ConfigProviderKind};
 use server::configs::server::ServerConfig;
 use server::http::http_server;
 #[cfg(not(feature = "tokio-console"))]
@@ -72,40 +74,60 @@ fn main() -> Result<(), ServerError> {
 
     let args = Args::parse();
     let config_provider = config_provider::resolve(&args.config_provider)?;
-    let config = ServerConfig::default();
-    //TODO: Load config.
-    /*
-    let xd = std::thread::scope(|scope| {
-        let config = scope.spawn(move || {
-            let mut rt = monoio::RuntimeBuilder::<monoio::IoUringDriver>::new().build().unwrap();
-            let config: Result<ServerConfig, ServerError> = rt.block_on(async {
-                let config = ServerConfig::load(&config_provider).await?;
-                create_directories().await;
-                create_root_user().await;
-                Ok(config)
-            });
-            config
-        }).join().unwrap();
+    let config = std::thread::scope(|scope| {
+        let config = scope
+            .spawn(move || {
+                async fn load_config(
+                    config_provider: &ConfigProviderKind,
+                ) -> Result<ServerConfig, ServerError> {
+                    let config = ServerConfig::load(config_provider).await?;
+                    Ok(config)
+                }
+
+                let mut rt = create_default_executor::<monoio::IoUringDriver>();
+                rt.block_on(load_config(&config_provider))
+            })
+            .join()
+            .expect("Failed to load config");
         config
+    })?;
+
+    // Create directories and root user.
+    // Remove `local_data` directory if run with `--fresh` flag. 
+    std::thread::scope(|scope| {
+        scope
+            .spawn(|| {
+                let mut rt = create_default_executor::<monoio::IoUringDriver>();
+                rt.block_on(async {
+                    if args.fresh {
+                        let system_path = config.system.get_system_path();
+                        if monoio::fs::metadata(&system_path).await.is_ok() {
+                            println!(
+                                "Removing system path at: {} because `--fresh` flag was set",
+                                system_path
+                            );
+                            //TODO: Impl dir walk and remove the files
+                            /*
+                            if let Err(e) = tokio::fs::remove_dir_all(&system_path).await {
+                                eprintln!("Failed to remove system path at {}: {}", system_path, e);
+                            }
+                            */
+                        }
+                    }
+
+                    // Create directories and root user
+                    create_directories().await;
+                    create_root_user().await;
+                });
+            })
+            .join()
+            .expect("Failed to create directories and root user");
     });
-    */
-    /*
-    let config = ServerConfig::load(&config_provider).await?;
-    if args.fresh {
-        let system_path = config.system.get_system_path();
-        if tokio::fs::metadata(&system_path).await.is_ok() {
-            println!(
-                "Removing system path at: {} because `--fresh` flag was set",
-                system_path
-            );
-            if let Err(e) = tokio::fs::remove_dir_all(&system_path).await {
-                eprintln!("Failed to remove system path at {}: {}", system_path, e);
-            }
-        }
-    }
+
+    // Initialize logging
     let mut logging = Logging::new(config.telemetry.clone());
     logging.early_init();
-    */
+
     // TODO: Make this configurable from config as a range
     // for example this instance of Iggy will use cores from 0..4
     let available_cpus = available_parallelism()
@@ -152,11 +174,11 @@ fn main() -> Result<(), ServerError> {
             .join()
             .expect(format!("Failed to join thread for shard-{id}").as_str());
     }
-    // From this point on, we can use tracing macros to log messages.
 
-    /*
+    // From this point on, we can use tracing macros to log messages.
     logging.late_init(config.system.get_system_path(), &config.system.logging)?;
 
+    /*
     #[cfg(feature = "disable-mimalloc")]
     tracing::warn!(
         "Using default system allocator because code was build with `disable-mimalloc` feature"
diff --git a/core/server/src/shard/builder.rs b/core/server/src/shard/builder.rs
index 08a02e54..750e2b71 100644
--- a/core/server/src/shard/builder.rs
+++ b/core/server/src/shard/builder.rs
@@ -61,13 +61,6 @@ impl IggyShardBuilder {
             .expect("Failed to find connection with the specified ID");
         let shards = connections.into_iter().map(Shard::new).collect();
 
-        IggyShard::new(
-            id,
-            shards,
-            config,
-            stop_receiver,
-            stop_sender,
-            receiver,
-        )
+        IggyShard::new(id, shards, config, stop_receiver, stop_sender, receiver)
     }
 }
diff --git a/core/server/src/shard/mod.rs b/core/server/src/shard/mod.rs
index bffaca45..dcb089d9 100644
--- a/core/server/src/shard/mod.rs
+++ b/core/server/src/shard/mod.rs
@@ -91,6 +91,5 @@ impl IggyShard {
         }
     }
 
-    pub fn assert_init(&self) {
-    }
+    pub fn assert_init(&self) {}
 }