Do retries when creating table for pull queue in PG
@@ -6,6 +6,7 @@ import re
import sys
import uuid
+from appscale.common import retrying
from appscale.common.unpackaged import APPSCALE_PYTHON_APPSERVER
from appscale.datastore.cassandra_env.retry_policies import (
BASIC_RETRIES,
@@ -255,15 +256,14 @@ class PostgresPullQueue(Queue):
app: A string containing the application ID.
pg_connection: A psycopg2 connection to PostgreSQL.
"""
+ from psycopg2 import IntegrityError # Import psycopg2 lazily
super(PostgresPullQueue, self).__init__(queue_info, app)
self.pg_connection = pg_connection
- self.ensure_tables_created()
- @property
- def tasks_table_name(self):
- return 'pullqueue-{}'.format(self.name)
-
- def ensure_tables_created(self):
+ # When multiple TQ servers are notified by ZK about new queue
+ # they sometimes get IntegrityError despite 'IF NOT EXISTS'
+ @retrying.retry(max_retries=5, retry_on_exception=IntegrityError)
+ def ensure_tables_created():
try:
self.pg_connection.cursor().execute(
'CREATE TABLE IF NOT EXISTS "{table_name}" ('
@@ -290,6 +290,12 @@ class PostgresPullQueue(Queue):
self.pg_connection.rollback()
raise
+ ensure_tables_created()
+
+ @property
+ def tasks_table_name(self):
+ return 'pullqueue-{}'.format(self.name)
+
def add_task(self, task):
""" Adds a task to the queue.
