-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.rs
278 lines (218 loc) · 10.4 KB
/
router.rs
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use std::env;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::time::Duration;
use clap::Parser;
use log::info;
use prost::Message;
use rand::Rng;
use tokio::sync::Mutex;
use tonic::{Request, Response, Status};
use tonic::transport::Server;
use configmanager::AddressManager;
use key_value_store::{CreateRequest, CreateResponse, DeleteRequest, DeleteResponse, GetRequest, GetResponse, UpdateRequest, UpdateResponse};
use key_value_store::key_value_service_client::KeyValueServiceClient;
use key_value_store::key_value_service_server::KeyValueService;
use crate::error::ServerError;
use crate::key_value_store::key_value_service_server::KeyValueServiceServer;
mod logging_middleware;
mod error;
pub mod key_value_store {
tonic::include_proto!("server");
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[arg(long, default_value = "localhost:2181,localhost:2182,localhost:2183")]
zookeeper_servers: String,
#[arg(long, default_value = "[::1]:50051")]
server_address: String,
#[arg(long, default_value = "/services")]
service_registry_paths: String,
}
pub struct KeyValueServiceRouter {
address_managers: Arc<Mutex<Vec<Box<dyn AddressManager>>>>,
}
impl KeyValueServiceRouter {
pub fn new(address_managers: Vec<Box<dyn AddressManager>>) -> Self {
Self {
address_managers: Arc::new(Mutex::new(address_managers))
}
}
}
#[tonic::async_trait]
impl KeyValueService for KeyValueServiceRouter {
async fn get(&self, request: Request<GetRequest>) -> Result<Response<GetResponse>, Status> {
let request = request.into_inner();
let key_val = request.clone().key
.ok_or_else(|| ServerError::InvalidArgument("key must be set".to_string()))?;
let key_bytes = key_val.encode_to_vec();
let hash = calculate_hash(&key_bytes);
let address = get_random_address(&self.address_managers, hash).await?;
let mut client = KeyValueServiceClient::connect(address).await
.map_err(|_| Status::internal("Could not connect to address"))?;
client.get(request).await
}
async fn create(&self, request: Request<CreateRequest>) -> Result<Response<CreateResponse>, Status> {
let request = request.into_inner();
let key_val = request.clone().key_value
.ok_or_else(|| ServerError::InvalidArgument("key_value must be set".to_string()))?.key
.ok_or_else(|| ServerError::InvalidArgument("key must be set".to_string()))?;
let key_bytes = key_val.encode_to_vec();
let hash = calculate_hash(&key_bytes);
let address = get_partitioned_leader_address(&self.address_managers, hash).await?;
let mut client = KeyValueServiceClient::connect(address).await
.map_err(|_| Status::internal("Could not connect to address"))?;
client.create(request).await
}
async fn update(&self, request: Request<UpdateRequest>) -> Result<Response<UpdateResponse>, Status> {
let request = request.into_inner();
let key_val = request.clone().key_value
.ok_or_else(|| ServerError::InvalidArgument("key_value must be set".to_string()))?.key
.ok_or_else(|| ServerError::InvalidArgument("key must be set".to_string()))?;
let key_bytes = key_val.encode_to_vec();
let hash = calculate_hash(&key_bytes);
let address = get_partitioned_leader_address(&self.address_managers, hash).await?;
let mut client = KeyValueServiceClient::connect(address).await
.map_err(|_| Status::internal("Could not connect to address"))?;
client.update(request).await
}
async fn delete(&self, request: Request<DeleteRequest>) -> Result<Response<DeleteResponse>, Status> {
let request = request.into_inner();
let key_val = request.clone().key
.ok_or_else(|| ServerError::InvalidArgument("key must be set".to_string()))?;
let key_bytes = key_val.encode_to_vec();
let hash = calculate_hash(&key_bytes);
let address = get_partitioned_leader_address(&self.address_managers, hash).await?;
let mut client = KeyValueServiceClient::connect(address).await
.map_err(|_| Status::internal("Could not connect to address"))?;
client.delete(request).await
}
}
async fn get_random_address(address_managers: &Arc<Mutex<Vec<Box<dyn AddressManager>>>>, hash: usize) -> Result<String, Status> {
let address_managers = address_managers.lock().await;
let address_manager_partition = hash % address_managers.len();
let addresses = address_managers[address_manager_partition].get_all_addresses()
.map_err(|_| Status::internal("Could not get addresses"))?;
if addresses.is_empty() {
return Err(Status::internal("No address managers"));
}
let random_address_index = rand::thread_rng().gen_range(0..addresses.len());
Ok(addresses[random_address_index].clone())
}
async fn get_partitioned_leader_address(address_managers: &Arc<Mutex<Vec<Box<dyn AddressManager>>>>, hash: usize) -> Result<String, Status> {
let address_managers = address_managers.lock().await;
let address_manager_partition = hash % address_managers.len();
let addresses = address_managers[address_manager_partition].get_leader_address()
.map_err(|_| Status::internal("Could not get addresses"))?;
Ok(addresses)
}
fn calculate_hash(key: &Vec<u8>) -> usize {
let mut hasher = std::collections::hash_map::DefaultHasher::new();
key.hash(&mut hasher);
hasher.finish() as usize
}
#[tokio::main]
async fn main() -> anyhow::Result<(), Box<dyn std::error::Error>> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
let args = Args::parse();
let server_address = env::var("SERVER_ADDRESS").ok().unwrap_or(args.server_address);
let service_registry_paths = env::var("SERVICE_REGISTRY_PATHS").ok().unwrap_or(args.service_registry_paths);
let zookeeper_servers = env::var("ZOOKEEPER_SERVERS").ok().unwrap_or(args.zookeeper_servers);
info!("start address managers");
let mut address_managers: Vec<Box<dyn AddressManager>> = Vec::new();
for service_registry_path in service_registry_paths.split(',') {
let address_manager = configmanager::ZooKeeperAddressManager::new(
service_registry_path,
zookeeper_servers.as_str(),
).expect("Could not create address manager");
address_managers.push(Box::new(address_manager));
}
info!("finished init address managers");
let addr = server_address.parse()?;
let server = KeyValueServiceRouter::new(address_managers);
let layer = tower::ServiceBuilder::new()
// Apply middleware from tower
.timeout(Duration::from_secs(30))
// Apply our own middleware
.layer(logging_middleware::LoggingInterceptorLayer)
.into_inner();
info!("start server");
Server::builder()
.layer(layer)
.add_service(KeyValueServiceServer::new(server))
.serve(addr)
.await?;
Ok(())
}
#[cfg(test)]
mod tests {
use anyhow::{anyhow, Result};
#[cfg(test)]
use mockall::*;
use super::*;
mock! {
AddressManager {}
impl AddressManager for AddressManager {
fn get_leader_address(&self) -> Result<String>;
fn get_follower_addresses(&self) -> Result<Vec<String>>;
fn get_all_addresses(&self) -> Result<Vec<String>>;
}
}
#[test]
fn test_calculate_hash() {
let key1 = vec![1, 2, 3, 4, 5];
let key2 = vec![5, 4, 3, 2, 1];
let key3 = vec![1, 2, 3, 4, 5];
let hash1 = calculate_hash(&key1);
let hash2 = calculate_hash(&key2);
let hash3 = calculate_hash(&key3);
assert_ne!(hash1, hash2);
assert_eq!(hash1, hash3);
}
#[tokio::test]
async fn test_get_random_address() {
let mut mock_address_manager = MockAddressManager::new();
mock_address_manager.expect_get_all_addresses().returning(|| Ok(vec!["localhost:50051".to_string(), "localhost:50052".to_string()]));
let address_managers = vec![Box::new(mock_address_manager) as Box<dyn AddressManager>];
let address_managers = Arc::new(Mutex::new(address_managers));
let address = get_random_address(&address_managers, 0).await.unwrap();
assert!(address == "localhost:50051" || address == "localhost:50052");
}
#[tokio::test]
async fn test_get_random_address_no_addresses() {
let mut mock_address_manager = MockAddressManager::new();
mock_address_manager.expect_get_all_addresses().returning(|| Ok(vec![]));
let address_managers = vec![Box::new(mock_address_manager) as Box<dyn AddressManager>];
let address_managers = Arc::new(Mutex::new(address_managers));
let result = get_random_address(&address_managers, 0).await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_get_random_address_error() {
let mut mock_address_manager = MockAddressManager::new();
mock_address_manager.expect_get_all_addresses().returning(move || Err(anyhow!("error")));
let address_managers = vec![Box::new(mock_address_manager) as Box<dyn AddressManager>];
let address_managers = Arc::new(Mutex::new(address_managers));
let result = get_random_address(&address_managers, 0).await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_get_partitioned_leader_address() {
let mut mock_address_manager = MockAddressManager::new();
mock_address_manager.expect_get_leader_address().returning(|| Ok("localhost:50051".to_string()));
let address_managers = vec![Box::new(mock_address_manager) as Box<dyn AddressManager>];
let address_managers = Arc::new(Mutex::new(address_managers));
let address = get_partitioned_leader_address(&address_managers, 0).await.unwrap();
assert_eq!(address, "localhost:50051");
}
#[tokio::test]
async fn test_get_partitioned_leader_address_error() {
let mut mock_address_manager = MockAddressManager::new();
mock_address_manager.expect_get_leader_address().returning(move || Err(anyhow!("error")));
let address_managers = vec![Box::new(mock_address_manager) as Box<dyn AddressManager>];
let address_managers = Arc::new(Mutex::new(address_managers));
let result = get_partitioned_leader_address(&address_managers, 0).await;
assert!(result.is_err());
}
}