error_injection: Re-use enter() code in inject() overloads

Most of inject() overloads check if the injection is enabled, then
optionally clear the one-shot one, then do the injection. Everything
but doing the injection is implemented in the enter() method, it's
perfectly worth re-using one.

Signed-off-by: Pavel Emelyanov <xemul@scylladb.com>

Closes scylladb/scylladb#21285
This commit is contained in:
Pavel Emelyanov
2024-10-25 15:43:17 +03:00
committed by Avi Kivity
parent 7610b907c6
commit b09bb6bc19

View File

@@ -363,27 +363,19 @@ public:
// \param f lambda to be run
[[gnu::always_inline]]
void inject(const std::string_view& name, handler_fun f) {
if (!is_enabled(name)) {
if (!enter(name)) {
return;
}
if (is_one_shot(name)) {
disable(name);
}
errinj_logger.debug("Triggering injection \"{}\"", name);
f();
}
// \brief Inject a sleep for milliseconds
[[gnu::always_inline]]
future<> inject(const std::string_view& name,
const std::chrono::milliseconds duration) {
if (!is_enabled(name)) {
future<> inject(const std::string_view& name, const std::chrono::milliseconds duration) {
if (!enter(name)) {
return make_ready_future<>();
}
if (is_one_shot(name)) {
disable(name);
}
errinj_logger.debug("Triggering sleep injection \"{}\" ({}ms)", name, duration.count());
return seastar::sleep(duration);
}
@@ -392,13 +384,9 @@ public:
template <typename Clock, typename Duration>
[[gnu::always_inline]]
future<> inject(const std::string_view& name, std::chrono::time_point<Clock, Duration> deadline) {
if (!is_enabled(name)) {
if (!enter(name)) {
return make_ready_future<>();
}
if (is_one_shot(name)) {
disable(name);
}
// Time left until deadline
auto duration = deadline - Clock::now();
@@ -412,10 +400,7 @@ public:
[[gnu::always_inline]]
std::invoke_result_t<Func> inject(const std::string_view& name, std::chrono::time_point<Clock, Duration> deadline,
Func&& func) {
if (is_enabled(name)) {
if (is_one_shot(name)) {
disable(name);
}
if (enter(name)) {
std::chrono::milliseconds duration = std::chrono::duration_cast<std::chrono::milliseconds>(deadline - Clock::now());
errinj_logger.debug("Triggering sleep injection \"{}\" ({}ms)", name, duration.count());
return seastar::sleep<Clock>(duration).then([func = std::move(func)] {
@@ -431,15 +416,11 @@ public:
requires std::is_invocable_r_v<std::exception_ptr, Func>
[[gnu::always_inline]]
future<>
inject(const std::string_view& name,
Func&& exception_factory) {
if (!is_enabled(name)) {
inject(const std::string_view& name, Func&& exception_factory) {
if (!enter(name)) {
return make_ready_future<>();
}
if (is_one_shot(name)) {
disable(name);
}
errinj_logger.debug("Triggering exception injection \"{}\"", name);
return make_exception_future<>(exception_factory());
}