Repository navigation
Improve parquet footer reading performance by speculatively reading footer bytes #22782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
36abb36
f6e7eca
ab0fdd0
30dc151
79ba978
8aca3e3
c5a5890
f0f2348
00bd441
08b47b8
a8e201b
cd0a5db
c567b10
b9ec4dd
836ce36
864769d
9d3d2c6
1b04e99
89c3f9e
4c1638a
d4eaa8e
2705e4e
f3e2cc2
37537fb
a81ed97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |||||||||||||||||||
| #include <cudf/detail/utilities/cuda_memcpy.hpp> | ||||||||||||||||||||
| #include <cudf/detail/utilities/host_worker_pool.hpp> | ||||||||||||||||||||
| #include <cudf/detail/utilities/integer_utils.hpp> | ||||||||||||||||||||
| #include <cudf/io/config_utils.hpp> | ||||||||||||||||||||
| #include <cudf/io/datasource.hpp> | ||||||||||||||||||||
| #include <cudf/io/parquet.hpp> | ||||||||||||||||||||
| #include <cudf/io/parquet_io_utils.hpp> | ||||||||||||||||||||
|
|
@@ -22,12 +23,17 @@ | |||||||||||||||||||
| #include <cuda/iterator> | ||||||||||||||||||||
| #include <cuda/std/tuple> | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #include <algorithm> | ||||||||||||||||||||
| #include <array> | ||||||||||||||||||||
| #include <cstring> | ||||||||||||||||||||
| #include <functional> | ||||||||||||||||||||
| #include <mutex> | ||||||||||||||||||||
| #include <numeric> | ||||||||||||||||||||
| #include <stdexcept> | ||||||||||||||||||||
| #include <string> | ||||||||||||||||||||
| #include <tuple> | ||||||||||||||||||||
| #include <type_traits> | ||||||||||||||||||||
| #include <vector> | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @file parquet_io_utils.cpp | ||||||||||||||||||||
|
|
@@ -86,23 +92,72 @@ auto dispatch_fetch_tasks(std::size_t num_sources, Task fetch_task) | |||||||||||||||||||
| std::vector<std::unique_ptr<cudf::io::datasource::buffer>> fetch_footers_to_host_impl( | ||||||||||||||||||||
| cudf::host_span<std::reference_wrapper<cudf::io::datasource> const> datasources) | ||||||||||||||||||||
| { | ||||||||||||||||||||
| // Look up runtime configuration once, as late as possible. | ||||||||||||||||||||
| auto const metadata_size_hint = cudf::io::parquet_reader::metadata_size_hint(); | ||||||||||||||||||||
| // Helper to fetch footer from a datasource | ||||||||||||||||||||
| auto const fetch_footer = [](cudf::io::datasource& datasource) { | ||||||||||||||||||||
| auto const fetch_footer = [metadata_size_hint](cudf::io::datasource& datasource) { | ||||||||||||||||||||
| constexpr auto header_len = sizeof(file_header_s); | ||||||||||||||||||||
| constexpr auto ender_len = sizeof(file_ender_s); | ||||||||||||||||||||
| size_t const len = datasource.size(); | ||||||||||||||||||||
| CUDF_EXPECTS(len > header_len + ender_len, "Incorrect data source"); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| auto header_buffer = datasource.host_read(0, header_len); | ||||||||||||||||||||
| auto const header = reinterpret_cast<file_header_s const*>(header_buffer->data()); | ||||||||||||||||||||
| auto ender_buffer = datasource.host_read(len - ender_len, ender_len); | ||||||||||||||||||||
| auto const ender = reinterpret_cast<file_ender_s const*>(ender_buffer->data()); | ||||||||||||||||||||
| CUDF_EXPECTS(header->magic == detail::parquet_magic, "Corrupted header"); | ||||||||||||||||||||
| CUDF_EXPECTS(ender->magic == detail::parquet_magic, "Corrupted footer"); | ||||||||||||||||||||
| CUDF_EXPECTS(ender->footer_len != 0 && ender->footer_len <= (len - header_len - ender_len), | ||||||||||||||||||||
| auto const speculative_read_size = | ||||||||||||||||||||
| std::min(len, std::max(metadata_size_hint, static_cast<size_t>(ender_len))); | ||||||||||||||||||||
| auto const speculative_read_offset = len - speculative_read_size; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| auto speculative_buffer = datasource.host_read(speculative_read_offset, speculative_read_size); | ||||||||||||||||||||
| CUDF_EXPECTS(speculative_buffer->size() >= ender_len, | ||||||||||||||||||||
| "Failed to read Parquet footer ender bytes: requested_offset=" + | ||||||||||||||||||||
| std::to_string(speculative_read_offset) + | ||||||||||||||||||||
| ", requested_size=" + std::to_string(speculative_read_size) + | ||||||||||||||||||||
| ", bytes_read=" + std::to_string(speculative_buffer->size()) + | ||||||||||||||||||||
| ", required_ender_size=" + std::to_string(ender_len)); | ||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| file_ender_s ender{}; | ||||||||||||||||||||
| std::memcpy( | ||||||||||||||||||||
| &ender, speculative_buffer->data() + speculative_buffer->size() - ender_len, ender_len); | ||||||||||||||||||||
|
mhaseeb123 marked this conversation as resolved.
Resolved
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| if (speculative_read_offset == 0) { | ||||||||||||||||||||
| file_header_s header{}; | ||||||||||||||||||||
| std::memcpy(&header, speculative_buffer->data(), header_len); | ||||||||||||||||||||
| CUDF_EXPECTS(header.magic == detail::parquet_magic, "Corrupted header"); | ||||||||||||||||||||
|
mhaseeb123 marked this conversation as resolved.
Resolved
|
||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| CUDF_EXPECTS(ender.magic == detail::parquet_magic, "Corrupted footer"); | ||||||||||||||||||||
| CUDF_EXPECTS(ender.footer_len != 0 && ender.footer_len <= (len - header_len - ender_len), | ||||||||||||||||||||
|
mhaseeb123 marked this conversation as resolved.
Resolved
|
||||||||||||||||||||
| "Incorrect footer length"); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return datasource.host_read(len - ender->footer_len - ender_len, ender->footer_len); | ||||||||||||||||||||
| auto const footer_offset = len - ender.footer_len - ender_len; | ||||||||||||||||||||
| if (footer_offset >= speculative_read_offset) { | ||||||||||||||||||||
| auto const footer_start_offset = footer_offset - speculative_read_offset; | ||||||||||||||||||||
| CUDF_EXPECTS( | ||||||||||||||||||||
| footer_start_offset + ender.footer_len <= speculative_buffer->size(), | ||||||||||||||||||||
| "Speculative metadata read did not include full footer bytes: file_size=" + | ||||||||||||||||||||
| std::to_string(len) + ", metadata_size_hint=" + std::to_string(metadata_size_hint) + | ||||||||||||||||||||
| ", speculative_read_offset=" + std::to_string(speculative_read_offset) + | ||||||||||||||||||||
| ", speculative_read_size=" + std::to_string(speculative_read_size) + | ||||||||||||||||||||
| ", bytes_read=" + std::to_string(speculative_buffer->size()) + ", footer_offset=" + | ||||||||||||||||||||
| std::to_string(footer_offset) + ", footer_len=" + std::to_string(ender.footer_len)); | ||||||||||||||||||||
| std::vector<uint8_t> footer_bytes(ender.footer_len); | ||||||||||||||||||||
| std::memcpy( | ||||||||||||||||||||
| footer_bytes.data(), speculative_buffer->data() + footer_start_offset, ender.footer_len); | ||||||||||||||||||||
| return cudf::io::datasource::buffer::create(std::move(footer_bytes)); | ||||||||||||||||||||
|
mhaseeb123 marked this conversation as resolved.
Resolved
|
||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Footer starts before the speculative read range. Read the missing prefix, then stitch. | ||||||||||||||||||||
| auto const missing_prefix_size = speculative_read_offset - footer_offset; | ||||||||||||||||||||
| auto missing_prefix = datasource.host_read(footer_offset, missing_prefix_size); | ||||||||||||||||||||
| CUDF_EXPECTS(missing_prefix->size() == missing_prefix_size, | ||||||||||||||||||||
| "Failed to read the missing footer prefix bytes: requested_offset=" + | ||||||||||||||||||||
| std::to_string(footer_offset) + | ||||||||||||||||||||
| ", requested_size=" + std::to_string(missing_prefix_size) + ", bytes_read=" + | ||||||||||||||||||||
| std::to_string(missing_prefix->size()) + ", file_size=" + std::to_string(len)); | ||||||||||||||||||||
| std::vector<uint8_t> footer_bytes(ender.footer_len); | ||||||||||||||||||||
| std::memcpy(footer_bytes.data(), missing_prefix->data(), missing_prefix_size); | ||||||||||||||||||||
|
mhaseeb123 marked this conversation as resolved.
Resolved
Comment on lines
+138
to
+143
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Saves us one
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @mhaseeb123. With that change, one of the (new) tests fails: And when running all the tests with It seems to me like datasource.host_read(footer_offset, missing_prefix_size, footer_bytes.data());doesn't get us the full footer. IIUC,
Then we'd have But I could easily be misreading things! |
||||||||||||||||||||
| auto const footer_suffix_size = ender.footer_len - missing_prefix_size; | ||||||||||||||||||||
| std::memcpy( | ||||||||||||||||||||
| footer_bytes.data() + missing_prefix_size, speculative_buffer->data(), footer_suffix_size); | ||||||||||||||||||||
| return cudf::io::datasource::buffer::create(std::move(footer_bytes)); | ||||||||||||||||||||
|
mhaseeb123 marked this conversation as resolved.
Resolved
|
||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return dispatch_fetch_tasks(datasources.size(), [&](std::size_t source_idx) { | ||||||||||||||||||||
|
|
@@ -338,10 +393,10 @@ fetch_byte_ranges_to_device_async_impl( | |||||||||||||||||||
| std::unique_ptr<cudf::io::datasource::buffer> fetch_footer_to_host(cudf::io::datasource& datasource) | ||||||||||||||||||||
| { | ||||||||||||||||||||
| CUDF_FUNC_RANGE(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Wrap the input into an array and delegate to the multi-source implementation | ||||||||||||||||||||
| std::array<std::reference_wrapper<cudf::io::datasource>, 1> datasources{std::ref(datasource)}; | ||||||||||||||||||||
| auto footer_buffers = fetch_footers_to_host_impl({datasources.data(), datasources.size()}); | ||||||||||||||||||||
| std::array<std::reference_wrapper<cudf::io::datasource>, 1> datasource_refs{std::ref(datasource)}; | ||||||||||||||||||||
| auto footer_buffers = | ||||||||||||||||||||
| fetch_footers_to_host_impl(cudf::host_span<std::reference_wrapper<cudf::io::datasource> const>{ | ||||||||||||||||||||
| datasource_refs.data(), datasource_refs.size()}); | ||||||||||||||||||||
|
TomAugspurger marked this conversation as resolved.
Resolved
|
||||||||||||||||||||
| return std::move(footer_buffers.front()); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,10 +28,13 @@ | |
| #include <src/io/parquet/parquet_gpu.hpp> | ||
| #include <src/io/parquet/stats_filter_helpers.hpp> | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <cstring> | ||
| #include <limits> | ||
| #include <memory> | ||
| #include <stdexcept> | ||
| #include <utility> | ||
|
|
||
| using ParquetDecompressionTest = DecompressionTest<ParquetReaderTest>; | ||
|
|
||
|
|
@@ -2872,6 +2875,109 @@ struct ParquetMetadataReaderTest : public cudf::test::BaseFixture { | |
| } | ||
| }; | ||
|
|
||
| namespace { | ||
| class TrackingFooterDatasource : public cudf::io::datasource { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea here was to actually validate in the test that we actually did the single-shot read. Maybe this isn't worth the complexity. We can verify things manually today with nsys: and see that with the default there's a single call to and with |
||
| public: | ||
| explicit TrackingFooterDatasource(std::vector<char> const& data) : data_(data) {} | ||
|
|
||
| [[nodiscard]] std::vector<std::pair<size_t, size_t>> const& reads() const { return reads_; } | ||
|
|
||
| void reset() { reads_.clear(); } | ||
|
|
||
| std::unique_ptr<cudf::io::datasource::buffer> host_read(size_t offset, size_t size) override | ||
| { | ||
| reads_.emplace_back(offset, size); | ||
| CUDF_EXPECTS(offset <= data_.size(), "Offset is out of bounds"); | ||
| auto const read_size = std::min(size, data_.size() - offset); | ||
| std::vector<std::byte> out(read_size); | ||
| std::memcpy(out.data(), data_.data() + offset, read_size); | ||
| return cudf::io::datasource::buffer::create(std::move(out)); | ||
| } | ||
|
|
||
| size_t host_read(size_t offset, size_t size, uint8_t* dst) override | ||
| { | ||
| reads_.emplace_back(offset, size); | ||
| CUDF_EXPECTS(offset <= data_.size(), "Offset is out of bounds"); | ||
| auto const read_size = std::min(size, data_.size() - offset); | ||
| std::memcpy(dst, data_.data() + offset, read_size); | ||
| return read_size; | ||
| } | ||
|
|
||
| [[nodiscard]] size_t size() const override { return data_.size(); } | ||
|
|
||
| private: | ||
| std::vector<char> const& data_; | ||
| std::vector<std::pair<size_t, size_t>> reads_; | ||
| }; | ||
|
|
||
| class ShortReadFooterDatasource : public cudf::io::datasource { | ||
| public: | ||
| ShortReadFooterDatasource(std::vector<char> const& data, | ||
| size_t short_read_call, | ||
| size_t short_read_size) | ||
| : data_(data), short_read_call_(short_read_call), short_read_size_(short_read_size) | ||
| { | ||
| } | ||
|
|
||
| std::unique_ptr<cudf::io::datasource::buffer> host_read(size_t offset, size_t size) override | ||
| { | ||
| ++call_count_; | ||
| CUDF_EXPECTS(offset <= data_.size(), "Offset is out of bounds"); | ||
| auto read_size = std::min(size, data_.size() - offset); | ||
| if (call_count_ == short_read_call_) { read_size = std::min(read_size, short_read_size_); } | ||
| std::vector<std::byte> out(read_size); | ||
| std::memcpy(out.data(), data_.data() + offset, read_size); | ||
| return cudf::io::datasource::buffer::create(std::move(out)); | ||
| } | ||
|
|
||
| size_t host_read(size_t offset, size_t size, uint8_t* dst) override | ||
| { | ||
| ++call_count_; | ||
| CUDF_EXPECTS(offset <= data_.size(), "Offset is out of bounds"); | ||
| auto read_size = std::min(size, data_.size() - offset); | ||
| if (call_count_ == short_read_call_) { read_size = std::min(read_size, short_read_size_); } | ||
| std::memcpy(dst, data_.data() + offset, read_size); | ||
| return read_size; | ||
| } | ||
|
|
||
| [[nodiscard]] size_t size() const override { return data_.size(); } | ||
|
|
||
| private: | ||
| std::vector<char> const& data_; | ||
| size_t short_read_call_; | ||
| size_t short_read_size_; | ||
| size_t call_count_{0}; | ||
| }; | ||
|
|
||
| std::vector<char> make_simple_parquet_bytes() | ||
| { | ||
| auto ints = random_values<int32_t>(128); | ||
| cudf::test::fixed_width_column_wrapper<int32_t> int_col(ints.begin(), ints.end()); | ||
| cudf::table_view input_table({int_col}); | ||
|
|
||
| std::vector<char> parquet_bytes; | ||
| auto const write_opts = | ||
| cudf::io::parquet_writer_options::builder(cudf::io::sink_info{&parquet_bytes}, input_table) | ||
| .build(); | ||
| cudf::io::write_parquet(write_opts); | ||
| return parquet_bytes; | ||
| } | ||
|
|
||
| template <typename Fn> | ||
| void expect_logic_error_contains(Fn&& fn, std::string const& needle) | ||
| { | ||
| try { | ||
| fn(); | ||
| FAIL() << "Expected cudf::logic_error"; | ||
| } catch (cudf::logic_error const& e) { | ||
| EXPECT_NE(std::string{e.what()}.find(needle), std::string::npos) | ||
| << "Actual message: " << e.what(); | ||
| } catch (...) { | ||
| FAIL() << "Expected cudf::logic_error"; | ||
| } | ||
| } | ||
| } // namespace | ||
|
|
||
| TEST_F(ParquetMetadataReaderTest, Basics) | ||
| { | ||
| auto const num_rows = 1200; | ||
|
|
@@ -2969,6 +3075,70 @@ TEST_F(ParquetMetadataReaderTest, PreMaterializedMetadata) | |
| test_parquet_metadata(3); | ||
| } | ||
|
|
||
| TEST_F(ParquetMetadataReaderTest, MetadataFooterErrorMessages) | ||
| { | ||
| auto parquet_bytes = make_simple_parquet_bytes(); | ||
|
|
||
| // Small source size check | ||
| { | ||
| std::vector<char> tiny(8, '\0'); | ||
| TrackingFooterDatasource tiny_source(tiny); | ||
| auto const source = cudf::io::source_info{&tiny_source}; | ||
| expect_logic_error_contains([&] { (void)cudf::io::read_parquet_metadata(source); }, | ||
| "Parquet source is too small to contain header and footer ender"); | ||
| } | ||
|
|
||
| // Speculative ender short read check | ||
| { | ||
| ShortReadFooterDatasource short_ender_source( | ||
| parquet_bytes, /*short_read_call=*/1, /*short_read_size=*/7); | ||
| auto const source = cudf::io::source_info{&short_ender_source}; | ||
| expect_logic_error_contains([&] { (void)cudf::io::read_parquet_metadata(source); }, | ||
| "Failed to read Parquet footer ender bytes"); | ||
| } | ||
|
|
||
| // Header magic check when speculative read starts at offset 0 | ||
| { | ||
| auto bad_header = parquet_bytes; | ||
| bad_header[0] = 'B'; | ||
| bad_header[1] = 'A'; | ||
| bad_header[2] = 'D'; | ||
| bad_header[3] = '!'; | ||
| TrackingFooterDatasource bad_header_source(bad_header); | ||
|
TomAugspurger marked this conversation as resolved.
Resolved
|
||
| auto const source = cudf::io::source_info{&bad_header_source}; | ||
| expect_logic_error_contains([&] { (void)cudf::io::read_parquet_metadata(source); }, | ||
| "Invalid Parquet file header magic"); | ||
| } | ||
|
|
||
| // Footer magic check | ||
| { | ||
| auto bad_footer_magic = parquet_bytes; | ||
| auto const footer_magic_offset = bad_footer_magic.size() - sizeof(uint32_t); | ||
| bad_footer_magic[footer_magic_offset + 0] = 'B'; | ||
| bad_footer_magic[footer_magic_offset + 1] = 'A'; | ||
| bad_footer_magic[footer_magic_offset + 2] = 'D'; | ||
| bad_footer_magic[footer_magic_offset + 3] = '!'; | ||
| TrackingFooterDatasource bad_footer_source(bad_footer_magic); | ||
| auto const source = cudf::io::source_info{&bad_footer_source}; | ||
| expect_logic_error_contains([&] { (void)cudf::io::read_parquet_metadata(source); }, | ||
| "Invalid Parquet footer magic"); | ||
| } | ||
|
|
||
| // Footer length check | ||
| { | ||
| auto bad_footer_len = parquet_bytes; | ||
| auto const footer_len_offset = bad_footer_len.size() - sizeof(cudf::io::parquet::file_ender_s); | ||
| bad_footer_len[footer_len_offset + 0] = 0; | ||
| bad_footer_len[footer_len_offset + 1] = 0; | ||
| bad_footer_len[footer_len_offset + 2] = 0; | ||
| bad_footer_len[footer_len_offset + 3] = 0; | ||
| TrackingFooterDatasource bad_footer_len_source(bad_footer_len); | ||
| auto const source = cudf::io::source_info{&bad_footer_len_source}; | ||
| expect_logic_error_contains([&] { (void)cudf::io::read_parquet_metadata(source); }, | ||
| "Invalid Parquet footer length"); | ||
| } | ||
| } | ||
|
|
||
| TEST_F(ParquetMetadataReaderTest, Nested) | ||
| { | ||
| auto const num_rows = 1200; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.