Skip to content

Commit db486cd

Browse files
authored
Count syncback connection errors and timeouts (#1195)
1 parent 2b2e4a1 commit db486cd

2 files changed

Lines changed: 370 additions & 33 deletions

File tree

‎inbox/transactions/actions.py‎

Lines changed: 108 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
"""
1010

11+
import contextlib
1112
import queue
1213
import random
1314
import threading
@@ -40,7 +41,10 @@
4041
from inbox.crispin import writable_connection_pool
4142
from inbox.events.actions.base import create_event, delete_event, update_event
4243
from inbox.ignition import engine_manager
43-
from inbox.interruptible_threading import InterruptibleThread
44+
from inbox.interruptible_threading import (
45+
InterruptibleThread,
46+
InterruptibleThreadTimeout,
47+
)
4448
from inbox.logging import get_logger
4549
from inbox.models import ActionLog, Event
4650
from inbox.models.session import session_scope, session_scope_by_shard_id
@@ -563,7 +567,32 @@ def _crispin_client_or_none(self): # type: ignore[no-untyped-def]
563567

564568
def execute(self) -> None:
565569
log = logger.new()
566-
with self.semaphore, self._crispin_client_or_none() as crispin_client:
570+
with self.semaphore, contextlib.ExitStack() as exit_stack:
571+
try:
572+
crispin_client = exit_stack.enter_context(
573+
self._crispin_client_or_none()
574+
)
575+
# A connection that uses up the deadline of the batch is a
576+
# failure too.
577+
interruptible_threading.check_interrupted()
578+
except Exception:
579+
log.warning(
580+
"Syncback connection failed",
581+
account_id=self.account_id,
582+
exc_info=True,
583+
)
584+
self._record_connection_failure()
585+
return
586+
except InterruptibleThreadTimeout:
587+
log.warning(
588+
"Syncback connection timed out",
589+
account_id=self.account_id,
590+
exc_info=True,
591+
)
592+
self._record_connection_failure()
593+
# The timeout must still stop the batch.
594+
raise
595+
567596
log.debug(
568597
"Syncback running batch of actions",
569598
num_actions=len(self.tasks),
@@ -581,6 +610,13 @@ def execute(self) -> None:
581610
# failed.
582611
break
583612

613+
def _record_connection_failure(self) -> None:
614+
# Only the tasks that use the connection need it. Count the failure
615+
# for the first of them with pending actions.
616+
for task in self.tasks:
617+
if task.uses_crispin_client() and task.record_failure():
618+
break
619+
584620
def uses_crispin_client(self): # type: ignore[no-untyped-def] # noqa: ANN201
585621
return any([task.uses_crispin_client() for task in self.tasks])
586622

@@ -688,14 +724,7 @@ def execute_with_lock(self) -> bool | None:
688724
"""
689725
interruptible_threading.check_interrupted()
690726

691-
self.log = logger.new(
692-
record_ids=list(set(self.record_ids)),
693-
action_log_ids=self.action_log_ids[:100],
694-
n_action_log_ids=len(self.action_log_ids),
695-
action=self.action_name,
696-
account_id=self.account_id,
697-
extra_args=self.extra_args,
698-
)
727+
self._bind_log()
699728

700729
# Double-check that the action is still pending.
701730
# Although the task queue is populated based on pending actions, it's
@@ -740,30 +769,67 @@ def execute_with_lock(self) -> bool | None:
740769
account_id=self.account_id,
741770
provider=self.provider,
742771
)
743-
with session_scope(self.account_id) as db_session:
744-
action_log_entries = db_session.query(ActionLog).filter(
745-
ActionLog.id.in_(action_ids_to_process)
746-
)
772+
self._increment_retries(action_ids_to_process)
773+
return False
774+
except InterruptibleThreadTimeout:
775+
self.log.warning(
776+
"Syncback action timed out",
777+
account_id=self.account_id,
778+
provider=self.provider,
779+
exc_info=True,
780+
)
781+
self._increment_retries(action_ids_to_process)
782+
# The timeout must still stop the batch.
783+
raise
747784

748-
marked_as_failed = False
749-
for action_log_entry in action_log_entries:
750-
action_log_entry.retries += 1
751-
if action_log_entry.retries == ACTION_MAX_NR_OF_RETRIES:
752-
marked_as_failed = True
753-
754-
if marked_as_failed:
755-
self.log.debug(
756-
"marking actions as failed",
757-
action_log_ids=action_ids_to_process,
758-
)
759-
# If we merged actions, fail them all at the same time.
760-
for action_log_entry in action_log_entries:
761-
self._mark_action_as_failed(
762-
action_log_entry, db_session
763-
)
764-
db_session.commit()
785+
def record_failure(self) -> bool:
786+
"""
787+
Count a failure of the task for its actions that are pending. Return
788+
`True` if the task had pending actions.
789+
"""
790+
self._bind_log()
791+
(_, action_ids_to_process) = self._get_records_and_actions_to_process()
792+
if not action_ids_to_process:
793+
return False
794+
self._increment_retries(action_ids_to_process)
795+
return True
765796

766-
return False
797+
def _bind_log(self) -> None:
798+
"""Set `self.log` to a new logger with the details of the task."""
799+
self.log = logger.new(
800+
record_ids=list(set(self.record_ids)),
801+
action_log_ids=self.action_log_ids[:100],
802+
n_action_log_ids=len(self.action_log_ids),
803+
action=self.action_name,
804+
account_id=self.account_id,
805+
extra_args=self.extra_args,
806+
)
807+
808+
def _increment_retries(self, action_ids: list[int]) -> None:
809+
"""
810+
Increment `retries` of the actions with the IDs in `action_ids`. If
811+
one of them reaches `ACTION_MAX_NR_OF_RETRIES`, mark all of them as
812+
failed.
813+
"""
814+
with session_scope(self.account_id) as db_session:
815+
action_log_entries = db_session.query(ActionLog).filter(
816+
ActionLog.id.in_(action_ids)
817+
)
818+
819+
marked_as_failed = False
820+
for action_log_entry in action_log_entries:
821+
action_log_entry.retries += 1
822+
if action_log_entry.retries >= ACTION_MAX_NR_OF_RETRIES:
823+
marked_as_failed = True
824+
825+
if marked_as_failed:
826+
self.log.debug(
827+
"marking actions as failed", action_log_ids=action_ids
828+
)
829+
# If we merged actions, fail them all at the same time.
830+
for action_log_entry in action_log_entries:
831+
self._mark_action_as_failed(action_log_entry, db_session)
832+
db_session.commit()
767833

768834
def _get_records_and_actions_to_process( # type: ignore[no-untyped-def]
769835
self,
@@ -882,7 +948,16 @@ def _run(self) -> None:
882948
with interruptible_threading.timeout(
883949
task.timeout(self.task_timeout)
884950
):
885-
task.execute()
951+
# Log every timeout of the batch, not only the timeouts
952+
# inside an action.
953+
try:
954+
task.execute()
955+
except InterruptibleThreadTimeout:
956+
self.log.warning(
957+
"Syncback batch timed out",
958+
account_id=task.account_id,
959+
)
960+
raise
886961
except Exception:
887962
self.log.exception(
888963
"SyncbackWorker caught exception",

0 commit comments

Comments
 (0)