Skip to content

Commit

Permalink
[identity] Add exponential backoff to refresh_user_prekeys
Browse files Browse the repository at this point in the history
Summary:
Part of [[ https://linear.app/comm/issue/ENG-9427/transactionconflictexception-on-identity-in-prod | ENG-9427 ]].
Added exponential backoff retry counter to `RefreshUserPrekeys` RPC

Depends on D13568

Test Plan: It's hard to do that on localstack, but I was able to reuse test plan from D13356 to block DB item with transactions and then confirmed that `refresh_user_prekeys` also retries.

Reviewers: varun, will

Reviewed By: varun, will

Subscribers: ashoat, tomek

Differential Revision: https://phab.comm.dev/D13569
  • Loading branch information
barthap committed Oct 3, 2024
1 parent c90015c commit 24a3b86
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions services/identity/src/database/device_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use comm_lib::{
},
},
database::{
AttributeExtractor, AttributeMap, DBItemAttributeError, DBItemError,
DynamoDBError, TryFromAttribute,
batch_operations::ExponentialBackoffConfig, AttributeExtractor,
AttributeMap, DBItemAttributeError, DBItemError, DynamoDBError,
TryFromAttribute,
},
};
use serde::Serialize;
Expand All @@ -24,6 +25,7 @@ use crate::{
error_types, USERS_TABLE, USERS_TABLE_DEVICELIST_TIMESTAMP_ATTRIBUTE_NAME,
USERS_TABLE_PARTITION_KEY,
},
ddb_utils::is_transaction_conflict,
error::{DeviceListError, Error},
grpc_services::{
protos::{self, unauth::DeviceType},
Expand Down Expand Up @@ -820,7 +822,8 @@ impl DatabaseClient {
);
return Err(Error::InvalidFormat);
}
self

let db_operation = self
.client
.update_item()
.table_name(devices_table::NAME)
Expand All @@ -837,18 +840,29 @@ impl DatabaseClient {
.expression_attribute_names("#content_prekey", ATTR_CONTENT_PREKEY)
.expression_attribute_names("#notif_prekey", ATTR_NOTIF_PREKEY)
.expression_attribute_values(":content_prekey", content_prekey.into())
.expression_attribute_values(":notif_prekey", notif_prekey.into())
.send()
.await
.map_err(|e| {
error!(
errorType = error_types::DEVICE_LIST_DB_LOG,
"Failed to update device prekeys: {:?}", e
);
Error::AwsSdk(e.into())
})?;

Ok(())
.expression_attribute_values(":notif_prekey", notif_prekey.into());

let retry_config = ExponentialBackoffConfig::default();
let mut exponential_backoff = retry_config.new_counter();
loop {
let result = db_operation.clone().send().await;

match result {
Ok(_) => return Ok(()),
Err(err) => match DynamoDBError::from(err) {
ref conflict_err if is_transaction_conflict(conflict_err) => {
exponential_backoff.sleep_and_retry().await?;
}
error => {
error!(
errorType = error_types::DEVICE_LIST_DB_LOG,
"Failed to update device prekeys: {:?}", error
);
return Err(error.into());
}
},
}
}
}

/// Checks if given device exists on user's current device list
Expand Down

0 comments on commit 24a3b86

Please sign in to comment.