Skip to content

Commit

Permalink
Enable multiQuery optimization for PropertyMapStep and ElementMapStep…
Browse files Browse the repository at this point in the history
… [cql-tests] [tp-tests]

Adds possibility to fetch properties and labels of vertices using valueMap, elementMap, propertyMap steps.

Adds fetching modes to properties, values, valueMap, elementMap, propertyMap steps to be able to preFetch all properties (single slice query) or only required properties (separate slice query per each requested property).

Fixes #2444

Signed-off-by: Oleksandr Porunov <alexandr.porunov@gmail.com>
  • Loading branch information
porunov committed Jun 9, 2023
1 parent dac1ea1 commit 4ee27d0
Show file tree
Hide file tree
Showing 18 changed files with 1,498 additions and 72 deletions.
3 changes: 2 additions & 1 deletion docs/configs/janusgraph-cfg.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ Configuration options to configure batch queries optimization behavior
| Name | Description | Datatype | Default Value | Mutability |
| ---- | ---- | ---- | ---- | ---- |
| query.batch.enabled | Whether traversal queries should be batched when executed against the storage backend. This can lead to significant performance improvement if there is a non-trivial latency to the backend. If `false` then all other configuration options under `query.batch` namespace are ignored. | Boolean | true | MASKABLE |
| query.batch.has-step-mode | Properties pre-fetching mode for `has` step. Used only when query.batch.enabled is `true`.<br>Supported modes:<br>- `all_properties` Pre-fetch all vertex properties on any property access<br>- `required_properties_only` Pre-fetch necessary vertex properties for the whole chain of foldable `has` steps<br>- `required_and_next_properties` Prefetch the same properties as with `required_properties_only` mode, but also prefetch
| query.batch.has-step-mode | Properties pre-fetching mode for `has` step. Used only when query.batch.enabled is `true`.<br>Supported modes:<br>- `all_properties` Pre-fetch all vertex properties on any property access (fetches all vertex properties in a single slice query)<br>- `required_properties_only` Pre-fetch necessary vertex properties for the whole chain of foldable `has` steps (uses a separate slice query per each required property)<br>- `required_and_next_properties` Prefetch the same properties as with `required_properties_only` mode, but also prefetch
properties which may be needed in the next properties access step like `values`, `properties,` `valueMap`, `elementMap`, or `propertyMap`.
In case the next step is not one of those properties access steps then this mode behaves same as `required_properties_only`.
In case the next step is one of the properties access steps with limited scope of properties, those properties will be
Expand All @@ -372,6 +372,7 @@ behaves same as `all_properties`.<br>- `required_and_next_properties_or_all` Pre
`values`, `properties,` `valueMap`, `elementMap`, or `propertyMap` then acts like `all_properties`.<br>- `none` Skips `has` step batch properties pre-fetch optimization.<br> | String | required_and_next_properties | MASKABLE |
| query.batch.limited | Configure a maximum batch size for queries against the storage backend. This can be used to ensure responsiveness if batches tend to grow very large. The used batch size is equivalent to the barrier size of a preceding `barrier()` step. If a step has no preceding `barrier()`, the default barrier of TinkerPop will be inserted. This option only takes effect if `query.batch.enabled` is `true`. | Boolean | true | MASKABLE |
| query.batch.limited-size | Default batch size (barrier() step size) for queries. This size is applied only for cases where `LazyBarrierStrategy` strategy didn't apply `barrier` step and where user didn't apply barrier step either. This option is used only when `query.batch.limited` is `true`. Notice, value `2147483647` is considered to be unlimited. | Integer | 2500 | MASKABLE |
| query.batch.properties-mode | Properties pre-fetching mode for `values`, `properties`, `valueMap`, `propertyMap`, `elementMap` steps. Used only when query.batch.enabled is `true`.<br>Supported modes:<br>- `all_properties` Pre-fetch all vertex properties on any property access (fetches all vertex properties in a single slice query)<br>- `required_properties_only` Pre-fetch necessary vertex properties only (uses a separate slice query per each required property)<br>- `none` Skips vertex properties pre-fetching optimization.<br> | String | required_properties_only | MASKABLE |
| query.batch.repeat-step-mode | Batch mode for `repeat` step. Used only when query.batch.enabled is `true`.<br>These modes are controlling how the child steps with batch support are behaving if they placed to the start of the `repeat`, `emit`, or `until` traversals.<br>Supported modes:<br>- `closest_repeat_parent` Child start steps are receiving vertices for batching from the closest `repeat` step parent only.<br>- `all_repeat_parents` Child start steps are receiving vertices for batching from all `repeat` step parents.<br>- `starts_only_of_all_repeat_parents` Child start steps are receiving vertices for batching from the closest `repeat` step parent (both for the parent start and for next iterations) and also from all `repeat` step parents for the parent start. | String | all_repeat_parents | MASKABLE |

### schema
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
// 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;

import org.apache.tinkerpop.gremlin.process.traversal.step.util.WithOptions;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraphTransaction;
import org.janusgraph.core.PropertyKey;
import org.janusgraph.core.schema.JanusGraphManagement;
import org.janusgraph.diskstorage.BackendException;
import org.janusgraph.diskstorage.configuration.ModifiableConfiguration;
import org.janusgraph.diskstorage.configuration.WriteConfiguration;
import org.janusgraph.diskstorage.cql.CQLConfigOptions;
import org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@Fork(1)
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class CQLMultiQueryPropertiesBenchmark {
@Param({"100", "5000", "50000"})
int verticesAmount;

@Param({"true", "false"})
boolean fastProperty;

@Param({
"all_properties",
"required_properties_only",
"none"})
String propertiesBatchMode;

JanusGraph graph;

public WriteConfiguration getConfiguration() {
ModifiableConfiguration config = GraphDatabaseConfiguration.buildGraphConfiguration();
config.set(GraphDatabaseConfiguration.STORAGE_BACKEND,"cql");
config.set(CQLConfigOptions.LOCAL_DATACENTER, "dc1");
config.set(GraphDatabaseConfiguration.USE_MULTIQUERY, true);
config.set(GraphDatabaseConfiguration.PROPERTIES_BATCH_MODE, propertiesBatchMode);
config.set(GraphDatabaseConfiguration.PROPERTY_PREFETCHING, fastProperty);
return config.getConfiguration();
}

@Setup
public void setUp() throws Exception {
graph = JanusGraphFactory.open(getConfiguration());
int propertiesAmount = 10;

JanusGraphManagement mgmt = graph.openManagement();
PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();

for(int i=0;i<propertiesAmount;i++){
mgmt.makePropertyKey("prop"+i).dataType(String.class).make();
}

mgmt.buildIndex("nameIndex", Vertex.class).addKey(name).buildCompositeIndex();

mgmt.commit();

for (int i = 0; i < verticesAmount; i++) {
Vertex vertex = graph.addVertex("name", "testVertex");
for(int j=0;j<propertiesAmount;j++){
vertex.property("prop"+j,
"SomeTestPropertyValue "+j+" 0123456789 ABCDEFG");
}
}

graph.tx().commit();
}

@TearDown
public void tearDown() throws BackendException {
JanusGraphFactory.drop(graph);
}

@Benchmark
public List<? extends Object> getValueMap() {
JanusGraphTransaction tx = graph.buildTransaction()
.start();
List<Map<Object, Object>> result = tx.traversal().V().has("name", "testVertex")
.valueMap()
.toList();
tx.rollback();
return result;
}

@Benchmark
public List<? extends Object> getValueMapWithOptions() {
JanusGraphTransaction tx = graph.buildTransaction()
.start();
List<Map<Object, Object>> result = tx.traversal().V().has("name", "testVertex")
.valueMap().with(WithOptions.tokens, WithOptions.all)
.toList();
tx.rollback();
return result;
}

@Benchmark
public List<? extends Object> getValueMapWithOptionsLimitedOne() {
JanusGraphTransaction tx = graph.buildTransaction()
.start();
List<Map<Object, Object>> result = tx.traversal().V().has("name", "testVertex")
.valueMap().with(WithOptions.tokens, WithOptions.all)
.limit(1)
.toList();
tx.rollback();
return result;
}

@Benchmark
public List<? extends Object> getValueMapWithOptionsLimitedBatchSizePlusOne() {
JanusGraphTransaction tx = graph.buildTransaction()
.start();
List<Map<Object, Object>> result = tx.traversal().V().valueMap().has("name", "testVertex")
.with(WithOptions.tokens, WithOptions.all)
.limit(GraphDatabaseConfiguration.LIMITED_BATCH_SIZE.getDefaultValue()+1)
.toList();
tx.rollback();
return result;
}

@Benchmark
public List<? extends Object> getPropertyMap() {
JanusGraphTransaction tx = graph.buildTransaction()
.start();
List<? extends Object> result = tx.traversal().V().has("name", "testVertex")
.propertyMap()
.toList();
tx.rollback();
return result;
}

@Benchmark
public List<? extends Object> getElementMap() {
JanusGraphTransaction tx = graph.buildTransaction()
.start();
List<? extends Object> result = tx.traversal().V().has("name", "testVertex")
.elementMap()
.toList();
tx.rollback();
return result;
}

@Benchmark
public List<? extends Object> getValues() {
JanusGraphTransaction tx = graph.buildTransaction()
.start();
List<? extends Object> result = tx.traversal().V().has("name", "testVertex")
.values()
.toList();
tx.rollback();
return result;
}

@Benchmark
public List<? extends Object> getProperties() {
JanusGraphTransaction tx = graph.buildTransaction()
.start();
List<? extends Object> result = tx.traversal().V().has("name", "testVertex")
.properties()
.toList();
tx.rollback();
return result;
}

@Benchmark
public List<? extends Object> getValueMapSingleProperty() {
JanusGraphTransaction tx = graph.buildTransaction()
.start();
List<? extends Object> result = tx.traversal().V().has("name", "testVertex")
.valueMap("prop1")
.toList();
tx.rollback();
return result;
}

@Benchmark
public List<? extends Object> getPropertyMapSingleProperty() {
JanusGraphTransaction tx = graph.buildTransaction()
.start();
List<? extends Object> result = tx.traversal().V().has("name", "testVertex")
.propertyMap("prop1")
.toList();
tx.rollback();
return result;
}

@Benchmark
public List<? extends Object> getElementMapSingleProperty() {
JanusGraphTransaction tx = graph.buildTransaction()
.start();
List<? extends Object> result = tx.traversal().V().has("name", "testVertex")
.elementMap("prop1")
.toList();
tx.rollback();
return result;
}

@Benchmark
public List<? extends Object> getValuesSingleProperty() {
JanusGraphTransaction tx = graph.buildTransaction()
.start();
List<? extends Object> result = tx.traversal().V().has("name", "testVertex")
.values("prop1")
.toList();
tx.rollback();
return result;
}

@Benchmark
public List<? extends Object> getPropertiesSingleProperty() {
JanusGraphTransaction tx = graph.buildTransaction()
.start();
List<? extends Object> result = tx.traversal().V().has("name", "testVertex")
.properties("prop1")
.toList();
tx.rollback();
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration;
import org.janusgraph.graphdb.tinkerpop.optimize.strategy.MultiQueryHasStepStrategyMode;
import org.janusgraph.graphdb.tinkerpop.optimize.strategy.MultiQueryPropertiesStrategyMode;

import java.time.Instant;

Expand Down Expand Up @@ -68,7 +69,7 @@ public interface TransactionBuilder {
TransactionBuilder propertyPrefetching(boolean enabled);

/**
* Enable or disable multi-query, i.e. query.batch
* Enable or disable multi-query, i.e. `query.batch.enabled`
*
* @param enabled
* @return Object containing properties that will enable/disable multi-query
Expand Down Expand Up @@ -151,12 +152,21 @@ public interface TransactionBuilder {
/**
* Sets `has` step strategy mode.
* <p>
* Doesn't have any effect if multi-query was disabled via config `query.batch`.
* Doesn't have any effect if multi-query was disabled via config `query.batch.enabled = false`.
*
* @return Object with the set `has` step strategy mode settings
*/
TransactionBuilder setHasStepStrategyMode(MultiQueryHasStepStrategyMode hasStepStrategyMode);

/**
* Sets properties strategy mode.
* <p>
* Doesn't have any effect if multi-query was disabled via config `query.batch.enabled = false`.
*
* @return Object with the set properties strategy mode settings
*/
TransactionBuilder setPropertiesStrategyMode(MultiQueryPropertiesStrategyMode propertiesStrategyMode);

/**
* Sets the group name for this transaction which provides a way for gathering
* reporting on multiple transactions into one group.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.janusgraph.graphdb.query.index.BruteForceIndexSelectionStrategy;
import org.janusgraph.graphdb.query.index.IndexSelectionStrategy;
import org.janusgraph.graphdb.query.index.ThresholdBasedIndexSelectionStrategy;
import org.janusgraph.graphdb.tinkerpop.optimize.strategy.MultiQueryPropertiesStrategyMode;
import org.janusgraph.graphdb.tinkerpop.optimize.strategy.MultiQueryStrategyRepeatStepMode;
import org.janusgraph.graphdb.tinkerpop.optimize.strategy.MultiQueryHasStepStrategyMode;
import org.janusgraph.graphdb.transaction.StandardTransactionBuilder;
Expand Down Expand Up @@ -331,8 +332,8 @@ public class GraphDatabaseConfiguration {
public static final ConfigOption<String> HAS_STEP_BATCH_MODE = new ConfigOption<>(QUERY_BATCH_NS,"has-step-mode",
String.format("Properties pre-fetching mode for `has` step. Used only when "+USE_MULTIQUERY.toStringWithoutRoot()+" is `true`.<br>" +
"Supported modes:<br>" +
"- `%s` Pre-fetch all vertex properties on any property access<br>" +
"- `%s` Pre-fetch necessary vertex properties for the whole chain of foldable `has` steps<br>" +
"- `%s` Pre-fetch all vertex properties on any property access (fetches all vertex properties in a single slice query)<br>" +
"- `%s` Pre-fetch necessary vertex properties for the whole chain of foldable `has` steps (uses a separate slice query per each required property)<br>" +
"- `%s` Prefetch the same properties as with `%s` mode, but also prefetch\n" +
"properties which may be needed in the next properties access step like `values`, `properties,` `valueMap`, `elementMap`, or `propertyMap`.\n" +
"In case the next step is not one of those properties access steps then this mode behaves same as `%s`.\n" +
Expand All @@ -355,6 +356,17 @@ public class GraphDatabaseConfiguration {
MultiQueryHasStepStrategyMode.NONE.getConfigName()),
ConfigOption.Type.MASKABLE, MultiQueryHasStepStrategyMode.REQUIRED_AND_NEXT_PROPERTIES.getConfigName());

public static final ConfigOption<String> PROPERTIES_BATCH_MODE = new ConfigOption<>(QUERY_BATCH_NS,"properties-mode",
String.format("Properties pre-fetching mode for `values`, `properties`, `valueMap`, `propertyMap`, `elementMap` steps. Used only when "+USE_MULTIQUERY.toStringWithoutRoot()+" is `true`.<br>" +
"Supported modes:<br>" +
"- `%s` Pre-fetch all vertex properties on any property access (fetches all vertex properties in a single slice query)<br>" +
"- `%s` Pre-fetch necessary vertex properties only (uses a separate slice query per each required property)<br>" +
"- `%s` Skips vertex properties pre-fetching optimization.<br>",
MultiQueryPropertiesStrategyMode.ALL_PROPERTIES.getConfigName(),
MultiQueryPropertiesStrategyMode.REQUIRED_PROPERTIES_ONLY.getConfigName(),
MultiQueryPropertiesStrategyMode.NONE.getConfigName()),
ConfigOption.Type.MASKABLE, MultiQueryPropertiesStrategyMode.REQUIRED_PROPERTIES_ONLY.getConfigName());

// ################ SCHEMA #######################
// ################################################

Expand Down Expand Up @@ -1326,6 +1338,7 @@ public boolean apply(@Nullable String s) {
private String metricsPrefix;
private String unknownIndexKeyName;
private MultiQueryHasStepStrategyMode hasStepStrategyMode;
private MultiQueryPropertiesStrategyMode propertiesStrategyMode;

private StoreFeatures storeFeatures = null;

Expand Down Expand Up @@ -1444,6 +1457,10 @@ public MultiQueryHasStepStrategyMode hasStepStrategyMode() {
return hasStepStrategyMode;
}

public MultiQueryPropertiesStrategyMode propertiesStrategyMode() {
return propertiesStrategyMode;
}

public boolean adjustQueryLimit() {
return adjustQueryLimit;
}
Expand Down Expand Up @@ -1572,6 +1589,7 @@ private void preLoadConfiguration() {
limitedBatchSize = configuration.get(LIMITED_BATCH_SIZE);
repeatStepMode = selectExactConfig(REPEAT_STEP_BATCH_MODE, MultiQueryStrategyRepeatStepMode.values());
hasStepStrategyMode = selectExactConfig(HAS_STEP_BATCH_MODE, MultiQueryHasStepStrategyMode.values());
propertiesStrategyMode = selectExactConfig(PROPERTIES_BATCH_MODE, MultiQueryPropertiesStrategyMode.values());

indexSelectionStrategy = Backend.getImplementationClass(configuration, configuration.get(INDEX_SELECT_STRATEGY),
REGISTERED_INDEX_SELECTION_STRATEGIES);
Expand Down
Loading

0 comments on commit 4ee27d0

Please sign in to comment.