-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathserver.rs
More file actions
637 lines (558 loc) · 24 KB
/
server.rs
File metadata and controls
637 lines (558 loc) · 24 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! Local CAS Server Implementation
//!
//! This module provides `LocalServer`, an HTTP server that wraps `LocalClient`
//! and exposes its functionality via REST API endpoints compatible with `RemoteClient`.
//!
//! # Architecture
//!
//! The server uses Axum as its HTTP framework and shares an `Arc<LocalClient>`
//! across all request handlers. Routes are organized to match the API expected
//! by `RemoteClient`, with some legacy route aliases for compatibility.
//!
//! # Example
//!
//! ```no_run
//! use cas_client::local_server::{LocalServer, LocalServerConfig};
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! let config = LocalServerConfig {
//! data_directory: "./data".into(),
//! host: "127.0.0.1".to_string(),
//! port: 8080,
//! };
//! let server = LocalServer::new(config)?;
//! server.run().await?;
//! Ok(())
//! }
//! ```
use std::net::{SocketAddr, TcpListener as StdTcpListener};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use axum::Router;
use axum::routing::{get, head, post};
use tokio::net::TcpListener;
use tokio::sync::oneshot;
use tower_http::cors::CorsLayer;
use super::handlers;
use crate::error::{CasClientError, Result};
use crate::{LocalClient, RemoteClient};
/// Configuration for the local CAS server.
#[derive(Clone, Debug)]
pub struct LocalServerConfig {
/// Directory where CAS data (XORBs, shards, indices) will be stored.
pub data_directory: PathBuf,
/// Network interface to bind to (e.g., "127.0.0.1" or "0.0.0.0").
pub host: String,
/// TCP port number for the HTTP server.
pub port: u16,
}
impl Default for LocalServerConfig {
fn default() -> Self {
Self {
data_directory: PathBuf::from("./local_cas_data"),
host: "127.0.0.1".to_string(),
port: 8080,
}
}
}
/// A local HTTP server that wraps `LocalClient` and exposes CAS operations via REST API.
///
/// This server implements the same API that `RemoteClient` expects, making it useful for:
/// - Integration testing without a remote backend
/// - Local development and debugging
/// - Offline CAS workflows
pub struct LocalServer {
config: LocalServerConfig,
client: Arc<LocalClient>,
}
impl LocalServer {
/// Creates a new server with the given configuration.
///
/// This will create a new `LocalClient` pointing to the configured data directory.
/// The directory will be created if it doesn't exist.
pub fn new(config: LocalServerConfig) -> Result<Self> {
let client = LocalClient::new(&config.data_directory)?;
Ok(Self { config, client })
}
/// Creates a server from an existing `LocalClient`.
///
/// Useful when you want to share a `LocalClient` instance between the server
/// and other code (e.g., for testing where you want to verify server behavior
/// against direct client access).
pub fn from_client(client: Arc<LocalClient>, host: String, port: u16) -> Self {
Self {
config: LocalServerConfig {
data_directory: PathBuf::new(),
host,
port,
},
client,
}
}
/// Returns a clone of the underlying LocalClient.
pub fn client(&self) -> Arc<LocalClient> {
self.client.clone()
}
/// Returns the server's bind address as "host:port".
pub fn addr(&self) -> String {
format!("{}:{}", self.config.host, self.config.port)
}
/// Builds the Axum router with all CAS API routes.
///
/// Routes follow the pattern used by RemoteClient:
/// - `/v1/` prefixed routes for chunks, xorbs, reconstructions, and files
/// - Root-level `/reconstructions` for batch queries and `/shards` for uploads
fn create_router(&self) -> Router {
Router::new()
.route("/health", get(handlers::health_check))
.nest(
"/v1",
Router::new()
.route("/reconstructions", get(handlers::batch_get_reconstruction))
.route("/reconstructions/{file_id}", get(handlers::get_reconstruction))
.route("/chunks/{prefix}/{hash}", get(handlers::get_dedup_info_by_chunk))
.route("/xorbs/{prefix}/{hash}", head(handlers::head_xorb).post(handlers::post_xorb))
.route("/files/{file_id}", head(handlers::head_file))
.route("/get_xorb/{prefix}/{hash}/", get(handlers::get_file_term_data))
.route("/fetch_term", get(handlers::fetch_term)),
)
// Routes used by RemoteClient without /v1/ prefix
.route("/reconstructions", get(handlers::batch_get_reconstruction))
.route("/shards", post(handlers::post_shard))
.layer(CorsLayer::very_permissive())
.with_state(self.client.clone())
}
/// Runs the server, listening for incoming HTTP requests.
///
/// This method blocks until the server is shut down via signal (Ctrl+C on Unix).
/// For programmatic shutdown, use `run_until_stopped` instead.
pub async fn run(&self) -> Result<()> {
let addr: SocketAddr = self
.addr()
.parse()
.map_err(|e| CasClientError::Other(format!("Failed to parse address: {e}")))?;
let listener = TcpListener::bind(addr)
.await
.map_err(|e| CasClientError::Other(format!("Failed to bind to {addr}: {e}")))?;
tracing::info!("Local CAS server listening on {}", addr);
let router = self.create_router();
axum::serve(listener, router.into_make_service())
.with_graceful_shutdown(shutdown_signal())
.await
.map_err(|e| CasClientError::Other(format!("Server error: {e}")))
}
/// Runs the server until a shutdown signal is received on the provided channel.
///
/// This is useful for tests where you want programmatic control over server lifecycle.
pub async fn run_until_stopped(&self, shutdown_rx: tokio::sync::oneshot::Receiver<()>) -> Result<()> {
let addr: SocketAddr = self
.addr()
.parse()
.map_err(|e| CasClientError::Other(format!("Failed to parse address: {e}")))?;
let listener = TcpListener::bind(addr)
.await
.map_err(|e| CasClientError::Other(format!("Failed to bind to {addr}: {e}")))?;
tracing::info!("Local CAS server listening on {}", addr);
let router = self.create_router();
axum::serve(listener, router.into_make_service())
.with_graceful_shutdown(async {
let _ = shutdown_rx.await;
})
.await
.map_err(|e| CasClientError::Other(format!("Server error: {e}")))
}
}
/// Waits for a shutdown signal (currently blocks forever as there's no SIGTERM handling).
async fn shutdown_signal() {
std::future::pending::<()>().await
}
/// A test server that wraps `LocalServer` and provides easy access to both
/// `RemoteClient` (for HTTP interactions) and `LocalClient` (for direct state access).
///
/// This is useful for integration tests where you want to verify that operations
/// through the HTTP API produce the same results as direct client access.
///
/// The server runs as a spawned tokio task and automatically shuts down when dropped
/// (no explicit shutdown call needed).
///
/// # Example
///
/// ```ignore
/// let server = LocalTestServer::start().await;
///
/// // Upload via RemoteClient
/// let file = server.remote_client().upload_random_file(&[(1, (0, 5))], 123).await?;
///
/// // Verify via LocalClient
/// let stored = server.local_client().get_file_data(&file.file_hash, None).await?;
/// assert_eq!(file.data, stored);
/// // Server automatically shuts down when dropped
/// ```
pub struct LocalTestServer {
endpoint: String,
server_shutdown_tx: Option<oneshot::Sender<()>>,
remote_client: Arc<RemoteClient>,
local_client: Arc<LocalClient>,
}
impl LocalTestServer {
/// Starts a new test server with a fresh temporary data directory.
///
/// The server listens on a randomly assigned available port on localhost.
pub async fn start() -> Self {
let local_client = LocalClient::temporary().await.unwrap();
Self::start_with_client(local_client).await
}
/// Starts a new test server using an existing `LocalClient`.
///
/// Useful when you need to pre-populate the client with data before starting the server.
pub async fn start_with_client(local_client: Arc<LocalClient>) -> Self {
let port = Self::find_available_port();
let host = "127.0.0.1".to_string();
let endpoint = format!("http://{}:{}", host, port);
let server = LocalServer::from_client(local_client.clone(), host, port);
let (shutdown_tx, shutdown_rx) = oneshot::channel();
tokio::spawn(async move {
let _ = server.run_until_stopped(shutdown_rx).await;
});
tokio::time::sleep(Duration::from_millis(50)).await;
let remote_client = RemoteClient::new(&endpoint, &None, None, "test-session", false, "test-agent");
Self {
endpoint,
server_shutdown_tx: Some(shutdown_tx),
remote_client,
local_client,
}
}
/// Returns the HTTP endpoint URL (e.g., "http://127.0.0.1:12345").
pub fn endpoint(&self) -> &str {
&self.endpoint
}
/// Returns the `RemoteClient` configured to connect to this test server.
pub fn remote_client(&self) -> &Arc<RemoteClient> {
&self.remote_client
}
/// Returns the underlying `LocalClient` for direct state access.
pub fn local_client(&self) -> &Arc<LocalClient> {
&self.local_client
}
fn find_available_port() -> u16 {
StdTcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port()
}
}
impl Drop for LocalTestServer {
fn drop(&mut self) {
if let Some(tx) = self.server_shutdown_tx.take() {
let _ = tx.send(());
}
}
}
#[cfg(test)]
mod tests {
use cas_types::FileRange;
use super::*;
use crate::Client;
use crate::client_testing_utils::ClientTestingUtils;
const CHUNK_SIZE: usize = 123;
/// Verifies basic server operations: upload, reconstruction (full/range/batch/multi-xorb),
/// file info, dedup queries, and fetch_term endpoint.
async fn check_basic_correctness(server: &LocalTestServer) {
// Upload via RemoteClient, verify via LocalClient
let file = server
.remote_client()
.upload_random_file(&[(1, (0, 5))], CHUNK_SIZE)
.await
.unwrap();
let local_data = server.local_client().get_file_data(&file.file_hash, None).await.unwrap();
assert_eq!(file.data, local_data);
// Full file reconstruction - compare remote and local
let remote_recon = server
.remote_client()
.get_reconstruction(&file.file_hash, None)
.await
.unwrap()
.unwrap();
let local_recon = server
.local_client()
.get_reconstruction(&file.file_hash, None)
.await
.unwrap()
.unwrap();
assert_eq!(remote_recon.terms.len(), local_recon.terms.len());
assert_eq!(remote_recon.offset_into_first_range, local_recon.offset_into_first_range);
for (remote_term, local_term) in remote_recon.terms.iter().zip(local_recon.terms.iter()) {
assert_eq!(remote_term.hash, local_term.hash);
assert_eq!(remote_term.range, local_term.range);
}
// Range reconstruction
let file_size = file.data.len() as u64;
let range = FileRange::new(file_size / 4, file_size * 3 / 4);
let range_recon = server
.remote_client()
.get_reconstruction(&file.file_hash, Some(range))
.await
.unwrap();
assert!(range_recon.is_some());
// Upload multi-xorb file
let term_spec = &[(1, (0, 3)), (2, (0, 2)), (1, (3, 5))];
let multi_file = server.local_client().upload_random_file(term_spec, CHUNK_SIZE).await.unwrap();
let multi_recon = server
.remote_client()
.get_reconstruction(&multi_file.file_hash, None)
.await
.unwrap()
.unwrap();
assert_eq!(multi_recon.terms.len(), 3);
// Batch reconstruction
let file_ids = vec![file.file_hash, multi_file.file_hash];
let batch_result = server.remote_client().batch_get_reconstruction(&file_ids).await.unwrap();
assert_eq!(batch_result.files.len(), 2);
// File info (MDBFileInfo)
let (remote_mdb, _) = server
.remote_client()
.get_file_reconstruction_info(&file.file_hash)
.await
.unwrap()
.unwrap();
let (local_mdb, _) = server
.local_client()
.get_file_reconstruction_info(&file.file_hash)
.await
.unwrap()
.unwrap();
assert_eq!(remote_mdb.file_size(), local_mdb.file_size());
// Dedup query - use chunk hash from RandomFileContents
let first_chunk_hash = file.terms[0].chunk_hashes[0];
let shard_result = server
.remote_client()
.query_for_global_dedup_shard("default", &first_chunk_hash)
.await
.unwrap();
let local_shard = server
.local_client()
.query_for_global_dedup_shard("default", &first_chunk_hash)
.await
.unwrap();
assert!(shard_result.is_some());
assert_eq!(shard_result.unwrap(), local_shard.unwrap());
// Fetch term endpoint - verify URLs are HTTP and data can be fetched
let http_client = reqwest::Client::new();
for fetch_infos in remote_recon.fetch_info.values() {
for fi in fetch_infos {
assert!(fi.url.starts_with("http://"));
assert!(fi.url.contains("/fetch_term?term="));
let response = http_client.get(&fi.url).send().await.unwrap();
assert!(response.status().is_success());
assert!(!response.bytes().await.unwrap().is_empty());
}
}
// Fetch term with range request
let first_fi = &remote_recon.fetch_info.values().next().unwrap()[0];
let full_data = http_client.get(&first_fi.url).send().await.unwrap().bytes().await.unwrap();
if full_data.len() > 100 {
let range_resp = http_client
.get(&first_fi.url)
.header(reqwest::header::RANGE, "bytes=0-99")
.send()
.await
.unwrap();
assert!(range_resp.status().is_success());
let range_data = range_resp.bytes().await.unwrap();
assert_eq!(range_data.len(), 100);
assert_eq!(&range_data[..], &full_data[..100]);
}
}
/// Tests that invalid requests return appropriate error responses.
async fn check_error_handling(server: &LocalTestServer) {
let http_client = reqwest::Client::new();
// Nonexistent file hash for reconstruction
let fake_hash =
merklehash::MerkleHash::from_hex("d760aaf4beb07581956e24c847c47f1abd2e419166aa68259035bc412232e9da")
.unwrap();
let result = server.remote_client().get_reconstruction(&fake_hash, None).await;
assert!(result.is_err() || result.unwrap().is_none());
// Nonexistent file for file info
let file_info = server.remote_client().get_file_reconstruction_info(&fake_hash).await;
assert!(file_info.is_err() || file_info.unwrap().is_none());
// Invalid fetch_term (valid base64 but nonexistent path)
let invalid_term_url = format!("{}/v1/fetch_term?term=aW52YWxpZF9wYXRo", server.endpoint());
let response = http_client.get(&invalid_term_url).send().await.unwrap();
assert!(response.status().is_client_error() || response.status().is_server_error());
// Malformed base64 in fetch_term
let malformed_url = format!("{}/v1/fetch_term?term=not-valid-base64!!!", server.endpoint());
let response = http_client.get(&malformed_url).send().await.unwrap();
assert_eq!(response.status(), reqwest::StatusCode::BAD_REQUEST);
}
/// Verifies that reconstruction responses contain valid HTTP URLs.
async fn check_url_transformation(server: &LocalTestServer) {
let http_client = reqwest::Client::new();
// Single XORB file
let file1 = server
.local_client()
.upload_random_file(&[(1, (0, 5))], CHUNK_SIZE)
.await
.unwrap();
// Multi-XORB file
let term_spec = &[(1, (0, 3)), (2, (0, 2)), (1, (3, 5))];
let multi_file = server.local_client().upload_random_file(term_spec, CHUNK_SIZE).await.unwrap();
// Verify single XORB URLs are HTTP
let recon1 = server
.remote_client()
.get_reconstruction(&file1.file_hash, None)
.await
.unwrap()
.unwrap();
for (hash, fetch_infos) in &recon1.fetch_info {
for fi in fetch_infos {
assert!(
fi.url.starts_with("http://") || fi.url.starts_with("https://"),
"URL for hash {} should be HTTP, got: {}",
hash,
fi.url
);
assert!(fi.url.contains("/fetch_term?term="));
assert!(!fi.url.contains("\":"));
}
}
// Verify multi-XORB file has HTTP URLs for all XORBs
let multi_recon = server
.remote_client()
.get_reconstruction(&multi_file.file_hash, None)
.await
.unwrap()
.unwrap();
assert!(multi_recon.fetch_info.len() >= 2);
for fetch_infos in multi_recon.fetch_info.values() {
for fi in fetch_infos {
assert!(fi.url.starts_with("http://"));
}
}
// Verify partial range reconstruction has HTTP URLs
let file_size = multi_file.data.len() as u64;
let range = FileRange::new(file_size / 4, file_size * 3 / 4);
let range_recon = server
.remote_client()
.get_reconstruction(&multi_file.file_hash, Some(range))
.await
.unwrap()
.unwrap();
for fetch_infos in range_recon.fetch_info.values() {
for fi in fetch_infos {
assert!(fi.url.starts_with("http://"));
assert!(fi.url.contains("/fetch_term?term="));
}
}
// Verify all term URLs are fetchable
for term in &recon1.terms {
let fetch_infos = recon1.fetch_info.get(&term.hash).unwrap();
for fi in fetch_infos {
let response = http_client.get(&fi.url).send().await.unwrap();
assert!(response.status().is_success());
assert!(!response.bytes().await.unwrap().is_empty());
}
}
}
/// Verifies reconstruction term hashes match the uploaded file's expected terms.
async fn check_reconstruction_term_hashes_match(server: &LocalTestServer) {
// Upload a multi-term file
let term_spec = &[(1, (0, 3)), (2, (0, 2)), (1, (3, 5))];
let file = server.local_client().upload_random_file(term_spec, CHUNK_SIZE).await.unwrap();
// Get reconstruction via remote client
let recon = server
.remote_client()
.get_reconstruction(&file.file_hash, None)
.await
.unwrap()
.unwrap();
// Verify term count matches
assert_eq!(recon.terms.len(), file.terms.len());
// Verify each term's XORB hash matches
for (i, recon_term) in recon.terms.iter().enumerate() {
let expected_term = &file.terms[i];
assert_eq!(recon_term.hash.0, expected_term.xorb_hash, "Term {} XORB hash mismatch", i);
}
}
/// Verifies that reconstruction data can be fetched and downloaded file matches expected data.
async fn check_downloaded_terms_match_expected_data(server: &LocalTestServer) {
// Upload a file with known term structure
let term_spec = &[(1, (0, 4)), (2, (0, 3))];
let file = server.local_client().upload_random_file(term_spec, CHUNK_SIZE).await.unwrap();
// Get reconstruction
let recon = server
.remote_client()
.get_reconstruction(&file.file_hash, None)
.await
.unwrap()
.unwrap();
// Verify term count and XORB hashes match
assert_eq!(recon.terms.len(), file.terms.len());
for (term_idx, recon_term) in recon.terms.iter().enumerate() {
let expected_term = &file.terms[term_idx];
assert_eq!(recon_term.hash.0, expected_term.xorb_hash);
// Verify fetch_info exists for each XORB
let fetch_infos = recon.fetch_info.get(&recon_term.hash).unwrap();
assert!(!fetch_infos.is_empty());
}
// Verify the complete file can be retrieved correctly via LocalClient
let retrieved_data = server.local_client().get_file_data(&file.file_hash, None).await.unwrap();
assert_eq!(retrieved_data, file.data);
// Verify term_matches works correctly for each term
for (term_idx, term) in file.terms.iter().enumerate() {
assert!(file.term_matches(term_idx, &term.data));
}
}
/// Verifies that the complete file can be reconstructed by concatenating term data.
async fn check_complete_file_reconstruction(server: &LocalTestServer) {
// Upload a multi-term file
let term_spec = &[(1, (0, 3)), (2, (0, 2)), (1, (3, 5))];
let file = server.local_client().upload_random_file(term_spec, CHUNK_SIZE).await.unwrap();
// Reconstruct file from term data
let mut reconstructed = Vec::new();
for term in &file.terms {
reconstructed.extend_from_slice(&term.data);
}
// Verify it matches the original file data
assert_eq!(reconstructed, file.data);
assert!(file.term_matches(0, &file.terms[0].data));
assert!(file.term_matches(1, &file.terms[1].data));
assert!(file.term_matches(2, &file.terms[2].data));
// Verify term_matches returns false for wrong data
assert!(!file.term_matches(0, &file.terms[1].data));
}
/// Verifies chunk hashes in RandomFileContents match expected values.
async fn check_chunk_hashes_correctness(server: &LocalTestServer) {
let file = server
.local_client()
.upload_random_file(&[(1, (0, 3))], CHUNK_SIZE)
.await
.unwrap();
// Verify we have the expected number of chunks
assert_eq!(file.terms.len(), 1);
assert_eq!(file.terms[0].chunk_hashes.len(), 3);
// Verify chunk hashes match the RawXorbData cas_info (keyed by xorb hash)
let xorb_hash = file.terms[0].xorb_hash;
let raw_xorb = file.xorbs.get(&xorb_hash).unwrap();
assert_eq!(raw_xorb.cas_info.chunks.len(), 3);
for (i, chunk_hash) in file.terms[0].chunk_hashes.iter().enumerate() {
assert_eq!(*chunk_hash, raw_xorb.cas_info.chunks[i].chunk_hash);
}
}
/// Main test that runs all server checks with a single shared server instance.
#[tokio::test]
async fn test_local_server() {
// Verify server creation works
let temp_client = LocalClient::temporary().await.unwrap();
let temp_server = LocalServer::from_client(temp_client.clone(), "127.0.0.1".to_string(), 0);
assert!(temp_server.client().get_all_entries().unwrap().is_empty());
// Start test server for HTTP operations
let server = LocalTestServer::start().await;
check_basic_correctness(&server).await;
check_error_handling(&server).await;
check_url_transformation(&server).await;
check_reconstruction_term_hashes_match(&server).await;
check_downloaded_terms_match_expected_data(&server).await;
check_complete_file_reconstruction(&server).await;
check_chunk_hashes_correctness(&server).await;
}
}