Skip to content

Adding Global Endpoint Manager and Location Cache #2801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub struct GlobalEndpointManager {}
3 changes: 3 additions & 0 deletions sdk/cosmos/azure_data_cosmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub(crate) mod utils;

pub mod models;

mod global_endpoint_manager;
mod location_cache;

#[doc(inline)]
pub use clients::CosmosClient;

Expand Down
95 changes: 95 additions & 0 deletions sdk/cosmos/azure_data_cosmos/src/location_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use std::collections::HashMap;

use url::Url;

#[derive(Clone)]
pub struct DatabaseAccountLocationsInfo {
pub preferred_locations: Vec<String>,
available_write_locations: Vec<String>,
available_read_locations: Vec<String>,
available_write_endpoints_by_location: HashMap<String, Url>,
available_read_endpoints_by_location: HashMap<String, Url>,
write_endpoints: Vec<Url>,
read_endpoints: Vec<Url>,
}

impl Default for DatabaseAccountLocationsInfo {
fn default() -> Self {
DatabaseAccountLocationsInfo {
preferred_locations: Vec::new(),
available_write_locations: Vec::new(),
available_read_locations: Vec::new(),
available_write_endpoints_by_location: HashMap::new(),
available_read_endpoints_by_location: HashMap::new(),
write_endpoints: Vec::new(),
read_endpoints: Vec::new(),
}
}
}

pub struct LocationCache {
pub default_endpoint: Url,
pub preferred_locations: Vec<String>,
pub locations_info: DatabaseAccountLocationsInfo,
}

impl LocationCache {
pub fn new(default_endpoint: Url, preferred_locations: Vec<String>) -> Self {
Self {
default_endpoint,
preferred_locations,
locations_info: DatabaseAccountLocationsInfo {
preferred_locations,
..Default::default()
},
}
}

pub fn update_location_cache(
&mut self,
write_locations: HashMap<String, Url>,
read_locations: HashMap<String, Url>,
preferred_locations: Vec<String>,
) {
let mut locations_info_copy = self.locations_info.clone();

if !preferred_locations.is_empty() {
locations_info_copy.preferred_locations = preferred_locations;
}

if !write_locations.is_empty() {
let (available_write_endpoints_by_location, available_write_locations) =
self.get_endpoints_by_location(write_locations);
locations_info_copy.available_write_endpoints_by_location =
available_write_endpoints_by_location;
locations_info_copy.available_write_locations = available_write_locations;
}

if !read_locations.is_empty() {
let (available_read_endpoints_by_location, available_read_locations) =
self.get_endpoints_by_location(read_locations);
locations_info_copy.available_read_endpoints_by_location =
available_read_endpoints_by_location;
locations_info_copy.available_read_locations = available_read_locations;
}

self.locations_info = locations_info_copy;
}

pub fn get_endpoints_by_location(
&mut self,
locations: HashMap<String, Url>,
) -> (HashMap<String, Url>, Vec<String>) {
let mut endpoints_by_location: HashMap<String, Url> = HashMap::new();
let mut parsed_locations: Vec<String> = Vec::new();

for (location, url) in locations {
if location != "" {
endpoints_by_location.insert(location.clone(), url.clone());
parsed_locations.push(location);
}
}

(endpoints_by_location, parsed_locations)
}
}
Loading