• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

llnl-asr / dftracer-utils / 45735107228

26 Nov 2026 01:20AM UTC coverage: 77.696% (+0.4%) from 77.313%
45735107228

push

github

rayandrew
fix: convert natively at the edge and read chunked nested columns safely

to_pandas, from_pandas, table files, loc, reindex, where, unnest and the
dask viewer called pyarrow kernels to convert columns, so they failed
without pyarrow and copied text twice. They now use native codes, masks
and buffers, and pyarrow appears only when a user asks for Arrow.
Readers of list offsets and struct children now go through flat pieces
of a column, so a chunked, selected or dictionary nested column no
longer crashes explode, the CLI printer or child access.

164371 of 253360 branches covered (64.88%)

Branch coverage included in aggregate %.

1455 of 1580 new or added lines in 20 files covered. (92.09%)

795 existing lines in 33 files now uncovered.

190713 of 203655 relevant lines covered (93.65%)

4484549.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.21
/src/dftracer/utils/python/trace/distribution.cpp
1
// SPDX-License-Identifier: MIT
2
#include <dftracer/utils/common/constants.h>
3
#include <dftracer/utils/common/error.h>
4
#include <dftracer/utils/common/scratch.h>
5
#include <dftracer/utils/coro/scope.h>
6
#include <dftracer/utils/dstd/fs.h>
7
#include <dftracer/utils/dstd/memory.h>
8
#include <dftracer/utils/dstd/optional.h>
9
#include <dftracer/utils/dstd/path.h>
10
#include <dftracer/utils/dstd/string.h>
11
#include <dftracer/utils/dstd/vector.h>
12
#include <dftracer/utils/python/core/call.h>
13
#include <dftracer/utils/python/core/errors.h>
14
#include <dftracer/utils/python/core/method.h>
15
#include <dftracer/utils/python/core/runtime_mixin.h>
16
#include <dftracer/utils/python/core/seq_helpers.h>
17
#include <dftracer/utils/python/core/str_helpers.h>
18
#include <dftracer/utils/python/core/type_helpers.h>
19
#include <dftracer/utils/python/frame/agg_config.h>
20
#include <dftracer/utils/python/runtime/runtime.h>
21
#include <dftracer/utils/python/trace/distribution.h>
22
#include <dftracer/utils/runtime/fs/fs.h>
23
#include <dftracer/utils/runtime/runtime.h>
24
#include <dftracer/utils/trace/index/build/batch_builder.h>
25
#include <dftracer/utils/trace/index/build/file_partition.h>
26
#include <dftracer/utils/trace/index/index.h>
27
#include <dftracer/utils/trace/index/schemas/dft/agg/aggregation_config.h>
28
#include <dftracer/utils/trace/index/schemas/dft/agg/aggregation_fold.h>
29
#include <dftracer/utils/trace/index/schemas/dft/agg/aggregation_intern.h>
30
#include <dftracer/utils/trace/index/store/root_layout.h>
31
#include <dftracer/utils/trace/table/table_source.h>
32
#include <dftracer/utils/trace/views/sharded_view.h>
33

34
#include <algorithm>
35
#include <cstdint>
36
#include <new>
37

38
using dftracer::utils::python::core::guard;
39
using dftracer::utils::python::core::parse_kw;
40
using dftracer::utils::python::core::parse_seq;
41

42
using dftracer::utils::Runtime;
43
using dftracer::utils::python::core::parse_string_seq;
44
using dftracer::utils::runtime::fs::FoundFile;
45
using dftracer::utils::trace::index::build::BatchBuilder;
46
using dftracer::utils::trace::index::build::IndexBuildBatchConfig;
47
using dftracer::utils::trace::index::build::IndexBuildBatchResult;
48
using dftracer::utils::trace::index::build::plan_lpt_partition;
49

50
// ---------------------------------------------------------------------------
51
// scan_files: parallel directory scan with size info
52
// ---------------------------------------------------------------------------
53

54
static PyObject *tables_among_traces_fn(PyObject * /*self*/, PyObject *arg) {
104 ✔
55
    dftracer::utils::dstd::Vector<dftracer::utils::dstd::String> paths;
104 ✔
56
    if (!parse_string_seq(arg, "paths must be a sequence of str", paths))
104 !
57
        return NULL;
58
    dftracer::utils::dstd::Vector<std::uint8_t> skip;
104 ✔
59
    if (!run_blocking([&] {
156 !
60
            dftracer::utils::dstd::Vector<dftracer::utils::dstd::StringView>
104 ✔
61
                views(paths.begin(), paths.end());
104 ✔
62
            auto found = dftracer::utils::trace::table::tables_among_traces(
104 !
63
                dftracer::utils::dstd::Span<
52 !
64
                    const dftracer::utils::dstd::StringView>(views.data(),
52 ✔
65
                                                             views.size()));
104 !
66
            if (!found)
104 ✔
67
                throw dftracer::utils::python::core::PyErrorException(
3 !
68
                    found.error());
3 ✔
69
            skip = std::move(*found);
102 ✔
70
        })) {
104 ✔
71
        return NULL;
1 ✔
72
    }
73
    PyObject *out = PyList_New(static_cast<Py_ssize_t>(skip.size()));
102 !
74
    if (!out) return NULL;
102 !
75
    for (std::size_t i = 0; i < skip.size(); ++i)
402 ✔
76
        PyList_SET_ITEM(out, static_cast<Py_ssize_t>(i),
300 !
77
                        PyBool_FromLong(skip[i]));
78
    return out;
51 ✔
79
}
104 ✔
80

81
static PyObject *scan_files_fn(PyObject * /*self*/, PyObject *args,
24 ✔
82
                               PyObject *kwds) {
83
    const char *directory;
24 ✔
84
    PyObject *patterns_obj = NULL;
24 ✔
85
    int recursive = 0;
24 ✔
86
    PyObject *runtime_arg = NULL;
24 ✔
87
    if (!parse_kw<"s|OpO">(
24 ✔
88
            args, kwds, {"directory", "patterns", "recursive", "runtime"},
12 ✔
89
            &directory, &patterns_obj, &recursive, &runtime_arg)) {
90
        return NULL;
91
    }
92

93
    dftracer::utils::dstd::Vector<dftracer::utils::dstd::String> patterns;
24 ✔
94
    if (patterns_obj && patterns_obj != Py_None) {
24 !
95
        if (!dftracer::utils::python::core::parse_string_seq(
24 !
96
                patterns_obj, "patterns must be a sequence", patterns))
12 ✔
97
            return NULL;
98
    }
12 ✔
99

100
    Runtime *rt = nullptr;
24 ✔
101
    if (runtime_arg && runtime_arg != Py_None) {
24 !
UNCOV
102
        if (!PyObject_TypeCheck(runtime_arg, &RuntimeType)) {
×
UNCOV
103
            PyObject *native = PyObject_GetAttrString(runtime_arg, "_native");
×
UNCOV
104
            if (!native || !PyObject_TypeCheck(native, &RuntimeType)) {
×
UNCOV
105
                Py_XDECREF(native);
×
UNCOV
106
                PyErr_SetString(PyExc_TypeError,
×
107
                                "runtime must be a Runtime instance or None");
108
                return NULL;
109
            }
UNCOV
110
            rt = ((RuntimeObject *)native)->runtime.get();
×
UNCOV
111
            Py_DECREF(native);
×
112
        } else {
×
UNCOV
113
            rt = ((RuntimeObject *)runtime_arg)->runtime.get();
×
114
        }
115
    } else {
116
        rt = dftracer::utils::python::runtime::get_default_runtime();
24 !
117
    }
118

119
    dftracer::utils::runtime::fs::FindOptions options;
24 ✔
120
    for (auto &pattern : patterns) {
48 ✔
121
        if (!pattern.empty() && pattern.front() == '.')
24 !
UNCOV
122
            pattern.insert(0, 1, '*');
×
123
    }
12 ✔
124
    options.patterns = std::move(patterns);
24 ✔
125
    options.skip_dirs = dftracer::utils::runtime::fs::names_of(
36 !
126
        dftracer::utils::common::constants::indexer::SCAN_SKIP_DIRS);
24 ✔
127
    options.recursive = recursive != 0;
24 ✔
128
    options.skip_unreadable = options.recursive;
24 ✔
129
    dftracer::utils::dstd::Vector<FoundFile> entries;
24 ✔
130
    if (!run_blocking([&] {
36 !
131
            auto found =
24 ✔
132
                rt->submit(
48 !
133
                      dftracer::utils::coro::with_scope(
24 !
134
                          [in = std::move(options),
48 ✔
135
                           directory](dftracer::utils::coro::Scope &scope) {
48 !
136
                              return dftracer::utils::runtime::fs::find_all(
36 !
137
                                  scope,
12 ✔
138
                                  dftracer::utils::dstd::PathView(directory),
24 !
139
                                  in);
36 !
140
                          }),
12 ✔
141
                      "scan-files")
12 !
142
                    .get();
24 !
143
            if (!found)
24 ✔
UNCOV
144
                throw dftracer::utils::python::core::PyErrorException(
×
UNCOV
145
                    found.error());
×
146
            entries = std::move(*found);
24 ✔
147
        })) {
24 ✔
148
        return NULL;
149
    }
150

151
    PyObject *out = PyList_New(static_cast<Py_ssize_t>(entries.size()));
24 !
152
    if (!out) return NULL;
24 !
153
    for (std::size_t i = 0; i < entries.size(); ++i) {
66 !
154
        PyObject *t = Py_BuildValue("(sn)", entries[i].path.c_str(),
63 !
155
                                    (Py_ssize_t)entries[i].size);
42 !
156
        if (!t) {
42 !
UNCOV
157
            Py_DECREF(out);
×
UNCOV
158
            return NULL;
×
159
        }
160
        PyList_SET_ITEM(out, i, t);
42 !
161
    }
21 !
162
    return out;
12 ✔
163
}
36 ✔
164

165
// ---------------------------------------------------------------------------
166
// plan_lpt_partition: LPT bin-packing of (path, size) pairs
167
// ---------------------------------------------------------------------------
168

169
static PyObject *plan_lpt_partition_fn(PyObject * /*self*/, PyObject *args) {
30 ✔
170
    PyObject *entries_obj;
30 ✔
171
    Py_ssize_t worker_count;
30 ✔
172
    if (!PyArg_ParseTuple(args, "On", &entries_obj, &worker_count)) return NULL;
30 !
173
    if (worker_count <= 0) worker_count = 1;
30 ✔
174

175
    dftracer::utils::dstd::Vector<FoundFile> entries;
30 ✔
176
    if (!parse_seq<FoundFile>(
30 !
177
            entries_obj, "entries must be a sequence of (path, size) tuples",
15 ✔
178
            entries,
179
            [](PyObject *item, dftracer::utils::dstd::Vector<FoundFile> &o) {
72 ✔
180
                const char *path = nullptr;
72 ✔
181
                Py_ssize_t size = 0;
72 ✔
182
                if (!PyArg_ParseTuple(item, "sn", &path, &size)) return false;
72 !
183
                FoundFile fe;
72 !
184
                fe.path = dftracer::utils::dstd::Path(
72 !
185
                    dftracer::utils::dstd::StringView(path));
72 !
186
                fe.size = static_cast<std::size_t>(size);
72 ✔
187
                o.push_back(std::move(fe));
72 !
188
                return true;
72 ✔
189
            }))
72 ✔
190
        return NULL;
191

192
    auto buckets = plan_lpt_partition(std::move(entries),
45 !
193
                                      static_cast<std::size_t>(worker_count));
30 !
194

195
    PyObject *out = PyList_New(static_cast<Py_ssize_t>(buckets.size()));
30 !
196
    if (!out) return NULL;
30 !
197
    for (std::size_t i = 0; i < buckets.size(); ++i) {
72 !
198
        PyObject *lst = PyList_New(static_cast<Py_ssize_t>(buckets[i].size()));
42 !
199
        if (!lst) {
42 !
UNCOV
200
            Py_DECREF(out);
×
UNCOV
201
            return NULL;
×
202
        }
203
        for (std::size_t j = 0; j < buckets[i].size(); ++j) {
114 !
204
            PyObject *t = Py_BuildValue("(sn)", buckets[i][j].path.c_str(),
144 !
205
                                        (Py_ssize_t)buckets[i][j].size);
72 !
206
            if (!t) {
72 !
UNCOV
207
                Py_DECREF(lst);
×
UNCOV
208
                Py_DECREF(out);
×
UNCOV
209
                return NULL;
×
210
            }
211
            PyList_SET_ITEM(lst, j, t);
72 !
212
        }
36 !
213
        PyList_SET_ITEM(out, i, lst);
42 !
214
    }
21 !
215
    return out;
15 ✔
216
}
30 ✔
217

218
static PyObject *build_index_batch_fn(PyObject * /*self*/, PyObject *args,
44 ✔
219
                                      PyObject *kwds) {
220
    PyObject *files_obj;
44 ✔
221
    const char *index_dir;
44 ✔
222
    Py_ssize_t checkpoint_size = static_cast<Py_ssize_t>(
44 ✔
223
        dftracer::utils::common::constants::indexer::DEFAULT_CHECKPOINT_SIZE);
224
    int force_rebuild = 0;
44 ✔
225
    int build_bloom = 1;
44 ✔
226
    PyObject *bloom_dims_obj = NULL;
44 ✔
227
    Py_ssize_t parallelism = 0;
44 ✔
228
    PyObject *runtime_arg = NULL;
44 ✔
229
    PyObject *aggregation_config_obj = NULL;
44 ✔
230
    PyObject *progress_obj = NULL;
44 ✔
231

232
    if (!parse_kw<"Os|nppOnOOO">(
44 ✔
233
            args, kwds,
22 ✔
234
            {"files", "index_dir", "checkpoint_size", "force", "build_bloom",
22 ✔
235
             "bloom_dimensions", "parallelism", "runtime", "aggregation_config",
236
             "progress"},
237
            &files_obj, &index_dir, &checkpoint_size, &force_rebuild,
238
            &build_bloom, &bloom_dims_obj, &parallelism, &runtime_arg,
239
            &aggregation_config_obj, &progress_obj)) {
240
        return NULL;
241
    }
242

243
    // Unpack files.
244
    dftracer::utils::dstd::Vector<dftracer::utils::dstd::String> files;
44 ✔
245
    {
22 ✔
246
        if (!parse_string_seq(files_obj, "files must be a sequence", files))
44 !
247
            return NULL;
248
    }
249
    if (files.empty()) Py_RETURN_NONE;
44 ✔
250

251
    // Optional bloom dimensions override.
252
    dftracer::utils::dstd::Vector<dftracer::utils::dstd::String> bloom_dims;
44 ✔
253
    if (bloom_dims_obj && bloom_dims_obj != Py_None) {
44 ✔
UNCOV
254
        if (!parse_string_seq(bloom_dims_obj,
×
255
                              "bloom_dimensions must be a sequence",
256
                              bloom_dims))
257
            return NULL;
258
    }
259

260
    // Resolve Runtime (matching CheckpointIndexer pattern).
261
    Runtime *rt = nullptr;
44 ✔
262
    if (runtime_arg && runtime_arg != Py_None) {
44 !
UNCOV
263
        if (PyObject_TypeCheck(runtime_arg, &RuntimeType)) {
×
UNCOV
264
            rt = ((RuntimeObject *)runtime_arg)->runtime.get();
×
265
        } else {
UNCOV
266
            PyObject *native = PyObject_GetAttrString(runtime_arg, "_native");
×
UNCOV
267
            if (!native || !PyObject_TypeCheck(native, &RuntimeType)) {
×
UNCOV
268
                Py_XDECREF(native);
×
UNCOV
269
                PyErr_SetString(PyExc_TypeError,
×
270
                                "runtime must be a Runtime instance or None");
271
                return NULL;
272
            }
UNCOV
273
            rt = ((RuntimeObject *)native)->runtime.get();
×
UNCOV
274
            Py_DECREF(native);
×
275
        }
×
276
    } else {
277
        rt = dftracer::utils::python::runtime::get_default_runtime();
44 !
278
    }
279

280
    // The aggregation tier is built by every batch, the dataclass only
281
    // customizes it.
282
    dftracer::utils::dstd::SharedPtr<
44 ✔
283
        dftracer::utils::trace::index::schemas::dft::agg::AggregationConfig>
284
        agg_config_ptr;
44 ✔
285
    if (aggregation_config_obj && aggregation_config_obj != Py_None)
44 ✔
286
        agg_config_ptr = dftracer::utils::dstd::make_shared<
63 !
287
            dftracer::utils::trace::index::schemas::dft::agg::
288
                AggregationConfig>(
42 !
289
            dftracer::utils::python::frame::agg_config_from_py(
84 !
290
                aggregation_config_obj));
42 !
291
    else
292
        agg_config_ptr = dftracer::utils::dstd::make_shared<
2 !
293
            dftracer::utils::trace::index::schemas::dft::agg::
294
                AggregationConfig>(dftracer::utils::trace::index::schemas::dft::
3 !
295
                                       agg::standard_aggregation_config());
2 !
296

297
    // A fresh build of the whole root stages on node-local scratch when the
298
    // destination is a slow filesystem, then publishes the store across.
299
    const dftracer::utils::dstd::String final_root =
44 ✔
300
        dftracer::utils::trace::index::normalize_index_root(index_dir);
44 !
301
    dftracer::utils::dstd::Optional<dftracer::utils::common::ScratchSession>
44 ✔
302
        scratch;
44 ✔
303
    dftracer::utils::dstd::String build_dir = index_dir;
44 !
304
    if (!dftracer::utils::python::core::py_unwrap(
66 !
305
            dftracer::utils::dstd::fs::exists(dftracer::utils::dstd::PathView(
66 !
306
                dftracer::utils::trace::index::store_dir(final_root)))) &&
132 !
307
        dftracer::utils::common::should_stage(final_root)) {
66 !
UNCOV
308
        auto staged = dftracer::utils::common::staged_build_scratch(files);
×
UNCOV
309
        if (staged) {
×
UNCOV
310
            build_dir = dftracer::utils::dstd::String(staged->dir().str());
×
UNCOV
311
            scratch.emplace(std::move(*staged));
×
312
        }
UNCOV
313
    }
×
314

315
    auto batch_config =
44 ✔
316
        dftracer::utils::dstd::make_shared<IndexBuildBatchConfig>();
44 !
317
    batch_config->file_paths = std::move(files);
44 ✔
318
    batch_config->index_dir = build_dir;
44 !
319
    batch_config->build_bloom = build_bloom != 0;
44 ✔
320
    batch_config->checkpoint_size = static_cast<std::size_t>(checkpoint_size);
44 ✔
321
    batch_config->force_rebuild = force_rebuild != 0;
44 ✔
322
    batch_config->bloom_dimensions = std::move(bloom_dims);
44 ✔
323
    batch_config->parallelism =
44 ✔
324
        parallelism > 0 ? static_cast<std::size_t>(parallelism)
88 ✔
325
                        : (rt ? std::max<std::size_t>(rt->threads(), 1) : 1);
44 !
326

327
    // The build runs with the GIL released; re-acquire it per call. The
328
    // GIL-holding deleter drops the ref safely after the build.
329
    if (progress_obj && progress_obj != Py_None) {
44 !
330
        Py_INCREF(progress_obj);
42 !
331
        auto cb = dftracer::utils::dstd::adopt_shared_ptr(
63 !
332
            progress_obj, [](PyObject *p) {
63 ✔
333
                PyGILState_STATE g = PyGILState_Ensure();
42 ✔
334
                Py_DECREF(p);
42 !
335
                PyGILState_Release(g);
42 ✔
336
            });
42 !
337
        batch_config->progress = [cb](std::size_t done, std::size_t total) {
246 !
338
            PyGILState_STATE g = PyGILState_Ensure();
72 ✔
339
            PyObject *r = PyObject_CallFunction(cb.get(), "nn",
108 ✔
340
                                                static_cast<Py_ssize_t>(done),
36 ✔
341
                                                static_cast<Py_ssize_t>(total));
36 ✔
342
            if (r) {
72 !
343
                Py_DECREF(r);
72 !
344
            } else {
36 ✔
345
                // A failing progress callback must not abort the build.
UNCOV
346
                PyErr_Clear();
×
347
            }
348
            PyGILState_Release(g);
72 ✔
349
        };
57 !
350
    }
42 ✔
351

352
    {
22 ✔
353
        auto agg_index = dftracer::utils::trace::index::Index::open(
44 !
354
            dftracer::utils::trace::index::normalize_index_root(build_dir));
44 !
355
        if (!agg_index) {
44 ✔
UNCOV
356
            dftracer::utils::python::core::set_py_error(agg_index.error());
×
357
            return NULL;
358
        }
359
        auto agg_generation = agg_index->generation();
44 !
360
        if (!agg_generation) {
44 ✔
UNCOV
361
            dftracer::utils::python::core::set_py_error(agg_generation.error());
×
UNCOV
362
            return NULL;
×
363
        }
364
        auto agg_intern =
44 ✔
365
            dftracer::utils::trace::index::schemas::dft::agg::intern_for_index(
44 !
366
                build_dir, *agg_generation);
44 !
367
        batch_config->agg_fold_factory =
44 !
368
            [agg_config_ptr,
103 !
369
             agg_intern](dftracer::utils::dstd::StringIntern &build_intern)
44 ✔
370
            -> dftracer::utils::dstd::UniquePtr<
371
                dftracer::utils::trace::index::schemas::dft::agg::
372
                    AggregationFold> {
373
            return dftracer::utils::dstd::make_unique<
37 ✔
374
                dftracer::utils::trace::index::schemas::dft::agg::
375
                    AggregationFold>(build_intern, agg_intern, *agg_config_ptr);
37 ✔
376
        };
59 !
377
    }
44 !
378

379
    IndexBuildBatchResult result;
44 ✔
380
    if (!run_blocking([&] {
66 !
381
            dftracer::utils::python::core::py_unwrap(
66 !
382
                rt->submit(dftracer::utils::coro::with_scope(
110 !
383
                               [cfg = batch_config,
198 !
384
                                out = &result](dftracer::utils::coro::Scope &)
88 !
385
                                   -> dftracer::utils::coro::CoroTask<void> {
22 !
386
                                   *out = co_await BatchBuilder::process(cfg);
66 !
387
                               }),
66 ✔
388
                           "build-index-batch")
22 !
389
                    .get());
66 !
390
        })) {
44 ✔
391
        return NULL;
392
    }
393

394
    // If any file failed, surface the first error.
395
    if (result.failed > 0) {
44 ✔
396
        for (const auto &r : result.results) {
3 !
397
            if (!r.success) {
2 ✔
398
                PyErr_SetString(dftracer::utils::python::core::g_dft_error,
3 !
399
                                r.error_message.c_str());
1 ✔
400
                return NULL;
2 ✔
401
            }
402
        }
1 !
403
    }
404

405
    // Publish once every handle to the staged store is closed, so a later open
406
    // reads the destination.
407
    if (scratch) {
42 ✔
UNCOV
408
        dftracer::utils::trace::index::release_root(
×
UNCOV
409
            dftracer::utils::trace::index::normalize_index_root(build_dir));
×
UNCOV
410
        dftracer::utils::trace::index::release_root(final_root);
×
UNCOV
411
        const auto published = dftracer::utils::common::publish_path(
×
412
            dftracer::utils::dstd::PathView(
UNCOV
413
                dftracer::utils::trace::index::store_dir(
×
UNCOV
414
                    dftracer::utils::trace::index::normalize_index_root(
×
415
                        build_dir))),
416
            dftracer::utils::dstd::PathView(
UNCOV
417
                dftracer::utils::trace::index::store_dir(final_root)));
×
UNCOV
418
        if (!published) {
×
UNCOV
419
            dftracer::utils::python::core::set_py_error(published.error());
×
UNCOV
420
            return NULL;
×
421
        }
UNCOV
422
    }
×
423

424
    Py_RETURN_NONE;
21 ✔
425
}
88 !
426

427
static PyObject *write_shard_set_fn(PyObject * /*self*/, PyObject *args) {
30 ✔
428
    const char *root;
30 ✔
429
    PyObject *shard_dirs_obj;
30 ✔
430
    if (!PyArg_ParseTuple(args, "sO", &root, &shard_dirs_obj)) return NULL;
30 !
431
    dftracer::utils::dstd::Vector<dftracer::utils::dstd::String> shard_dirs;
30 ✔
432
    if (!parse_string_seq(shard_dirs_obj, "shard_dirs must be a sequence",
30 !
433
                          shard_dirs))
434
        return NULL;
435
    return guard([&]() -> PyObject * {
45 !
436
        const auto normalized_root =
30 ✔
437
            dftracer::utils::trace::index::normalize_index_root(root);
30 ✔
438
        dftracer::utils::dstd::Vector<dftracer::utils::dstd::String> dirs;
30 ✔
439
        dirs.reserve(shard_dirs.size());
30 !
440
        for (const auto &d : shard_dirs)
72 ✔
441
            dirs.emplace_back(dftracer::utils::dstd::StringView(d));
42 !
442
        dftracer::utils::python::core::py_unwrap(
45 !
443
            dftracer::utils::trace::views::write_shard_set(
45 !
444
                dftracer::utils::dstd::String(normalized_root), dirs));
15 !
445
        Py_RETURN_NONE;
30 ✔
446
    });
45 !
447
}
30 ✔
448

449
static PyObject *consolidate_shard_set_fn(PyObject * /*self*/, PyObject *args) {
30 ✔
450
    const char *root;
30 ✔
451
    if (!PyArg_ParseTuple(args, "s", &root)) return NULL;
30 !
452
    return guard([&]() -> PyObject * {
45 ✔
453
        const auto normalized_root =
30 ✔
454
            dftracer::utils::trace::index::normalize_index_root(root);
30 ✔
455
        const std::size_t files = dftracer::utils::python::core::py_unwrap(
45 !
456
            dftracer::utils::trace::views::consolidate_shard_set(
45 !
457
                dftracer::utils::dstd::String(normalized_root)));
15 !
458
        return PyLong_FromSize_t(files);
30 !
459
    });
30 ✔
460
}
15 ✔
461

462
static PyObject *enable_aggregation_deterministic_ids_fn(PyObject * /*self*/,
42 ✔
463
                                                         PyObject * /*args*/) {
464
    dftracer::utils::trace::index::schemas::dft::agg::
42 ✔
465
        enable_deterministic_intern_ids();
21 ✔
466
    Py_RETURN_NONE;
42 ✔
467
}
468

469
// ---------------------------------------------------------------------------
470
// Module registration
471
// ---------------------------------------------------------------------------
472

473
static PyMethodDef DistributionMethods[] = {
474
    {"build_index_batch", DFTU_PYCFUNCTION(build_index_batch_fn),
475
     METH_VARARGS | METH_KEYWORDS,
476
     "build_index_batch(files, index_dir, ...) -> None\n"
477
     "Run the indexer pipeline on `files` into the index at `index_dir`, "
478
     "which this worker writes directly and numbers its own files in. "
479
     "Its aggregation folds are always created; `aggregation_config` "
480
     "customizes them and defaults to the standard config."},
481
    {"plan_lpt_partition", DFTU_PYCFUNCTION(plan_lpt_partition_fn),
482
     METH_VARARGS,
483
     "plan_lpt_partition(entries, num_workers) -> list[list[(path, size)]]\n"
484
     "Greedy Longest-Processing-Time-first bin-packing of (path, size) "
485
     "tuples across num_workers buckets. Minimises the maximum per-worker "
486
     "total size."},
487
    {"tables_among_traces", DFTU_PYCFUNCTION(tables_among_traces_fn), METH_O,
488
     "tables_among_traces(paths) -> list[bool]\n"
489
     "True for each dftu.table file of `paths` when `paths` also holds a "
490
     "file that is not a table, else all False."},
491
    {"scan_files", DFTU_PYCFUNCTION(scan_files_fn),
492
     METH_VARARGS | METH_KEYWORDS,
493
     "scan_files(directory, patterns=None, recursive=False, runtime=None) "
494
     "-> list[(path, size)]\n"
495
     "Parallel directory scan returning (path, size) tuples for regular "
496
     "files matching the patterns."},
497
    {"enable_aggregation_deterministic_ids",
498
     DFTU_PYCFUNCTION(enable_aggregation_deterministic_ids_fn), METH_NOARGS,
499
     "enable_aggregation_deterministic_ids() -> None\n"
500
     "Flip the global aggregation dftracer::utils::dstd::StringIntern into "
501
     "deterministic-id "
502
     "mode "
503
     "so the same string maps to the same 32-bit id in every worker "
504
     "process. Call once at worker startup BEFORE any aggregation work."},
505
    {"write_shard_set", DFTU_PYCFUNCTION(write_shard_set_fn), METH_VARARGS,
506
     "write_shard_set(index_dir, shard_dirs) -> None\n"
507
     "Catalog `shard_dirs` as the shards of the index root, in the root's "
508
     "shards.json. A shard under the root is stored relative to it."},
509
    {"consolidate_shard_set", DFTU_PYCFUNCTION(consolidate_shard_set_fn),
510
     METH_VARARGS,
511
     "consolidate_shard_set(index_dir) -> int\n"
512
     "Merge the shards the root's shards.json names into the root's own "
513
     "store by moving their data files, then remove the manifest and the "
514
     "shard directories. Returns the number of trace files the shards "
515
     "indexed. A failed move keeps the manifest and the shards for a retry."},
516
    {NULL, NULL, 0, NULL}};
517

518
int dftracer::utils::python::trace::init_distribution(PyObject *m) {
218 ✔
519
    if (PyModule_AddFunctions(m, DistributionMethods) < 0) return -1;
218 ✔
520
    return 0;
109 ✔
521
}
109 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc