Skip to content

Commit 353541d

Browse files
committed
fix: serialize anchor bootstrap so a rolled back grant cannot zero the task entry
Until the anchor lands, a short grant is handed back to Spark whole. In the gap between that grant returning and its rollback release landing, a sibling acquire of the same task can enter Spark and park if another task has left the pool meanwhile, and the release then removes the task's entry under it. Carriers now run one at a time from the bridge acquire through the rollback release. Releases never take that lock, so a parked carrier cannot hold up the release it waits for, and Spark serializes a task's acquires anyway.
1 parent e4edd86 commit 353541d

1 file changed

Lines changed: 108 additions & 28 deletions

File tree

‎native/core/src/execution/memory_pools/fair_pool.rs‎

Lines changed: 108 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,19 @@ const ANCHOR_BYTES: usize = 1;
8383
/// as a short grant, so the caller spills instead of running on a balance that a full release
8484
/// could zero. That spill is one the plain request would not have needed, although the plain
8585
/// grant leaves the task at the edge of the pool where later requests come back short anyway.
86-
/// The extra byte matters to Spark only when its free memory equals the request exactly. The
87-
/// grant then comes back short and the caller spills, or, below the task's minimum share, Spark
88-
/// parks the request until any task releases memory, where a plain request would have been
89-
/// granted at once.
86+
/// The extra byte matters to Spark only when the request exactly meets its limit, whether that
87+
/// is the pool's free memory or the task's own share. The grant then comes back short and the
88+
/// caller spills, or, below the task's minimum share, Spark parks the request until any task
89+
/// releases memory, where a plain request would have been granted at once. Until the anchor
90+
/// lands, carriers run one at a time, so a short grant handed back whole can never land while
91+
/// a sibling of this task is parked; Spark serializes a task's acquires anyway.
9092
pub struct CometFairMemoryPool {
9193
bridge: Box<dyn TaskMemoryBridge>,
9294
pool_size: usize,
9395
state: Mutex<CometFairPoolState>,
96+
/// Held by a carrier from its bridge acquire through its rollback release. Releases never
97+
/// take it, so a parked carrier can never hold up the release it is waiting for.
98+
bootstrap: Mutex<()>,
9499
}
95100

96101
/// Whether the pool holds its anchor byte on the JVM side.
@@ -146,6 +151,7 @@ impl CometFairMemoryPool {
146151
jvm_held: 0,
147152
anchor: Anchor::Absent,
148153
}),
154+
bootstrap: Mutex::new(()),
149155
}
150156
}
151157

@@ -334,6 +340,9 @@ impl MemoryPool for CometFairMemoryPool {
334340
}
335341
}
336342
};
343+
// Taken after the state lock is released, and only by carriers, so no lock is ever
344+
// held while waiting for it and non-carriers never queue behind a parked one.
345+
let _bootstrap = carries_anchor.then(|| self.bootstrap.lock());
337346
let requested = if carries_anchor {
338347
additional + ANCHOR_BYTES
339348
} else {
@@ -423,9 +432,10 @@ mod tests {
423432
const MIB: usize = 1 << 20;
424433
const GIB: usize = 1 << 30;
425434

426-
/// The task this pool belongs to and a neighbour that competes with it for the same pool.
435+
/// The task this pool belongs to and neighbours that compete with it for the same pool.
427436
const THIS_TASK: i64 = 0;
428437
const OTHER_TASK: i64 = 1;
438+
const THIRD_TASK: i64 = 2;
429439

430440
/// Real Spark parks a starved acquire forever; the stub gives up after this long and fails
431441
/// the test instead, so a deadlock shows up as a panic rather than a hung test binary. A
@@ -561,41 +571,55 @@ mod tests {
561571
self.pool.lock().memory_free()
562572
}
563573

564-
/// Another task of the same executor takes `bytes` from the pool, which also makes
565-
/// `numActiveTasks` two and halves this task's shares.
566-
fn other_task_holds(&self, bytes: i64) {
574+
/// Another task of the same executor takes `bytes` from the pool, which also raises
575+
/// `numActiveTasks` and shrinks this task's shares.
576+
fn task_holds(&self, task: i64, bytes: i64) {
567577
let mut pool = self.pool.lock();
568578
assert!(
569579
bytes <= pool.memory_free(),
570-
"other task cannot hold {bytes} bytes, only {} free",
580+
"task {task} cannot hold {bytes} bytes, only {} free",
571581
pool.memory_free()
572582
);
573-
*pool.memory_for_task.entry(OTHER_TASK).or_insert(0) += bytes;
583+
*pool.memory_for_task.entry(task).or_insert(0) += bytes;
574584
}
575585

576-
/// The neighbour hands `bytes` back, which wakes every parked acquire like Spark's
577-
/// notifyAll.
578-
fn other_task_releases(&self, bytes: i64) {
586+
fn other_task_holds(&self, bytes: i64) {
587+
self.task_holds(OTHER_TASK, bytes);
588+
}
589+
590+
/// A neighbour hands `bytes` back, dropping out of the active set at zero, which wakes
591+
/// every parked acquire like Spark's notifyAll.
592+
fn task_releases(&self, task: i64, bytes: i64) {
579593
let mut pool = self.pool.lock();
580594
let balance = pool
581595
.memory_for_task
582-
.get_mut(&OTHER_TASK)
583-
.expect("other task holds nothing");
584-
assert!(*balance >= bytes, "other task holds only {balance} bytes");
596+
.get_mut(&task)
597+
.expect("task holds nothing");
598+
assert!(*balance >= bytes, "task {task} holds only {balance} bytes");
585599
*balance -= bytes;
586600
if *balance <= 0 {
587-
pool.memory_for_task.remove(&OTHER_TASK);
601+
pool.memory_for_task.remove(&task);
588602
}
589603
self.lock.notify_all();
590604
}
591605

606+
fn other_task_releases(&self, bytes: i64) {
607+
self.task_releases(OTHER_TASK, bytes);
608+
}
609+
592610
fn wait_parked(&self, what: &str) {
593611
self.parked
594612
.1
595613
.lock()
596614
.recv_timeout(TEST_TIMEOUT)
597615
.unwrap_or_else(|_| panic!("{what} never parked inside Spark"));
598616
}
617+
618+
/// Whether some acquire parks within `timeout`, for scenarios where parking is the
619+
/// failure and the fixed code never reaches Spark at all.
620+
fn parked_within(&self, timeout: Duration) -> bool {
621+
self.parked.1.lock().recv_timeout(timeout).is_ok()
622+
}
599623
}
600624

601625
impl TaskMemoryBridge for Arc<StubTaskMemory> {
@@ -1132,18 +1156,17 @@ mod tests {
11321156
}
11331157

11341158
/// The pool's very first acquire parks inside Spark with the anchor still only requested.
1135-
/// A second consumer's acquire that starts meanwhile must carry the extra byte too: if it
1136-
/// takes a plain grant and then frees it in full, the task's entry goes to zero under the
1137-
/// parked first acquire.
1159+
/// A second consumer's acquire that starts meanwhile carries the extra byte too and waits
1160+
/// for the first carrier to settle; once the anchor is held it hands its own byte back. A
1161+
/// plain grant here, freed in full, would take the task's entry to zero under the waiter.
11381162
#[test]
11391163
fn acquire_started_while_the_anchor_is_in_flight_keeps_the_entry_alive() {
11401164
let stub = Arc::new(StubTaskMemory::new(100));
11411165
let pool = pool_with(&stub, 1_000_000);
11421166

11431167
let first = MemoryConsumer::new("first").register(&pool);
11441168
let second = MemoryConsumer::new("second").register(&pool);
1145-
// 5 bytes free with a 25 byte minimum share: a 20 byte request parks, a 4 byte one
1146-
// (plus the anchor byte) is granted.
1169+
// 5 bytes free with a 25 byte minimum share: a 20 byte request parks.
11471170
stub.other_task_holds(95);
11481171

11491172
let first_thread = thread::spawn(move || {
@@ -1153,15 +1176,72 @@ mod tests {
11531176
});
11541177
stub.wait_parked("first acquire");
11551178

1156-
second.try_grow(4).unwrap();
1157-
second.free();
1179+
let second_thread = thread::spawn(move || {
1180+
let result = second.try_grow(4);
1181+
second.free();
1182+
result
1183+
});
11581184

1159-
// Room for the parked request, so a surviving waiter completes instead of timing out.
1185+
// Room for the parked request; the second acquire follows once the first has settled.
11601186
stub.other_task_releases(50);
1161-
let result = first_thread
1187+
let first_result = first_thread
11621188
.join()
1163-
.expect("parked first acquire crashed after the second consumer's full release");
1164-
result.expect("parked first acquire was not granted once memory was freed");
1189+
.expect("parked first acquire crashed after the second consumer's release");
1190+
first_result.expect("parked first acquire was not granted once memory was freed");
1191+
let second_result = second_thread
1192+
.join()
1193+
.expect("second acquire crashed while the anchor was in flight");
1194+
second_result.expect("second acquire was not granted");
1195+
assert_eq!(pool.reserved(), 0);
1196+
assert_eq!(stub.outstanding(), 1, "only the anchor may remain");
1197+
}
1198+
1199+
/// While the anchor has not landed, a short grant is handed back whole, and that release
1200+
/// must never land while a sibling of this task is parked inside Spark. Here the first
1201+
/// consumer's short grant is held at the JNI hop, a third task then leaves the pool (which
1202+
/// raises this task's minimum share), and a second consumer's acquire arrives. If it reaches
1203+
/// Spark it parks, and the pending release then removes the task's entry under it.
1204+
#[test]
1205+
fn short_grant_rollback_cannot_land_under_a_sibling_parked_in_spark() {
1206+
let stub = Arc::new(StubTaskMemory::new(100));
1207+
let pool = pool_with(&stub, 1_000_000);
1208+
1209+
let first = MemoryConsumer::new("first").register(&pool);
1210+
let second = MemoryConsumer::new("second").register(&pool);
1211+
// Three active tasks: 17 free with a 16 byte minimum share, so a 30 byte request gets
1212+
// a short grant of 17 rather than parking.
1213+
stub.task_holds(OTHER_TASK, 82);
1214+
stub.task_holds(THIRD_TASK, 1);
1215+
1216+
stub.release_gate.arm();
1217+
let first_thread = thread::spawn(move || {
1218+
let result = first.try_grow(30);
1219+
assert!(result.is_err(), "30 bytes cannot fit into 17 free bytes");
1220+
first.free();
1221+
});
1222+
stub.release_gate
1223+
.wait_entered("first consumer's rollback release");
1224+
1225+
// The third task leaves: two active tasks now, a 25 byte minimum share, 1 byte free.
1226+
stub.task_releases(THIRD_TASK, 1);
1227+
let second_thread = thread::spawn(move || {
1228+
second.try_grow(10).unwrap();
1229+
second.free();
1230+
});
1231+
// The pool holds the second acquire back until the rollback lands, so this probe only
1232+
// fires if an acquire slips into Spark; the margin covers a slow CI scheduler.
1233+
let parked = stub.parked_within(Duration::from_secs(2));
1234+
1235+
stub.release_gate.disarm();
1236+
stub.release_gate.open();
1237+
first_thread.join().unwrap();
1238+
second_thread
1239+
.join()
1240+
.expect("second acquire crashed when the rollback release landed");
1241+
assert!(
1242+
!parked,
1243+
"an acquire entered Spark while a short grant was being rolled back"
1244+
);
11651245
assert_eq!(pool.reserved(), 0);
11661246
assert_eq!(stub.outstanding(), 1, "only the anchor may remain");
11671247
}

0 commit comments

Comments
 (0)