diff --git a/swh/storage/postgresql/storage.py b/swh/storage/postgresql/storage.py --- a/swh/storage/postgresql/storage.py +++ b/swh/storage/postgresql/storage.py @@ -219,24 +219,27 @@ with self.db() as db: with db.transaction() as cur: - missing = list( - self.content_missing( - map(Content.to_dict, contents), - key_hash="sha1_git", - db=db, - cur=cur, - ) - ) - contents = [c for c in contents if c.sha1_git in missing] - - self.journal_writer.content_add(contents) - self._content_add_metadata(db, cur, contents) + nb_added = self._content_add(contents, db, cur) return { - "content:add": len(contents), + "content:add": nb_added, "content:add:bytes": objstorage_summary["content:add:bytes"], } + def _content_add(self, contents: List[Content], db, cur) -> int: + """Called by `content_add`, and also used by `test_content_add_race`.""" + missing = list( + self.content_missing( + map(Content.to_dict, contents), key_hash="sha1_git", db=db, cur=cur, + ) + ) + contents = [c for c in contents if c.sha1_git in missing] + + self.journal_writer.content_add(contents) + self._content_add_metadata(db, cur, contents) + + return len(contents) + @timed @db_transaction() def content_update( diff --git a/swh/storage/tests/test_postgresql.py b/swh/storage/tests/test_postgresql.py --- a/swh/storage/tests/test_postgresql.py +++ b/swh/storage/tests/test_postgresql.py @@ -48,14 +48,14 @@ class TestStorageRaceConditions: @pytest.mark.xfail def test_content_add_race(self, swh_storage, sample_data): - content = sample_data.content + content = attr.evolve(sample_data.content, ctime=now()) results = queue.Queue() def thread(): try: with db_transaction(swh_storage) as (db, cur): - ret = swh_storage.content_add([content], db=db, cur=cur) + ret = swh_storage._content_add([content], db=db, cur=cur) results.put((threading.get_ident(), "data", ret)) except Exception as e: results.put((threading.get_ident(), "exc", e))