Skip to content

Commit

Permalink
feat postgresql: add more logs info when drop connection
Browse files Browse the repository at this point in the history
add more info when drop connection
7617f7f1a1ab11c1fa8f545c2887cd6b727e1a5c
  • Loading branch information
ArkadyRudenko committed Jun 21, 2024
1 parent ea75a90 commit 5a38a1d
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions postgresql/src/storages/postgres/detail/pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <userver/storages/postgres/exceptions.hpp>
#include <userver/testsuite/testpoint.hpp>
#include <userver/utils/assert.hpp>
#include <userver/utils/async.hpp>
#include <userver/utils/impl/userver_experiments.hpp>

USERVER_NAMESPACE_BEGIN
Expand Down Expand Up @@ -62,6 +63,12 @@ class Stopwatch {
SteadyClock::time_point start_;
};

auto MakeLogExtraFromConnectionStats(const InstanceStatistics& stats) {
return logging::LogExtra{{{"pg_conn_active", stats.connection.active},
{"pg_conn_open", stats.connection.open_total},
{"pg_conn_max", stats.connection.maximum}}};
}

} // namespace

class ConnectionPool::EmplaceEnabler {};
Expand Down Expand Up @@ -275,8 +282,8 @@ void ConnectionPool::Release(Connection* connection) {
} else {
// Connection cleanup is done asynchronously while returning control to
// the user
close_task_storage_.Detach(engine::CriticalAsyncNoSpan(
[this, connection, dec_cnt = std::move(dg)] {
close_task_storage_.Detach(USERVER_NAMESPACE::utils::CriticalAsync(
"clear_conn_after_cancel", [this, connection, dec_cnt = std::move(dg)] {
LOG_LIMITED_WARNING()
<< "Released connection in busy state. Trying to clean up...";
TESTPOINT("pg_cleanup", formats::json::Value{});
Expand Down Expand Up @@ -405,7 +412,7 @@ bool ConnectionPool::DoConnect(engine::SemaphoreLock size_lock) {
engine::SemaphoreLock connecting_lock{connecting_semaphore_,
kConnectingTimeout};
if (!connecting_lock) {
LOG_LIMITED_WARNING() << "Pool has too many establishing connections";
LOG_WARNING() << "Pool has too many establishing connections";
return false;
}
const uint32_t conn_id = ++stats_.connection.open_total;
Expand Down Expand Up @@ -433,7 +440,7 @@ bool ConnectionPool::DoConnect(engine::SemaphoreLock size_lock) {
} catch (const Error& ex) {
++stats_.connection.error_total;
++stats_.connection.drop_total;
LOG_LIMITED_WARNING() << "Connection creation failed with error: " << ex;
LOG_WARNING() << "Connection creation failed with error: " << ex;
throw;
}
LOG_TRACE() << "PostgreSQL connection created";
Expand Down Expand Up @@ -495,8 +502,7 @@ void ConnectionPool::Push(Connection* connection) {
conn_available_.NotifyOne();
} else {
// TODO Reflect this as a statistics error
LOG_LIMITED_WARNING()
<< "Couldn't push connection back to the pool. Deleting...";
LOG_WARNING() << "Couldn't push connection back to the pool. Deleting...";
DeleteConnection(connection);
}
}
Expand Down Expand Up @@ -551,7 +557,11 @@ Connection* ConnectionPool::Pop(engine::Deadline deadline) {
}

++stats_.pool_exhaust_errors;
throw PoolError("No available connections found", db_name_);
throw PoolError(
fmt::format("No available connections found. Active {}. Open {}. Max {}",
stats_.connection.active, stats_.connection.open_total,
stats_.connection.maximum),
db_name_);
}

void ConnectionPool::Clear() {
Expand Down Expand Up @@ -591,7 +601,8 @@ void ConnectionPool::CleanupConnection(Connection* connection) {
} catch (const std::exception& e) {
LOG_WARNING() << "Exception while cleaning up a dirty connection: " << e;
}
LOG_WARNING() << "Failed to cleanup a dirty connection, deleting...";
LOG_WARNING() << "Failed to cleanup a dirty connection, deleting..."
<< MakeLogExtraFromConnectionStats(stats_);
++stats_.connection.error_total;
DeleteConnection(connection);
}
Expand All @@ -603,17 +614,20 @@ void ConnectionPool::DeleteConnection(Connection* connection) {

void ConnectionPool::DeleteBrokenConnection(Connection* connection) {
++stats_.connection.error_total;
LOG_LIMITED_WARNING() << "Released connection in closed state. Deleting...";
LOG_WARNING() << "Released connection in closed state. Deleting..."
<< MakeLogExtraFromConnectionStats(stats_);
DeleteConnection(connection);
}

void ConnectionPool::DropExpiredConnection(Connection* connection) {
LOG_LIMITED_INFO() << "Dropping expired connection";
LOG_INFO() << "Dropping expired connection"
<< MakeLogExtraFromConnectionStats(stats_);
DeleteConnection(connection);
}

void ConnectionPool::DropOutdatedConnection(Connection* connection) {
LOG_LIMITED_INFO() << "Dropping connection with outdated settings";
LOG_INFO() << "Dropping connection with outdated settings"
<< MakeLogExtraFromConnectionStats(stats_);
DeleteConnection(connection);
}

Expand Down

0 comments on commit 5a38a1d

Please sign in to comment.