Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
Add autocommit and retry to read only sql sessions for CAI temp table. (
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoying committed Mar 27, 2019
1 parent e14b30a commit 53ef39e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions google/cloud/forseti/services/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ def create_scoped_readonly_session(engine):
Returns:
object: Scoped session maker.
"""
session = sessionmaker(bind=engine, autocommit=False, autoflush=False)()
# Set session autocommit to true to ensure each query gets a new connection
# from the pool.
session = sessionmaker(bind=engine, autocommit=True, autoflush=False)()

return ScopedSession(session, auto_commit=False, readonly=True)

Expand All @@ -152,6 +154,8 @@ def create_readonly_session(engine):
Returns:
object: Scoped session maker.
"""
session = sessionmaker(bind=engine, autocommit=False, autoflush=False)()
# Set session autocommit to true to ensure each query gets a new connection
# from the pool.
session = sessionmaker(bind=engine, autocommit=True, autoflush=False)()
stub_out_flush_operation(session)
return session
9 changes: 9 additions & 0 deletions google/cloud/forseti/services/inventory/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import json
import enum

from retrying import retry
from sqlalchemy import and_
from sqlalchemy import BigInteger
from sqlalchemy import case
Expand Down Expand Up @@ -803,9 +804,13 @@ def populate_cai_data(data, session):
return num_rows

@staticmethod
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000,
stop_max_attempt_number=5)
def iter_cai_assets(content_type, asset_type, parent_name, session):
"""Iterate the objects in the cai temporary table.
Retries query on exception up to 5 times.
Args:
content_type (ContentTypes): The content type to return.
asset_type (str): The asset type to return.
Expand All @@ -831,9 +836,13 @@ def iter_cai_assets(content_type, asset_type, parent_name, session):
yield row.extract_asset_data(content_type)

@staticmethod
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000,
stop_max_attempt_number=5)
def fetch_cai_asset(content_type, asset_type, name, session):
"""Returns a single resource from the cai temporary store.
Retries query on exception up to 5 times.
Args:
content_type (ContentTypes): The content type to return.
asset_type (str): The asset type to return.
Expand Down

0 comments on commit 53ef39e

Please sign in to comment.