Skip to content

Commit

Permalink
feat: quiet remove_node_id's conditional failure case errors (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjenvey committed Nov 15, 2023
1 parent 2a67dd2 commit 7fe9d48
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
8 changes: 6 additions & 2 deletions autoendpoint/src/routers/webpush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl Router for WebPushRouter {
}
}
Err(error) => {
// Can't communicate with the node, so we should stop using it
// Can't communicate with the node, attempt to stop using it
debug!("✉ Error while triggering notification check: {}", error);
self.remove_node_id(&user, node_id).await?;
Ok(self.make_stored_response(notification))
Expand Down Expand Up @@ -195,9 +195,13 @@ impl WebPushRouter {
/// connected to the node.
async fn remove_node_id(&self, user: &User, node_id: &str) -> ApiResult<()> {
self.metrics.incr("updates.client.host_gone").ok();
self.db
let removed = self
.db
.remove_node_id(&user.uaid, node_id, user.connected_at)
.await?;
if !removed {
debug!("✉ The node id was not removed");
}
Ok(())
}

Expand Down
12 changes: 9 additions & 3 deletions autopush-common/src/db/bigtable/bigtable_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,12 @@ impl DbClient for BigTableClientImpl {
}

/// Remove the node_id. Can't really "surgically strike" this
async fn remove_node_id(&self, uaid: &Uuid, _node_id: &str, connected_at: u64) -> DbResult<()> {
async fn remove_node_id(
&self,
uaid: &Uuid,
_node_id: &str,
connected_at: u64,
) -> DbResult<bool> {
trace!(
"🉑 Removing node_ids for {} up to {:?} ",
&uaid.simple().to_string(),
Expand All @@ -723,8 +728,9 @@ impl DbClient for BigTableClientImpl {
&["node_id"].to_vec(),
Some(&time_range),
)
.await
.map_err(|e| e.into())
.await?;
// TODO: is a conditional check possible?
Ok(true)
}

/// Write the notification to storage.
Expand Down
9 changes: 5 additions & 4 deletions autopush-common/src/db/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ pub trait DbClient: Send + Sync {
/// Remove a channel from a user. Returns if the removed channel did exist.
async fn remove_channel(&self, uaid: &Uuid, channel_id: &Uuid) -> DbResult<bool>;

/// Remove the node ID from a user in the router table.
/// The node ID will only be cleared if `connected_at` matches up with the
/// item's `connected_at`.
async fn remove_node_id(&self, uaid: &Uuid, node_id: &str, connected_at: u64) -> DbResult<()>;
/// Remove the node ID from a user in the router table. Returns whether the
/// removal occurred. The node ID will only be removed if `connected_at`
/// matches up with the item's `connected_at`.
async fn remove_node_id(&self, uaid: &Uuid, node_id: &str, connected_at: u64)
-> DbResult<bool>;

/// Save a message to the message table
async fn save_message(&self, uaid: &Uuid, message: Notification) -> DbResult<()>;
Expand Down
18 changes: 13 additions & 5 deletions autopush-common/src/db/dynamodb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,12 @@ impl DbClient for DdbClientImpl {
.unwrap_or(false))
}

async fn remove_node_id(&self, uaid: &Uuid, node_id: &str, connected_at: u64) -> DbResult<()> {
async fn remove_node_id(
&self,
uaid: &Uuid,
node_id: &str,
connected_at: u64,
) -> DbResult<bool> {
let input = UpdateItemInput {
key: ddb_item! { uaid: s => uaid.simple().to_string() },
update_expression: Some("REMOVE node_id".to_string()),
Expand All @@ -350,14 +355,17 @@ impl DbClient for DdbClientImpl {
..Default::default()
};

retry_policy()
let result = retry_policy()
.retry_if(
|| self.db_client.update_item(input.clone()),
retryable_updateitem_error(self.metrics.clone()),
)
.await?;

Ok(())
.await;
match result {
Ok(_) => Ok(true),
Err(RusotoError::Service(UpdateItemError::ConditionalCheckFailed(_))) => Ok(false),
Err(e) => Err(e.into()),
}
}

async fn fetch_topic_messages(
Expand Down
7 changes: 6 additions & 1 deletion autopush-common/src/db/mock.rs