Skip to content

Commit

Permalink
Returning a copied collection to avoid corrupted iterator by multiple…
Browse files Browse the repository at this point in the history
… threads

Signed-off-by: ntisseyre <[email protected]>
  • Loading branch information
ntisseyre committed Feb 26, 2024
1 parent 1530f0a commit 0feb953
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,7 @@ public synchronized void commit() {
}
try {
if (hasModifications()) {
graph.commit(addedRelations.getAll(), deletedRelations.values(), this);
graph.commit(addedRelations.getAllUnsafe(), deletedRelations.values(), this);

Check warning on line 1571 in janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/StandardJanusGraphTx.java

View check run for this annotation

Codecov / codecov/patch

janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/StandardJanusGraphTx.java#L1571

Added line #L1571 was not covered by tests
} else {
txHandle.commit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface AddedRelationsContainer {
* of the transaction after there are no additional changes. Otherwise the behavior is non deterministic.
* @return
*/
Collection<InternalRelation> getAll();
Collection<InternalRelation> getAllUnsafe();

/**
* Clears the container which releases allocated memory.
Expand Down Expand Up @@ -69,7 +69,7 @@ public boolean isEmpty() {
}

@Override
public Collection<InternalRelation> getAll() {
public Collection<InternalRelation> getAllUnsafe() {
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -37,11 +39,17 @@ public synchronized boolean remove(final InternalRelation relation) {

@Override
public synchronized Iterable<InternalRelation> getView(final Predicate<InternalRelation> filter) {
return super.getView(filter);
return copyView(super.getView(filter));

Check warning on line 42 in janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/ConcurrentAddedRelations.java

View check run for this annotation

Codecov / codecov/patch

janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/ConcurrentAddedRelations.java#L42

Added line #L42 was not covered by tests
}

@Override
public synchronized Collection<InternalRelation> getAll() {
return super.getAll();
public synchronized Collection<InternalRelation> getAllUnsafe() {
return super.getAllUnsafe();

Check warning on line 47 in janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/ConcurrentAddedRelations.java

View check run for this annotation

Codecov / codecov/patch

janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/ConcurrentAddedRelations.java#L47

Added line #L47 was not covered by tests
}

private Iterable<InternalRelation> copyView(Iterable<InternalRelation> currentView) {
ArrayList<InternalRelation> view = new ArrayList<>();
Iterables.addAll(view, currentView);
return view;

Check warning on line 53 in janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/ConcurrentAddedRelations.java

View check run for this annotation

Codecov / codecov/patch

janusgraph-core/src/main/java/org/janusgraph/graphdb/transaction/addedrelations/ConcurrentAddedRelations.java#L51-L53

Added lines #L51 - L53 were not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public boolean isEmpty() {
}

@Override
public Collection<InternalRelation> getAll() {
public Collection<InternalRelation> getAllUnsafe() {
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public boolean isEmpty() {
}

@Override
public Collection<InternalRelation> getAll() {
public Collection<InternalRelation> getAllUnsafe() {
return Collections.unmodifiableCollection(new AbstractCollection<InternalRelation>() {
@Override
@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public void isEmpty() {
}

@Test
public void getAllReturnsEmpty() {
assertTrue(EmptyAddedRelations.getInstance().getAll().isEmpty());
public void getAllUnsafeReturnsEmpty() {
assertTrue(EmptyAddedRelations.getInstance().getAllUnsafe().isEmpty());
}

@Test
Expand Down

0 comments on commit 0feb953

Please sign in to comment.