-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable multiQuery optimization for has step
Fixes #3244 Signed-off-by: Oleksandr Porunov <alexandr.porunov@gmail.com>
- Loading branch information
Showing
7 changed files
with
201 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...-core/src/main/java/org/janusgraph/graphdb/tinkerpop/optimize/step/JanusGraphHasStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright 2023 JanusGraph Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.janusgraph.graphdb.tinkerpop.optimize.step; | ||
|
||
import org.apache.tinkerpop.gremlin.process.traversal.Traversal; | ||
import org.apache.tinkerpop.gremlin.process.traversal.Traverser; | ||
import org.apache.tinkerpop.gremlin.process.traversal.step.Profiling; | ||
import org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep; | ||
import org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer; | ||
import org.apache.tinkerpop.gremlin.process.traversal.util.MutableMetrics; | ||
import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalInterruptedException; | ||
import org.apache.tinkerpop.gremlin.structure.Element; | ||
import org.apache.tinkerpop.gremlin.structure.Vertex; | ||
import org.janusgraph.core.BaseVertexQuery; | ||
import org.janusgraph.core.JanusGraphException; | ||
import org.janusgraph.core.JanusGraphMultiVertexQuery; | ||
import org.janusgraph.core.JanusGraphVertex; | ||
import org.janusgraph.graphdb.query.profile.QueryProfiler; | ||
import org.janusgraph.graphdb.query.vertex.BasicVertexCentricQueryBuilder; | ||
import org.janusgraph.graphdb.tinkerpop.optimize.JanusGraphTraversalUtil; | ||
import org.janusgraph.graphdb.tinkerpop.profile.TP3ProfileWrapper; | ||
import org.janusgraph.graphdb.util.CopyStepUtil; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class JanusGraphHasStep<S extends Element> extends HasStep<S> implements Profiling, MultiQueriable<S,S> { | ||
|
||
private boolean useMultiQuery = false; | ||
private boolean batchPropertyPrefetching = false; | ||
private int txVertexCacheSize = 20000; | ||
private Set<JanusGraphVertex> verticesToPrefetch = new HashSet<>(); | ||
private Set<Vertex> prefetchedVertices = new HashSet<>(); | ||
|
||
private QueryProfiler queryProfiler = QueryProfiler.NO_OP; | ||
|
||
public JanusGraphHasStep(HasStep<S> originalStep){ | ||
this(originalStep.getTraversal(), originalStep.getHasContainers().toArray(new HasContainer[0])); | ||
CopyStepUtil.copyAbstractStepModifiableFields(originalStep, this); | ||
|
||
if (originalStep instanceof JanusGraphHasStep) { | ||
JanusGraphHasStep originalJanusGraphHasStep = (JanusGraphHasStep) originalStep; | ||
this.useMultiQuery = originalJanusGraphHasStep.useMultiQuery; | ||
} | ||
} | ||
|
||
public JanusGraphHasStep(Traversal.Admin traversal, HasContainer... hasContainers) { | ||
super(traversal, hasContainers); | ||
} | ||
|
||
@Override | ||
protected boolean filter(final Traverser.Admin<S> traverser) { | ||
if (useMultiQuery && traverser.get() instanceof Vertex) { | ||
JanusGraphVertex vertex = JanusGraphTraversalUtil.getJanusGraphVertex(traverser); | ||
if(!prefetchedVertices.contains(vertex)){ | ||
prefetchNextBatch(vertex); | ||
} | ||
} | ||
return super.filter(traverser); | ||
} | ||
|
||
public <Q extends BaseVertexQuery> Q makeQuery(Q query) { | ||
if(!batchPropertyPrefetching){ | ||
//TODO: return only specific properties (we should recursively traverse hasContainers and return any property keys) | ||
// final String[] keys = getPropertyKeys(); | ||
// query.keys(keys); | ||
} | ||
// TODO: do we need to apply has containers to this multi query? | ||
// if yes, then skipped vertices in the result should be filtered always as `false` and non-filtered vertices | ||
// should be filtered as `true` | ||
|
||
// for (final HasContainer condition : getHasContainers()) { | ||
// query.has(condition.getKey(), JanusGraphPredicateUtils.convert(condition.getBiPredicate()), condition.getValue()); | ||
// } | ||
((BasicVertexCentricQueryBuilder) query).profiler(queryProfiler); | ||
return query; | ||
} | ||
|
||
/** | ||
* This initialisation method is called when an attempt to retrieve a vertex from the cached multiQuery results | ||
* doesn't find an entry. | ||
*/ | ||
private void prefetchNextBatch(final JanusGraphVertex requiredVertex) { | ||
if(txVertexCacheSize < 1){ | ||
return; | ||
} | ||
//TODO: prefetch both properties and / or vertex labels of the vertices depending on internal hasContainers | ||
|
||
verticesToPrefetch.add(requiredVertex); | ||
final JanusGraphMultiVertexQuery multiQuery = JanusGraphTraversalUtil.getTx(getTraversal()).multiQuery(verticesToPrefetch); | ||
|
||
makeQuery(multiQuery); | ||
|
||
try { | ||
multiQuery.preFetch(); | ||
prefetchedVertices.clear(); | ||
prefetchedVertices.addAll(verticesToPrefetch); | ||
verticesToPrefetch.clear(); | ||
} catch (JanusGraphException janusGraphException) { | ||
if (janusGraphException.isCausedBy(InterruptedException.class)) { | ||
throw new TraversalInterruptedException(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void setUseMultiQuery(boolean useMultiQuery) { | ||
this.useMultiQuery = useMultiQuery; | ||
} | ||
|
||
@Override | ||
public void registerFutureVertexForPrefetching(Vertex futureVertex) { | ||
verticesToPrefetch.add((JanusGraphVertex) futureVertex); | ||
} | ||
|
||
@Override | ||
public void setMetrics(MutableMetrics metrics) { | ||
queryProfiler = new TP3ProfileWrapper(metrics); | ||
} | ||
|
||
public void setBatchPropertyPrefetching(boolean batchPropertyPrefetching) { | ||
this.batchPropertyPrefetching = batchPropertyPrefetching; | ||
} | ||
|
||
public void setTxVertexCacheSize(int txVertexCacheSize) { | ||
this.txVertexCacheSize = txVertexCacheSize; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
janusgraph-core/src/main/java/org/janusgraph/graphdb/util/CopyStepUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2023 JanusGraph Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.janusgraph.graphdb.util; | ||
|
||
import org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep; | ||
|
||
public class CopyStepUtil { | ||
|
||
public static void copyAbstractStepModifiableFields(AbstractStep<?,?> originalStep, AbstractStep<?,?> copiedStep){ | ||
for(String label : originalStep.getLabels()){ | ||
copiedStep.addLabel(label); | ||
} | ||
} | ||
|
||
} |