Merge pull request #6026 from backstage/rugvip/skip-locked

catalog-backend: avoid duplication of work due to race conditions in task pickup
This commit is contained in:
Patrik Oldsberg
2021-06-14 15:12:52 +02:00
committed by GitHub
2 changed files with 15 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---
Rely on `SELECT ... FOR UPDATE SKIP LOCKED` where available in order to speed up processing item acquisition and reduce work duplication.
@@ -355,8 +355,16 @@ export class DefaultProcessingDatabase implements ProcessingDatabase {
): Promise<GetProcessableEntitiesResult> {
const tx = txOpaque as Knex.Transaction;
const items = await tx<DbRefreshStateRow>('refresh_state')
.select()
let itemsQuery = tx<DbRefreshStateRow>('refresh_state').select();
// This avoids duplication of work because of race conditions and is
// also fast because locked rows are ignored rather than blocking.
// It's only available in MySQL and PostgreSQL
if (['mysql', 'mysql2', 'pg'].includes(tx.client.config.client)) {
itemsQuery = itemsQuery.forUpdate().skipLocked();
}
const items = await itemsQuery
.where('next_update_at', '<=', tx.fn.now())
.limit(request.processBatchSize)
.orderBy('next_update_at', 'asc');