westonpace commented on a change in pull request #9533: URL: https://github.com/apache/arrow/pull/9533#discussion_r581464660
########## File path: cpp/src/arrow/util/iterator_test.cc ########## @@ -726,6 +730,142 @@ TEST(TestAsyncUtil, CompleteBackgroundStressTest) { } } +template <typename T> +class SlowSourcePreventingReentrant { + public: + explicit SlowSourcePreventingReentrant(AsyncGenerator<T> source) + : state_(std::make_shared<State>(std::move(source))) {} + + Future<TestInt> operator()() { + if (state_->in.load()) { + state_->valid.store(false); + } + state_->in.store(true); + auto result = state_->source(); + result.AddCallback(Callback{state_}); + return result; + } + + void AssertValid() { + EXPECT_EQ(true, state_->valid.load()) + << "The generator was accessed in a reentrant manner"; + } + + private: + struct State { + explicit State(AsyncGenerator<T> source_) + : source(std::move(source_)), in(false), valid(true) {} + + AsyncGenerator<T> source; + std::atomic<bool> in; + std::atomic<bool> valid; + }; + struct Callback { + void operator()(const Result<T>& result) { state_->in.store(false); } + std::shared_ptr<State> state_; + }; + + std::shared_ptr<State> state_; +}; + +TEST(TestAsyncUtil, SerialReadaheadSlowProducer) { + AsyncGenerator<TestInt> it = BackgroundAsyncVectorIt({1, 2, 3, 4, 5}); + SlowSourcePreventingReentrant<TestInt> tracker(std::move(it)); + SerialReadaheadGenerator<TestInt> serial_readahead(tracker, 2); + AssertAsyncGeneratorMatch({1, 2, 3, 4, 5}, + static_cast<AsyncGenerator<TestInt>>(serial_readahead)); + tracker.AssertValid(); +} + +TEST(TestAsyncUtil, SerialReadaheadSlowConsumer) { + int num_delivered = 0; + auto source = [&num_delivered]() { + if (num_delivered < 5) { + return Future<TestInt>::MakeFinished(num_delivered++); + } else { + return Future<TestInt>::MakeFinished(IterationTraits<TestInt>::End()); + } + }; + SerialReadaheadGenerator<TestInt> serial_readahead(std::move(source), 3); + SleepABit(); + ASSERT_EQ(0, num_delivered); + ASSERT_FINISHES_OK_AND_ASSIGN(auto next, serial_readahead()); + ASSERT_EQ(0, next.value); + ASSERT_EQ(3, num_delivered); + AssertAsyncGeneratorMatch({1, 2, 3, 4}, + static_cast<AsyncGenerator<TestInt>>(serial_readahead)); +} + +TEST(TestAsyncUtil, SerialReadaheadStress) { + constexpr int NTASKS = 20; + constexpr int NITEMS = 50; + constexpr int EXPECTED_SUM = (NITEMS * (NITEMS - 1)) / 2; + for (int i = 0; i < NTASKS; i++) { + AsyncGenerator<TestInt> it = BackgroundAsyncVectorIt(RangeVector(NITEMS)); + SerialReadaheadGenerator<TestInt> serial_readahead(it, 2); + unsigned int sum = 0; + auto visit_fut = VisitAsyncGenerator<TestInt>( + serial_readahead, [&sum](TestInt test_int) -> Status { + sum += test_int.value; + // Normally sleeping in a visit function would be a faux-pas but we want to slow + // the reader down to match the producer to maximize the stress + std::this_thread::sleep_for(kYieldDuration); + return Status::OK(); + }); + ASSERT_FINISHES_OK(visit_fut); + ASSERT_EQ(EXPECTED_SUM, sum); + } +} + +TEST(TestAsyncUtil, SerialReadaheadStressFast) { + constexpr int NTASKS = 20; + constexpr int NITEMS = 50; + constexpr int EXPECTED_SUM = (NITEMS * (NITEMS - 1)) / 2; + for (int i = 0; i < NTASKS; i++) { + AsyncGenerator<TestInt> it = BackgroundAsyncVectorIt(RangeVector(NITEMS), false); + SerialReadaheadGenerator<TestInt> serial_readahead(it, 2); + unsigned int sum = 0; + auto visit_fut = VisitAsyncGenerator<TestInt>(serial_readahead, + [&sum](TestInt test_int) -> Status { + sum += test_int.value; + return Status::OK(); + }); + ASSERT_FINISHES_OK(visit_fut); + ASSERT_EQ(EXPECTED_SUM, sum); Review comment: It's more to check for race conditions. The background thread filling callbacks and the visitor thread pulling the serial readahead both share a pointer to State. Both of these tests were pretty useful in exposing issues with the old AddCallback version. However, I added a reentrancy check in there for good measure and got rid of the sum stuff. ########## File path: cpp/src/arrow/util/queue_benchmark.cc ########## @@ -0,0 +1,87 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <algorithm> +#include <iterator> +#include <thread> +#include <vector> + +#include <benchmark/benchmark.h> + +#include "arrow/buffer.h" +#include "arrow/util/queue.h" + +namespace arrow { + +namespace util { + +static constexpr int64_t kSize = 100000; + +void throughput(benchmark::State& state) { Review comment: Done ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org