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
This commit is contained in:
haali1
2016-08-03 14:50:09 -07:00
committed by Haneef Ali
parent be6fb21e19
commit 9ee7f5c6fe

View File

@@ -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)
raise exc.DatabaseException(e)