// SPDX-FileCopyrightText: 2026 Epic Games, Inc. // SPDX-License-Identifier: MIT use std::future::Future; use std::sync::Arc; use std::sync::LazyLock; use std::time::Duration; use lore_base::retry::Retry; use lore_revision::util::time::RetryPolicy; use lore_telemetry::InstrumentProvider; use lore_telemetry::LabelArray; use lore_telemetry::METRICS_OPERATION_LATENCY_METRIC_NAME; use lore_telemetry::drop_record::DropRecord; use lore_telemetry::drop_time::DropTimeMs; use lore_telemetry::observe::Observe; #[cfg(feature = "test-util")] use mockall::mock; use opentelemetry::KeyValue; use opentelemetry::metrics::Counter; use opentelemetry::metrics::Histogram; use tokio::task::JoinSet; use tracing::Instrument; use uuid::Uuid; use crate::aws::dynamodb::BatchGetItemOutput; use crate::aws::dynamodb::Condition; use crate::aws::dynamodb::DynamoClient; use crate::aws::dynamodb::DynamoResult; pub use crate::aws::dynamodb::ExprNames; pub use crate::aws::dynamodb::ExprValues; use crate::aws::dynamodb::GetItemOutput; use crate::aws::dynamodb::QueryOutput; use crate::aws::dynamodb::QueryRequest; pub use crate::aws::dynamodb::ReturnValue; use crate::aws::dynamodb::ScanRequest; pub use crate::aws::dynamodb::Select; pub use crate::aws::dynamodb::TransactDelete; pub use crate::aws::dynamodb::TransactPut; pub use crate::aws::dynamodb::TransactWriteItem; use crate::aws::dynamodb::WriteOutput; use crate::aws::error::AwsCallError; use crate::aws::error::DynamoErrorCode; use crate::aws::item::Item; use crate::aws::observe::observe_aws_operation_callback; use crate::aws_error::AwsError; use crate::dynamodb::query_output_accumulation::QueryOutputAccumulation; pub mod cancellation_reason; pub mod query_output_accumulation; /// What a call to this facade can fail with. /// /// One alias for every operation: the error code is a typed enum, so the operation it came from is /// not part of the type. pub type DynamoError = AwsError>; pub const METRICS_TABLE_NAME_KEY: &str = "table_name"; const METRICS_PUT_ITEM_CONTEXT: &str = "put_item"; const METRICS_DELETE_ITEM_CONTEXT: &str = "delete_item"; const METRICS_CONDITIONAL_ATTRIBUTE: &str = "conditional"; const METRICS_BATCH_OPERATION_TOTAL_SIZE: &str = "batch_operation_size"; const SIZE_BOUNDARIES: [f64; 15] = [ 1., 5., 10., 50., 100., 200., 300., 500., 2_500., 5_000., 10_000., 20_000., 40_000., 60_000., 80_000., ]; const METRICS_NESTED_API_CALLS: &str = "nested_api_calls.total"; const NESTED_CALL_VOLUMES: [f64; 11] = [1., 2., 3., 6., 10., 20., 40., 80., 100., 150., 200.]; const METRICS_UNPROCESSED_RETRIES: &str = "unprocessed_retries.total"; const UNPROCESSED_RETRIES_BOUNDARIES: [f64; 11] = [0., 1., 2., 4., 7., 12., 18., 25., 30., 50., 100.]; const METRICS_UNPROCESSED_EXHAUSTED: &str = "unprocessed_retries.exhausted"; const DEFAULT_UNPROCESSED_KEYS_BACKOFF_START_MS: u64 = 50; const DEFAULT_UNPROCESSED_KEYS_BACKOFF_MAX_MS: u64 = 1_000; const DEFAULT_UNPROCESSED_KEYS_MAX_RESENDS: usize = 6; /// The default backoff for resending one `BatchGetItem` chunk's unprocessed keys. Its limit counts /// resends. pub fn default_unprocessed_keys_retry() -> RetryPolicy { RetryPolicy::builder() .with_initial_backoff_millis(DEFAULT_UNPROCESSED_KEYS_BACKOFF_START_MS) .with_max_backoff_millis(DEFAULT_UNPROCESSED_KEYS_BACKOFF_MAX_MS) .with_limit(DEFAULT_UNPROCESSED_KEYS_MAX_RESENDS) .build() } const METRICS_QUERY_COUNT: &str = "query.accumulation.count"; const METRICS_QUERY_SCANNED_ROWS: &str = "query.accumulation.scanned_count"; const QUERY_ROWS: [f64; 10] = [ 10., 50., 100., 400., 1_200., 2_400., 7_200., 21_600., 64_800., 194_400., ]; pub const METRICS_ACCUMULATION_OPERATION_LATENCY_METRIC_NAME: &str = "accumulation_operation_duration"; /// Temporary environment variable to allow enabling/disabling query pagination. /// TODO(jcohen): Remove this when we're confident in the pagination support. static ENABLE_QUERY_PAGINATION: LazyLock = LazyLock::new(|| match std::env::var("LORE_ENABLE_QUERY_PAGINATION") { Ok(v) => v.eq_ignore_ascii_case("true"), Err(_) => true, // Default to enabled if the env var isn't present }); /// A query this facade can run. /// /// The two expression maps are ordered small vectors of `&'static str` keys rather than /// `HashMap`: every placeholder and attribute name in every implementation is a literal /// or a `const`, so nothing here needs to own a `String` or hash it. pub trait DynamoDbQuery { fn index_name(&self) -> Option<&'static str> { None } fn key_condition_expression(&self) -> &str; fn expression_attribute_names(&self) -> ExprNames; fn expression_attribute_values(&self) -> ExprValues; fn filter_expression(&self) -> Option<&'static str> { None } fn limit(&self) -> Option { None } fn select(&self) -> Option