From 9ee7f5c6fea024a4da40e39d803a4b6e0fd61590 Mon Sep 17 00:00:00 2001 From: haali1 Date: Wed, 3 Aug 2016 14:50:09 -0700 Subject: [PATCH] Fix race condition in notification type inserts We are inserting notification type plugin names on monasca_notification start up. If another instance of monasca_notification services is started at the same time in another controller, then this will occur. Change-Id: Id7401bbcbdc9d6942baa49e3b335a12fe4366acf --- .../common/repositories/mysql/mysql_repo.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/monasca_notification/common/repositories/mysql/mysql_repo.py b/monasca_notification/common/repositories/mysql/mysql_repo.py index d9e857e..7a29d94 100644 --- a/monasca_notification/common/repositories/mysql/mysql_repo.py +++ b/monasca_notification/common/repositories/mysql/mysql_repo.py @@ -108,7 +108,17 @@ class MysqlRepo(BaseRepo): cur = self._mysql.cursor() cur.executemany(self._insert_notification_types_sql, notification_types) + except pymysql.IntegrityError as ignoredException: + # If multiple instances of the notification engine tries to write the + # same content at the same time, only one of them will succeed and others will + # get duplicate primary key, integrity error. We can safely ignore this error. + # This may happen only during the first start when the tables are empty. + code, mesg = ignoredException.args + if code == pymysql.constants.ER.DUP_ENTRY: + log.debug("Notification type exists in DB. Ignoring the exception {}".format(mesg)) + else: + raise exc.DatabaseException(ignoredException) except pymysql.Error as e: self._mysql = None log.exception("Couldn't insert notification types %s", e) - raise exc.DatabaseException(e) \ No newline at end of file + raise exc.DatabaseException(e)