Repository navigation
perf(proxy): track the first reply to each simple query #531
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ use crate::proxy::EncryptionService; | |
| use crate::EqlCiphertext; | ||
| use metrics::{counter, histogram}; | ||
| use pg_proto::{BackendMessage, BackendMiddlewareOutput, DataRow, DiagnosticResponse}; | ||
| use std::collections::VecDeque; | ||
| use std::time::Instant; | ||
| use tracing::debug; | ||
|
|
||
|
|
@@ -66,6 +67,8 @@ use tracing::debug; | |
| pub struct Backend<S: EncryptionService> { | ||
| /// Session context with portal and statement metadata | ||
| context: Context<S>, | ||
| /// Forwarded simple Queries awaiting their first reply, in wire order. | ||
| pending_queries: VecDeque<OperationId>, | ||
| encrypted_rows: Vec<DataRow>, | ||
| encrypted_rows_operation: Option<OperationId>, | ||
| encrypted_rows_bytes: usize, | ||
|
|
@@ -138,13 +141,37 @@ impl<S: EncryptionService> Backend<S> { | |
| pub fn new(context: Context<S>) -> Self { | ||
| Backend { | ||
| context, | ||
| pending_queries: VecDeque::new(), | ||
| encrypted_rows: Vec::new(), | ||
| encrypted_rows_operation: None, | ||
| encrypted_rows_bytes: 0, | ||
| discard_execution: false, | ||
| } | ||
| } | ||
|
|
||
| pub(super) fn query_forwarded(&mut self, operation: OperationId) { | ||
| self.pending_queries.push_back(operation); | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| pub(super) fn pending_query_count(&self) -> usize { | ||
| self.pending_queries.len() | ||
| } | ||
|
|
||
| fn take_first_query_reply(&mut self, operation: OperationId) -> bool { | ||
| while let Some(&query) = self.pending_queries.front() { | ||
| if query > operation { | ||
|
tobyhede marked this conversation as resolved.
Resolved
|
||
| break; | ||
| } | ||
| // A reply to a later operation means PostgreSQL skipped the earlier Query. | ||
| self.pending_queries.pop_front(); | ||
| if query == operation { | ||
| return true; | ||
| } | ||
| } | ||
| false | ||
| } | ||
|
|
||
| pub async fn intercept( | ||
| &mut self, | ||
| operation: Option<OperationId>, | ||
|
|
@@ -160,11 +187,8 @@ impl<S: EncryptionService> Backend<S> { | |
| ) -> Result<BackendMiddlewareOutput, Error> { | ||
| let mut outbound_message = protocol_message.clone(); | ||
|
|
||
| // A row is never the first answer to an operation, so skip the lock for it | ||
| if let Some(operation) = | ||
| operation.filter(|_| !matches!(protocol_message, BackendMessage::DataRow(_))) | ||
| { | ||
| self.context.operation_answered(operation)?; | ||
| if let Some(query) = operation.filter(|&operation| self.take_first_query_reply(operation)) { | ||
| self.context.release_unnamed_statement(query)?; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: No test forwards an unnamed Impact: Evidence: Fix: Add a test next to Found by 1 model: claude |
||
| } | ||
|
|
||
| if let (BackendMessage::CommandComplete(tag), Some(operation)) = | ||
|
|
@@ -850,6 +874,24 @@ mod tests { | |
| (Backend::new(context.clone()), context) | ||
| } | ||
|
|
||
| #[test] | ||
| fn only_the_first_reply_to_each_forwarded_query_releases_the_unnamed_statement() { | ||
| let mut backend = create_backend(); | ||
| let earlier = operation_id(); | ||
| let first = operation_id(); | ||
| let skipped = operation_id(); | ||
| let second = operation_id(); | ||
| backend.query_forwarded(first); | ||
| backend.query_forwarded(skipped); | ||
| backend.query_forwarded(second); | ||
|
|
||
| assert!(!backend.take_first_query_reply(earlier)); | ||
| assert!(backend.take_first_query_reply(first)); | ||
| assert!(!backend.take_first_query_reply(first)); | ||
| assert!(backend.take_first_query_reply(second)); | ||
| assert!(!backend.take_first_query_reply(second)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn decryption_failure_is_returned_as_a_postgresql_error() { | ||
| let mut backend = create_backend(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.