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] Eliminating duplicate key constraint violations #3712

Merged
merged 1 commit into from
Sep 20, 2023
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
36 changes: 32 additions & 4 deletions web/server/codechecker_server/api/mass_store_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,26 @@ def add_file_record(
return file_record.id

try:
file_record = File(file_path, content_hash, None, None)
session.add(file_record)
# Parallel storage of runs containing common file paths results a
# "duplicate key violation" error. This is handled by CodeChecker, so
# practically it causes no problem. The INSERT command of the second
# transaction will be thrown away. However, some DB systems are
# supporting "ON CONFLICT DO NOTHING" clause in INSERT statement which
# solves the same issue gracefully.
# TODO: "ON CONFLICT DO NOTHING" feature is available for SQLite engine
# too in SQLAlchemy 1.4.
if session.bind.dialect.name == 'postgresql':
insert_stmt = sqlalchemy.dialects.postgresql.insert(File).values(
filepath=file_path,
filename=os.path.basename(file_path),
content_hash=content_hash).on_conflict_do_nothing(
index_elements=['filepath', 'content_hash'])
file_id = session.execute(insert_stmt).inserted_primary_key[0]
session.commit()
return file_id
else:
file_record = File(file_path, content_hash, None, None)
session.add(file_record)
session.commit()
except sqlalchemy.exc.IntegrityError as ex:
LOG.error(ex)
Expand Down Expand Up @@ -537,9 +555,19 @@ def __add_file_content(
compressed_content = zlib.compress(
source_file_content, zlib.Z_BEST_COMPRESSION)

fc = FileContent(content_hash, compressed_content, None)
if session.bind.dialect.name == 'postgresql':
insert_stmt = sqlalchemy.dialects.postgresql \
.insert(FileContent).values(
content_hash=content_hash,
content=compressed_content,
blame_info=None).on_conflict_do_nothing(
index_elements=['content_hash'])

session.execute(insert_stmt)
else:
fc = FileContent(content_hash, compressed_content, None)
session.add(fc)

session.add(fc)
session.commit()
except sqlalchemy.exc.IntegrityError:
# Other transaction moght have added the same content in
Expand Down