I am getting very strange behaviour when trying to multithread read from a tinkergraph. I have the following situation:
Graph graph = TinkerGraph.open();
Set<Vertex> verticesAdded = randomGraph(graph);
verticesAdded
is a set of vertices which I added during the randomGraph(graph)
process. Once I have this list I check an internal property of this vertex and depending on that property I pass the vertex to a thread for some additional work. The process I follow is roughly:
public class ValidateVerticies(){
private Set<Vertex> vertices;
private ExecutorService executor;
public ValidateVerticies(Set<Vertex> verticesAdded){
vertices = verticesAdded;
executor = Executors.newSingleThreadExecutor(); //Single just for testing purposes
}
public validate(){
for(Vertex vertex: vertices){
String prop = vertex.property("type");
if(type.equals("1"))
executor.submit(() -> validateRule1(vertex))
else if(type.equals("2"))
executor.submit(() -> validateRule2(vertex))
else if(type.equals("n"))
executor.submit(() -> validateRule3(vertex))
...
else if(type.equals("n"))
executor.submit(() -> validateRulen(vertex))
}
}
}
The above code works when it is entirely Single Threaded but once I introduced the thread pool I am getting a strange variety of errors. Including:
- The vertex property
"type"
not existing. java.lang.IndexOutOfBoundsException: Index: 0, Size: -1
when trying to access certain vertex properties.- Validation rules failing when originally they passed.
Is there something subtle when doing multithreaded reads from tinkergraph ?
Edit:
I will try to simplify the problem: The following works:
public class ValidateVerticies(){
private Set<Vertex> vertices;
public ValidateVerticies(Set<Vertex> verticesAdded){
vertices = verticesAdded;
}
public validate(){
for(Vertex vertex: vertices){
String prop = vertex.property("type");
if(type.equals("1"))
validateRule1(vertex);
...
else if(type.equals("n"))
validateRulen(vertex);
}
}
}
While the multithreaded version above fails with TinkerGraph (same applies to Titan which supports transactions) returns inconsistent results when reading from the graph.