Skip to content
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion core/crates/drivers/mysql/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ pub(crate) async fn catalog_rows(
table: &'static str,
binds: &[Option<&str>],
) -> Result<Vec<CatalogCells>, DriverError> {
let mut query = sqlx::query(sql).persistent(false);
// The text is fixed, so the connection keeps it prepared and a later read only executes it.
let mut query = sqlx::query(sql).persistent(true);
for bind in binds {
query = query.bind(bind.map(str::to_owned));
}
Expand Down
13 changes: 10 additions & 3 deletions core/crates/drivers/mysql/src/physical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ use crate::relay::Relay;
use crate::session_statements::{EDITOR_SQL_MODE_STATEMENT, SessionLimits, for_session};
use crate::workspace_session::{WORKSPACE_SQL_MODE_STATEMENT, WORKSPACE_TIME_ZONE};

// Room for every fixed catalog statement of either flavour.
const CACHED_STATEMENTS: usize = 32;

/// Which session a physical connection opens as.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SessionKind {
Expand Down Expand Up @@ -80,8 +83,8 @@ impl MysqlEndpoint {
.no_engine_substitution(false)
.set_names(false)
.timezone(None::<String>)
// No statement outlives the call that prepared it, so a pool
// connection never piles up server-side statement handles.
// No statement outlives the call that prepared it, so a
// session never piles up server-side statement handles.
.statement_cache_capacity(0)
// sqlx would log every statement, values and all.
.disable_statement_logging();
Expand Down Expand Up @@ -147,7 +150,11 @@ impl MysqlEndpoint {
},
};
Ok(match kind {
SessionKind::Workspace => options.timezone(Some(WORKSPACE_TIME_ZONE.to_owned())),
// The catalog's fixed statements stay prepared, so a later read
// skips the PREPARE; the bound caps the handles a pool member holds.
SessionKind::Workspace => options
.timezone(Some(WORKSPACE_TIME_ZONE.to_owned()))
.statement_cache_capacity(CACHED_STATEMENTS),
SessionKind::Editor | SessionKind::Control => options,
})
}
Expand Down
9 changes: 3 additions & 6 deletions core/crates/drivers/mysql/src/stream_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use futures_util::TryStreamExt;
use sqlx::mysql::{MySqlConnection, MySqlDatabaseError};
use sqlx::{AssertSqlSafe, Either, Executor as _, Row as _, SqlSafeStr as _, Statement as _};
use sqlx::{AssertSqlSafe, Either, Executor as _, Row as _, Statement as _};
use tablepro_core::column::ResultColumn;
use tablepro_core::sql_syntax::{SqlGrammar, StatementAccess, statement_access};
use tablepro_core::{
Expand All @@ -15,7 +15,7 @@ use crate::errors::map_error;
use crate::flavour::MySqlFlavour;
use crate::mysql_timestamp_mode::MySqlTimestampMode;
use crate::mysql_wire_format::MySqlWireFormat;
use crate::workspace_call::{describe, prepared};
use crate::workspace_call::{describe, prepare_uncached, prepared};

const BATCH_ROWS: usize = 256;

Expand Down Expand Up @@ -106,10 +106,7 @@ async fn prepare_as_one(
flavour: MySqlFlavour,
sql: &str,
) -> Result<Vec<ResultColumn>, DriverError> {
let prepared = (&mut *connection)
.prepare(AssertSqlSafe(sql.to_owned()).into_sql_str())
.await;
match prepared {
match prepare_uncached(connection, sql).await {
Ok(statement) => Ok(statement
.columns()
.iter()
Expand Down
23 changes: 18 additions & 5 deletions core/crates/drivers/mysql/src/workspace_call.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use futures_util::TryStreamExt;
use futures_util::future::BoxFuture;
use sqlx::mysql::MySqlConnection;
use sqlx::{AssertSqlSafe, Executor as _, Row as _, SqlSafeStr as _, Statement as _};
use sqlx::mysql::{MySqlConnection, MySqlStatement};
use sqlx::{AssertSqlSafe, Connection as _, Executor as _, Row as _, SqlSafeStr as _, Statement as _};
use tablepro_core::column::ResultColumn;
use tablepro_core::statement::Statement;
use tablepro_core::{DriverError, QueryResult, RowLimit};
Expand Down Expand Up @@ -83,14 +83,13 @@ pub(crate) async fn query_bound(
}

/// The columns of a statement that returned no rows: the prepare
/// carries them, and with no cache the statement is closed right after.
/// carries them.
pub(crate) async fn describe(
connection: &mut MySqlConnection,
flavour: MySqlFlavour,
sql: &str,
) -> Result<Vec<ResultColumn>, DriverError> {
let prepared = (&mut *connection)
.prepare(AssertSqlSafe(sql.to_owned()).into_sql_str())
let prepared = prepare_uncached(connection, sql)
.await
.map_err(|error| map_error(&error, flavour))?;
Ok(prepared
Expand All @@ -100,6 +99,20 @@ pub(crate) async fn describe(
.collect())
}

// The cache keys a statement by its text alone and keeps the columns of its first PREPARE, and after a schema change
// the same text has other columns. sqlx can only empty the whole cache, so the catalog's statements go with it and are
// prepared again on their next read.
pub(crate) async fn prepare_uncached(
connection: &mut MySqlConnection,
sql: &str,
) -> Result<MySqlStatement, sqlx::Error> {
let prepared = (&mut *connection)
.prepare(AssertSqlSafe(sql.to_owned()).into_sql_str())
.await;
connection.clear_cached_statements().await?;
prepared
}

pub(crate) fn count_op(statement: Statement) -> ReadOp<u64> {
read_op(move |connection, flavour| Box::pin(async move { count(connection, flavour, &statement).await }))
}
Expand Down
1 change: 1 addition & 0 deletions core/crates/drivers/mysql/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ mod support;
mod tls;
mod user_read;
mod value_corpus;
mod wire_round_trips;
mod write_batch;
76 changes: 76 additions & 0 deletions core/crates/drivers/mysql/tests/integration/wire_round_trips.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! How many times a workspace read waits on the server, counted on the
//! wire.

use std::sync::Arc;

use tablepro_core::{RowLimit, TlsConfig, WorkspaceSettings};
use tablepro_test_fixtures::MysqlFlavour;
use tablepro_test_support::RoundTripCounter;
use tablepro_test_support::conformance::test_liveness;

use crate::support::{CALL, connect_with, direct, options_over, password, setup, start, statement, table, within};

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "requires docker"]
async fn a_catalog_read_on_a_warm_connection_skips_the_prepare() {
let fixture = start(MysqlFlavour::Mysql).await;
let liveness = test_liveness();
let counter = Arc::new(RoundTripCounter::new(direct(fixture.host(), fixture.port(), &liveness)));
let connection = connect_with(options_over(
counter.clone(),
password(&fixture.password_credentials()),
fixture.database(),
TlsConfig::disabled(),
WorkspaceSettings::workspace(false),
liveness,
))
.await;
setup(
connection.as_ref(),
&["CREATE TABLE t (id int PRIMARY KEY, name varchar(20))"],
)
.await;
let columns = || async {
let before = counter.round_trips();
connection
.fetch_columns(&table("t"), within(CALL))
.await
.expect("the columns");
counter.round_trips() - before
};
columns().await;

// The pool's ping before the read is the other round trip.
assert_eq!([columns().await, columns().await], [2, 2]);
connection.close().await.expect("a close");
}

/// The columns of an empty page come from a prepare, which must not
/// serve the columns the same text had before a schema change.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "requires docker"]
async fn an_empty_page_shows_the_columns_after_a_type_change() {
let fixture = start(MysqlFlavour::Mysql).await;
let connection = crate::support::connect(&fixture, false).await;
setup(connection.as_ref(), &["CREATE TABLE e (id int PRIMARY KEY, n int)"]).await;
let columns = || async {
connection
.query_bound(&statement("SELECT * FROM e"), RowLimit::EDITOR_DEFAULT, within(CALL))
.await
.expect("an empty page")
.columns
.iter()
.map(|column| column.column_type.name.clone())
.collect::<Vec<_>>()
};
let before = columns().await;

setup(connection.as_ref(), &["ALTER TABLE e MODIFY n varchar(20)"]).await;

assert_ne!(
columns().await,
before,
"the empty page kept the columns from before the change"
);
connection.close().await.expect("a close");
}
1 change: 1 addition & 0 deletions core/crates/drivers/postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ tablepro-net.workspace = true
async-trait.workspace = true
bytes.workspace = true
chrono.workspace = true
deadpool.workspace = true
deadpool-postgres.workspace = true
fallible-iterator.workspace = true
futures-util.workspace = true
Expand Down
48 changes: 39 additions & 9 deletions core/crates/drivers/postgres/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use async_trait::async_trait;
use deadpool_postgres::{Pool, PoolError, TimeoutType, Timeouts};
use deadpool_postgres::{PoolError, TimeoutType, Timeouts};
use tablepro_core::column::ColumnInfo;
use tablepro_core::ddl::{DatabaseChoices, InboundForeignKey, OutboundForeignKey, TableDefinition};
use tablepro_core::dialect::SqlDialect;
Expand Down Expand Up @@ -31,7 +31,7 @@ use crate::errors::{map_error, map_pool_error};
use crate::metadata;
use crate::pg_call::PgCall;
use crate::physical::PgEndpoint;
use crate::pool::{probe_timeouts, workspace_timeouts};
use crate::pool::{PgObject, PgPool, probe_timeouts, workspace_timeouts};
use crate::standalone::Standalone;
use crate::stream_read::stream;
use crate::workspace_call::{
Expand All @@ -43,7 +43,7 @@ use crate::write_batch::{WritePlan, perform, precheck};
/// connections behind the sidebar, the grid and every save.
pub(crate) struct PgConnection {
endpoint: Arc<PgEndpoint>,
pool: Pool,
pool: PgPool,
calls: OwnedCalls,
tracker: TaskTracker,
settings: WorkspaceSettings,
Expand All @@ -56,7 +56,7 @@ pub(crate) struct PgConnection {
impl PgConnection {
pub(crate) fn new(
endpoint: Arc<PgEndpoint>,
pool: Pool,
pool: PgPool,
tracker: TaskTracker,
settings: WorkspaceSettings,
server: ServerInfo,
Expand Down Expand Up @@ -229,6 +229,35 @@ impl PgConnection {
}
}

// A member that has not answered may sit on a dead route, so it leaves the pool instead of going back, also when the
// probe's deadline drops it mid-question.
struct Asking(Option<PgObject>);

impl Asking {
async fn answer(mut self) -> Result<(), DriverError> {
let answered = match self.0.as_ref() {
Some(object) => object
.simple_query("SELECT 1")
.await
.map(|_| ())
.map_err(|error| map_error(&error)),
None => Ok(()),
};
if answered.is_ok() {
drop(self.0.take());
}
answered
}
}

impl Drop for Asking {
fn drop(&mut self) {
if let Some(object) = self.0.take() {
drop(PgObject::take(object));
}
}
}

/// A failure that may mean the route is gone rather than one statement:
/// a lost connection, a transport error, or a timeout the server never
/// acknowledged.
Expand Down Expand Up @@ -292,11 +321,12 @@ impl Connection for PgConnection {
let probing = async {
let quick: Timeouts = probe_timeouts(self.liveness());
match self.pool.timeout_get(&quick).await {
Ok(object) => object
.simple_query("SELECT 1")
.await
.map(|_| ())
.map_err(|error| map_error(&error)),
Ok(object) => match Asking(Some(object)).answer().await {
// Only this member may have gone stale; a login of its own clears the route, and otherwise the
// member's answer stands.
Err(error) if suspects_link(&error) => self.probe_standalone(deadline).await.map_err(|_| error),
answered => answered,
},
// Every slot is busy, which is not the server being gone.
Err(PoolError::Timeout(TimeoutType::Wait)) => self.probe_standalone(deadline).await,
Err(error) => Err(map_pool_error(&error)),
Expand Down
Loading
Loading