Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/core/pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,13 @@ static inline void pool_exec(ray_pool_t* pool, uint32_t worker_id, uint32_t tick
}
ray_pool_task_t* t = &pool->tasks[ticket & (pool->task_cap - 1)];
int64_t _qs_t0; uint32_t _qs_m = ray_qstats_task_begin(&_qs_t0);
#ifdef DEBUG
ray_test_in_pool_task++;
#endif
t->fn(t->ctx, worker_id, t->start, t->end);
#ifdef DEBUG
ray_test_in_pool_task--;
#endif
ray_qstats_task_end(_qs_m, worker_id, t->end - t->start, _qs_t0);
atomic_fetch_sub_explicit(&pool->pending, 1, memory_order_acq_rel);
}
Expand Down
10 changes: 9 additions & 1 deletion src/mem/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,13 @@ void ray_alloc_fail_set_for_test(int64_t nth, int64_t count) {
g_alloc_fail_at = count > 0 && nth > 0 ? nth : 0;
g_alloc_fail_count = count;
}
int64_t ray_alloc_fail_armed_for_test(void) { return g_alloc_fail_at > 0 ? g_alloc_fail_at : 0; }
/* Pool tasks the calling thread is running (pool.c raises it around each),
* and whether the armed failure skips their allocations: which tasks the
* calling thread steals varies from run to run, its own code does not. */
_Thread_local int ray_test_in_pool_task;
static _Thread_local bool g_alloc_fail_skip_tasks;
void ray_alloc_fail_skip_tasks_for_test(bool skip) { g_alloc_fail_skip_tasks = skip; }
static bool alloc_fails_for_test(void) {
if (--g_alloc_fail_at > 0) return false;
if (--g_alloc_fail_count > 0) g_alloc_fail_at = 1; /* the next one too */
Expand Down Expand Up @@ -2261,7 +2268,8 @@ static ray_t* heap_alloc_impl(size_t data_size, bool* fresh) {

ray_t* ray_alloc(size_t data_size) {
#ifdef DEBUG
if (RAY_UNLIKELY(g_alloc_fail_at > 0) && alloc_fails_for_test()) return NULL;
if (RAY_UNLIKELY(g_alloc_fail_at > 0) &&
!(g_alloc_fail_skip_tasks && ray_test_in_pool_task) && alloc_fails_for_test()) return NULL;
#endif
bool fresh;
ray_t* v = heap_alloc_impl(data_size, &fresh);
Expand Down
7 changes: 7 additions & 0 deletions src/mem/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ ray_t* ray_cow(ray_t* v);
/* Test seam: ray_alloc on this thread returns NULL for `count` calls,
* starting with the nth from now; nth 0 disarms. */
void ray_alloc_fail_set_for_test(int64_t nth, int64_t count);
/* How many allocations on this thread are still to come before the armed
* failure (0: it fired, or none is armed). */
int64_t ray_alloc_fail_armed_for_test(void);
/* Pool tasks running on this thread (raised by pool.c), and: the armed
* failure skips allocations made inside one (true) or counts them. */
extern _Thread_local int ray_test_in_pool_task;
void ray_alloc_fail_skip_tasks_for_test(bool skip);
/* Test seam: the pools a heap may hold (0: RAY_MAX_POOLS), how many pools
* share a size before the next ones double (0: the default), and the most
* doublings (UINT32_MAX: the default; 0: standard pools only, the old
Expand Down
95 changes: 88 additions & 7 deletions src/ops/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -6065,9 +6065,10 @@ static bool group_ht_wire_wide(group_ht_t* ht, uint32_t nk) {

#ifdef DEBUG
/* Test seam (ray_group_test_ht_rows_fail): the nth partition-table init of
* the v2 radix route's phase 2 from now, on any thread, fails as a failed
* rows allocation does (slots allocated, rows NULL); with cancel, it also
* raises the cancel. t_ht_seam_site marks the inits it counts. */
* a radix route's phase 2 (v2 or classic) from now, on any thread, fails
* as a failed rows allocation does (slots allocated, rows NULL); with
* cancel, it also raises the cancel. t_ht_seam_site marks the inits it
* counts. */
static _Atomic int64_t g_ht_rows_fail_for_test;
static _Atomic bool g_ht_rows_fail_cancel_for_test;
static _Thread_local bool t_ht_seam_site;
Expand Down Expand Up @@ -8631,8 +8632,19 @@ static void radix_phase2_fn(void* ctx, uint32_t worker_id, int64_t start, int64_
uint32_t fe_target = total < 4096 ? total : 4096;
while (fe_cap < fe_target) fe_cap <<= 1;
uint32_t fe_blk = total < 256 ? total : 256;
if (!group_ht_init_sized(&c->part_hts[p], fe_cap, &c->layout, fe_blk))
#ifdef DEBUG
t_ht_seam_site = true;
#endif
bool inited = group_ht_init_sized(&c->part_hts[p], fe_cap, &c->layout, fe_blk);
#ifdef DEBUG
t_ht_seam_site = false;
#endif
if (!inited) {
/* its groups would be missing from the answer: the caller
* answers `oom` (the same flag a failed grow sets) */
c->part_hts[p].oom = 1;
continue;
}
/* Wide keys need source-column resolution during probe/rehash. */
if (c->layout.any_wide_key && c->key_data) {
group_ht_set_key_data(&c->part_hts[p], c->key_data);
Expand Down Expand Up @@ -15225,13 +15237,42 @@ static bool group_firstn_where_ok(ray_graph_t* g, ray_op_t* op, ray_t* tbl, int6
return agg_v2_admission(g, op, tbl) == AGG_V2_ADMITTED;
}

#ifdef DEBUG
/* Test seam (ray_group_alloc_fail_for_test): the nth allocation of the
* calling thread's own code (not the pool tasks it runs) while the
* outermost group runs fails. */
static _Thread_local int64_t g_group_fail_nth;
static _Thread_local bool g_group_fail_fired;
static _Thread_local int64_t g_group_fail_left;
static _Thread_local int g_group_fail_depth;
void ray_group_alloc_fail_for_test(int64_t nth) { g_group_fail_nth = nth; g_group_fail_fired = false; }
bool ray_group_alloc_fail_fired_for_test(void) { return g_group_fail_fired; }
/* The allocations the last armed group made on this thread, when its
* failure did not fire (armed past them). */
int64_t ray_group_alloc_count_for_test(void) { return g_group_fail_nth - g_group_fail_left; }
#endif

ray_t* exec_group(ray_graph_t* g, ray_op_t* op, ray_t* tbl, int64_t group_limit) {
uint32_t gid = op->id;
if (g->firstn_where && !group_firstn_where_ok(g, op, tbl, group_limit)) {
g->firstn_declined = true;
return NULL;
}
return group_retag_duration_means(g, gid, exec_group_core(g, op, tbl, group_limit));
#ifdef DEBUG
bool arm = g_group_fail_nth && g_group_fail_depth++ == 0;
if (arm) { ray_alloc_fail_skip_tasks_for_test(true); ray_alloc_fail_set_for_test(g_group_fail_nth, 1); }
#endif
ray_t* r = group_retag_duration_means(g, gid, exec_group_core(g, op, tbl, group_limit));
#ifdef DEBUG
if (g_group_fail_nth) g_group_fail_depth--;
if (arm) {
g_group_fail_left = ray_alloc_fail_armed_for_test();
g_group_fail_fired = g_group_fail_left == 0;
ray_alloc_fail_set_for_test(0, 0);
ray_alloc_fail_skip_tasks_for_test(false);
}
#endif
return r;
}

/* ============================================================================
Expand Down Expand Up @@ -21597,6 +21638,23 @@ v2_done:;
ray_pool_dispatch_n(pool, radix_phase2_fn, &p2ctx, radix_n_parts);
scratch_free(p2_kp_hdr);
CHECK_CANCEL_GOTO(pool, cleanup);
/* A partition that could not allocate (its init, or a grow while
* folding rows in) lost groups: the query's `oom`. Every partition
* is freed here, one whose init failed holding slots but no rows,
* which cleanup's rows-only free would keep. */
{
bool p2_oom = false;
for (uint32_t p = 0; p < radix_n_parts && !p2_oom; p++)
p2_oom = part_hts[p].oom != 0;
if (p2_oom) {
for (uint32_t p = 0; p < radix_n_parts; p++) group_ht_free(&part_hts[p]);
scratch_free(part_hts_hdr);
part_hts = NULL;
part_hts_hdr = NULL;
result = ray_error("oom", NULL);
goto cleanup;
}
}

if (radix_bufs) {
size_t n_bufs_free = (size_t)n_total * radix_n_parts;
Expand Down Expand Up @@ -22002,13 +22060,20 @@ v2_emit:;
char* key_dsts[vla_keys];
int8_t key_out_types[vla_keys];
uint8_t key_esizes[vla_keys];
/* Every key and aggregate column is written by phase 3, so one that
* is missing (a failed allocation, or a key left unread by one) is
* the query's `oom`, checked once both loops have set every slot.
* Skipping it wrote phase 3 through a NULL key column, or dropped
* the aggregate's column from the answer. */
bool emit_oom = false;
ray_t* emit_err = NULL; /* the first allocation's own error, the answer */
for (uint32_t k = 0; k < n_keys; k++) {
ray_t* src_col = key_vecs[k];
key_cols[k] = NULL;
key_dsts[k] = NULL;
key_out_types[k] = 0;
key_esizes[k] = 0;
if (!src_col) continue;
if (!src_col) { emit_oom = true; continue; }
uint8_t esz = ray_sym_elem_size(src_col->type, src_col->attrs);
ray_t* new_col;
if (src_col->type == RAY_SYM) {
Expand All @@ -22032,7 +22097,12 @@ v2_emit:;
}
} else
new_col = ray_vec_new(src_col->type, (int64_t)total_grps);
if (!new_col || RAY_IS_ERR(new_col)) continue;
if (!new_col || RAY_IS_ERR(new_col)) {
emit_oom = true;
if (new_col && !emit_err) emit_err = new_col;
else if (new_col) ray_error_free(new_col);
continue;
}
new_col->len = (int64_t)total_grps;
/* Seed HAS_NULLS from the source key column, exactly as the
* gather-based emit does (agg_gather_key_col). radix_phase3_fn
Expand Down Expand Up @@ -22109,6 +22179,9 @@ v2_emit:;
if (!new_col || RAY_IS_ERR(new_col)) {
agg_cols[a] = NULL;
memset(&agg_outs[a], 0, sizeof(agg_outs[a]));
emit_oom = true;
if (new_col && !emit_err) emit_err = new_col;
else if (new_col) ray_error_free(new_col);
continue;
}
/* SYM MIN/MAX/FIRST/LAST emit RAW cell ids accumulated from
Expand All @@ -22129,6 +22202,14 @@ v2_emit:;
};
}

if (emit_oom) {
for (uint32_t k = 0; k < n_keys; k++) if (key_cols[k]) ray_release(key_cols[k]);
for (uint32_t a = 0; a < n_aggs; a++) if (agg_cols[a]) ray_release(agg_cols[a]);
ray_release(result);
result = emit_err ? emit_err : ray_error("oom", NULL);
goto cleanup;
}

/* Pass 3: parallel key gather + agg result building from inline rows */
{
radix_phase3_ctx_t p3ctx = {
Expand Down
14 changes: 12 additions & 2 deletions src/ops/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
* than ray_interrupted() does. */
#define RAY_CANCEL_ROWS ((int64_t)65536)

#ifdef DEBUG
/* Test seam (group.c): the nth allocation of the calling thread's own code
* (not the pool tasks it runs) while the outermost group (exec_group) runs
* fails; fired says whether it was reached, count how many the last
* unfired run made. nth 0 disarms. */
void ray_group_alloc_fail_for_test(int64_t nth);
bool ray_group_alloc_fail_fired_for_test(void);
int64_t ray_group_alloc_count_for_test(void);
#endif

/* Test seam (DEBUG): a holistic aggregate's per-group buffers fail their
* nth allocation on the calling thread. HOLISTIC_FAIL_ARM/_DISARM bound
* the code that computes a median, quantile or mode column from its
Expand All @@ -79,8 +89,8 @@ int64_t ray_holistic_fail_regions_for_test(void);
#define HOLISTIC_FAIL_DISARM() do { } while (0)
#endif
#ifdef DEBUG
/* Test seam (group.c): the nth partition-table init of the v2 radix
* route's phase 2 from now, on any thread, fails as a failed rows
/* Test seam (group.c): the nth partition-table init of a radix route's
* phase 2 (v2 or classic) from now, on any thread, fails as a failed rows
* allocation does (its slots allocated); with cancel it also raises the
* cancel. nth 0 disarms. */
void ray_group_test_ht_rows_fail(int64_t nth, bool cancel);
Expand Down
102 changes: 102 additions & 0 deletions test/test_aware.c
Original file line number Diff line number Diff line change
Expand Up @@ -3683,6 +3683,107 @@ static test_result_t test_radix_cancel_keeps_nothing(void) {
PASS();
}

/* An allocation that fails while a group on the radix route runs (an F64
* key, alone or beside another) answers `oom` and keeps nothing, or the
* answer the group gives without the failure; never a crash or another
* answer. The radix emit skipped a key column it could not allocate
* (phase 3 then wrote through its NULL data: SIGSEGV) and an aggregate
* column (the answer lost that column), leaking the allocation's error;
* the classic route's phase 2 skipped a partition whose table it could not
* allocate (the answer lost its groups). The seam fails the nth
* allocation of the calling thread's own code while the outermost group
* runs (not of the pool tasks it steals, whose share varies from run to
* run), every n the group makes; a second seam fails the nth partition
* table init inside phase 2's tasks, on any thread. "Keeps nothing" is
* the process's allocation trace (every heap) over the run, the error's
* release and a GC. */
static test_result_t test_group_alloc_fail(void) {
ray_pool_destroy();
TEST_ASSERT_EQ_I(ray_pool_init_total(4), RAY_OK);
aw_eval_ok("(set F (table [k j v] (list (as 'F64 (% (* (til 262144) 7919) 3001)) (% (til 262144) 7) (til 262144))))");
static const char* const qs[] = {
"(select {from: F by: k})", /* v2 radix */
"(select {c: (count v) s: (sum v) from: F by: [k j]})",
"(select {m: (max v) from: F by: k})", /* classic radix */
};
int part_failed = 0;
for (size_t q = 0; q < sizeof qs / sizeof *qs; q++) {
char setq[200];
snprintf(setq, sizeof setq, "(set R0 %s)", qs[q]);
aw_eval_ok(setq);
/* the route's allocations on this thread, armed past them */
ray_group_alloc_fail_for_test((int64_t)1 << 40);
ray_t* r0 = aw_eval(qs[q]);
int64_t total = ray_group_alloc_count_for_test();
ray_group_alloc_fail_for_test(0);
if (r0 && RAY_IS_ERR(r0)) ray_error_free(r0); else if (r0) ray_release(r0);
TEST_ASSERT_FMT(total > 0, "%s: the group made no allocation", qs[q]);
int failed = 0;
for (int64_t n = 1; n <= total; n++) {
ray_heap_gc();
bool traced = ray_mem_trace_begin();
ray_group_alloc_fail_for_test(n);
ray_t* got = aw_eval(qs[q]);
ray_group_alloc_fail_for_test(0);
if (got && RAY_IS_ERR(got)) {
char code[32];
snprintf(code, sizeof code, "%s", ray_err_code(got));
ray_error_free(got);
ray_heap_gc();
ray_mem_trace_t tr = {0};
if (traced) ray_mem_trace_end(&tr);
TEST_ASSERT_FMT(traced, "no allocation trace");
TEST_ASSERT_FMT(strcmp(code, "oom") == 0, "%s, allocation %lld of %lld fails: %s, not oom", qs[q], (long long)n, (long long)total, code);
TEST_ASSERT_FMT(tr.net_bytes <= 0, "%s, allocation %lld of %lld fails: %lld bytes kept", qs[q], (long long)n, (long long)total, (long long)tr.net_bytes);
failed++;
continue;
}
if (traced) { ray_mem_trace_t t0; ray_mem_trace_end(&t0); }
TEST_ASSERT_NOT_NULL(got);
ray_env_set(ray_sym_intern("Q", 1), got);
ray_release(got);
ray_t* v = ray_eval_str("(and (== (count (ser Q)) (count (ser R0))) (all (== (ser Q) (ser R0))))");
bool ok = v && !RAY_IS_ERR(v) && v->type == -RAY_BOOL && v->b8;
if (v && RAY_IS_ERR(v)) ray_error_free(v); else if (v) ray_release(v);
TEST_ASSERT_FMT(ok, "%s, allocation %lld of %lld fails: an answer, not the reference", qs[q], (long long)n, (long long)total);
}
TEST_ASSERT_FMT(failed > 0, "%s: no allocation failure answered oom", qs[q]);
/* inside phase 2's tasks: the nth partition-table init fails (its
* slots allocated, rows NULL), on whichever thread runs it */
for (int64_t n = 1; n <= 32; n++) {
ray_heap_gc();
bool traced = ray_mem_trace_begin();
ray_group_test_ht_rows_fail(n, false);
ray_t* got = aw_eval(qs[q]);
ray_group_test_ht_rows_fail(0, false);
bool err = got && RAY_IS_ERR(got);
char code[32] = "";
if (err) { snprintf(code, sizeof code, "%s", ray_err_code(got)); ray_error_free(got); }
ray_heap_gc();
ray_mem_trace_t tr = {0};
if (traced) ray_mem_trace_end(&tr);
if (!err) {
TEST_ASSERT_NOT_NULL(got);
ray_env_set(ray_sym_intern("Q", 1), got);
ray_release(got);
ray_t* v = ray_eval_str("(and (== (count (ser Q)) (count (ser R0))) (all (== (ser Q) (ser R0))))");
bool ok = v && !RAY_IS_ERR(v) && v->type == -RAY_BOOL && v->b8;
if (v && RAY_IS_ERR(v)) ray_error_free(v); else if (v) ray_release(v);
TEST_ASSERT_FMT(ok, "%s, partition init %lld fails: an answer, not the reference", qs[q], (long long)n);
continue;
}
TEST_ASSERT_FMT(strcmp(code, "oom") == 0, "%s, partition init %lld fails: %s, not oom", qs[q], (long long)n, code);
TEST_ASSERT_FMT(tr.net_bytes <= 0, "%s, partition init %lld fails: %lld bytes kept", qs[q], (long long)n, (long long)tr.net_bytes);
part_failed++;
}
}
/* the v2 route falls back to the classic one on a failed init and
* answers; the classic route's failed init answers `oom` */
TEST_ASSERT_FMT(part_failed > 0, "no partition init failure answered oom");
aw_eval_ok("(set F 0)"); aw_eval_ok("(set R0 0)"); aw_eval_ok("(set Q 0)");
PASS();
}

/* #1780: a cancel that lands while a group runs answers `cancel`, never
* `oom` and never an answer it did not finish. For each query, the nth
* pool dispatch finds the query cancelled (mode 0: a cancel request; mode
Expand Down Expand Up @@ -6801,6 +6902,7 @@ const test_entry_t aware_entries[] = {
{ "aware/sort_head_late_chunked", test_sort_head_late_chunked, aw_setup, aw_teardown },
{ "aware/topk_cancel_at_dispatch", test_topk_cancel_at_dispatch, aw_setup, aw_teardown },
{ "aware/group_cancel_status", test_group_cancel_status, aw_setup, aw_teardown },
{ "aware/group_alloc_fail", test_group_alloc_fail, aw_setup, aw_teardown },
{ "aware/holistic_oom", test_holistic_oom, aw_setup, aw_teardown },
{ "aware/radix_cancel_keeps_nothing", test_radix_cancel_keeps_nothing, aw_setup, aw_teardown },
{ "aware/sel_compact_cancel_at_dispatch", test_sel_compact_cancel_at_dispatch, aw_setup, aw_teardown },
Expand Down
Loading