Skip to content

Commit fb925f9

Browse files
committed
Auto merge of #2154 - jtgeibel:prod/reduce-dl-count-contention, r=sgrif
Update `version_downloads.counted` last to minimize lock contention If we defer the update to `version_downloads.counted` until immediately before the commit, it should eliminate the contention between counting new downloads and propagating previous downloads. Refs: #2153 See also: #2153 (comment)
2 parents 373ae24 + 97e9997 commit fb925f9

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/tasks/update_downloads.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ fn collect(conn: &PgConnection, rows: &[VersionDownload]) -> QueryResult<()> {
5252
let amt = download.downloads - download.counted;
5353

5454
conn.transaction::<_, diesel::result::Error, _>(|| {
55-
// increment the number of counted downloads
56-
update(version_downloads::table.find(download.id()))
57-
.set(version_downloads::counted.eq(version_downloads::counted + amt))
58-
.execute(conn)?;
59-
6055
// Update the total number of version downloads
6156
let crate_id = update(versions::table.find(download.version_id))
6257
.set(versions::downloads.eq(versions::downloads + amt))
@@ -68,12 +63,18 @@ fn collect(conn: &PgConnection, rows: &[VersionDownload]) -> QueryResult<()> {
6863
.set(crates::downloads.eq(crates::downloads + amt))
6964
.execute(conn)?;
7065

71-
// Now that everything else for this crate is done, update the global counter of total
72-
// downloads
66+
// Update the global counter of total downloads
7367
update(metadata::table)
7468
.set(metadata::total_downloads.eq(metadata::total_downloads + i64::from(amt)))
7569
.execute(conn)?;
7670

71+
// Record that these downloads have been propagated to the other tables. This is done
72+
// last, immediately before the transaction is committed, to minimize lock contention
73+
// with counting new downloads.
74+
update(version_downloads::table.find(download.id()))
75+
.set(version_downloads::counted.eq(version_downloads::counted + amt))
76+
.execute(conn)?;
77+
7778
Ok(())
7879
})?;
7980
}

0 commit comments

Comments
 (0)