From 9b1fa1fe414be61696b580cd18ed1ce47dd453d8 Mon Sep 17 00:00:00 2001 From: ntisseyre Date: Thu, 10 Aug 2023 10:18:23 -0500 Subject: [PATCH] Returning a copied collection to avoid corrupted iterator by multiple threads Signed-off-by: ntisseyre --- .../addedrelations/AddedRelationsContainer.java | 4 ++-- .../addedrelations/ConcurrentAddedRelations.java | 14 +++++++++++--- .../addedrelations/EmptyAddedRelations.java | 2 +- .../addedrelations/SimpleAddedRelations.java | 2 +- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/AddedRelationsContainer.java b/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/AddedRelationsContainer.java index c0ece092535..52c74de3c46 100644 --- a/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/AddedRelationsContainer.java +++ b/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/AddedRelationsContainer.java @@ -39,7 +39,7 @@ public interface AddedRelationsContainer { * of the transaction after there are no additional changes. Otherwise the behavior is non deterministic. * @return */ - Collection getAll(); + Collection getAllUnsafe(); /** * Clears the container which releases allocated memory. @@ -69,7 +69,7 @@ public boolean isEmpty() { } @Override - public Collection getAll() { + public Collection getAllUnsafe() { return Collections.emptyList(); } diff --git a/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/ConcurrentAddedRelations.java b/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/ConcurrentAddedRelations.java index 78a8ef75440..32fb69827c1 100644 --- a/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/ConcurrentAddedRelations.java +++ b/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/ConcurrentAddedRelations.java @@ -15,8 +15,10 @@ package org.janusgraph.graphdb.transaction.addedrelations; import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; import org.janusgraph.graphdb.internal.InternalRelation; +import java.util.ArrayList; import java.util.Collection; /** @@ -37,11 +39,17 @@ public synchronized boolean remove(final InternalRelation relation) { @Override public synchronized Iterable getView(final Predicate filter) { - return super.getView(filter); + return copyView(super.getView(filter)); } @Override - public synchronized Collection getAll() { - return super.getAll(); + public synchronized Collection getAllUnsafe() { + return super.getAllUnsafe(); + } + + private Iterable copyView(Iterable currentView) { + ArrayList view = new ArrayList<>(); + Iterables.addAll(view, currentView); + return view; } } diff --git a/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/EmptyAddedRelations.java b/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/EmptyAddedRelations.java index c139065120e..0069daa67a6 100644 --- a/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/EmptyAddedRelations.java +++ b/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/EmptyAddedRelations.java @@ -52,7 +52,7 @@ public boolean isEmpty() { } @Override - public Collection getAll() { + public Collection getAllUnsafe() { return Collections.emptyList(); } diff --git a/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/SimpleAddedRelations.java b/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/SimpleAddedRelations.java index fcc534c9b59..afc2bbeb141 100644 --- a/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/SimpleAddedRelations.java +++ b/janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/SimpleAddedRelations.java @@ -62,7 +62,7 @@ public boolean isEmpty() { } @Override - public Collection getAll() { + public Collection getAllUnsafe() { return Collections.unmodifiableCollection(new AbstractCollection() { @Override @Nonnull