Skip to content

Commit

Permalink
Merge pull request #595 from helium/jg/fix-skf-update
Browse files Browse the repository at this point in the history
fixing update order/operations for iot session key filter reconciliation
  • Loading branch information
madninja authored Aug 11, 2023
2 parents 55d077f + e856b58 commit d968ada
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions iot_config/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,16 +713,18 @@ pub async fn update_skfs(
) -> anyhow::Result<()> {
let mut transaction = db.begin().await?;

let added_updates: Vec<(Skf, proto::ActionV1)> = insert_skfs(to_add, &mut transaction)
// Always process removes before adds to ensure updating existing values doesn't result in
// removing a value that was just added
let removed_updates: Vec<(Skf, proto::ActionV1)> = remove_skfs(to_remove, &mut transaction)
.await?
.into_iter()
.map(|added_skf| (added_skf, proto::ActionV1::Add))
.map(|removed_skf| (removed_skf, proto::ActionV1::Remove))
.collect();

let removed_updates: Vec<(Skf, proto::ActionV1)> = remove_skfs(to_remove, &mut transaction)
let added_updates: Vec<(Skf, proto::ActionV1)> = insert_skfs(to_add, &mut transaction)
.await?
.into_iter()
.map(|removed_skf| (removed_skf, proto::ActionV1::Remove))
.map(|added_skf| (added_skf, proto::ActionV1::Add))
.collect();

transaction.commit().await?;
Expand Down Expand Up @@ -766,8 +768,11 @@ async fn insert_skfs(skfs: &[Skf], db: impl sqlx::PgExecutor<'_>) -> anyhow::Res

const SKF_INSERT_VALS: &str =
" insert into route_session_key_filters (route_id, devaddr, session_key, max_copies) ";
// changes to existing records are always treated as an upsert and override the existing value
// instead of ignoring. this avoids reconciliation bugs attempting to update fields of an
// existing skf from succeeding on the HPRs but being ignored by the Config Service
const SKF_INSERT_CONFLICT: &str =
" on conflict (route_id, devaddr, session_key) do nothing returning * ";
" on conflict (route_id, devaddr, session_key) update set max_copies = excluded.max_copies returning * ";

let mut query_builder: sqlx::QueryBuilder<sqlx::Postgres> =
sqlx::QueryBuilder::new(SKF_INSERT_VALS);
Expand Down

0 comments on commit d968ada

Please sign in to comment.