/* * Copyright (C) 2019-present ScyllaDB * * Modified by ScyllaDB */ /* * SPDX-License-Identifier: AGPL-3.0-or-later */ #include "error_injection_fcts.hh" #include "utils/error_injection.hh" #include "types/list.hh" #include namespace cql3 { namespace functions { namespace error_injection { namespace { template class failure_injection_function_for : public failure_injection_function { Func _func; public: failure_injection_function_for(sstring name, data_type return_type, const std::vector arg_types, Func&& func) : failure_injection_function(std::move(name), std::move(return_type), std::move(arg_types)) , _func(std::forward(func)) {} bool is_pure() const override { return Pure; } bytes_opt execute(std::span parameters) override { return _func(parameters); } }; template shared_ptr make_failure_injection_function(sstring name, data_type return_type, std::vector args_type, Func&& func) { return ::make_shared>(std::move(name), std::move(return_type), std::move(args_type), std::forward(func)); } } // anonymous namespace shared_ptr make_enable_injection_function() { return make_failure_injection_function("enable_injection", empty_type, { ascii_type, ascii_type }, [] (std::span parameters) { sstring injection_name = ascii_type->get_string(parameters[0].value()); const bool one_shot = ascii_type->get_string(parameters[1].value()) == "true"; smp::invoke_on_all([injection_name, one_shot] () mutable { utils::get_local_injector().enable(injection_name, one_shot); }).get(); return std::nullopt; }); } shared_ptr make_disable_injection_function() { return make_failure_injection_function("disable_injection", empty_type, { ascii_type }, [] (std::span parameters) { sstring injection_name = ascii_type->get_string(parameters[0].value()); smp::invoke_on_all([injection_name] () mutable { utils::get_local_injector().disable(injection_name); }).get(); return std::nullopt; }); } shared_ptr make_enabled_injections_function() { const auto list_type_inst = list_type_impl::get_instance(ascii_type, false); return make_failure_injection_function("enabled_injections", list_type_inst, {}, [list_type_inst] (std::span) -> bytes { return seastar::map_reduce(smp::all_cpus(), [] (unsigned) { return make_ready_future>(utils::get_local_injector().enabled_injections()); }, std::vector(), [](std::vector a, std::vector&& b) -> std::vector { for (auto&& x : b) { if (a.end() == std::find(a.begin(), a.end(), x)) { a.push_back(data_value(std::move(x))); } } return a; }).then([list_type_inst](std::vector const& active_injections) { auto list_val = make_list_value(list_type_inst, active_injections); return list_type_inst->decompose(list_val); }).get(); }); } } // namespace error_injection } // namespace functions } // namespace cql3