Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Allow setting transaction limit for db connections #10440

Merged
merged 5 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions synapse/config/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
# 'name' gives the database engine to use: either 'sqlite3' (for SQLite) or
# 'psycopg2' (for PostgreSQL).
#
# 'txn_limit' gives the maximum number of transactions to run per connection
# before reconnecting
hifi marked this conversation as resolved.
Show resolved Hide resolved
#
# 'args' gives options which are passed through to the database engine,
# except for options starting 'cp_', which are used to configure the Twisted
# connection pool. For a reference to valid arguments, see:
Expand All @@ -53,6 +56,7 @@
#
#database:
# name: psycopg2
# txn_limit: 10000
# args:
# user: synapse_user
# password: secretpassword
Expand Down
18 changes: 18 additions & 0 deletions synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ def __init__(
self._previous_txn_total_time = 0.0
self._current_txn_total_time = 0.0
self._previous_loop_ts = 0.0
self._txn_counters: Dict[int, int] = {}
hifi marked this conversation as resolved.
Show resolved Hide resolved
self._txn_limit = database_config.config.get("txn_limit", 0)
hifi marked this conversation as resolved.
Show resolved Hide resolved

# TODO(paul): These can eventually be removed once the metrics code
# is running in mainline, and we have some nice monitoring frontends
Expand Down Expand Up @@ -750,10 +752,26 @@ def inner_func(conn, *args, **kwargs):
sql_scheduling_timer.observe(sched_duration_sec)
context.add_database_scheduled(sched_duration_sec)

if self._txn_limit > 0:
tid = self._db_pool.threadID()
if tid not in self._txn_counters:
self._txn_counters[tid] = 0
self._txn_counters[tid] += 1

if self._txn_counters[tid] > self._txn_limit:
logger.debug(
"Reconnecting database connection over transaction limit"
)
conn.reconnect()
opentracing.log_kv({"message": "reconnected"})
hifi marked this conversation as resolved.
Show resolved Hide resolved
self._txn_counters[tid] = 0
hifi marked this conversation as resolved.
Show resolved Hide resolved

if self.engine.is_connection_closed(conn):
logger.debug("Reconnecting closed database connection")
conn.reconnect()
opentracing.log_kv({"message": "reconnected"})
if self._txn_limit > 0:
self._txn_counters[tid] = 0
hifi marked this conversation as resolved.
Show resolved Hide resolved

try:
if db_autocommit:
Expand Down