algeon-cugraph 0.1.2

Safe libcugraph execution component for Algeon
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::error::Error as StdError;

use cudf::{
    core::error::{FactKeyName, kind_for_code},
    interop::contract::{InteropError, InteropErrorKind},
};

use crate::error::{Error, ErrorKind, ErrorStatus};

pub(super) fn map_prepared_lease_activation_interop_error(error: InteropError, operation: &'static str) -> Error {
    map_interop_boundary_error(
        error,
        operation,
        "failed to activate prepared export read lease",
        classify_prepared_lease_interop_error,
    )
}

pub(super) fn map_prepared_lease_completion_interop_error(error: InteropError, operation: &'static str) -> Error {
    map_interop_boundary_error(
        error,
        operation,
        "failed to complete prepared export read lease",
        classify_prepared_lease_interop_error,
    )
}

pub(super) fn map_owned_import_interop_error(error: InteropError, operation: &'static str) -> Error {
    map_interop_boundary_error(
        error,
        operation,
        "failed to import shared interop input into the consumer cudf runtime",
        classify_owned_import_interop_error,
    )
}

pub(super) fn map_prepared_projection_interop_error(error: InteropError, operation: &'static str) -> Error {
    map_interop_boundary_error(
        error,
        operation,
        "failed to prepare cudf-owned input for prepared device export",
        classify_projection_interop_error,
    )
}

fn classify_prepared_lease_interop_error(kind: InteropErrorKind) -> (ErrorKind, Option<ErrorStatus>) {
    match kind {
        InteropErrorKind::UnsupportedType
        | InteropErrorKind::UnsupportedLayout
        | InteropErrorKind::PlacementUnsupported
        | InteropErrorKind::ProfileUnsupported => (ErrorKind::UnsupportedType, Some(ErrorStatus::Permanent)),
        InteropErrorKind::ExecutionBindingMismatch | InteropErrorKind::MixedDeviceUnsupported => {
            (ErrorKind::InvalidInput, Some(ErrorStatus::Permanent))
        },
        InteropErrorKind::ExecutionBindingUnavailable
        | InteropErrorKind::NotFound
        | InteropErrorKind::InvalidView
        | InteropErrorKind::ReadinessMismatch => (ErrorKind::InvalidState, Some(ErrorStatus::Permanent)),
        InteropErrorKind::ReadinessUnavailable => (ErrorKind::Unexpected, None),
    }
}

fn classify_owned_import_interop_error(kind: InteropErrorKind) -> (ErrorKind, Option<ErrorStatus>) {
    match kind {
        InteropErrorKind::UnsupportedType
        | InteropErrorKind::UnsupportedLayout
        | InteropErrorKind::PlacementUnsupported
        | InteropErrorKind::ProfileUnsupported => (ErrorKind::UnsupportedType, Some(ErrorStatus::Permanent)),
        InteropErrorKind::ExecutionBindingMismatch | InteropErrorKind::MixedDeviceUnsupported => {
            (ErrorKind::InvalidInput, Some(ErrorStatus::Permanent))
        },
        InteropErrorKind::ExecutionBindingUnavailable
        | InteropErrorKind::NotFound
        | InteropErrorKind::InvalidView
        | InteropErrorKind::ReadinessMismatch => (ErrorKind::InvalidState, Some(ErrorStatus::Permanent)),
        InteropErrorKind::ReadinessUnavailable => (ErrorKind::Unexpected, None),
    }
}

fn classify_projection_interop_error(kind: InteropErrorKind) -> (ErrorKind, Option<ErrorStatus>) {
    match kind {
        InteropErrorKind::UnsupportedType
        | InteropErrorKind::UnsupportedLayout
        | InteropErrorKind::PlacementUnsupported
        | InteropErrorKind::ProfileUnsupported => (ErrorKind::UnsupportedType, Some(ErrorStatus::Permanent)),
        InteropErrorKind::ExecutionBindingMismatch | InteropErrorKind::MixedDeviceUnsupported => {
            (ErrorKind::InvalidInput, Some(ErrorStatus::Permanent))
        },
        InteropErrorKind::ExecutionBindingUnavailable
        | InteropErrorKind::NotFound
        | InteropErrorKind::InvalidView
        | InteropErrorKind::ReadinessMismatch => (ErrorKind::InvalidState, Some(ErrorStatus::Permanent)),
        InteropErrorKind::ReadinessUnavailable => (ErrorKind::Unexpected, None),
    }
}

fn map_interop_boundary_error(
    error: InteropError,
    operation: &'static str,
    message: &'static str,
    classify: fn(InteropErrorKind) -> (ErrorKind, Option<ErrorStatus>),
) -> Error {
    let (kind, explicit_status) = classify(error.kind());
    let mut mapped = Error::new(kind, operation).with_message(interop_boundary_message(message, error.message()));
    if let Some(status) = explicit_status.or_else(|| interop_error_source_status(&error)) {
        mapped = apply_error_status(mapped, status);
    }
    mapped = mapped.with_context("interop_kind", format!("{:?}", error.kind()));
    mapped = mapped.with_context("interop_message", error.message().to_string());

    for (key, value) in error.context() {
        mapped = mapped.with_context(key, value.clone());
    }

    mapped.set_source(error)
}

fn interop_boundary_message(boundary_message: &'static str, leaf_message: &str) -> String {
    let leaf_message = leaf_message.trim();
    if leaf_message.is_empty() { boundary_message.to_string() } else { format!("{boundary_message}: {leaf_message}") }
}

fn interop_error_source_status(error: &InteropError) -> Option<ErrorStatus> {
    let mut source = error.source();
    while let Some(current) = source {
        if let Some(cudf_error) = current.downcast_ref::<cudf::Error>() {
            return Some(match cudf_error.status() {
                cudf::ErrorStatus::Permanent => ErrorStatus::Permanent,
                cudf::ErrorStatus::Temporary => ErrorStatus::Temporary,
                cudf::ErrorStatus::Persistent => ErrorStatus::Persistent,
            });
        }
        source = current.source();
    }
    None
}

fn apply_error_status(mapped: Error, status: ErrorStatus) -> Error {
    match status {
        ErrorStatus::Permanent => mapped.set_permanent(),
        ErrorStatus::Temporary => mapped.set_temporary(),
        ErrorStatus::Persistent => mapped.set_persistent(),
    }
}

fn map_cudf_error_code(code: cudf::ErrorCode) -> ErrorKind {
    match code {
        cudf::ErrorCode::MissingColumn
        | cudf::ErrorCode::DuplicateColumnName
        | cudf::ErrorCode::DuplicateKey
        | cudf::ErrorCode::ColumnLengthMismatch
        | cudf::ErrorCode::TypeMismatch
        | cudf::ErrorCode::IndexOutOfBounds
        | cudf::ErrorCode::NullUnexpected
        | cudf::ErrorCode::CrossDeviceInputs
        | cudf::ErrorCode::CrossProvenanceInputs
        | cudf::ErrorCode::ForeignInvalidArgument => ErrorKind::InvalidInput,
        cudf::ErrorCode::EmptyConstructionRequiresRuntime
        | cudf::ErrorCode::RuntimeBindingMismatch
        | cudf::ErrorCode::ReadinessUnavailable
        | cudf::ErrorCode::ExecutionBindingUnavailable
        | cudf::ErrorCode::InvariantViolation
        | cudf::ErrorCode::ForeignInvariantViolation => ErrorKind::InvalidState,
        cudf::ErrorCode::UnsupportedDtype
        | cudf::ErrorCode::UnsupportedLayout
        | cudf::ErrorCode::UnsupportedOperation => ErrorKind::UnsupportedType,
        cudf::ErrorCode::OutOfMemory | cudf::ErrorCode::ForeignResourceExhausted => ErrorKind::OutOfMemory,
        cudf::ErrorCode::IoFailure => ErrorKind::IoError,
        cudf::ErrorCode::CudaRuntime
        | cudf::ErrorCode::FatalCuda
        | cudf::ErrorCode::ForeignTransientDependencyFailure
        | cudf::ErrorCode::ForeignPersistentDependencyFailure => ErrorKind::Unexpected,
        _ => match kind_for_code(code) {
            cudf::ErrorKind::InvalidInput => ErrorKind::InvalidInput,
            cudf::ErrorKind::NotFound => ErrorKind::IoError,
            cudf::ErrorKind::Unsupported => ErrorKind::UnsupportedType,
            cudf::ErrorKind::ExecutionContext => ErrorKind::InvalidState,
            cudf::ErrorKind::ResourceExhausted => ErrorKind::OutOfMemory,
            cudf::ErrorKind::DependencyFailure => ErrorKind::Unexpected,
            cudf::ErrorKind::InvariantViolation => ErrorKind::InvalidState,
            _ => ErrorKind::Unexpected,
        },
    }
}

fn cudf_fact_context_key(key: FactKeyName) -> &'static str {
    match key {
        FactKeyName::ColumnName => "cudf_column_name",
        FactKeyName::ColumnIndex => "cudf_column_index",
        FactKeyName::ExpectedRows => "cudf_expected_rows",
        FactKeyName::ActualRows => "cudf_actual_rows",
        FactKeyName::ExpectedCount => "cudf_expected_count",
        FactKeyName::ActualCount => "cudf_actual_count",
        FactKeyName::LeftTypeId => "cudf_left_type_id",
        FactKeyName::RightTypeId => "cudf_right_type_id",
        FactKeyName::LeftDevice => "cudf_left_device",
        FactKeyName::RightDevice => "cudf_right_device",
        FactKeyName::TableRowCount => "cudf_table_row_count",
        FactKeyName::TableColumnCount => "cudf_table_column_count",
        FactKeyName::KeysCount => "cudf_keys_count",
        FactKeyName::DistinctKeysCount => "cudf_distinct_keys_count",
        FactKeyName::TypeId => "cudf_type_id",
        _ => "cudf_fact",
    }
}

fn map_cudf_status(mut mapped: Error, status: cudf::ErrorStatus) -> Error {
    match status {
        cudf::ErrorStatus::Permanent => mapped = mapped.set_permanent(),
        cudf::ErrorStatus::Temporary => mapped = mapped.set_temporary(),
        cudf::ErrorStatus::Persistent => mapped = mapped.set_persistent(),
    }
    mapped
}

fn attach_cudf_error_context(mut mapped: Error, error: &cudf::Error) -> Error {
    mapped = mapped.with_context("cudf_code", error.code().to_string());
    mapped = mapped.with_context("cudf_kind", error.kind().to_string());
    mapped = mapped.with_context("cudf_status", error.status().to_string());
    mapped = mapped.with_context("cudf_operation", error.operation().to_string());

    for called in error.call_chain() {
        mapped = mapped.with_context("cudf_called", (*called).to_string());
    }

    for (key, value) in error.facts().iter() {
        mapped = mapped.with_context(cudf_fact_context_key(key), value.to_string());
    }

    for (key, value) in error.details() {
        mapped = mapped.with_context("cudf_detail", format!("{key}={value}"));
    }

    mapped
}

#[cfg(test)]
fn map_cudf_error(error: cudf::Error, operation: &'static str) -> Error {
    map_cudf_error_with(error, operation, "cudf operation failed", std::iter::empty())
}

pub(super) fn map_cudf_error_with<I>(
    error: cudf::Error,
    operation: &'static str,
    message: impl Into<String>,
    context: I,
) -> Error
where
    I: IntoIterator<Item = (&'static str, String)>,
{
    let source_code = error.code();
    let source_status = error.status();
    let mut mapped = Error::new(map_cudf_error_code(source_code), operation).with_message(message.into());
    mapped = map_cudf_status(mapped, source_status);
    mapped = attach_cudf_error_context(mapped, &error);

    for (key, value) in context {
        mapped = mapped.with_context(key, value);
    }

    mapped.set_source(error)
}

#[cfg(test)]
mod tests {
    use cudf::{
        interop::contract::{InteropError, InteropErrorKind},
        testing::error as cudf_testing_error,
    };

    use super::*;
    use crate::error::ErrorKind;

    #[test]
    fn map_cudf_error_missing_column_is_invalid_input() {
        let cudf_err = cudf_testing_error::new_with_for_test(
            cudf::ErrorCode::MissingColumn,
            "cudf::testing::missing_column",
            "column 'foo' not found",
        );
        let err =
            map_cudf_error_with(cudf_err, "test_op", "input adapter failed", [("column_role", "src".to_string())]);
        assert_eq!(err.kind(), ErrorKind::InvalidInput);
        assert!(err.is_permanent(), "missing columns should be permanent");
        assert_eq!(err.message(), "input adapter failed");
        assert!(err.to_string().contains("cudf_code"), "mapped error should retain cudf metadata");
    }

    #[test]
    fn map_cudf_error_runtime_binding_mismatch_is_invalid_state() {
        let cudf_err = cudf_testing_error::reanchor_for_test(
            cudf_testing_error::new_with_for_test(
                cudf::ErrorCode::RuntimeBindingMismatch,
                "cudf::testing::runtime_binding::inner",
                "runtime must not use ambient provenance",
            ),
            "cudf::testing::runtime_binding::outer",
        );
        let err = map_cudf_error(cudf_err, "test_op");
        assert_eq!(err.kind(), ErrorKind::InvalidState);
        assert!(err.is_permanent(), "runtime binding mismatches should be permanent");
        assert!(err.to_string().contains("cudf_called"), "call-chain breadcrumbs should be preserved");
    }

    #[test]
    fn map_cudf_error_cuda_runtime_is_temporary_unexpected() {
        let cudf_err = cudf_testing_error::new_with_for_test(
            cudf::ErrorCode::CudaRuntime,
            "cudf::testing::cuda_runtime",
            "device launch failed",
        );
        let err = map_cudf_error(cudf_err, "test_op");
        assert_eq!(err.kind(), ErrorKind::Unexpected);
        assert!(err.is_temporary(), "CUDA runtime failures should be temporary");
    }

    #[test]
    fn map_cudf_error_out_of_memory() {
        let cudf_err =
            cudf_testing_error::new_with_for_test(cudf::ErrorCode::OutOfMemory, "cudf::testing::oom", "alloc failed");
        let err = map_cudf_error(cudf_err, "test_op");
        assert_eq!(err.kind(), ErrorKind::OutOfMemory);
        assert!(err.is_temporary(), "OOM should be temporary");
    }

    #[test]
    fn map_cudf_error_io_error() {
        let cudf_err =
            cudf_testing_error::new_with_for_test(cudf::ErrorCode::IoFailure, "cudf::testing::io", "file not found");
        let err = map_cudf_error(cudf_err, "test_op");
        assert_eq!(err.kind(), ErrorKind::IoError);
        assert!(err.is_permanent(), "I/O errors should be permanent by default");
    }

    #[test]
    fn map_cudf_error_fatal_cuda_is_persistent_unexpected() {
        let cudf_err = cudf_testing_error::new_with_for_test(
            cudf::ErrorCode::FatalCuda,
            "cudf::testing::fatal_cuda",
            "device entered fatal state",
        );
        let err = map_cudf_error(cudf_err, "test_op");
        assert_eq!(err.kind(), ErrorKind::Unexpected);
        assert!(err.is_persistent(), "fatal CUDA failures should be persistent");
    }

    #[test]
    fn map_cudf_error_foreign_invariant_violation_is_invalid_state() {
        let cudf_err = cudf_testing_error::foreign_invariant_violation_for_test();
        let err = map_cudf_error(cudf_err, "test_op");
        assert_eq!(err.kind(), ErrorKind::InvalidState);
        assert!(err.is_permanent(), "foreign invariant violations should be permanent");
        assert!(err.to_string().contains("cudf_detail"), "foreign details should be preserved");
    }

    #[test]
    fn prepared_lease_activation_interop_preserves_leaf_failure_message() {
        let interop_err = InteropError::new(InteropErrorKind::InvalidView, "test backend forced activation failure");
        let err = map_prepared_lease_activation_interop_error(interop_err, "test_op");
        assert_eq!(err.kind(), ErrorKind::InvalidState);
        assert!(
            err.message().contains("test backend forced activation failure"),
            "top-level error message should preserve the leaf failure reason for fast diagnosis"
        );
    }

    #[test]
    fn prepared_lease_activation_interop_without_leaf_message_keeps_boundary_message_clean() {
        let interop_err = InteropError::new(InteropErrorKind::InvalidView, "");
        let err = map_prepared_lease_activation_interop_error(interop_err, "test_op");
        assert_eq!(err.kind(), ErrorKind::InvalidState);
        assert_eq!(err.message(), "failed to activate prepared export read lease");
    }

    #[test]
    fn prepared_lease_completion_interop_readiness_unavailable_uses_persistent_source_status() {
        let interop_err =
            InteropError::new(InteropErrorKind::ReadinessUnavailable, "failed to finalize producer lease").set_source(
                cudf_testing_error::new_with_for_test(
                    cudf::ErrorCode::FatalCuda,
                    "cudf::testing::fatal_cuda",
                    "device entered fatal state",
                ),
            );
        let err = map_prepared_lease_completion_interop_error(interop_err, "test_op");
        assert_eq!(err.kind(), ErrorKind::Unexpected);
        assert!(err.is_persistent(), "lease completion should preserve persistent dependency failure semantics");
    }

    #[test]
    fn owned_import_interop_execution_binding_mismatch_is_invalid_input() {
        let interop_err = InteropError::new(
            InteropErrorKind::ExecutionBindingMismatch,
            "prepared export import requires the destination runtime to use the same CUDA device",
        );
        let err = map_owned_import_interop_error(interop_err, "test_op");
        assert_eq!(err.kind(), ErrorKind::InvalidInput);
        assert!(err.is_permanent(), "cross-device import mismatches are caller input failures");
    }

    #[test]
    fn prepared_projection_interop_readiness_unavailable_uses_temporary_source_status() {
        let interop_err = InteropError::new(InteropErrorKind::ReadinessUnavailable, "failed to project device export")
            .set_source(cudf_testing_error::new_with_for_test(
                cudf::ErrorCode::OutOfMemory,
                "cudf::testing::oom",
                "allocation failed",
            ));
        let err = map_prepared_projection_interop_error(interop_err, "test_op");
        assert_eq!(err.kind(), ErrorKind::Unexpected);
        assert!(err.is_temporary(), "projection should preserve temporary source failures instead of flattening them");
    }
}