Skip to content

Commit 79b7847

Browse files
committed
Remove references to locks
1 parent 29cf095 commit 79b7847

File tree

1 file changed

+17
-44
lines changed

1 file changed

+17
-44
lines changed

downstairs/src/lib.rs

+17-44
Original file line numberDiff line numberDiff line change
@@ -1908,40 +1908,12 @@ impl Downstairs {
19081908
}
19091909
}
19101910

1911-
/*
1912-
* Only grab the lock if the UpstairsConnection matches.
1913-
*
1914-
* Multiple Upstairs connecting to this Downstairs will spawn multiple
1915-
* threads that all can potentially add work to the same `active` hash
1916-
* map. Only one Upstairs can be "active" at any one time though.
1917-
* When promote_to_active takes the work lock, it will clear out the
1918-
* `active` hash map and (if applicable) will signal to the currently
1919-
* active Upstairs to terminate the connection.
1920-
*
1921-
* `new_work` and `add_work` both grab their work lock through this
1922-
* function. Let's say `promote_to_active` and `add_work` are racing for
1923-
* the work lock. If `add_work` wins the race it will put work into
1924-
* `active`, then `promote_to_active` will clear it out. If
1925-
* `promote_to_active` wins the race, it will change the Downstairs'
1926-
* active UpstairsConnection, and send the terminate signal to the
1927-
* tasks that are communicating to the previously active Upstairs
1928-
* (along with terminating the Downstairs tasks). If `add_work` for
1929-
* the previous Upstairs then does fire, it will fail to
1930-
* grab the lock because the UpstairsConnection is no longer active, and
1931-
* that `add_work` thread should close.
1932-
*
1933-
* Let's say `new_work` and `promote_to_active` are racing. If `new_work`
1934-
* wins, then it will return and run those jobs in `do_work_task`.
1935-
* However, `promote_to_active` will grab the lock and change the
1936-
* active UpstairsConnection, causing `do_work` to return
1937-
* UpstairsInactive for the jobs that were just returned. If
1938-
* `promote_to_active` wins, it will clear out the jobs of the old
1939-
* Upstairs.
1940-
*
1941-
* Grabbing the lock in this way should properly clear out the previously
1942-
* active Upstairs without causing jobs to be incorrectly sent to the
1943-
* newly active Upstairs.
1944-
*/
1911+
/// Mutably borrow a connection's `Work` if the `UpstairsConnection` matches
1912+
///
1913+
/// Because this function takes a `&mut self` and returns a `&mut Work`
1914+
/// (extending the lifetime of the initial borrow), it is impossible for
1915+
/// anyone else to interfere with the work map for the lifetime of the
1916+
/// borrow.
19451917
fn work_mut(
19461918
&mut self,
19471919
upstairs_connection: UpstairsConnection,
@@ -1954,6 +1926,7 @@ impl Downstairs {
19541926
Ok(&mut active_upstairs.work)
19551927
}
19561928

1929+
/// Borrow a connection's `Work` if the `UpstairsConnection` matches
19571930
fn work(&self, upstairs_connection: UpstairsConnection) -> Result<&Work> {
19581931
self.check_upstairs_active(upstairs_connection)?;
19591932
let active_upstairs = self
@@ -1971,7 +1944,7 @@ impl Downstairs {
19711944
if !self.active_upstairs.contains_key(&upstairs_uuid) {
19721945
warn!(
19731946
self.log,
1974-
"{:?} cannot grab work lock, {} is not active!",
1947+
"{:?} cannot get active upstairs, {} is not active!",
19751948
upstairs_connection,
19761949
upstairs_uuid,
19771950
);
@@ -1984,7 +1957,7 @@ impl Downstairs {
19841957
if active_upstairs.upstairs_connection != upstairs_connection {
19851958
warn!(
19861959
self.log,
1987-
"{:?} cannot grab lock, does not match {:?}!",
1960+
"{:?} cannot get active upstairs, does not match {:?}!",
19881961
upstairs_connection,
19891962
active_upstairs.upstairs_connection,
19901963
);
@@ -2418,7 +2391,7 @@ impl Downstairs {
24182391
) -> Result<()> {
24192392
let work = self.work_mut(upstairs_connection)?;
24202393

2421-
// If upstairs_connection grabs the work lock, it is the active
2394+
// If upstairs_connection borrows the work map, it is the active
24222395
// connection for this Upstairs UUID. The job should exist in the Work
24232396
// struct. If it does not, then we're in the case where the same
24242397
// Upstairs has reconnected and been promoted to active, meaning
@@ -2495,8 +2468,8 @@ impl Downstairs {
24952468
if self.read_only {
24962469
// Multiple active read-only sessions are allowed, but multiple
24972470
// sessions for the same Upstairs UUID are not. Kick out a
2498-
// previously active session for this UUID if one exists. Do this
2499-
// while holding the work lock so the previously active Upstairs
2471+
// previously active session for this UUID if one exists. This
2472+
// function is called on a `&mut self`, so we're guaranteed that the
25002473
// isn't adding more work.
25012474
if let Some(active_upstairs) = self
25022475
.active_upstairs
@@ -2882,9 +2855,9 @@ impl Work {
28822855
if let Some(job) = self.active.get_mut(&ds_id) {
28832856
if job.state == WorkState::New || job.state == WorkState::DepWait {
28842857
/*
2885-
* Before we can make this in_progress, we have to, while
2886-
* holding this locked, check the dep list if there is one
2887-
* and make sure all dependencies are completed.
2858+
* Before we can make this in_progress, we have to check the dep
2859+
* list if there is one and make sure all dependencies are
2860+
* completed.
28882861
*/
28892862
let dep_list = job.work.deps();
28902863

@@ -6029,7 +6002,7 @@ mod test {
60296002
assert_eq!(rx1.try_recv().unwrap(), upstairs_connection_2);
60306003

60316004
// This should error with UpstairsInactive - upstairs_connection_1 isn't
6032-
// active anymore and can't grab the work lock.
6005+
// active anymore and can't borrow the work map.
60336006
let result = ds.complete_work(upstairs_connection_1, JobId(1000), m);
60346007
assert!(matches!(
60356008
result.unwrap_err().downcast::<CrucibleError>().unwrap(),
@@ -6125,7 +6098,7 @@ mod test {
61256098
assert_eq!(rx1.try_recv().unwrap(), upstairs_connection_2);
61266099

61276100
// This should error with UpstairsInactive - upstairs_connection_1 isn't
6128-
// active anymore and can't grab the work lock.
6101+
// active anymore and can't borrow the work map.
61296102
let result = ds.complete_work(upstairs_connection_1, JobId(1000), m);
61306103
assert!(matches!(
61316104
result.unwrap_err().downcast::<CrucibleError>().unwrap(),

0 commit comments

Comments
 (0)