Skip to content

Commit

Permalink
[reaper] throw if ES update-by-query or delete-by-query don't actuall…
Browse files Browse the repository at this point in the history
…y mutate the expected number of documents
  • Loading branch information
twrichards committed Oct 6, 2023
1 parent a10d7e8 commit 8d16052
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions thrall/app/lib/elasticsearch/ElasticSearch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,20 @@ class ElasticSearch(
for {
ids <- getNextBatchOfImageIdsForDeletion(query, count, "soft")
// unfortunately 'updateByQuery' doesn't return the affected IDs so can't do this whole thing in one operation - https://github.com/elastic/elasticsearch/issues/48624
_ <- migrationAwareUpdater(
actuallyMarkedAsSoftDeleted <- migrationAwareUpdater(
requestFromIndexName = indexName =>
updateByQuery(
indexName,
idsQuery(ids),
).script(softDeletedMetadataAsPainlessScript(softDeletedMetadata)),
logMessageFromIndexName = indexName => s"ES7 soft delete $count images in $indexName by ${softDeletedMetadata.deletedBy}"
)
// TODO check that the number of soft deleted images matches the number of ids
} yield ids
logMessageFromIndexName = indexName => s"ES7 soft delete ${ids.size} images in $indexName by ${softDeletedMetadata.deletedBy}"
).map(_.result.updated)
} yield {
if (actuallyMarkedAsSoftDeleted != ids.size) {
throw new Exception(s"Expected to soft delete ${ids.size} images but only $actuallyMarkedAsSoftDeleted were marked as soft deleted in ES")
}
ids
}
}

def hardDeleteNextBatchOfImages(isReapable: ReapableEligibility, count: Int)
Expand All @@ -345,16 +349,20 @@ class ElasticSearch(
for {
ids <- getNextBatchOfImageIdsForDeletion(query, count, "hard")
// unfortunately 'deleteByQuery' doesn't return the affected IDs so can't do this whole thing in one operation - https://github.com/elastic/elasticsearch/issues/45460
_ <- migrationAwareUpdater(
actuallyDeletedFromES <- migrationAwareUpdater(
requestFromIndexName = indexName =>
deleteByQuery(
indexName,
idsQuery(ids),
),
logMessageFromIndexName = indexName => s"ES7 hard delete $count images in $indexName"
)
// TODO check that the number of hard deleted images matches the number of ids
} yield ids
logMessageFromIndexName = indexName => s"ES7 hard delete ${ids.size} images in $indexName"
).map(_.result.left.map(_.deleted).getOrElse(0))
} yield {
if (actuallyDeletedFromES != ids.size) {
throw new Exception(s"Expected to hard delete ${ids.size} images but only $actuallyDeletedFromES were hard deleted from ES")
}
ids
}
}

def getInferredSyndicationRightsImages(photoshoot: Photoshoot, excludedImageId: Option[String])
Expand Down

0 comments on commit 8d16052

Please sign in to comment.