Skip to content

Commit

Permalink
fix(api): improve is_healthy checking (#436)
Browse files Browse the repository at this point in the history
Improve is_healthy checking
  • Loading branch information
Alexandcoats authored Jul 14, 2022
1 parent 5bcbb7a commit 683efa4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
14 changes: 3 additions & 11 deletions bin/inx-chronicle/src/api/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async fn login(
}

async fn is_healthy(database: Extension<MongoDb>) -> bool {
let end = match database.find_last_milestone(u32::MAX.into()).await {
let end = match database.get_latest_milestone().await {
Ok(Some(last)) => last,
_ => return false,
};
Expand All @@ -74,17 +74,9 @@ async fn is_healthy(database: Extension<MongoDb>) -> bool {
return false;
}

let start = match database.find_first_milestone(0.into()).await {
Ok(Some(first)) => first,
_ => return false,
};

// Check if there are no gaps in the sync status.
match database
.get_sync_data(start.milestone_index..=end.milestone_index)
.await
{
Ok(sync) => sync.gaps.is_empty(),
match database.get_gaps().await {
Ok(gaps) => gaps.is_empty(),
_ => false,
}
}
Expand Down
47 changes: 44 additions & 3 deletions src/db/collections/milestone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ impl MongoDb {
.0
.collection::<MilestoneDocument>(MilestoneDocument::COLLECTION)
.find(
doc! {"milestone_timestamp": { "$gte": start_timestamp }},
doc! {
"milestone_timestamp": { "$gte": start_timestamp },
"is_synced": true
},
FindOptions::builder()
.sort(doc! {"milestone_index": 1})
.limit(1)
Expand All @@ -247,7 +250,10 @@ impl MongoDb {
.0
.collection::<MilestoneDocument>(MilestoneDocument::COLLECTION)
.find(
doc! {"milestone_timestamp": { "$lte": end_timestamp }},
doc! {
"milestone_timestamp": { "$lte": end_timestamp },
"is_synced": true
},
FindOptions::builder()
.sort(doc! {"milestone_index": -1})
.limit(1)
Expand All @@ -267,7 +273,7 @@ impl MongoDb {
self.0
.collection::<MilestoneIndexTimestamp>(MilestoneDocument::COLLECTION)
.find(
doc! {},
doc! { "is_synced": true },
FindOptions::builder()
.sort(doc! {"milestone_index": -1})
.limit(1)
Expand Down Expand Up @@ -357,6 +363,41 @@ impl MongoDb {
Ok(sync_data)
}

/// Retrieves gaps in the milestones collection.
pub async fn get_gaps(&self) -> Result<Vec<RangeInclusive<MilestoneIndex>>, Error> {
#[derive(Deserialize)]
struct SyncEntry {
milestone_index: MilestoneIndex,
}

let mut synced_ms = self
.0
.collection::<SyncEntry>(MilestoneDocument::COLLECTION)
.find(
doc! { "is_synced": true },
FindOptions::builder()
.sort(doc! {"milestone_index": 1})
.projection(doc! {"milestone_index": 1})
.build(),
)
.await
.map(|c| c.map_ok(|e| e.milestone_index))?;

let mut gaps = Vec::new();
let mut last_record: Option<MilestoneIndex> = None;

while let Some(milestone_index) = synced_ms.try_next().await? {
// Missing records go into gaps
if let Some(&last) = last_record.as_ref() {
if last + 1 < milestone_index {
gaps.push(last + 1..=milestone_index - 1);
}
}
last_record.replace(milestone_index);
}
Ok(gaps)
}

/// Streams all available receipt milestone options together with their corresponding `MilestoneIndex`.
pub async fn stream_all_receipts(
&self,
Expand Down

0 comments on commit 683efa4

Please sign in to comment.