Is it possible to Atomically check limit and Increment/decrement a counter field in multiple tables ?

15 views
Skip to first unread message

sarvi

unread,
Feb 14, 2017, 5:40:18 PM2/14/17
to Django users
I have 2 tables Cluster and Node, each having a volume_limit and volume_count column in it.
To atomically check and increment the volume count for a Cluster and Node I do the following
updated = Cluster.objects.filter(id=cluster_id, volume_count__lt==F('volume_limit')).update(volume_count = F('volume_count')+1)
if not updated:
    raise Validation('limit reached ......')
updated = Node.objects.filter(id=node_id, volume_count__lt==F('volume_limit')).update(volume_count = F('volume_count')+1)
if not updated:
    Cluster.objects.filter(id=cluser_id).update(volume_count = F('volume_count')-1)
    raise Validation('limit reached ......')


I am wondering if there is way to do both of this atomically?

Basically I want to 
   1. create a query for Cluster, Node, etc, N such objects/tables queries, that each filter match one row by pk and volume_count__lt=F('volume_count')
   2. Check that the union of all these queries translates to exactly N objects, one for each Object/Table. i.e. the counts are within limit for each object/table
   3. Do a volume_count=F('volume_count')+1 for all those objects/table together.

How do I do this?

Thanks,
Sarvi
Reply all
Reply to author
Forward
0 new messages