($INBOX_DIR/description missing)
 help / color / mirror / Atom feed
From: Tobias Hagelborn <tobiasha@axis.com>
To: <bitbake-devel@lists.openembedded.org>
Subject: [PATCH v2] hashserv: Postgres adaptations for ignoring duplicate inserts
Date: Fri, 9 Feb 2024 10:14:43 +0100	[thread overview]
Message-ID: <20240209091443.2737892-1-tobiasha@axis.com> (raw)

Hash Equivalence server performs unconditional insert also of duplicate
hash entries. This causes excessive error log entries in Postgres.
Rather ignore the duplicate inserts.

The alternate behavior should be isolated to the postgres
engine type.

Signed-off-by: Tobias Hagelborn <tobiasha@axis.com>
---

Updated return codes based on rows updated according to review comments.

 lib/hashserv/sqlalchemy.py | 41 ++++++++++++++++++++++++++++----------
 1 file changed, 31 insertions(+), 10 deletions(-)

diff --git a/lib/hashserv/sqlalchemy.py b/lib/hashserv/sqlalchemy.py
index cee04bff..b17a1762 100644
--- a/lib/hashserv/sqlalchemy.py
+++ b/lib/hashserv/sqlalchemy.py
@@ -32,6 +32,7 @@ from sqlalchemy import (
 import sqlalchemy.engine
 from sqlalchemy.orm import declarative_base
 from sqlalchemy.exc import IntegrityError
+from sqlalchemy.dialects.postgresql import insert as postgres_insert
 
 Base = declarative_base()
 
@@ -287,16 +288,28 @@ class Database(object):
             return result.rowcount
 
     async def insert_unihash(self, method, taskhash, unihash):
-        statement = insert(UnihashesV2).values(
-            method=method,
-            taskhash=taskhash,
-            unihash=unihash,
-        )
+        # Postgres specific ignore on insert duplicate
+        if self.engine.name == 'postgresql':
+            statement = postgres_insert(UnihashesV2).values(
+                method=method,
+                taskhash=taskhash,
+                unihash=unihash,
+            )
+            statement = statement.on_conflict_do_nothing(
+                index_elements=("method", "taskhash")
+            )
+        else:
+            statement = insert(UnihashesV2).values(
+                method=method,
+                taskhash=taskhash,
+                unihash=unihash,
+            )
+
         self.logger.debug("%s", statement)
         try:
             async with self.db.begin():
-                await self.db.execute(statement)
-            return True
+                result = await self.db.execute(statement)
+                return result.rowcount != 0
         except IntegrityError:
             self.logger.debug(
                 "%s, %s, %s already in unihash database", method, taskhash, unihash
@@ -311,12 +324,20 @@ class Database(object):
         if "created" in data and not isinstance(data["created"], datetime):
             data["created"] = datetime.fromisoformat(data["created"])
 
-        statement = insert(OuthashesV2).values(**data)
+        # Postgres specific ignore on insert duplicate
+        if self.engine.name == 'postgresql':
+            statement = postgres_insert(OuthashesV2).values(**data)
+            statement = statement.on_conflict_do_nothing(
+                index_elements=("method", "taskhash", "outhash")
+            )
+        else:
+            statement = insert(OuthashesV2).values(**data)
+
         self.logger.debug("%s", statement)
         try:
             async with self.db.begin():
-                await self.db.execute(statement)
-            return True
+                result = await self.db.execute(statement)
+                return result.rowcount != 0
         except IntegrityError:
             self.logger.debug(
                 "%s, %s already in outhash database", data["method"], data["outhash"]
-- 
2.30.2



                 reply	other threads:[~2024-02-09  9:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240209091443.2737892-1-tobiasha@axis.com \
    --to=tobiasha@axis.com \
    --cc=bitbake-devel@lists.openembedded.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).