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: Manually update the spanner session approximate_last_used_time #1009

Merged
merged 18 commits into from
Mar 12, 2021
Merged
Changes from 8 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
79 changes: 47 additions & 32 deletions src/db/spanner/manager/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use grpcio::{CallOption, ChannelBuilder, ChannelCredentials, Environment, Metada
use std::sync::Arc;
use std::time::SystemTime;

use crate::{
db::error::{DbError, DbErrorKind},
server::metrics::Metrics,
};
use crate::{db::error::DbError, server::metrics::Metrics};

const SPANNER_ADDRESS: &str = "spanner.googleapis.com:443";

Expand Down Expand Up @@ -74,44 +71,62 @@ pub async fn recycle_spanner_session(
let now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
if let Some(max_life) = max_lifetime {
// get the current UTC seconds
if let Some(age) = conn.session.create_time.clone().into_option() {
let age = now - age.seconds as u64;
if age > max_life as u64 {
metrics.incr("db.connection.max_life");
dbg!("### aging out", conn.session.get_name());
return Err(DbErrorKind::Expired.into());
.as_secs() as i64;
let mut req = GetSessionRequest::new();
req.set_name(conn.session.get_name().to_owned());
/*
Connections can sometimes produce GOAWAY errors. GOAWAYs are HTTP2 frame
errors that are (usually) sent before a given connection is shut down. It
appears that GRPC passes these up the chain. The problem is that since the
connection is being closed, further retries will (probably?) also fail. The
best course of action is to spin up a new session.

In theory, UNAVAILABLE-GOAWAY messages are retryable. How we retry them,
however, is not so clear. There are a few places in spanner functions where
we could possibly do this, but they get complicated quickly. (e.g. pass a
`&mut SpannerDb` to `db.execute_async`, but that gets REALLY messy, REALLY
fast.)

For now, we try a slightly different tactic here. Connections can age out
both from overall age and from lack of use. We can try to pre-emptively
kill off connections before we get the GOAWAY messages. Any additional
GOAWAY messages would be returned to the client as a 500 which will
result in the client re-trying.

*/
match conn.client.get_session_async(&req)?.await {
Ok(session) => {
if let Some(max_life) = max_lifetime {
let create_time = session.get_create_time().seconds;
let age = now - create_time;
if age > max_life as i64 {
metrics.incr("db.connection.max_life");
dbg!("### aging out", conn.session.get_name());
conn.session = create_session(&conn.client, database_name).await?;
Copy link
Member

@pjenvey pjenvey Mar 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, forgot to get back around to this last week.

The last conversation we had about this I had a realization: the create_time/approximate_last_use_time stats we've begun using are for the Spanner Session but the GOAWAYs are likely more about the lower level GRPC connection/channel.

The case Err(e) => block below potentially recreates sessions on existing channels. In that case the Session create_time doesn't tell us about the GRPC channel but instead a Session possibly created much later than the original channel was.

The point of all this:

  • We really want to throw away the entire GRPC channel/connection (disconnecting it in the process) during max_life/idle events. Instead of recreating the session here, let's return Err(DbErrorKind::Expired.into()) which should cause deadpool to drop our SpannerSession struct (which is a confusing name here, it's really a combination of both the SpannerClient with its underlying grpcio::Channel and the spanner Session on top of it). Drop of the grpcio::Channel will disconnect it.
  • We should probably manually tracking the grpc channel/connection create time ourselves and use it for this check because the Spanner Session start_time could be different from the parent channel's (though I think its idle time would always be accurate?) I'm fine punting on this for now though (doing it later in a separate PR) because the Session's version probably doesn't differ very often -- with the return Err( change this should hopefully avoid a significant amount of our GOAWAYS as is. 🤷‍♂️

}
}
}
}
// check how long that this has been idle...
if let Some(max_idle) = max_idle {
if let Some(idle) = conn.session.approximate_last_use_time.clone().into_option() {
// get current UTC seconds
let idle = std::cmp::max(0, now as i64 - idle.seconds);
if idle > max_idle as i64 {
metrics.incr("db.connection.max_idle");
dbg!("### idling out", conn.session.get_name());
return Err(DbErrorKind::Expired.into());
// check how long that this has been idle...
if let Some(max_idle) = max_idle {
let last_use = session.get_approximate_last_use_time().seconds;
let idle = std::cmp::max(0, now - last_use);
if idle > max_idle as i64 {
metrics.incr("db.connection.max_idle");
dbg!("### idling out", session.get_name());
conn.session = create_session(&conn.client, database_name).await?;
}
}
Ok(())
}
}

let mut req = GetSessionRequest::new();
req.set_name(conn.session.get_name().to_owned());
if let Err(e) = conn.client.get_session_async(&req)?.await {
match e {
Err(e) => match e {
grpcio::Error::RpcFailure(ref status)
if status.status == grpcio::RpcStatusCode::NOT_FOUND =>
{
conn.session = create_session(&conn.client, database_name).await?;
Ok(())
}
_ => return Err(e.into()),
}
},
}
Ok(())
}

async fn create_session(
Expand Down