Skip to content

Commit 47145c4

Browse files
author
lexeyo
committed
fix postgres: fix min pool size adjustment with automatic connection limit tracking
commit_hash:517fbf595a4d509d3177ee8d0bfe0278c33e6b21
1 parent 7ccf37b commit 47145c4

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

postgresql/src/storages/postgres/detail/cluster_impl.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <storages/postgres/detail/cluster_impl.hpp>
22

33
#include <fmt/format.h>
4+
#include <algorithm>
45

56
#include <userver/dynamic_config/value.hpp>
67
#include <userver/engine/async.hpp>
@@ -379,21 +380,27 @@ void ClusterImpl::SetPoolSettings(const PoolSettings& new_settings) {
379380
{
380381
auto cluster = cluster_settings_.StartWrite();
381382

383+
cluster->original_min_pool_size = new_settings.min_size;
382384
cluster->pool_settings = new_settings;
383-
auto& settings = cluster->pool_settings;
384385
if (IsConnlimitModeAuto(*cluster)) {
385386
auto connlimit = connlimit_watchdog_.GetConnlimit();
386387
if (connlimit > 0) {
387-
settings.max_size = connlimit;
388-
if (settings.min_size > settings.max_size) {
389-
settings.min_size = settings.max_size;
390-
}
388+
AdjustPoolSettings(*cluster, connlimit);
391389
}
392390
}
393391

394392
cluster.Commit();
395393
}
396394

395+
PropagateSettingsToPools();
396+
}
397+
398+
void ClusterImpl::AdjustPoolSettings(ExtendedClusterSettings& cluster, std::size_t max_size) {
399+
cluster.pool_settings.max_size = max_size;
400+
cluster.pool_settings.min_size = std::min(cluster.original_min_pool_size, max_size);
401+
}
402+
403+
void ClusterImpl::PropagateSettingsToPools() {
397404
auto td = topology_data_.SharedLock();
398405
auto cluster_settings = cluster_settings_.Read();
399406
for (const auto& pool : td->host_pools) {
@@ -409,18 +416,19 @@ void ClusterImpl::SetTopologySettings(const TopologySettings& settings) {
409416
void ClusterImpl::OnConnlimitChanged() {
410417
auto max_size = connlimit_watchdog_.GetConnlimit();
411418
auto cluster = cluster_settings_.StartWrite();
419+
412420
if (!IsConnlimitModeAuto(*cluster)) {
413421
return;
414422
}
415423

416424
if (cluster->pool_settings.max_size == max_size) {
417425
return;
418426
}
419-
cluster->pool_settings.max_size = max_size;
427+
AdjustPoolSettings(*cluster, max_size);
428+
420429
cluster.Commit();
421430

422-
auto cluster_settings = cluster_settings_.Read();
423-
SetPoolSettings(cluster_settings->pool_settings);
431+
PropagateSettingsToPools();
424432
}
425433

426434
bool ClusterImpl::IsConnlimitModeAuto(const ClusterSettings& settings) {
@@ -478,6 +486,11 @@ void ClusterImpl::SetDsnList(const DsnList& dsn) {
478486
TESTPOINT("postgres-new-dsn-list", {});
479487
}
480488

489+
ClusterImpl::ExtendedClusterSettings::ExtendedClusterSettings(const ClusterSettings& settings)
490+
: ClusterSettings(settings),
491+
original_min_pool_size(settings.pool_settings.min_size)
492+
{}
493+
481494
} // namespace storages::postgres::detail
482495

483496
USERVER_NAMESPACE_END

postgresql/src/storages/postgres/detail/cluster_impl.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,21 @@ class ClusterImpl {
8080
void SetDsnList(const DsnList&);
8181

8282
private:
83+
struct ExtendedClusterSettings : public ClusterSettings {
84+
explicit ExtendedClusterSettings(const ClusterSettings& settings);
85+
// It keeps the original pool_settings.min_size from the ClusterSettings passed to the ClusterImpl instance.
86+
// While pool_settings.min_size could be automatically capped in OnConnlimitChanged
87+
std::size_t original_min_pool_size{kDefaultPoolMinSize};
88+
};
89+
8390
void OnConnlimitChanged();
8491

8592
bool IsConnlimitModeAuto(const ClusterSettings& settings);
8693

94+
void AdjustPoolSettings(ExtendedClusterSettings& cluster, std::size_t max_size);
95+
96+
void PropagateSettingsToPools();
97+
8798
using ConnectionPoolPtr = std::shared_ptr<ConnectionPool>;
8899

89100
ConnectionPoolPtr FindPool(ClusterHostTypeFlags);
@@ -95,7 +106,7 @@ class ClusterImpl {
95106

96107
void CreateTopology(const DsnList& dsns);
97108

98-
rcu::Variable<ClusterSettings> cluster_settings_;
109+
rcu::Variable<ExtendedClusterSettings> cluster_settings_;
99110
concurrent::Variable<TopologyData, engine::SharedMutex> topology_data_;
100111
clients::dns::Resolver* resolver_{};
101112
engine::TaskProcessor& bg_task_processor_;

0 commit comments

Comments
 (0)