From: Matt Amos Date: Thu, 21 May 2009 14:57:03 +0000 (+0000) Subject: Added locking around update and delete methods on main API objects. This should remov... X-Git-Tag: live~7375 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/4ab495f5db90c687574d4e4cee59cf73f79c31c6?hp=94c89838d4e7f3c767cea5f20709c52e1b8e4816 Added locking around update and delete methods on main API objects. This should remove the 500 errors which come from concurrent overlapping updates. Experimental - might be too slow for production. --- diff --git a/app/models/node.rb b/app/models/node.rb index e37a10250..01b0f220e 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -130,6 +130,7 @@ class Node < ActiveRecord::Base # provide repeatable reads for the used-by checks. this means it # shouldn't be possible to get race conditions. Node.transaction do + self.lock! check_consistency(self, new_node, user) way = WayNode.find(:first, :joins => :way, :conditions => [ "current_ways.visible = ? AND current_way_nodes.node_id = ?", true, self.id ]) @@ -150,25 +151,28 @@ class Node < ActiveRecord::Base end def update_from(new_node, user) - check_consistency(self, new_node, user) - - # update changeset first - self.changeset_id = new_node.changeset_id - self.changeset = new_node.changeset - - # update changeset bbox with *old* position first - changeset.update_bbox!(bbox); - - # FIXME logic needs to be double checked - self.latitude = new_node.latitude - self.longitude = new_node.longitude - self.tags = new_node.tags - self.visible = true - - # update changeset bbox with *new* position - changeset.update_bbox!(bbox); - - save_with_history! + Node.transaction do + self.lock! + check_consistency(self, new_node, user) + + # update changeset first + self.changeset_id = new_node.changeset_id + self.changeset = new_node.changeset + + # update changeset bbox with *old* position first + changeset.update_bbox!(bbox); + + # FIXME logic needs to be double checked + self.latitude = new_node.latitude + self.longitude = new_node.longitude + self.tags = new_node.tags + self.visible = true + + # update changeset bbox with *new* position + changeset.update_bbox!(bbox); + + save_with_history! + end end def create_with_history(user) diff --git a/app/models/relation.rb b/app/models/relation.rb index 1553414e6..32dab6254 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -240,6 +240,7 @@ class Relation < ActiveRecord::Base # provide repeatable reads for the used-by checks. this means it # shouldn't be possible to get race conditions. Relation.transaction do + self.lock! check_consistency(self, new_relation, user) # This will check to see if this relation is used by another relation rel = RelationMember.find(:first, :joins => :relation, @@ -255,16 +256,19 @@ class Relation < ActiveRecord::Base end def update_from(new_relation, user) - check_consistency(self, new_relation, user) - unless new_relation.preconditions_ok?(self.members) - raise OSM::APIPreconditionFailedError.new("Cannot update relation #{self.id}: data or member data is invalid.") + Relation.transaction do + self.lock! + check_consistency(self, new_relation, user) + unless new_relation.preconditions_ok?(self.members) + raise OSM::APIPreconditionFailedError.new("Cannot update relation #{self.id}: data or member data is invalid.") + end + self.changeset_id = new_relation.changeset_id + self.changeset = new_relation.changeset + self.tags = new_relation.tags + self.members = new_relation.members + self.visible = true + save_with_history! end - self.changeset_id = new_relation.changeset_id - self.changeset = new_relation.changeset - self.tags = new_relation.tags - self.members = new_relation.members - self.visible = true - save_with_history! end def create_with_history(user) diff --git a/app/models/way.rb b/app/models/way.rb index 935a7422c..4f988dc9d 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -204,17 +204,20 @@ class Way < ActiveRecord::Base end def update_from(new_way, user) - check_consistency(self, new_way, user) - unless new_way.preconditions_ok?(self.nds) - raise OSM::APIPreconditionFailedError.new("Cannot update way #{self.id}: data is invalid.") + Way.transaction do + self.lock! + check_consistency(self, new_way, user) + unless new_way.preconditions_ok?(self.nds) + raise OSM::APIPreconditionFailedError.new("Cannot update way #{self.id}: data is invalid.") + end + + self.changeset_id = new_way.changeset_id + self.changeset = new_way.changeset + self.tags = new_way.tags + self.nds = new_way.nds + self.visible = true + save_with_history! end - - self.changeset_id = new_way.changeset_id - self.changeset = new_way.changeset - self.tags = new_way.tags - self.nds = new_way.nds - self.visible = true - save_with_history! end def create_with_history(user) @@ -258,6 +261,7 @@ class Way < ActiveRecord::Base # provide repeatable reads for the used-by checks. this means it # shouldn't be possible to get race conditions. Way.transaction do + self.lock! check_consistency(self, new_way, user) rel = RelationMember.find(:first, :joins => :relation, :conditions => [ "visible = ? AND member_type='Way' and member_id=? ", true, self.id])