Skip to content

Commit ef04ab6

Browse files
committed
Do less work in update-downloads
I was noticing that query to increment the downloads column on `crates` was getting run way more than I'd expect it to be. I also was seeing logs about a lock contention issue with that query. Specifically, those logs pointed me at one of two things wrong here. The first is that we were running this query even if we were incrementing the value by 0. Second, we have our cutoff set to 2 days ago. I think the reason for both of these is that we wanted to make sure the `processed` column got set to true. We can pretty easily avoid this by just setting it to true for all rows at the very end when all our preconditions are met. This doesn't necessarily fix the lock contention problem, but it does result in hitting far fewer rows, which should alleviate the problem.
1 parent 9da11a4 commit ef04ab6

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/bin/update-downloads.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![deny(warnings)]
22

33
extern crate cargo_registry;
4-
extern crate chrono;
54
#[macro_use]
65
extern crate diesel;
76

@@ -30,6 +29,7 @@ fn main() {
3029
}
3130

3231
fn update(conn: &PgConnection) -> QueryResult<()> {
32+
use diesel::dsl::now;
3333
use diesel::select;
3434
use version_downloads::dsl::*;
3535

@@ -38,13 +38,23 @@ fn update(conn: &PgConnection) -> QueryResult<()> {
3838
let rows = version_downloads
3939
.filter(processed.eq(false))
4040
.filter(id.gt(m))
41+
.filter(downloads.ne(counted))
4142
.order(id)
4243
.limit(LIMIT)
4344
.load(conn)?;
4445
collect(conn, &rows)?;
4546
max = rows.last().map(|d| d.id);
4647
}
4748

49+
// Anything older than 24 hours ago will be frozen and will not be queried
50+
// against again.
51+
diesel::update(version_downloads)
52+
.set(processed.eq(true))
53+
.filter(date.lt(diesel::dsl::date(now)))
54+
.filter(downloads.eq(counted))
55+
.filter(processed.eq(false))
56+
.execute(conn)?;
57+
4858
no_arg_sql_function!(refresh_recent_crate_downloads, ());
4959
select(refresh_recent_crate_downloads).execute(conn)?;
5060
Ok(())
@@ -53,28 +63,15 @@ fn update(conn: &PgConnection) -> QueryResult<()> {
5363
fn collect(conn: &PgConnection, rows: &[VersionDownload]) -> QueryResult<()> {
5464
use diesel::{insert_into, update};
5565

56-
// Anything older than 24 hours ago will be frozen and will not be queried
57-
// against again.
58-
let now = chrono::Utc::now();
59-
let cutoff = now.naive_utc().date() - chrono::Duration::days(1);
60-
61-
println!(
62-
"updating {} versions (cutoff {})",
63-
rows.len(),
64-
now.to_rfc2822()
65-
);
66+
println!("updating {} versions", rows.len());
6667

6768
for download in rows {
6869
let amt = download.downloads - download.counted;
6970

7071
conn.transaction::<_, diesel::result::Error, _>(|| {
71-
// Flag this row as having been processed if we're passed the cutoff,
72-
// and unconditionally increment the number of counted downloads.
72+
// increment the number of counted downloads
7373
update(version_downloads::table.find(download.id))
74-
.set((
75-
version_downloads::processed.eq(download.date < cutoff),
76-
version_downloads::counted.eq(version_downloads::counted + amt),
77-
))
74+
.set(version_downloads::counted.eq(version_downloads::counted + amt))
7875
.execute(conn)?;
7976

8077
// Update the total number of version downloads

0 commit comments

Comments
 (0)