Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent idle transactions #371

Merged
merged 10 commits into from
Aug 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion irrd/server/whois/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def run(self, keep_running=True) -> None:

try:
self.preloader = Preloader()
self.database_handler = DatabaseHandler()
self.database_handler = DatabaseHandler(readonly=True)
except Exception as e:
logger.error(f'Whois worker failed to initialise preloader or database,'
f'unable to start, traceback follows: {e}', exc_info=e)
Expand Down
25 changes: 18 additions & 7 deletions irrd/storage/database_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,21 @@ class DatabaseHandler:
# The ROA insert buffer is a list of dicts with columm names and their values.
_roa_insert_buffer: List[Dict[str, Union[str, int]]]

def __init__(self):
def __init__(self, readonly=False):
"""
Create a new database handler.

If readonly is True, this instance will expect read queries only.
No transaction will be started, all queries will use autocommit.
"""
self.journaling_enabled = True
self.readonly = readonly
self.journaling_enabled = not readonly
self._connection = get_engine().connect()
self._start_transaction()
self.preloader = Preloader(enable_queries=False)
if self.readonly:
self._connection.execution_options(isolation_level="AUTOCOMMIT")
else:
self._start_transaction()
self.preloader = Preloader(enable_queries=False)

def refresh_connection(self) -> None:
"""
Expand All @@ -58,15 +64,19 @@ def refresh_connection(self) -> None:
SQL server has restarted.
"""
try:
self.rollback(start_transaction=False)
if not self.readonly:
self.rollback(start_transaction=False)
except Exception: # pragma: no cover
pass
try:
self.close()
except Exception: # pragma: no cover
pass
self._connection = get_engine().connect()
self._start_transaction()
if self.readonly:
self._connection.execution_options(isolation_level="AUTOCOMMIT")
else:
self._start_transaction()

def _start_transaction(self) -> None:
"""Start a fresh transaction."""
Expand Down Expand Up @@ -115,7 +125,8 @@ def rollback(self, start_transaction=True) -> None:
def execute_query(self, query: Union[BaseRPSLObjectDatabaseQuery, DatabaseStatusQuery, RPSLDatabaseObjectStatisticsQuery, ROADatabaseObjectQuery]) -> Iterator[Dict[str, Any]]:
"""Execute an RPSLDatabaseQuery within the current transaction."""
# To be able to query objects that were just created, flush the buffer.
self._flush_rpsl_object_writing_buffer()
if not self.readonly:
self._flush_rpsl_object_writing_buffer()
statement = query.finalise_statement()
result = self._connection.execute(statement)
for row in result.fetchall():
Expand Down
2 changes: 1 addition & 1 deletion irrd/storage/preload.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def update(self, mock_database_handler=None) -> None:

if not mock_database_handler: # pragma: no cover
from .database_handler import DatabaseHandler
dh = DatabaseHandler()
dh = DatabaseHandler(readonly=True)
else:
dh = mock_database_handler

Expand Down
6 changes: 6 additions & 0 deletions irrd/storage/tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ def test_object_writing_and_status_checking(self, monkeypatch, irrd_database):
assert len(self.dh._rpsl_upsert_buffer) == 1
self.dh.upsert_rpsl_object(rpsl_obj_ignored, JournalEntryOrigin.auth_change)
assert len(self.dh._rpsl_upsert_buffer) == 1
# Flush the buffer to make sure the INSERT is issued but then rolled back
self.dh._flush_rpsl_object_writing_buffer()
self.dh.rollback()

statistics = list(self.dh.execute_query(RPSLDatabaseObjectStatisticsQuery()))
Expand Down Expand Up @@ -637,6 +639,10 @@ def test_more_less_specific_filters(self, irrd_database, database_handler_with_r
self.dh.upsert_rpsl_object(rpsl_route_more_specific_26, JournalEntryOrigin.auth_change)
self.dh.commit()

self.dh.close()
self.dh = DatabaseHandler(readonly=True)
self.dh.refresh_connection()

q = RPSLDatabaseQuery().ip_more_specific(IP('192.0.2.0/24'))
rpsl_pks = [r['rpsl_pk'] for r in self.dh.execute_query(q)]
assert len(rpsl_pks) == 3, f'Failed query: {q}'
Expand Down