Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding assignment-unit cache to singleton DB #441

Merged
merged 3 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions mephisto/abstractions/databases/local_singleton_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(self, database_path=None):

# Create singleton dictionaries for entries
self._singleton_cache = {k: dict() for k in self._cached_classes}
self._assignment_to_unit_mapping: Dict[str, List[Unit]] = {}

def shutdown(self) -> None:
"""Close all open connections"""
Expand Down Expand Up @@ -128,3 +129,93 @@ def new_agent(
unit.db_status = AssignmentState.ASSIGNED
unit.worker_id = agent.worker_id
return agent_id

def find_units(
self,
task_id: Optional[str] = None,
task_run_id: Optional[str] = None,
requester_id: Optional[str] = None,
assignment_id: Optional[str] = None,
unit_index: Optional[int] = None,
provider_type: Optional[str] = None,
task_type: Optional[str] = None,
agent_id: Optional[str] = None,
worker_id: Optional[str] = None,
sandbox: Optional[bool] = None,
status: Optional[str] = None,
) -> List[Unit]:
"""
Uses caching to offset the cost of the most common queries. Defers
to the underlying DB for anything outside of those cases.
"""

# Finding units is the most common small DB call to be optimized, as
# every assignment has multiple units. Thus, we try to break up the
# units to be queried by assignment, ensuring the most commonly
# queried edge is improved.
if assignment_id is not None:
if all(
v is None
for v in [
task_id,
task_run_id,
requester_id,
unit_index,
provider_type,
task_type,
agent_id,
worker_id,
sandbox,
status,
]
):
units = self._assignment_to_unit_mapping.get(assignment_id)
if units is None:
units = super().find_units(assignment_id=assignment_id)
self._assignment_to_unit_mapping[assignment_id] = units
return units

# Any other cases are less common and more complicated, and so we don't cache
return super().find_units(
task_id=task_id,
task_run_id=task_run_id,
requester_id=requester_id,
assignment_id=assignment_id,
unit_index=unit_index,
provider_type=provider_type,
task_type=task_type,
agent_id=agent_id,
worker_id=worker_id,
sandbox=sandbox,
status=status,
)

def new_unit(
self,
task_id: str,
task_run_id: str,
requester_id: str,
assignment_id: str,
unit_index: int,
pay_amount: float,
provider_type: str,
task_type: str,
sandbox: bool = True,
) -> str:
"""
Create a new unit with the given index. Raises EntryAlreadyExistsException
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does throwing the exception happen in the super class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

if there is already a unit for the given assignment with the given index.
"""
if assignment_id in self._assignment_to_unit_mapping:
del self._assignment_to_unit_mapping[assignment_id]
JackUrb marked this conversation as resolved.
Show resolved Hide resolved
return super().new_unit(
task_id=task_id,
task_run_id=task_run_id,
requester_id=requester_id,
assignment_id=assignment_id,
unit_index=unit_index,
pay_amount=pay_amount,
provider_type=provider_type,
task_type=task_type,
sandbox=sandbox,
)
2 changes: 1 addition & 1 deletion mephisto/operations/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def _track_and_kill_runs(self):
tracked_run.architect.shutdown()
tracked_run.task_launcher.shutdown()
del self._task_runs_tracked[task_run.db_id]
time.sleep(2)
time.sleep(10)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add this as hardcoded constant (probably all caps) at the top of the file or in a constants file (if you are using one). Specially having a descriptive name helps someone who may want to debug later see what is readily available for tweaking.


def force_shutdown(self, timeout=5):
"""
Expand Down