From fa789765d48eeba490ba7837222da2601ee7ac29 Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Sat, 4 Mar 2017 13:30:34 -0500 Subject: [PATCH] Add nesting stress tests up to 500 recursion depth, expand size of memory map Change-Id: Id6bdee0aad6ebea2338c383ea31e47b6febdae1d --- cpp/src/arrow/ipc/adapter.h | 6 ++- cpp/src/arrow/ipc/ipc-adapter-test.cc | 60 ++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/cpp/src/arrow/ipc/adapter.h b/cpp/src/arrow/ipc/adapter.h index b7d8fa93d365..933d3a4639fe 100644 --- a/cpp/src/arrow/ipc/adapter.h +++ b/cpp/src/arrow/ipc/adapter.h @@ -47,8 +47,10 @@ namespace ipc { // ---------------------------------------------------------------------- // Write path -// We have trouble decoding flatbuffers if the size i > 70, so 64 is a nice round number -// TODO(emkornfield) investigate this more +// +// ARROW-109: We set this number arbitrarily to help catch user mistakes. For +// deeply nested schemas, it is expected the user will indicate explicitly the +// maximum allowed recursion depth constexpr int kMaxIpcRecursionDepth = 64; // Write the RecordBatch (collection of equal-length Arrow arrays) to the diff --git a/cpp/src/arrow/ipc/ipc-adapter-test.cc b/cpp/src/arrow/ipc/ipc-adapter-test.cc index 899936389328..6678fd522a86 100644 --- a/cpp/src/arrow/ipc/ipc-adapter-test.cc +++ b/cpp/src/arrow/ipc/ipc-adapter-test.cc @@ -213,7 +213,8 @@ class RecursionLimits : public ::testing::Test, public io::MemoryMapFixture { void TearDown() { io::MemoryMapFixture::TearDown(); } Status WriteToMmap(int recursion_level, bool override_level, int32_t* metadata_length, - int64_t* body_length, std::shared_ptr* schema) { + int64_t* body_length, std::shared_ptr* batch, + std::shared_ptr* schema) { const int batch_length = 5; TypePtr type = int32(); std::shared_ptr array; @@ -230,18 +231,18 @@ class RecursionLimits : public ::testing::Test, public io::MemoryMapFixture { *schema = std::shared_ptr(new Schema({f0})); std::vector> arrays = {array}; - auto batch = std::make_shared(*schema, batch_length, arrays); + *batch = std::make_shared(*schema, batch_length, arrays); std::string path = "test-write-past-max-recursion"; - const int memory_map_size = 1 << 16; + const int memory_map_size = 1 << 20; io::MemoryMapFixture::InitMemoryMap(memory_map_size, path, &mmap_); if (override_level) { - return WriteRecordBatch(*batch, 0, mmap_.get(), metadata_length, body_length, pool_, - recursion_level + 1); + return WriteRecordBatch(**batch, 0, mmap_.get(), metadata_length, body_length, + pool_, recursion_level + 1); } else { return WriteRecordBatch( - *batch, 0, mmap_.get(), metadata_length, body_length, pool_); + **batch, 0, mmap_.get(), metadata_length, body_length, pool_); } } @@ -254,15 +255,21 @@ TEST_F(RecursionLimits, WriteLimit) { int32_t metadata_length = -1; int64_t body_length = -1; std::shared_ptr schema; - ASSERT_RAISES( - Invalid, WriteToMmap((1 << 8) + 1, false, &metadata_length, &body_length, &schema)); + std::shared_ptr batch; + ASSERT_RAISES(Invalid, + WriteToMmap((1 << 8) + 1, false, &metadata_length, &body_length, &batch, &schema)); } TEST_F(RecursionLimits, ReadLimit) { int32_t metadata_length = -1; int64_t body_length = -1; std::shared_ptr schema; - ASSERT_OK(WriteToMmap(64, true, &metadata_length, &body_length, &schema)); + + const int recursion_depth = 64; + + std::shared_ptr batch; + ASSERT_OK(WriteToMmap( + recursion_depth, true, &metadata_length, &body_length, &batch, &schema)); std::shared_ptr message; ASSERT_OK(ReadMessage(0, metadata_length, mmap_.get(), &message)); @@ -273,8 +280,39 @@ TEST_F(RecursionLimits, ReadLimit) { io::BufferReader reader(payload); - std::shared_ptr batch; - ASSERT_RAISES(Invalid, ReadRecordBatch(*metadata, schema, &reader, &batch)); + std::shared_ptr result; + ASSERT_RAISES(Invalid, ReadRecordBatch(*metadata, schema, &reader, &result)); +} + +TEST_F(RecursionLimits, StressLimit) { + auto CheckDepth = [this](int recursion_depth, bool* it_works) { + int32_t metadata_length = -1; + int64_t body_length = -1; + std::shared_ptr schema; + std::shared_ptr batch; + ASSERT_OK(WriteToMmap( + recursion_depth, true, &metadata_length, &body_length, &batch, &schema)); + + std::shared_ptr message; + ASSERT_OK(ReadMessage(0, metadata_length, mmap_.get(), &message)); + auto metadata = std::make_shared(message); + + std::shared_ptr payload; + ASSERT_OK(mmap_->ReadAt(metadata_length, body_length, &payload)); + + io::BufferReader reader(payload); + + std::shared_ptr result; + ASSERT_OK(ReadRecordBatch(*metadata, schema, recursion_depth + 1, &reader, &result)); + *it_works = result->Equals(*batch); + }; + + bool it_works = false; + CheckDepth(100, &it_works); + ASSERT_TRUE(it_works); + + CheckDepth(500, &it_works); + ASSERT_TRUE(it_works); } } // namespace ipc