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

fixes #1705 by locking node while Assigning rel #1706

Closed
wants to merge 7 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def delete_all_rels
# Executed in the database, callbacks will not be run.
def replace_with(node_or_nodes)
node_or_nodes = Array(node_or_nodes).map { |arg| arg.is_a?(ActiveGraph::Node) ? arg : @model.find(arg) }
ActiveGraph::Base.lock_node(start_object) unless start_object.new_record?
original_ids = self.pluck(:id)
delete_rels_for_nodes(original_ids, node_or_nodes.collect(&:id))
add_rels(node_or_nodes, original_ids)
Expand All @@ -62,12 +63,13 @@ def add_rels(node_or_nodes, original_ids)
end

def delete_rels_for_nodes(original_ids, new_ids)
ids = original_ids.select { |id| !new_ids.include?(id) }
Copy link
Member Author

Choose a reason for hiding this comment

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

using - is faster compared to using select loop and then checking with include? again

return unless ids.present?
ids_to_be_removed = original_ids - new_ids
return unless ids_to_be_removed.present?

if association.dependent
start_object.public_send("dependent_#{association.dependent}_callback", association, ids)
start_object.public_send("dependent_#{association.dependent}_callback", association, ids_to_be_removed)
else
self.where(id: ids).delete_all_rels
self.where(id: ids_to_be_removed).delete_all_rels
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/active_graph/transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def read_transaction(**config, &block)

alias transaction write_transaction

def lock_node(node)
node.as(:n).query.set('n._LOCK_ = null').exec if tx&.open? || explicit_session&.open?
end

private

def send_transaction(method, **config, &block)
Expand Down
52 changes: 52 additions & 0 deletions spec/e2e/relationship/persistence/query_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,58 @@ def self.count
expect(from_node.reload.to_classes).to be_empty
end

context 'concurrent update' do
before do
allow(ActiveGraph::Base).to receive(:lock_node).and_wrap_original do |original, *args|
$concurrency_queue << 'ready'
Thread.stop
Copy link
Member

Choose a reason for hiding this comment

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

I think the right thing would be to stop at delete_rels_for_nodes after the rels are read so you can force failure with just 2 threads not 100. And then after adding lock see it passing.

original.call(*args)
$concurrency_queue << Thread.current
Thread.stop
end
end
after { $concurrency_queue = nil }
let!(:from_node) { FromClass.create(name: 'foo') }
let!(:to_node) { ToClass.create(name: 'bar') }
let(:from_node_two) { FromClass.create(name: 'foo-2') }

it 'does not create duplicate has_one relationship' do
$concurrency_queue = Thread::Queue.new
t1 = Thread.new { to_node.update(from_class: from_node) }
t2 = Thread.new { to_node.update(from_class: from_node) }
sleep(0.1) until $concurrency_queue.size == 2
$concurrency_queue.clear
[t1, t2].each(&:run)
sleep(0.1) until $concurrency_queue.size == 1 && t1.status == 'sleep' && t2.status == 'sleep'
$concurrency_queue.pop.run
sleep(0.1) until !(t1.status && t2.status)

expect(ActiveGraph::Base.query("MATCH (node2:`ToClass`)<-[rel1:`HAS_REL`]-(from_class:`FromClass`) return from_class").to_a.size).to eq(1)
(t1.status == 'sleep' ? t1.run : t2.run).join
expect(ActiveGraph::Base.query("MATCH (node2:`ToClass`)<-[rel1:`HAS_REL`]-(from_class:`FromClass`) return from_class").to_a.size).to eq(1)
end

it 'does not create two rels with different nodes in has_one relationship' do
$concurrency_queue = Thread::Queue.new
t1 = Thread.new { to_node.update(from_class: from_node) }
t2 = Thread.new { to_node.update(from_class: from_node_two) }
sleep(0.1) until $concurrency_queue.size == 2
$concurrency_queue.clear
[t1, t2].each(&:run)
sleep(0.1) until $concurrency_queue.size == 1 && t1.status == 'sleep' && t2.status == 'sleep'
$concurrency_queue.pop.run
sleep(0.1) until !(t1.status && t2.status)

first_assigned_from_class, second_assigned_from_class = t1.status == 'sleep' ? [from_node_two, from_node] : [from_node, from_node_two]

expect(ToClass.find(to_node.id).from_class.id).to eq(first_assigned_from_class.id)
(t1.status == 'sleep' ? t1.run : t2.run).join

expect(to_node.reload.from_class.id).to eq(second_assigned_from_class.id)
expect(ActiveGraph::Base.query("MATCH (node2:`ToClass`)<-[rel1:`HAS_REL`]-(from_class:`FromClass`) return from_class").to_a.size).to eq(1)
end
end

it 'delets has_one rel from from_node when new relation is created' do
to_node_two = ToClass.new(name: 'bar-2')
Rel2Class.new(from_node: from_node, to_node: to_node, score: 10).save
Expand Down
Loading