diff --git a/Cargo.lock b/Cargo.lock index 673ff79..b623121 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1397,7 +1397,7 @@ dependencies = [ [[package]] name = "rdsys_backend" -version = "0.1.0" +version = "0.2.0" dependencies = [ "bytes", "chrono", diff --git a/crates/lox-distributor/Cargo.toml b/crates/lox-distributor/Cargo.toml index c859543..f2c6517 100644 --- a/crates/lox-distributor/Cargo.toml +++ b/crates/lox-distributor/Cargo.toml @@ -26,8 +26,8 @@ zkp = "0.8.0" lox-library = { path = "../lox-library", version = "0.1.0"} lox_utils = { path = "../lox-utils", version = "0.1.0"} -rdsys_backend = { path = "../rdsys-backend-api", version = "0.1.0"} +rdsys_backend = { path = "../rdsys-backend-api", version = "0.2"} [dependencies.chrono] version = "0.4.19" -features = ["serde"] \ No newline at end of file +features = ["serde"] diff --git a/crates/lox-distributor/src/main.rs b/crates/lox-distributor/src/main.rs index 1876841..45bde1c 100644 --- a/crates/lox-distributor/src/main.rs +++ b/crates/lox-distributor/src/main.rs @@ -129,35 +129,43 @@ async fn context_manager(mut context_rx: mpsc::Receiver) { let mut bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET]; for pt in new_resources { println!("A NEW RESOURCE: {:?}", pt); - for resource in pt.1 { - let bridgeline = parse_resource(resource); - println!("Now it's a bridgeline: {:?}", bridgeline); - if context.unreplaced_bridges.lock().unwrap().len() > 0 { - println!("BridgeLine to be replaced: {:?}", bridgeline); - let res = context.replace_with_new(bridgeline); - if res == lox_library::ReplaceSuccess::NotFound { - println!( + if let Some(resources) = pt.1 { + for resource in resources { + let bridgeline = parse_resource(resource); + println!("Now it's a bridgeline: {:?}", bridgeline); + if context.unreplaced_bridges.lock().unwrap().len() > 0 { + println!("BridgeLine to be replaced: {:?}", bridgeline); + let res = context.replace_with_new(bridgeline); + if res == lox_library::ReplaceSuccess::NotFound { + println!( "BridgeLine not found in bridge_table, already updated {:?}", bridgeline ); - } else if res == lox_library::ReplaceSuccess::Replaced { - println!("BridgeLine successfully replaced: {:?}", bridgeline); + } else if res == lox_library::ReplaceSuccess::Replaced { + println!( + "BridgeLine successfully replaced: {:?}", + bridgeline + ); + } else { + assert!( + res == lox_library::ReplaceSuccess::NotReplaced, + "ReplaceSuccess incorrectly set somehow" + ); + // Add the bridge to the list of unreplaced bridges in the Lox context and try + // again to replace at the next update (nothing changes in the Lox Authority) + println!("'Gone' BridgeLine NOT replaced, saved for next update! : {:?}", bridgeline); + context.new_unreplaced_bridge(bridgeline); + } + } else if count < MAX_BRIDGES_PER_BUCKET { + bucket[count] = bridgeline; + count += 1; } else { - assert!(res == lox_library::ReplaceSuccess::NotReplaced, "ReplaceSuccess incorrectly set somehow"); - // Add the bridge to the list of unreplaced bridges in the Lox context and try - // again to replace at the next update (nothing changes in the Lox Authority) - println!("'Gone' BridgeLine NOT replaced, saved for next update! : {:?}", bridgeline); - context.new_unreplaced_bridge(bridgeline); + // TODO: Decide the circumstances under which a bridge is allocated to an open_inv or spare bucket, + // eventually also do some more fancy grouping of new resources, i.e., by type or region + context.add_openinv_bucket(bucket); + count = 0; + bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET]; } - } else if count < MAX_BRIDGES_PER_BUCKET { - bucket[count] = bridgeline; - count += 1; - } else { - // TODO: Decide the circumstances under which a bridge is allocated to an open_inv or spare bucket, - // eventually also do some more fancy grouping of new resources, i.e., by type or region - context.add_openinv_bucket(bucket); - count = 0; - bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET]; } } } @@ -178,19 +186,21 @@ async fn context_manager(mut context_rx: mpsc::Receiver) { if let Some(changed_resources) = resourcediff.changed { for pt in changed_resources { println!("A NEW CHANGED RESOURCE: {:?}", pt); - for resource in pt.1 { - let bridgeline = parse_resource(resource); - println!("BridgeLine to be changed: {:?}", bridgeline); - let res = context.update_bridge(bridgeline); - if res { - println!("BridgeLine successfully updated: {:?}", bridgeline); - } else { - println!("BridgeLine: {:?} not found in Lox's Bridgetable. Save it as a new resource for now!", bridgeline); - if context.extra_bridges.lock().unwrap().len() < 2 { - context.append_extra_bridges(bridgeline); + if let Some(resources) = pt.1 { + for resource in resources { + let bridgeline = parse_resource(resource); + println!("BridgeLine to be changed: {:?}", bridgeline); + let res = context.update_bridge(bridgeline); + if res { + println!("BridgeLine successfully updated: {:?}", bridgeline); } else { - let bucket = context.remove_extra_bridges(); - context.add_spare_bucket(bucket); + println!("BridgeLine: {:?} not found in Lox's Bridgetable. Save it as a new resource for now!", bridgeline); + if context.extra_bridges.lock().unwrap().len() < 2 { + context.append_extra_bridges(bridgeline); + } else { + let bucket = context.remove_extra_bridges(); + context.add_spare_bucket(bucket); + } } } } @@ -205,27 +215,33 @@ async fn context_manager(mut context_rx: mpsc::Receiver) { if let Some(gone_resources) = resourcediff.gone { for pt in gone_resources { println!("A NEW GONE RESOURCE: {:?}", pt); - for resource in pt.1 { - // If resource last passed tests 3 hours ago, it should be replaced with a working - // resource and be removed from the bridgetable. If it has been gone for more than 7 hours, - // we should stop trying to remove it from the bridge table and assume it has successfully been - // removed already - if resource.last_passed < (Utc::now() - chrono::Duration::hours(3)) - || resource.last_passed > (Utc::now() - chrono::Duration::hours(7)) - { - let bridgeline = parse_resource(resource); - println!("BridgeLine to be replaced: {:?}", bridgeline); - let res = context.replace_with_new(bridgeline); - if res == lox_library::ReplaceSuccess::Replaced { - println!("BridgeLine successfully replaced: {:?}", bridgeline); - } else if res == lox_library::ReplaceSuccess::NotReplaced { - // Add the bridge to the list of unreplaced bridges in the Lox context and try - // again to replace at the next update (nothing changes in the Lox Authority) - println!( + if let Some(resources) = pt.1 { + for resource in resources { + // If resource last passed tests 3 hours ago, it should be replaced with a working + // resource and be removed from the bridgetable. If it has been gone for more than 7 hours, + // we should stop trying to remove it from the bridge table and assume it has successfully been + // removed already + if resource.last_passed < (Utc::now() - chrono::Duration::hours(3)) + || resource.last_passed + > (Utc::now() - chrono::Duration::hours(7)) + { + let bridgeline = parse_resource(resource); + println!("BridgeLine to be replaced: {:?}", bridgeline); + let res = context.replace_with_new(bridgeline); + if res == lox_library::ReplaceSuccess::Replaced { + println!( + "BridgeLine successfully replaced: {:?}", + bridgeline + ); + } else if res == lox_library::ReplaceSuccess::NotReplaced { + // Add the bridge to the list of unreplaced bridges in the Lox context and try + // again to replace at the next update (nothing changes in the Lox Authority) + println!( "'Gone' BridgeLine NOT replaced, saved for next update! : {:?}", bridgeline ); - context.new_unreplaced_bridge(bridgeline); + context.new_unreplaced_bridge(bridgeline); + } } } } diff --git a/crates/lox-distributor/src/request_handler.rs b/crates/lox-distributor/src/request_handler.rs index 82813b5..2394cd4 100644 --- a/crates/lox-distributor/src/request_handler.rs +++ b/crates/lox-distributor/src/request_handler.rs @@ -252,7 +252,10 @@ mod tests { self.context.advance_days_test(days) } - fn simulate_blocking(&mut self, cred: lox_library::cred::Lox) -> (lox_library::cred::Lox, u32, [u8; 16]) { + fn simulate_blocking( + &mut self, + cred: lox_library::cred::Lox, + ) -> (lox_library::cred::Lox, u32, [u8; 16]) { let (id, key) = bridge_table::from_scalar(cred.bucket).unwrap(); let mut bdb = self.context.db.lock().unwrap(); let mut lox_auth = self.context.ba.lock().unwrap();