Skip to content

Commit 9c1d268

Browse files
author
lexeyo
committed
feat postgres: new connections reservation strategy in postgres connections auto-limiter
Changes a number of reserved connection from 5 to 5% of the total limit capped to mimimum 2 and maximum 10 connections. commit_hash:c1407d39e0c870b796b90a69c356f9e6aa5bbec9
1 parent de1c82e commit 9c1d268

4 files changed

Lines changed: 43 additions & 3 deletions

File tree

core/include/userver/utils/impl/userver_experiments.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ extern UserverExperiment kPgCcExperiment;
6060
extern UserverExperiment kYdbDeadlinePropagationExperiment;
6161
extern UserverExperiment kWaitAllCheckedUpgradeExperiment;
6262
extern UserverExperiment kPgConnlimitWatchdogFallbackExperiment;
63+
extern UserverExperiment kPgConnlimitWatchdogReservationExperiment;
6364

6465
} // namespace utils::impl
6566

core/src/utils/impl/userver_experiments.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ UserverExperiment kPgCcExperiment{"pg-cc"};
9696
UserverExperiment kYdbDeadlinePropagationExperiment{"ydb-deadline-propagation"};
9797
UserverExperiment kWaitAllCheckedUpgradeExperiment{"wait-all-checked-upgrade"};
9898
UserverExperiment kPgConnlimitWatchdogFallbackExperiment{"pg-connlimit-watchdog-new-fallback"};
99+
UserverExperiment kPgConnlimitWatchdogReservationExperiment{"pg-connlimit-watchdog-new-reservation"};
99100

100101
} // namespace utils::impl
101102

postgresql/src/storages/postgres/connlimit_watchdog.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ namespace {
1313
constexpr CommandControl kCommandControl{std::chrono::seconds(2), std::chrono::seconds(2)};
1414
constexpr std::size_t kTestsuiteConnlimit = 100;
1515
constexpr std::size_t kReservedConn = 5;
16+
constexpr std::size_t kMinReservedConnections = 2;
17+
constexpr std::size_t kMaxReservedConnections = 10;
18+
constexpr double kReservedConnectionsPercentage = 0.05;
1619

1720
constexpr int kMaxStepsWithError = 3;
1821
constexpr std::size_t kFallbackConnlimit = 20;
@@ -27,8 +30,18 @@ std::size_t GetMaxConnections(Transaction& trx) {
2730
}
2831
std::size_t max_connections = std::min(max_server_connections, max_user_connections);
2932

30-
if (max_connections > kReservedConn) {
31-
max_connections -= kReservedConn;
33+
const auto reserved_connections =
34+
!USERVER_NAMESPACE::utils::impl::kPgConnlimitWatchdogReservationExperiment.IsEnabled()
35+
? kReservedConn
36+
: std::max(
37+
kMinReservedConnections,
38+
std::min(
39+
kMaxReservedConnections,
40+
static_cast<std::size_t>(max_connections * kReservedConnectionsPercentage)
41+
)
42+
);
43+
if (max_connections > reserved_connections) {
44+
max_connections -= reserved_connections;
3245
} else {
3346
max_connections = 1;
3447
}

postgresql/src/storages/postgres/tests/connlimit_watchdog_pgtest.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ pg::Transaction GetTransaction(pgd::ClusterImpl& cluster) {
6969
}
7070

7171
constexpr size_t kReservedConn = 5;
72-
constexpr size_t kTestsuiteConnlimit = 100 - kReservedConn;
72+
constexpr size_t kTestsuiteServerConnlimit = 100;
73+
constexpr size_t kTestsuiteConnlimit = kTestsuiteServerConnlimit - kReservedConn;
7374
constexpr size_t kFallbackConnlimit = 17;
7475
constexpr size_t kMaxStepsWithError = 3;
7576

@@ -127,6 +128,17 @@ class Watchdog : public PostgreSQLBase {
127128
utils::impl::UserverExperimentsScope scope_;
128129
};
129130

131+
class WatchdogWithNewConnectionsReservation : public Watchdog {
132+
public:
133+
void SetUp() override {
134+
Watchdog::SetUp();
135+
scope_.Set(utils::impl::kPgConnlimitWatchdogReservationExperiment, true);
136+
}
137+
138+
private:
139+
utils::impl::UserverExperimentsScope scope_;
140+
};
141+
130142
UTEST_F(Watchdog, Basic) {
131143
EXPECT_EQ(kTestsuiteConnlimit, DoStepV1());
132144

@@ -243,4 +255,17 @@ UTEST_F(Watchdog, FallbackConnlimit) {
243255
ASSERT_EQ(kFallbackConnlimit, watchdog.GetConnlimit());
244256
}
245257

258+
UTEST_F(WatchdogWithNewConnectionsReservation, CheckLimit) {
259+
constexpr auto
260+
kConnectionsLimit = kTestsuiteServerConnlimit - static_cast<std::size_t>(kTestsuiteServerConnlimit * 0.05);
261+
262+
EXPECT_EQ(kConnectionsLimit, DoStepV1());
263+
264+
// There are two hosts after 'StepV2'.
265+
EXPECT_EQ(kConnectionsLimit / 2, DoStepV2());
266+
267+
// There are two hosts after 'StepV2'.
268+
EXPECT_EQ(kConnectionsLimit / 2, DoStepV1());
269+
}
270+
246271
USERVER_NAMESPACE_END

0 commit comments

Comments
 (0)