Skip to content

Commit

Permalink
fix: remove nested
Browse files Browse the repository at this point in the history
  • Loading branch information
talboren committed Dec 12, 2024
1 parent 99fce73 commit 142a705
Showing 1 changed file with 58 additions and 61 deletions.
119 changes: 58 additions & 61 deletions keep/api/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4723,74 +4723,71 @@ def set_last_alert(
logger.info(f"Seting last alert for `{alert.fingerprint}`")
with existed_or_new_session(session) as session:
for attempt in range(max_retries):
with session.begin_nested() as transaction:
logger.debug(
f"Attempt {attempt} to set last alert for `{alert.fingerprint}`",
extra={
"alert_id": alert.id,
"tenant_id": tenant_id,
"fingerprint": alert.fingerprint,
},
logger.debug(
f"Attempt {attempt} to set last alert for `{alert.fingerprint}`",
extra={
"alert_id": alert.id,
"tenant_id": tenant_id,
"fingerprint": alert.fingerprint,
},
)
try:
last_alert = get_last_alert_by_fingerprint(
tenant_id, alert.fingerprint, session, for_update=True
)
try:
last_alert = get_last_alert_by_fingerprint(
tenant_id, alert.fingerprint, session, for_update=True
)

# To prevent rare, but possible race condition
# For example if older alert failed to process
# and retried after new one
if last_alert and last_alert.timestamp.replace(
tzinfo=tz.UTC
) < alert.timestamp.replace(tzinfo=tz.UTC):
# To prevent rare, but possible race condition
# For example if older alert failed to process
# and retried after new one
if last_alert and last_alert.timestamp.replace(
tzinfo=tz.UTC
) < alert.timestamp.replace(tzinfo=tz.UTC):

logger.info(
f"Update last alert for `{alert.fingerprint}`: {last_alert.alert_id} -> {alert.id}"
)
last_alert.timestamp = alert.timestamp
last_alert.alert_id = alert.id
session.add(last_alert)
logger.info(
f"Update last alert for `{alert.fingerprint}`: {last_alert.alert_id} -> {alert.id}"
)
last_alert.timestamp = alert.timestamp
last_alert.alert_id = alert.id
session.add(last_alert)

elif not last_alert:
logger.info(
f"No last alert for `{alert.fingerprint}`, creating new"
)
last_alert = LastAlert(
tenant_id=tenant_id,
fingerprint=alert.fingerprint,
timestamp=alert.timestamp,
first_timestamp=alert.timestamp,
alert_id=alert.id,
alert_hash=alert.alert_hash,
)
elif not last_alert:
logger.info(
f"No last alert for `{alert.fingerprint}`, creating new"
)
last_alert = LastAlert(
tenant_id=tenant_id,
fingerprint=alert.fingerprint,
timestamp=alert.timestamp,
first_timestamp=alert.timestamp,
alert_id=alert.id,
alert_hash=alert.alert_hash,
)

session.add(last_alert)
transaction.commit()
except OperationalError as ex:
if "no such savepoint" in ex.args[0]:
logger.info(
f"No such savepoint while updating lastalert for `{alert.fingerprint}`, retry #{attempt}"
)
transaction.rollback()
if attempt >= max_retries:
raise ex
session.add(last_alert)
session.commit()
except OperationalError as ex:
if "no such savepoint" in ex.args[0]:
logger.info(
f"No such savepoint while updating lastalert for `{alert.fingerprint}`, retry #{attempt}"
)
if attempt >= max_retries:
raise ex

if "Deadlock found" in ex.args[0]:
logger.info(
f"Deadlock found while updating lastalert for `{alert.fingerprint}`, retry #{attempt}"
)
transaction.rollback()
if attempt >= max_retries:
raise ex
except NoActiveSqlTransaction:
logger.exception(
f"No active sql transaction while updating lastalert for `{alert.fingerprint}`, retry #{attempt}",
extra={
"alert_id": alert.id,
"tenant_id": tenant_id,
"fingerprint": alert.fingerprint,
},
if "Deadlock found" in ex.args[0]:
logger.info(
f"Deadlock found while updating lastalert for `{alert.fingerprint}`, retry #{attempt}"
)
if attempt >= max_retries:
raise ex
except NoActiveSqlTransaction:
logger.exception(
f"No active sql transaction while updating lastalert for `{alert.fingerprint}`, retry #{attempt}",
extra={
"alert_id": alert.id,
"tenant_id": tenant_id,
"fingerprint": alert.fingerprint,
},
)
logger.debug(
f"Successfully updated lastalert for `{alert.fingerprint}`",
extra={
Expand Down

0 comments on commit 142a705

Please sign in to comment.