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

[db] Garbage collection of analysis_info timeout #3775

Merged
merged 1 commit into from
Nov 8, 2022
Merged
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
41 changes: 27 additions & 14 deletions web/server/codechecker_server/database/db_cleanup.py
Original file line number Diff line number Diff line change
@@ -197,24 +197,37 @@ def upgrade_severity_levels(session_maker, checker_labels):

def remove_unused_analysis_info(session_maker):
""" Remove unused analysis information from the database. """
# Analysis info deletion is a relatively slow operation due to database
# cascades. Removing files in smaller chunks prevents reaching a potential
# database statement timeout. This hard-coded value is a safe choice
# according to some measurements.
CHUNK_SIZE = 500
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this batch size of 500 will completely resolve all database timeout during cleanup? If not maybe we should fall back to smaller size?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it seems to fix the timeout issue. Of course it depends on the database manager settings, but we are aiming 2min timeout threshold and this looks a proper choice for that. I agree that chunking is not an elegant solution, but we used such hard-coded values at other parts of the code too. In the long terms we should review our database schema and find some possibilities for optimizations.


LOG.debug("Garbage collection of dangling analysis info started...")

with DBSession(session_maker) as session:
try:
run_history_analysis_info = session \
.query(RunHistoryAnalysisInfo.c.analysis_info_id.distinct()) \
.subquery()

report_analysis_info = session \
.query(ReportAnalysisInfo.c.analysis_info_id.distinct()) \
.subquery()

session.query(AnalysisInfo) \
.filter(AnalysisInfo.id.notin_(run_history_analysis_info),
AnalysisInfo.id.notin_(report_analysis_info)) \
.delete(synchronize_session=False)

session.commit()
to_delete = session.query(AnalysisInfo.id) \
.join(
RunHistoryAnalysisInfo,
RunHistoryAnalysisInfo.c.analysis_info_id ==
AnalysisInfo.id,
isouter=True) \
.join(
ReportAnalysisInfo,
ReportAnalysisInfo.c.analysis_info_id == AnalysisInfo.id,
isouter=True) \
.filter(
RunHistoryAnalysisInfo.c.analysis_info_id.is_(None),
ReportAnalysisInfo.c.analysis_info_id.is_(None))

to_delete = map(lambda x: x[0], to_delete)

for chunk in util.chunks(to_delete, CHUNK_SIZE):
session.query(AnalysisInfo) \
.filter(AnalysisInfo.id.in_(chunk)) \
.delete(synchronize_session=False)
session.commit()

LOG.debug("Garbage collection of dangling analysis info finished.")
except (sqlalchemy.exc.OperationalError,