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

add a select_for_update lock #76

Merged
merged 2 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions src/edge/models/fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,22 @@ def sorter_f(e):

return out_edges[0].to_chunk

def lock(self):
"""
Do a select for update, which, when executed inside a transaction, places
a lock on the fragment.
"""

fragments = Fragment.objects.select_for_update().filter(pk=self.id)
# Lock only happens when queryset is evaluated, therefore need to do at least fragments[0]
fragment = fragments[0]
print(f"Lock fragment {fragment.id}")
return fragment

@transaction.atomic()
def index_fragment_chunk_locations(self):
self.lock()

# remove old index
self.fragment_chunk_location_set.all().delete()

Expand Down
7 changes: 6 additions & 1 deletion src/edge/recombine.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,13 @@ def find_root_genome(genome):


def lock_genome(genome):
"""
Do a select for update, which, when executed inside a transaction, places a
Copy link
Collaborator

Choose a reason for hiding this comment

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

FYI I only spent two minutes poking into this, but from what I could tell, the places where you call lock_genome aren't inside transactions. Unless they're happening higher up in the call chain...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. It's upstream caller that's calling the atomic, because you need the locking and the rest of the work to happen all within a transaction. Should I change the method name to lock_genome_call_if_in_transaction? or you think the comment is sufficient.

lock on the genome.
"""

genomes = Genome.objects.select_for_update().filter(pk=find_root_genome(genome).id)
# Lock only happens when querset is evaluated, therefore need to do at least genomes[0]
# Lock only happens when queryset is evaluated, therefore need to do at least genomes[0]
genome = genomes[0]
print(f"Lock genome {genome.id}")
return genome
Expand Down