Files
scylladb/test/boost/flush_queue_test.cc
Kefu Chai f9091066b7 treewide: replace boost::irange with std::views::iota where possible
when building scylla with the standard library from GCC-14.2, shipped by
fedora 41, we have following build failure:

```
/home/kefu/.local/bin/clang++ -DDEBUG -DDEBUG_LSA_SANITIZER -DFMT_SHARED -DSANITIZE -DSCYLLA_BUILD_MODE=debug -DSCYLLA_ENABLE_ERROR_INJECTION -DSEASTAR_API_LEVEL=7 -DSEASTAR_DEBUG -DSEASTAR_DEBUG_PROMISE -DSEASTAR_DEBUG_SHARED_PTR -DSEASTAR_DEFAULT_ALLOCATOR -DSEASTAR_LOGGER_COMPILE_TIME_FMT -DSEASTAR_LOGGER_TYPE_STDOUT -DSEASTAR_SCHEDULING_GROUPS_COUNT=16 -DSEASTAR_SHUFFLE_TASK_QUEUE -DSEASTAR_SSTRING -DSEASTAR_TYPE_ERASE_MORE -DXXH_PRIVATE_API -DCMAKE_INTDIR=\"Debug\" -I/home/kefu/dev/scylladb -I/home/kefu/dev/scylladb/build/gen -I/home/kefu/dev/scylladb/seastar/include -I/home/kefu/dev/scylladb/build/seastar/gen/include -I/home/kefu/dev/scylladb/build/seastar/gen/src -isystem /home/kefu/dev/scylladb/abseil -g -Og -g -gz -std=gnu++23 -fvisibility=hidden -Wall -Werror -Wextra -Wno-error=deprecated-declarations -Wimplicit-fallthrough -Wno-c++11-narrowing -Wno-deprecated-copy -Wno-mismatched-tags -Wno-missing-field-initializers -Wno-overloaded-virtual -Wno-unsupported-friend -Wno-unused-parameter -ffile-prefix-map=/home/kefu/dev/scylladb/build=. -march=x86-64-v3 -mpclmul -Xclang -fexperimental-assignment-tracking=disabled -Werror=unused-result -fstack-clash-protection -fsanitize=address -fsanitize=undefined -MD -MT CMakeFiles/scylla-main.dir/Debug/init.cc.o -MF CMakeFiles/scylla-main.dir/Debug/init.cc.o.d -o CMakeFiles/scylla-main.dir/Debug/init.cc.o -c /home/kefu/dev/scylladb/init.cc
In file included from /home/kefu/dev/scylladb/init.cc:12:
In file included from /home/kefu/dev/scylladb/db/config.hh:20:
In file included from /home/kefu/dev/scylladb/locator/abstract_replication_strategy.hh:26:
/home/kefu/dev/scylladb/locator/tablets.hh:410:30: error: unexpected type name 'size_t': expected expression
  410 |         return boost::irange<size_t>(0, tablet_count()) | boost::adaptors::transformed([] (size_t i) {
      |                              ^
/home/kefu/dev/scylladb/locator/tablets.hh:410:23: error: no member named 'irange' in namespace 'boost'
  410 |         return boost::irange<size_t>(0, tablet_count()) | boost::adaptors::transformed([] (size_t i) {
      |                ~~~~~~~^
/home/kefu/dev/scylladb/locator/tablets.hh:410:38: error: left operand of comma operator has no effect [-Werror,-Wunused-value]
  410 |         return boost::irange<size_t>(0, tablet_count()) | boost::adaptors::transformed([] (size_t i) {
      |                                      ^
3 errors generated.
[16/782] Building CXX object CMakeFiles/scylla-main.dir/Debug/keys.cc.o
[17/782] Building CXX object CMakeFiles/scylla-main.dir/Debug/counters.cc.o
[18/782] Building CXX object CMakeFiles/scylla-main.dir/Debug/partition_slice_builder.cc.o
[19/782] Building CXX object CMakeFiles/scylla-main.dir/Debug/mutation_query.cc.o
FAILED: CMakeFiles/scylla-main.dir/Debug/mutation_query.cc.o
/home/kefu/.local/bin/clang++ -DDEBUG -DDEBUG_LSA_SANITIZER -DFMT_SHARED -DSANITIZE -DSCYLLA_BUILD_MODE=debug -DSCYLLA_ENABLE_ERROR_INJECTION -DSEASTAR_API_LEVEL=7 -DSEASTAR_DEBUG -DSEASTAR_DEBUG_PROMISE -DSEASTAR_DEBUG_SHARED_PTR -DSEASTAR_DEFAULT_ALLOCATOR -DSEASTAR_LOGGER_COMPILE_TIME_FMT -DSEASTAR_LOGGER_TYPE_STDOUT -DSEASTAR_SCHEDULING_GROUPS_COUNT=16 -DSEASTAR_SHUFFLE_TASK_QUEUE -DSEASTAR_SSTRING -DSEASTAR_TYPE_ERASE_MORE -DXXH_PRIVATE_API -DCMAKE_INTDIR=\"Debug\" -I/home/kefu/dev/scylladb -I/home/kefu/dev/scylladb/build/gen -I/home/kefu/dev/scylladb/seastar/include -I/home/kefu/dev/scylladb/build/seastar/gen/include -I/home/kefu/dev/scylladb/build/seastar/gen/src -isystem /home/kefu/dev/scylladb/abseil -g -Og -g -gz -std=gnu++23 -fvisibility=hidden -Wall -Werror -Wextra -Wno-error=deprecated-declarations -Wimplicit-fallthrough -Wno-c++11-narrowing -Wno-deprecated-copy -Wno-mismatched-tags -Wno-missing-field-initializers -Wno-overloaded-virtual -Wno-unsupported-friend -Wno-unused-parameter -ffile-prefix-map=/home/kefu/dev/scylladb/build=. -march=x86-64-v3 -mpclmul -Xclang -fexperimental-assignment-tracking=disabled -Werror=unused-result -fstack-clash-protection -fsanitize=address -fsanitize=undefined -MD -MT CMakeFiles/scylla-main.dir/Debug/mutation_query.cc.o -MF CMakeFiles/scylla-main.dir/Debug/mutation_query.cc.o.d -o CMakeFiles/scylla-main.dir/Debug/mutation_query.cc.o -c /home/kefu/dev/scylladb/mutation_query.cc
In file included from /home/kefu/dev/scylladb/mutation_query.cc:12:
In file included from /home/kefu/dev/scylladb/schema/schema_registry.hh:17:
In file included from /home/kefu/dev/scylladb/replica/database.hh:11:
In file included from /home/kefu/dev/scylladb/locator/abstract_replication_strategy.hh:26:
/home/kefu/dev/scylladb/locator/tablets.hh:410:30: error: unexpected type name 'size_t': expected expression
  410 |         return boost::irange<size_t>(0, tablet_count()) | boost::adaptors::transformed([] (size_t i) {
      |                              ^
/home/kefu/dev/scylladb/locator/tablets.hh:410:23: error: no member named 'irange' in namespace 'boost'
  410 |         return boost::irange<size_t>(0, tablet_count()) | boost::adaptors::transformed([] (size_t i) {
      |                ~~~~~~~^
/home/kefu/dev/scylladb/locator/tablets.hh:410:38: error: left operand of comma operator has no effect [-Werror,-Wunused-value]
  410 |         return boost::irange<size_t>(0, tablet_count()) | boost::adaptors::transformed([] (size_t i) {
      |                                      ^
In file included from /home/kefu/dev/scylladb/mutation_query.cc:12:
In file included from /home/kefu/dev/scylladb/schema/schema_registry.hh:17:
In file included from /home/kefu/dev/scylladb/replica/database.hh:37:
In file included from /home/kefu/dev/scylladb/db/snapshot-ctl.hh:20:
/home/kefu/dev/scylladb/tasks/task_manager.hh:403:54: error: no member named 'irange' in namespace 'boost'
  403 |         co_await coroutine::parallel_for_each(boost::irange(0u, smp::count), [&tm, id, &res, &func] (unsigned shard) -> future<> {
      |                                               ~~~~~~~^
4 errors generated.
```

so let's take the opportunity to switch from `boost::irange` to
`std::views::iota`.

in this change, we:

- switch from boost::irange to std::views::iota for better standard library compatibility
- retain boost::irange where step parameter is used, as std::views::iota doesn't support it
- this change partially modernizes our range usage while maintaining
- existing functionality

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>

Closes scylladb/scylladb#20924
2024-10-03 10:33:33 +03:00

195 lines
6.3 KiB
C++

/*
* Copyright 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <random>
#include <bitset>
#include <ranges>
#include <boost/test/unit_test.hpp>
#include <boost/range/irange.hpp>
#include <seastar/core/semaphore.hh>
#include <seastar/core/seastar.hh>
#include <seastar/core/thread.hh>
#include <seastar/testing/random.hh>
#include "seastarx.hh"
#include "test/lib/scylla_test_case.hh"
#include <seastar/core/print.hh>
#include "utils/flush_queue.hh"
SEASTAR_TEST_CASE(test_queue_ordering_random_ops) {
struct env {
env(size_t n) : promises(n) {}
utils::flush_queue<int> queue;
std::vector<promise<>> promises;
std::vector<int> result;
};
auto r = std::views::iota(0, 100);
return do_for_each(r, [](int) {
constexpr size_t num_ops = 1000;
auto e = make_lw_shared<env>(num_ops);
int i = 0;
for (auto& p : e->promises) {
// FIXME: discarded future.
(void)e->queue.run_with_ordered_post_op(i, [&p, i] {
return p.get_future().then([i] {
return make_ready_future<int>(i);
});
}, [e](int i) {
e->result.emplace_back(i);
});
++i;
}
auto res = e->queue.wait_for_pending();
std::uniform_int_distribution<size_t> dist(0, num_ops - 1);
std::bitset<num_ops> set;
while (!set.all()) {
auto& e1 = seastar::testing::local_random_engine;
size_t i = dist(e1);
if (!set.test(i)) {
set[i] = true;
e->promises[i].set_value();
}
}
return res.then([e] {
BOOST_CHECK_EQUAL(e->result.size(), e->promises.size());
BOOST_REQUIRE(std::is_sorted(e->result.begin(), e->result.end()));
}).finally([e] {
return e->queue.close().finally([e] { });
});
});
}
SEASTAR_TEST_CASE(test_queue_ordering_multi_ops) {
struct env {
env() : sem(0) {}
utils::flush_queue<int> queue;
std::vector<int> result;
semaphore sem;
size_t n = 0;
};
auto r = std::views::iota(0, 100);
return do_for_each(r, [](int) {
constexpr size_t num_ops = 1000;
auto e = make_lw_shared<env>();
std::uniform_int_distribution<size_t> dist(0, num_ops - 1);
for (size_t k = 0; k < num_ops*10; ++k) {
auto& e1 = seastar::testing::local_random_engine;
int i = dist(e1);
if (e->queue.has_operation(i) || (!e->queue.empty() && e->queue.highest_key() < i)) {
// FIXME: discarded future.
(void)e->queue.run_with_ordered_post_op(i, [e, i] {
return e->sem.wait().then([i] {
return make_ready_future<int>(i);
});
}, [e](int i) {
e->result.emplace_back(i);
});
++e->n;
}
}
auto res = e->queue.wait_for_pending();
e->sem.signal(e->n);
return res.then([e] {
BOOST_CHECK_EQUAL(e->result.size(), e->n);
BOOST_REQUIRE(std::is_sorted(e->result.begin(), e->result.end()));
}).finally([e] {
return e->queue.close().finally([e] { });
});
});
}
static future<> test_propagation(bool propagate,
noncopyable_function<future<> ()> func,
noncopyable_function<future<> ()> post,
noncopyable_function<void ()> thn,
bool want_except_in_run, bool want_except_in_wait) {
auto queue = ::make_shared<utils::flush_queue<int>>(propagate);
auto sem = ::make_shared<semaphore>(0);
auto xr = ::make_shared<bool>(false);
auto xw = ::make_shared<bool>(false);
auto f1 = queue->run_with_ordered_post_op(0, [sem, func = std::move(func)]() mutable {
return sem->wait().then(std::move(func));
}, std::move(post)).handle_exception([xr](auto p) {
*xr = true;
}).discard_result();
auto f2 = queue->wait_for_pending(0).then(std::move(thn)).handle_exception([xw](auto p) {
*xw = true;
}).discard_result();
sem->signal();
return seastar::when_all_succeed(std::move(f1), std::move(f2)).finally([sem, queue, want_except_in_run, want_except_in_wait, xr, xw] {
BOOST_CHECK_EQUAL(want_except_in_run, *xr);
BOOST_CHECK_EQUAL(want_except_in_wait, *xw);
}).discard_result().finally([queue] {
return queue->close().finally([queue] { });
});
}
SEASTAR_TEST_CASE(test_propagate_exception_in_op) {
return test_propagation(true, // propagate exception to waiter
[] { return make_exception_future(std::runtime_error("hej")); }, // ex in op
[] { BOOST_FAIL("should not reach (1)"); return make_ready_future(); }, // should not reach post
[] { BOOST_FAIL("should not reach (2)"); }, // should not reach waiter "then"
true,
true
);
}
SEASTAR_TEST_CASE(test_propagate_exception_in_post) {
return test_propagation(true, // propagate exception to waiter
[] { return make_ready_future(); }, // ok func
[] { return make_exception_future(std::runtime_error("hej")); }, // ex in post
[] { BOOST_FAIL("should not reach"); }, // should not reach waiter "then"
true,
true
);
}
SEASTAR_TEST_CASE(test_no_propagate_exception_in_op) {
return test_propagation(false, // do not propagate exception to waiter
[] { return make_exception_future(std::runtime_error("hej")); }, // ex in op
[] { BOOST_FAIL("should not reach"); return make_ready_future(); }, // should not reach post
[] {}, // should reach waiter "then"
true,
false
);
}
SEASTAR_TEST_CASE(test_no_propagate_exception_in_post) {
return test_propagation(false, // do not propagate exception to waiter
[] { return make_ready_future(); }, // ok func
[] { return make_exception_future(std::runtime_error("hej")); }, // ex in post
[] {}, // should reach waiter "then"
true,
false
);
}