Cleanup and add syncing of resources

This commit is contained in:
onyinyang 2023-09-13 12:08:48 -04:00
parent d4c54e969c
commit be0d026fe8
No known key found for this signature in database
GPG Key ID: 156A6435430C2036
5 changed files with 89 additions and 74 deletions

View File

@ -25,6 +25,19 @@ pub struct LoxServerContext {
} }
impl LoxServerContext { impl LoxServerContext {
pub fn bridgetable_is_empty(&self) -> bool {
let mut ba_obj = self.ba.lock().unwrap();
ba_obj.is_empty()
}
// Populate an empty bridgetable for the first time
/* pub fn populate_bridgetable(&self, bridgelines: Vec<BridgeLine>, percent_spares: Option<usize>) {
if Some(percent_spares) {
let partition: usize = bridgelines.len()*percent_spares/100;
}
} */
pub fn append_extra_bridges(&self, bridge: BridgeLine) { pub fn append_extra_bridges(&self, bridge: BridgeLine) {
let mut extra_bridges = self.extra_bridges.lock().unwrap(); let mut extra_bridges = self.extra_bridges.lock().unwrap();
extra_bridges.push(bridge); extra_bridges.push(bridge);

View File

@ -1,7 +1,5 @@
use chrono::Utc;
use clap::Parser; use clap::Parser;
use futures::future; use futures::future;
use futures::StreamExt;
use hyper::{ use hyper::{
server::conn::AddrStream, server::conn::AddrStream,
service::{make_service_fn, service_fn}, service::{make_service_fn, service_fn},
@ -9,7 +7,7 @@ use hyper::{
}; };
use lox_library::bridge_table::{BridgeLine, MAX_BRIDGES_PER_BUCKET}; use lox_library::bridge_table::{BridgeLine, MAX_BRIDGES_PER_BUCKET};
use rdsys_backend::{proto::Resource, proto::ResourceDiff, request_resources, start_stream}; use rdsys_backend::{proto::Resource, request_resources};
use serde::Deserialize; use serde::Deserialize;
use std::{ use std::{
@ -22,7 +20,7 @@ mod lox_context;
mod request_handler; mod request_handler;
use request_handler::handle; use request_handler::handle;
mod resource_parser; mod resource_parser;
use resource_parser::parse_resource; use resource_parser::parse_resources;
use tokio::{ use tokio::{
signal, spawn, signal, spawn,
@ -171,51 +169,61 @@ async fn context_manager(
Rdsys { resources } => { Rdsys { resources } => {
let mut count = 0; let mut count = 0;
let mut bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET]; let mut bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET];
for resource in resources { if context.bridgetable_is_empty() {
let bridgeline = parse_resource(resource); // otherwise, for each resource, check if the resource fingerprint is failing tests, if it is check for how long
println!("What is the bridgeline: {:?}", bridgeline); // check if the resource is already in the Lox bridgetable
if context.to_be_replaced_bridges.lock().unwrap().len() > 0 { // if it is, it's probably fine to remove or replace the existing resource with the incoming one
println!("BridgeLine to be replaced: {:?}", bridgeline); // to account for changes unless we want to track the number of changes on the lox side?
let res = context.replace_with_new(bridgeline); // that should be sufficient to keep it in sync
if res == lox_library::ReplaceSuccess::NotFound { let bridgelines = parse_resources(resources);
println!( for bridgeline in bridgelines {
"BridgeLine not found in bridge_table, already updated {:?}", //context.populate_bridgetable(bridgelines. None);
bridgeline println!("What is the bridgeline: {:?}", bridgeline);
); if context.to_be_replaced_bridges.lock().unwrap().len() > 0 {
} else if res == lox_library::ReplaceSuccess::Replaced { println!("BridgeLine to be replaced: {:?}", bridgeline);
println!("BridgeLine successfully 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 {
assert!(
res == lox_library::ReplaceSuccess::NotReplaced,
"ReplaceSuccess incorrectly set somehow"
);
// Add the bridge to the list of to_be_replaced 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_to_be_replaced_bridge(bridgeline);
}
} else if count < MAX_BRIDGES_PER_BUCKET {
bucket[count] = bridgeline;
count += 1;
} else { } else {
assert!( // TODO: Decide the circumstances under which a bridge is allocated to an open_inv or spare bucket,
res == lox_library::ReplaceSuccess::NotReplaced, // eventually also do some more fancy grouping of new resources, i.e., by type or region
"ReplaceSuccess incorrectly set somehow" context.add_openinv_bucket(bucket);
); count = 0;
// Add the bridge to the list of to_be_replaced bridges in the Lox context and try bucket = [BridgeLine::default(); MAX_BRIDGES_PER_BUCKET];
// 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_to_be_replaced_bridge(bridgeline);
} }
} 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];
} }
} // Handle the extra buckets that were not allocated already
// Handle the extra buckets that were not allocated already if count != 0 {
if count != 0 { for val in 0..count {
for val in 0..count { if context.extra_bridges.lock().unwrap().len()
if context.extra_bridges.lock().unwrap().len() < (MAX_BRIDGES_PER_BUCKET) { < (MAX_BRIDGES_PER_BUCKET)
context.append_extra_bridges(bucket[val]); {
} else { context.append_extra_bridges(bucket[val]);
bucket = context.remove_extra_bridges(); } else {
context.add_spare_bucket(bucket); bucket = context.remove_extra_bridges();
context.add_spare_bucket(bucket);
}
} }
} }
} }

View File

@ -1,13 +1,15 @@
use lox_library::bridge_table::{BridgeLine, BRIDGE_BYTES}; use lox_library::bridge_table::{BridgeLine, BRIDGE_BYTES};
use rdsys_backend::proto::Resource; use rdsys_backend::proto::Resource;
pub fn parse_resource(resource: Resource) -> BridgeLine { pub fn parse_resources(resources: Vec<Resource>) -> Vec<BridgeLine> {
let mut ip_bytes: [u8; 16] = [0; 16]; let mut bridgelines: Vec<BridgeLine> = Vec::new();
ip_bytes[..resource.address.len()].copy_from_slice(resource.address.as_bytes()); for resource in resources {
let resource_uid = resource let mut ip_bytes: [u8; 16] = [0; 16];
.get_uid() ip_bytes[..resource.address.len()].copy_from_slice(resource.address.as_bytes());
.expect("Unable to get Fingerprint UID of resource"); let resource_uid = resource
let infostr: String = format!( .get_uid()
.expect("Unable to get Fingerprint UID of resource");
let infostr: String = format!(
"type={} blocked_in={:?} protocol={} fingerprint={:?} or_addresses={:?} distribution={} flags={:?} params={:?}", "type={} blocked_in={:?} protocol={} fingerprint={:?} or_addresses={:?} distribution={} flags={:?} params={:?}",
resource.r#type, resource.r#type,
resource.blocked_in, resource.blocked_in,
@ -18,13 +20,15 @@ pub fn parse_resource(resource: Resource) -> BridgeLine {
resource.flags, resource.flags,
resource.params, resource.params,
); );
let mut info_bytes: [u8; BRIDGE_BYTES - 26] = [0; BRIDGE_BYTES - 26]; let mut info_bytes: [u8; BRIDGE_BYTES - 26] = [0; BRIDGE_BYTES - 26];
info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes()); info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes());
BridgeLine { bridgelines.push(BridgeLine {
addr: ip_bytes, addr: ip_bytes,
port: resource.port, port: resource.port,
uid_fingerprint: resource_uid, uid_fingerprint: resource_uid,
info: info_bytes, info: info_bytes,
})
} }
bridgelines
} }

View File

@ -330,6 +330,10 @@ impl BridgeAuth {
} }
} }
pub fn is_empty(&mut self) -> bool {
self.bridge_table.buckets.is_empty()
}
/// Insert a set of open invitation bridges. /// Insert a set of open invitation bridges.
/// ///
/// Each of the bridges will be given its own open invitation /// Each of the bridges will be given its own open invitation
@ -378,7 +382,6 @@ impl BridgeAuth {
// TODO Ensure synchronization of Lox bridge_table with rdsys // TODO Ensure synchronization of Lox bridge_table with rdsys
pub fn sync_table(&mut self) { pub fn sync_table(&mut self) {
// Create a hashtable (?) of bridges in the lox distributor from new resources // Create a hashtable (?) of bridges in the lox distributor from new resources
// accept the hashtable and recreate the bridge table from the hash table here // accept the hashtable and recreate the bridge table from the hash table here
// using existing reachable bridges, other table checks and placements from existing bridge table // using existing reachable bridges, other table checks and placements from existing bridge table

View File

@ -40,17 +40,6 @@ impl From<io::Error> for Error {
} }
} }
pub struct StaticResourceRequest {}
impl StaticResourceRequest {
pub fn new(rx: mpsc::Receiver<Bytes>) -> StaticResourceRequest {
StaticResourceRequest {
}
}
}
/// An iterable wrapper of ResourceDiff items for the streamed chunks of Bytes /// An iterable wrapper of ResourceDiff items for the streamed chunks of Bytes
/// received from the connection to the rdsys backend /// received from the connection to the rdsys backend
pub struct ResourceStream { pub struct ResourceStream {
@ -295,7 +284,6 @@ pub async fn request_resources( api_endpoint: String,
.body(json) .body(json)
.send() .send()
.await.unwrap(); .await.unwrap();
println!("Success? {:?}", response);
match response.status() { match response.status() {
reqwest::StatusCode::OK => { reqwest::StatusCode::OK => {
fetched_resources = match dbg!(response.json::<Vec<proto::Resource>>().await) { fetched_resources = match dbg!(response.json::<Vec<proto::Resource>>().await) {
@ -307,6 +295,5 @@ pub async fn request_resources( api_endpoint: String,
fetched_resources = Err(Error::String(other)) fetched_resources = Err(Error::String(other))
} }
}; };
println!("Resources: {:?}", fetched_resources);
fetched_resources fetched_resources
} }