From d4dbbda97b42bad8a41297ba1c3a693f1fe66902 Mon Sep 17 00:00:00 2001 From: omagdy Date: Wed, 23 Jul 2025 01:26:04 +0300 Subject: feat: Added replicationID and replicationOffset in info command for master nodes --- src/lib.rs | 12 ++++++++++++ src/resp_commands.rs | 15 +++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 94e7a20..8a88674 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,12 +7,16 @@ pub mod resp_commands; pub mod resp_parser; pub mod shared_cache; +// TODO: Model this in a better way there could be an enum where a Slave Server is distinguised +// from master servers in 2 different structs #[derive(Debug, Clone, Default)] pub struct RedisServer { pub role: String, pub port: String, pub master_host: String, pub master_port: String, + pub master_replid: Option, + pub master_repl_offset: Option, } impl RedisServer { @@ -22,6 +26,9 @@ impl RedisServer { port: "6379".to_string(), master_host: "".to_string(), master_port: "".to_string(), + // HACK: Hardcoded for now + master_replid: Some("8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb".to_string()), + master_repl_offset: Some("0".to_string()), } } } @@ -86,6 +93,11 @@ impl Config { .unwrap_or(("", "")); redis_server.role = "slave".to_string(); + + // slaves don't have master attributes!! + redis_server.master_replid = None; + redis_server.master_repl_offset = None; + redis_server.master_host = master_host.to_string(); redis_server.master_port = master_port.to_string(); i += 2; diff --git a/src/resp_commands.rs b/src/resp_commands.rs index 2ca9e02..2c9c09c 100644 --- a/src/resp_commands.rs +++ b/src/resp_commands.rs @@ -1,5 +1,5 @@ -use crate::SharedConfig; use crate::{resp_parser::*, shared_cache::*}; +use crate::{RedisServer, SharedConfig}; use regex::Regex; use std::time::{SystemTime, UNIX_EPOCH}; @@ -237,11 +237,18 @@ impl RedisCommands { RC::Info(_sub_command) => { use RespType as RT; let config = config.clone(); - let mut role = "master".to_string(); + let mut server = RedisServer::new(); if let Some(conf) = config.as_ref() { - role = conf.server.clone().role; + server = conf.server.clone(); } - let response = format!("# Replication\r\nrole:{role}",).as_bytes().to_vec(); + let response = format!( + "# Replication\r\nrole:{}master_replid:{}master_repl_offset:{}", + server.role, + server.master_replid.unwrap_or("".to_string()), + server.master_repl_offset.unwrap_or("".to_string()) + ) + .as_bytes() + .to_vec(); RT::BulkString(response).to_resp_bytes() } RC::Invalid => todo!(), -- cgit v1.2.3