Skip to content
Open
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
11 changes: 8 additions & 3 deletions snpdb/tasks/cohort_genotype_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,11 @@ def common_variant_classified_task(variant_id, common_filter_id):
variant = Variant.objects.get(pk=variant_id)
common_filter = CohortGenotypeCommonFilterVersion.objects.get(pk=common_filter_id)

# We should only be called if CommonVariantClassified doesn't exist
# Callers skip variants that already have a CommonVariantClassified, but it is only created once this
# finishes, so a variant can be queued again while its first task waits - the later ones have nothing to do
if CommonVariantClassified.objects.filter(variant=variant, common_filter=common_filter).exists():
logging.info("common_variant_classified_task(%s, %s) already handled", variant, common_filter)
return
logging.info("common_variant_classified_task(%s, %s)", str(variant), str(common_filter))

delete_ids_by_partition = {}
Expand All @@ -288,7 +292,8 @@ def common_variant_classified_task(variant_id, common_filter_id):
cg.collection = uncommon
insert_sql = model_to_insert_sql([cg], ignore_fields=['id'],
db_table=uncommon.get_partition_table())[0]
run_sql(insert_sql)
# A concurrent duplicate task may have moved it already, in which case the common row is just removed
run_sql(insert_sql + " ON CONFLICT DO NOTHING")
if delete_ids:
delete_ids_by_partition[cgc.get_partition_table()] = delete_ids

Expand All @@ -302,7 +307,7 @@ def common_variant_classified_task(variant_id, common_filter_id):
CohortGenotype.objects.filter(pk__in=delete_ids).delete()

# When it's completed - we can create the record
CommonVariantClassified.objects.create(variant=variant, common_filter=common_filter)
CommonVariantClassified.objects.get_or_create(variant=variant, common_filter=common_filter)
except:
log_traceback()
raise
19 changes: 19 additions & 0 deletions snpdb/tests/test_cohort_genotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ def test_moves_genotypes_from_the_common_to_the_uncommon_partition(self):
self.assertTrue(CommonVariantClassified.objects.filter(variant=self.variant,
common_filter=self.common_filter).exists())

def test_queued_twice_completes_both_times(self):
""" A variant can be queued again before its first task has created the CommonVariantClassified (#2948) """
common_variant_classified_task(self.variant.pk, self.common_filter.pk)
common_variant_classified_task(self.variant.pk, self.common_filter.pk)

self.assertEqual(self._partition_count(self.common_cgc), 0)
self.assertEqual(self._partition_count(self.uncommon_cgc), 1)

def test_overlapping_duplicate_finishes_the_move(self):
""" A concurrent duplicate has inserted into uncommon but not yet deleted from common (#2948) """
with temporary_db_table(CohortGenotype, self.uncommon_cgc.get_partition_table()):
CohortGenotype.objects.create(collection=self.uncommon_cgc, variant=self.variant,
samples_zygosity="OOO", het_count=0, hom_count=3)

common_variant_classified_task(self.variant.pk, self.common_filter.pk)

self.assertEqual(self._partition_count(self.common_cgc), 0)
self.assertEqual(self._partition_count(self.uncommon_cgc), 1)

def test_delete_does_not_expand_over_the_inheritance_parent(self):
""" A delete off snpdb_cohortgenotype locks every other collection's partition, which deadlocks
concurrent VCF imports (SACGF/variantgrid_sapath#450) """
Expand Down
Loading