Skip to content

Commit

Permalink
refactor all: remove userver_experiments_force_enabled
Browse files Browse the repository at this point in the history
commit_hash:d8d0c884ec514d41b20232f4d2657b3b667035e3
  • Loading branch information
Anton3 committed Nov 10, 2024
1 parent b4b12aa commit bb0f1be
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 32 deletions.
15 changes: 8 additions & 7 deletions core/include/userver/utils/impl/userver_experiments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,19 @@ namespace utils::impl {

class UserverExperiment final {
public:
// Setting 'userver_experiments_force_enabled: true' in the static config
// root results in batch-enabling the experiments created with
// 'force_enabling_allowed = true'.
explicit UserverExperiment(std::string name, bool force_enabling_allowed = false);
explicit UserverExperiment(std::string name);

UserverExperiment(UserverExperiment&&) = delete;
UserverExperiment& operator=(UserverExperiment&&) = delete;

bool IsEnabled() const noexcept { return enabled_; }
bool IsForceEnablingAllowed() const { return force_enabling_allowed_; }
const std::string& GetName() const { return name_; }

private:
friend struct UserverExperimentSetter;

std::string name_;
bool enabled_{false};
bool force_enabling_allowed_{false};
};

class InvalidUserverExperiments final : public std::runtime_error {
Expand All @@ -51,13 +46,19 @@ class UserverExperimentsScope final {
void Set(UserverExperiment& experiment, bool value) noexcept;

/// @throws InvalidUserverExperiments on name mismatch
void EnableOnly(const UserverExperimentSet& enabled_experiments, bool force_enable = false);
void EnableOnly(const UserverExperimentSet& enabled_experiments);

private:
const std::vector<utils::NotNull<UserverExperiment*>> old_enabled_;
};

// All userver experiments should be registered here to ensure that they can be globally enabled
// for all services, no matter whether a specific userver library is used in a given service.
extern UserverExperiment kJemallocBgThread;
extern UserverExperiment kCoroutineStackUsageMonitorExperiment;
extern UserverExperiment kServerSelectionTimeoutExperiment;
extern UserverExperiment kPgCcExperiment;
extern UserverExperiment kYdbDeadlinePropagationExperiment;

} // namespace utils::impl

Expand Down
8 changes: 6 additions & 2 deletions core/src/components/manager_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ ManagerConfig ParseFromAny(
) {
constexpr std::string_view kConfigVarsField = "config_vars";
constexpr std::string_view kManagerConfigField = "components_manager";
constexpr std::string_view kUserverExperimentsForceEnabledField = "userver_experiments_force_enabled";

formats::yaml::Value config_yaml;
try {
Expand Down Expand Up @@ -64,8 +63,13 @@ ManagerConfig ParseFromAny(

auto config =
yaml_config::YamlConfig(config_yaml, std::move(config_vars), yaml_config::YamlConfig::Mode::kEnvAndFileAllowed);
config.CheckObject();
for (const auto& [key, value] : Items(config)) {
if (key != kManagerConfigField && key != kConfigVarsField) {
throw std::runtime_error(fmt::format("Invalid config: extra key '{}' at the root level", key));
}
}
auto result = config[kManagerConfigField].As<ManagerConfig>();
result.experiments_force_enabled = config[kUserverExperimentsForceEnabledField].As<bool>(false);

return result;
}
Expand Down
1 change: 0 additions & 1 deletion core/src/components/manager_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ struct ManagerConfig {
std::string default_task_processor;
ValidationMode validate_components_configs{};
utils::impl::UserverExperimentSet enabled_experiments;
bool experiments_force_enabled{false};
bool mlock_debug_info{true};
bool disable_phdr_cache{false};
bool preheat_stacktrace_collector{true};
Expand Down
6 changes: 2 additions & 4 deletions core/src/components/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ class LogScope final {
logging::DefaultLoggerLevelScope level_scope_;
};

const utils::impl::UserverExperiment kJemallocBgThread{"jemalloc-bg-thread"};

void HandleJemallocSettings() {
static constexpr size_t kDefaultMaxBgThreads = 1;

Expand All @@ -82,7 +80,7 @@ void HandleJemallocSettings() {
LOG_WARNING() << "Failed to set max_background_threads to " << kDefaultMaxBgThreads;
}

if (kJemallocBgThread.IsEnabled()) {
if (utils::impl::kJemallocBgThread.IsEnabled()) {
utils::jemalloc::EnableBgThreads();
}
}
Expand Down Expand Up @@ -201,7 +199,7 @@ void DoRun(
std::optional<Manager> manager;

try {
experiments_scope.EnableOnly(manager_config.enabled_experiments, manager_config.experiments_force_enabled);
experiments_scope.EnableOnly(manager_config.enabled_experiments);

HandleJemallocSettings();
if (manager_config.preheat_stacktrace_collector) {
Expand Down
16 changes: 8 additions & 8 deletions core/src/utils/impl/userver_experiments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ auto GetEnabledUserverExperiments() {

} // namespace

UserverExperiment::UserverExperiment(std::string name, bool force_enabling_allowed)
: name_(name), force_enabling_allowed_(force_enabling_allowed) {
RegisterExperiment(*this);
}
UserverExperiment::UserverExperiment(std::string name) : name_(name) { RegisterExperiment(*this); }

UserverExperimentsScope::UserverExperimentsScope() : old_enabled_(GetEnabledUserverExperiments()) {}

Expand All @@ -69,7 +66,7 @@ void UserverExperimentsScope::Set(UserverExperiment& experiment, bool value) noe
}

// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
void UserverExperimentsScope::EnableOnly(const UserverExperimentSet& enabled_experiments, bool force_enable) {
void UserverExperimentsScope::EnableOnly(const UserverExperimentSet& enabled_experiments) {
utils::impl::AssertStaticRegistrationFinished();

const auto& exp_map = GetExperimentsInfo();
Expand All @@ -81,12 +78,15 @@ void UserverExperimentsScope::EnableOnly(const UserverExperimentSet& enabled_exp

for (const auto& [_, experiment] : exp_map) {
const bool enabled = enabled_experiments.count(experiment->GetName()) != 0;
const bool force_enabled = experiment->IsForceEnablingAllowed() && force_enable;
UserverExperimentSetter::Set(*experiment, enabled || force_enabled);
UserverExperimentSetter::Set(*experiment, enabled);
}
}

UserverExperiment kCoroutineStackUsageMonitorExperiment{"coro-stack-usage-monitor", true};
UserverExperiment kJemallocBgThread{"jemalloc-bg-thread"};
UserverExperiment kCoroutineStackUsageMonitorExperiment{"coro-stack-usage-monitor"};
UserverExperiment kServerSelectionTimeoutExperiment{"mongo-server-selection-timeout"};
UserverExperiment kPgCcExperiment{"pg-cc"};
UserverExperiment kYdbDeadlinePropagationExperiment{"ydb-deadline-propagation"};

} // namespace utils::impl

Expand Down
4 changes: 1 addition & 3 deletions mongo/src/storages/mongo/cdriver/pool_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ namespace {
}
}

utils::impl::UserverExperiment kServerSelectionTimeoutExperiment{"mongo-server-selection-timeout"};

const std::string kMaintenanceTaskName = "mongo_maintenance";
constexpr size_t kIdleConnectionDropRate = 1;

Expand Down Expand Up @@ -74,7 +72,7 @@ UriPtr MakeUri(const std::string& pool_id, const std::string& uri_string, const
mongoc_uri_set_option_as_int32(
uri.get(), MONGOC_URI_CONNECTTIMEOUTMS, CheckedDurationMs(config.conn_timeout, MONGOC_URI_CONNECTTIMEOUTMS)
);
if (kServerSelectionTimeoutExperiment.IsEnabled()) {
if (utils::impl::kServerSelectionTimeoutExperiment.IsEnabled()) {
mongoc_uri_set_option_as_int32(uri.get(), MONGOC_URI_SERVERSELECTIONTIMEOUTMS, 3000);
}
mongoc_uri_set_option_as_int32(
Expand Down
4 changes: 1 addition & 3 deletions postgresql/src/storages/postgres/detail/pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ namespace storages::postgres::detail {

namespace {

USERVER_NAMESPACE::utils::impl::UserverExperiment kCcExperiment("pg-cc");

constexpr std::chrono::seconds kRecentErrorPeriod{15};

// Part of max_pool that can be cancelled at once
Expand Down Expand Up @@ -114,7 +112,7 @@ ConnectionPool::ConnectionPool(
config_source,
[](const dynamic_config::Snapshot& config) { return config[kCcConfig]; }
) {
if (kCcExperiment.IsEnabled()) {
if (USERVER_NAMESPACE::utils::impl::kPgCcExperiment.IsEnabled()) {
cc_controller_.Start();
}
}
Expand Down
2 changes: 1 addition & 1 deletion testsuite/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
requests >= 2.28.0
websockets >= 11.0.3
websockets >= 11.0.3, < 13.0.0
# Not ready to migrate to 0.23.0+ because of
# https://github.com/pytest-dev/pytest-asyncio/issues/706
pytest_asyncio >= 0.21.2, < 0.22.0
Expand Down
4 changes: 1 addition & 3 deletions ydb/src/ydb/impl/request_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,14 @@ void PrepareSettings(
if (cc.get_session_timeout_ms) os.get_session_timeout_ms = cc.get_session_timeout_ms.value();
}

utils::impl::UserverExperiment kYdbDeadlinePropagationExperiment("ydb-deadline-propagation");

engine::Deadline GetDeadline(tracing::Span& span, const dynamic_config::Snapshot& config_snapshot) {
if (config_snapshot[impl::kDeadlinePropagationVersion] != impl::kDeadlinePropagationExperimentVersion) {
LOG_DEBUG() << "Wrong DP experiment version, config=" << config_snapshot[impl::kDeadlinePropagationVersion]
<< ", experiment=" << impl::kDeadlinePropagationExperimentVersion;
return {};
}

if (!kYdbDeadlinePropagationExperiment.IsEnabled()) {
if (!utils::impl::kYdbDeadlinePropagationExperiment.IsEnabled()) {
LOG_DEBUG() << "Deadline propagation is disabled via experiment";
return {};
}
Expand Down

0 comments on commit bb0f1be

Please sign in to comment.