-
Notifications
You must be signed in to change notification settings - Fork 650
Ensure the update_downloads job doesn't run concurrently #2266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
jtgeibel
wants to merge
1
commit into
rust-lang:master
from
jtgeibel:add-session-lock-for-update-downloads-job
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
pub mod dump_db; | ||
mod update_downloads; | ||
mod util; | ||
|
||
pub use dump_db::dump_db; | ||
pub use update_downloads::update_downloads; | ||
|
||
pub(self) use self::util::advisory_lock::with_advisory_lock; | ||
|
||
const UPDATE_DOWNLOADS_ADVISORY_LOCK_KEY: i64 = 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(super) mod advisory_lock; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use std::error::Error; | ||
|
||
use diesel::prelude::*; | ||
use diesel::sql_types::BigInt; | ||
use diesel::PgConnection; | ||
|
||
sql_function!(fn pg_try_advisory_lock(key: BigInt) -> Bool); | ||
sql_function!(fn pg_advisory_unlock(key: BigInt) -> Bool); | ||
|
||
/// Run the callback if the session advisory lock for the given key can be obtained. | ||
/// | ||
/// If the lock is not already held, the callback will be called and the lock will be unlocked | ||
/// after the closure returns. If the lock is already held, the function returns an error without | ||
/// calling the callback. | ||
pub(crate) fn with_advisory_lock<F>( | ||
conn: &PgConnection, | ||
key: i64, | ||
f: F, | ||
) -> Result<(), Box<dyn Error>> | ||
where | ||
F: FnOnce(&PgConnection) -> QueryResult<()>, | ||
{ | ||
if !diesel::select(pg_try_advisory_lock(key)).get_result(conn)? { | ||
let string = format!( | ||
"A job holding the session advisory lock for key {} is already running", | ||
key | ||
); | ||
println!("return"); | ||
return Err(string.into()); | ||
} | ||
println!("umm"); | ||
let _dont_drop_yet = DropGuard { conn, key }; | ||
f(conn).map_err(Into::into) | ||
} | ||
|
||
struct DropGuard<'a> { | ||
conn: &'a PgConnection, | ||
key: i64, | ||
} | ||
|
||
impl<'a> Drop for DropGuard<'a> { | ||
fn drop(&mut self) { | ||
match diesel::select(pg_advisory_unlock(self.key)).get_result(self.conn) { | ||
Ok(true) => (), | ||
Ok(false) => println!( | ||
"Error: job advisory lock for key {} was not locked", | ||
self.key | ||
), | ||
Err(err) => println!("Error unlocking advisory lock (key: {}): {}", self.key, err), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar here. We could do something (or just panic) to ensure the connection is closed and the lock unlocked. |
||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::test_util::*; | ||
|
||
#[test] | ||
fn lock_released_after_callback_returns() { | ||
const KEY: i64 = -1; | ||
let conn1 = pg_connection(); | ||
let conn2 = pg_connection(); | ||
|
||
let mut callback_run = false; | ||
let result = with_advisory_lock(&conn1, KEY, |_| { | ||
// Another connection cannot obtain the lock | ||
assert!(!diesel::select(pg_try_advisory_lock(KEY)).get_result(&conn2)?); | ||
callback_run = true; | ||
Ok(()) | ||
}); | ||
assert!(result.is_ok()); | ||
assert!(callback_run); | ||
|
||
// Another connection can now obtain the lock | ||
assert_eq!( | ||
diesel::select(pg_try_advisory_lock(KEY)).get_result(&conn2), | ||
Ok(true) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_already_locked() { | ||
const KEY: i64 = -2; | ||
let conn1 = pg_connection(); | ||
let conn2 = pg_connection(); | ||
|
||
// Another connection obtains the lock first | ||
assert_eq!( | ||
diesel::select(pg_try_advisory_lock(KEY)).get_result(&conn2), | ||
Ok(true) | ||
); | ||
|
||
let mut callback_run = false; | ||
let result = with_advisory_lock(&conn1, KEY, |_| { | ||
callback_run = true; | ||
Ok(()) | ||
}); | ||
assert!(dbg!(result).is_err()); | ||
assert!(!callback_run); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be a panic, to forbid the callback from releasing the lock on its own.