1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#![allow(unused_imports)]
use core::time;
use std::{
collections::HashMap,
env,
fmt::format,
io::{Read, Write},
net::{TcpListener, TcpStream},
sync::{Arc, Mutex},
thread,
time::{Duration, SystemTime, UNIX_EPOCH},
};
use codecrafters_redis::{
rdb::{KeyExpiry, ParseError, RDBFile, RedisValue},
resp_bytes,
shared_cache::*,
};
use codecrafters_redis::{resp_commands::RedisCommands, server::RedisServer};
use codecrafters_redis::{
resp_parser::{parse, RespType},
server::SlaveServer,
};
fn spawn_cleanup_thread(cache: SharedCache) {
let cache_clone = cache.clone();
std::thread::spawn(move || {
loop {
std::thread::sleep(Duration::from_secs(10)); // Check every 10 seconds
let mut cache = cache_clone.lock().unwrap();
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis() as u64;
// Remove expired keys
cache.retain(|_, entry| entry.expires_at.map_or(true, |expiry| now <= expiry));
}
});
}
use base64::{engine::general_purpose, Engine as _};
fn write_rdb_to_stream<W: Write>(writer: &mut W) -> Result<(), Box<dyn std::error::Error>> {
let hardcoded_rdb = "UkVESVMwMDEx+glyZWRpcy12ZXIFNy4yLjD6CnJlZGlzLWJpdHPAQPoFY3RpbWXCbQi8ZfoIdXNlZC1tZW3CsMQQAPoIYW9mLWJhc2XAAP/wbjv+wP9aog==";
let bytes = general_purpose::STANDARD.decode(hardcoded_rdb)?;
let mut response = format!("${}\r\n", bytes.len()).into_bytes();
response.extend_from_slice(&bytes);
// Write the binary RDB data
writer.write_all(&response)?;
Ok(())
}
// TODO: This should return a Result to handle the plethora of different errors
fn handle_client(mut stream: TcpStream, server: Arc<Mutex<RedisServer>>) {
let mut buffer = [0; 512];
loop {
let _ = match stream.read(&mut buffer) {
Ok(0) => return, // connection closed
Ok(n) => n,
Err(_) => return, // error occurred
};
let request = parse(&buffer).unwrap();
let server_clone = Arc::clone(&server);
let response = RedisCommands::from(request.0.clone()).execute(server_clone);
let mut request_command = "".to_string();
// FIXME: Find a solution for this mess!!
match &request.0 {
RespType::Array(arr) => {
if let RespType::BulkString(s) = arr[0].clone() {
request_command = String::from_utf8(s).unwrap();
}
}
_ => {}
}
// Store the persistent connection
let shared_stream = Arc::new(Mutex::new(
stream.try_clone().expect("What could go wrong? :)"),
));
// if this true immediately write and send back rdb file after response
// HACK: This just feels wrong I feel this shouldn't be handled here and should be handled
// in the exexute command
if request_command.starts_with("PSYNC") {
stream.write(&response).unwrap();
let _ = write_rdb_to_stream(&mut stream);
// handshake completed and I should add the server sending me the handshake to my replicas
let replica_addr = stream
.peer_addr()
.expect("This shouldn't fail right? right?? :)");
let mut server = server.lock().unwrap();
server.add_replica(replica_addr, shared_stream);
} else {
// write respose back to the client
stream.write(&response).unwrap();
}
}
}
fn main() -> std::io::Result<()> {
let server = match RedisServer::new() {
Ok(Some(server)) => server,
Ok(None) => RedisServer::master(), // Default to master if no args
Err(e) => {
eprintln!("Error: {}", e);
std::process::exit(1);
}
};
// Load RDB file if dir and dbfilename are provided
if let (Some(dir), Some(dbfilename)) = (server.dir().clone(), server.dbfilename().clone()) {
if let Ok(rdb_file) = RDBFile::read(dir, dbfilename) {
if let Some(rdb) = rdb_file {
let mut cache = server.cache().lock().unwrap();
let hash_table = &rdb.databases.get(&0).unwrap().hash_table;
for (key, db_entry) in hash_table.iter() {
let value = match &db_entry.value {
RedisValue::String(data) => String::from_utf8(data.clone()).unwrap(),
RedisValue::Integer(data) => data.to_string(),
_ => {
unreachable!()
}
};
let expires_at = if let Some(key_expiry) = &db_entry.expiry {
Some(key_expiry.timestamp)
} else {
None
};
let cache_entry = CacheEntry { value, expires_at };
cache.insert(String::from_utf8(key.clone()).unwrap(), cache_entry);
}
}
}
}
let port = server.port().to_string();
let listener = TcpListener::bind(format!("127.0.0.1:{}", port)).unwrap();
spawn_cleanup_thread(server.cache().clone());
let server = Arc::new(Mutex::new(server));
for stream in listener.incoming() {
match stream {
Ok(stream) => {
let server_clone = Arc::clone(&server);
// TODO: Use tokio instead of multi threads to handle multiple clients
thread::spawn(|| {
handle_client(stream, server_clone);
});
}
Err(e) => {
eprintln!("Connection failed: {}", e);
}
}
}
Ok(())
}
|