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

fix: Retry writing lease when error code 429 (too many requests). #2773

Merged
merged 1 commit into from
Mar 12, 2024
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
45 changes: 43 additions & 2 deletions crates/metastore/src/storage/lease.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use protogen::gen::metastore::storage;
use protogen::metastore::types::storage::{LeaseInformation, LeaseState};
use tokio::sync::{mpsc, oneshot};
use tokio::task::JoinHandle;
use tracing::{debug_span, error, Instrument};
use tracing::{debug, debug_span, error, Instrument};
use uuid::Uuid;

use crate::storage::{Result, SingletonStorageObject, StorageError, StorageObject};
Expand Down Expand Up @@ -339,11 +339,16 @@ impl LeaseRenewer {
Ok(lease)
}

async fn write_lease(&self, lease: LeaseInformation) -> Result<()> {
async fn try_write_lease(&self, lease: LeaseInformation) -> Result<()> {
// Write to storage.
let proto: storage::LeaseInformation = lease.into();
let mut bs = BytesMut::new();
proto.encode(&mut bs)?;

// TODO: Generate a new temp path everytime? This will definitely avoid
// the rate limitation over over-writing the same object here and might
// just be enough to delay the "renaming" process so we don't exceed
// the limit there as well.
Comment on lines +348 to +351
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@scsmithr how do you feel about this?

Copy link
Member

Choose a reason for hiding this comment

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

I think that'd be reasonable, we can just generate a temp path every time. For some reason, I had read the original error message thinking that we were errorring on writing to the actual lease, and not the temp. But if it's just the temp, then this seems good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I merged this PR before reading this. Will open another to add this as well.

self.store.put(&self.tmp_path, bs.freeze()).await?;

// Rename...
Expand All @@ -360,6 +365,42 @@ impl LeaseRenewer {

Ok(())
}

async fn write_lease(&self, lease: LeaseInformation) -> Result<()> {
let mut final_err = None;

for num_try in 0..3 {
if let Err(err) = self.try_write_lease(lease.clone()).await {
final_err = Some(err.to_string());

if let StorageError::ObjectStore(err) = &err {
if let ObjectStoreError::Generic { store, source } = err {
let source = source.to_string();
// Error code 429 is not handled by object_store's retry
// configuration. We should maybe upstream to catch this
// and retry properly.
if store == &"GCS" && source.contains("429") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I worry about the fragility of this little block, (in terms of the matching/destructuring of the error as well as the constants/string parsing of the error.

debug!(%err, %num_try, "retrying writing the lease");
// Sleep for some time (half the time of when the
// request wouldn't throttle) before trying to write
// the lease.
//
// See: https://cloud.google.com/storage/docs/objects#immutability
tokio::time::sleep(Duration::from_millis(500)).await;
continue;
}
}
}

return Err(err);
} else {
return Ok(());
}
}

// Return the original error if failed after retries.
Err(StorageError::LeaseWriteError(final_err.unwrap()))
}
}

#[cfg(test)]
Expand Down
3 changes: 3 additions & 0 deletions crates/metastore/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pub enum StorageError {
#[error("Lease renewer exited.")]
LeaseRenewerExited,

#[error("Unable to write to lease object after retries: {0}")]
LeaseWriteError(String),

#[error(transparent)]
ProtoConv(#[from] protogen::errors::ProtoConvError),

Expand Down