Skip to content
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

feat: Default endpoints and gateway accessors are cached between requests #337

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
51 changes: 47 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions crates/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tracing-config = ["tracing", "qcs-api-client-common/tracing-config", "qcs-api-cl
otel-tracing = ["tracing-config", "qcs-api-client-grpc/otel-tracing", "qcs-api-client-openapi/otel-tracing"]

[dependencies]
cached = "0.44.0"
dirs = "5.0.0"
enum-as-inner = "0.5.1"
futures = "0.3.24"
Expand Down Expand Up @@ -43,8 +44,6 @@ uuid = { version = "1.2.1", features = ["v4"] }
tonic = { version = "0.9.2", features = ["tls", "tls-roots"] }
zmq = { version = "0.10.0" }
itertools = "0.11.0"
rstest = "0.17.0"
insta = "1.29.0"
derive_builder = "0.12.0"

[dev-dependencies]
Expand All @@ -60,6 +59,8 @@ warp = { version = "0.3.3", default-features = false }
regex = "1.7.0"
test-case = "3.1.0"
tracing-subscriber = "0.3.17"
rstest = "0.17.0"
insta = "1.29.0"

[build-dependencies]
built = "0.6.1"
127 changes: 87 additions & 40 deletions crates/lib/src/qpu/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use std::{fmt, time::Duration};

use cached::proc_macro::cached;
use derive_builder::Builder;
use qcs_api_client_common::{configuration::RefreshError, ClientConfiguration};
pub use qcs_api_client_grpc::channel::Error as GrpcError;
Expand All @@ -22,7 +23,8 @@ use qcs_api_client_grpc::{
pub use qcs_api_client_openapi::apis::Error as OpenApiError;
use qcs_api_client_openapi::apis::{
endpoints_api::{
get_default_endpoint, get_endpoint, GetDefaultEndpointError, GetEndpointError,
get_default_endpoint as api_get_default_endpoint, get_endpoint, GetDefaultEndpointError,
GetEndpointError,
},
quantum_processors_api::{
list_quantum_processor_accessors, ListQuantumProcessorAccessorsError,
Expand Down Expand Up @@ -307,55 +309,100 @@ impl ExecutionOptions {
quantum_processor_id: &str,
client: &Qcs,
) -> Result<String, QpuApiError> {
let mut min = None;
let mut next_page_token = None;
loop {
let accessors = list_quantum_processor_accessors(
&client.get_openapi_client(),
quantum_processor_id,
Some(100),
next_page_token.as_deref(),
)
.await?;

let accessor = accessors
.accessors
.into_iter()
.filter(|acc| {
acc.live
// `as_deref` needed to work around the `Option<Box<_>>` type.
&& acc.access_type.as_deref() == Some(&QuantumProcessorAccessorType::GatewayV1)
})
.min_by_key(|acc| acc.rank.unwrap_or(i64::MAX));

min = std::cmp::min_by_key(min, accessor, |acc| {
acc.as_ref().and_then(|acc| acc.rank).unwrap_or(i64::MAX)
});

next_page_token = accessors.next_page_token.clone();
if next_page_token.is_none() {
break;
}
}
min.map(|accessor| accessor.url)
.ok_or_else(|| QpuApiError::GatewayNotFound(quantum_processor_id.to_string()))
get_accessor_with_cache(quantum_processor_id, client).await
}

async fn get_default_endpoint_address(
&self,
quantum_processor_id: &str,
client: &Qcs,
) -> Result<String, QpuApiError> {
let default_endpoint =
get_default_endpoint(&client.get_openapi_client(), quantum_processor_id).await?;
let addresses = default_endpoint.addresses.as_ref();
let grpc_address = addresses.grpc.as_ref();
grpc_address
.ok_or_else(|| QpuApiError::QpuEndpointNotFound(quantum_processor_id.into()))
.cloned()
get_default_endpoint_with_cache(quantum_processor_id, client).await
}
}

#[cached(
result = true,
time = 60,
BatmanAoD marked this conversation as resolved.
Show resolved Hide resolved
time_refresh = true,
sync_writes = true,
key = "String",
convert = r"{ String::from(quantum_processor_id)}"
)]
async fn get_accessor_with_cache(
quantum_processor_id: &str,
client: &Qcs,
) -> Result<String, QpuApiError> {
#[cfg(feature = "tracing")]
tracing::info!(quantum_processor_id=%quantum_processor_id, "get_accessor cache miss");
get_accessor(quantum_processor_id, client).await
}

async fn get_accessor(quantum_processor_id: &str, client: &Qcs) -> Result<String, QpuApiError> {
let mut min = None;
let mut next_page_token = None;
loop {
let accessors = list_quantum_processor_accessors(
&client.get_openapi_client(),
quantum_processor_id,
Some(100),
next_page_token.as_deref(),
)
.await?;

let accessor = accessors
.accessors
.into_iter()
.filter(|acc| {
acc.live
// `as_deref` needed to work around the `Option<Box<_>>` type.
&& acc.access_type.as_deref() == Some(&QuantumProcessorAccessorType::GatewayV1)
})
.min_by_key(|acc| acc.rank.unwrap_or(i64::MAX));
BatmanAoD marked this conversation as resolved.
Show resolved Hide resolved

min = std::cmp::min_by_key(min, accessor, |acc| {
acc.as_ref().and_then(|acc| acc.rank).unwrap_or(i64::MAX)
});

next_page_token = accessors.next_page_token.clone();
if next_page_token.is_none() {
break;
}
}
min.map(|accessor| accessor.url)
.ok_or_else(|| QpuApiError::GatewayNotFound(quantum_processor_id.to_string()))
}

#[cached(
result = true,
time = 60,
time_refresh = true,
sync_writes = true,
key = "String",
convert = r"{ String::from(quantum_processor_id)}"
)]
async fn get_default_endpoint_with_cache(
BatmanAoD marked this conversation as resolved.
Show resolved Hide resolved
quantum_processor_id: &str,
client: &Qcs,
) -> Result<String, QpuApiError> {
#[cfg(feature = "tracing")]
tracing::info!(quantum_processor_id=%quantum_processor_id, "get_default_endpoint cache miss");
get_default_endpoint(quantum_processor_id, client).await
}

async fn get_default_endpoint(
quantum_processor_id: &str,
client: &Qcs,
) -> Result<String, QpuApiError> {
let default_endpoint =
api_get_default_endpoint(&client.get_openapi_client(), quantum_processor_id).await?;
let addresses = default_endpoint.addresses.as_ref();
let grpc_address = addresses.grpc.as_ref();
grpc_address
.ok_or_else(|| QpuApiError::QpuEndpointNotFound(quantum_processor_id.into()))
.cloned()
}

/// Errors that can occur while attempting to establish a connection to the QPU.
#[derive(Debug, thiserror::Error)]
pub enum QpuApiError {
Expand Down
Loading
Loading