Skip to content

Commit 68cbf4f

Browse files
emerkle826adutra
authored andcommitted
JAVA-1579: Change default result format to latest Graph format (apache#281)
1 parent 26383ef commit 68cbf4f

33 files changed

+984
-650
lines changed

changelog/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### NGDG (in progress)
66

7+
- [improvement] JAVA-1579: Change default result format to latest GraphSON format
78
- [improvement] JAVA-2496: Revisit timeouts for paged graph queries
89
- [bug] JAVA-2510: Fix GraphBinaryDataTypesTest Codec registry initialization
910
- [bug] JAVA-2492: Parse edge metadata using internal identifiers

core/src/main/java/com/datastax/dse/driver/internal/core/graph/ContinuousGraphRequestHandler.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ public class ContinuousGraphRequestHandler
5757
@NonNull DefaultSession session,
5858
@NonNull InternalDriverContext context,
5959
@NonNull String sessionLogPrefix,
60-
@NonNull GraphBinaryModule graphBinaryModule) {
60+
@NonNull GraphBinaryModule graphBinaryModule,
61+
@NonNull GraphSupportChecker graphSupportChecker) {
6162
super(statement, session, context, sessionLogPrefix, AsyncGraphResultSet.class);
6263
this.graphBinaryModule = graphBinaryModule;
63-
subProtocol = GraphConversions.inferSubProtocol(statement, executionProfile);
64+
subProtocol = graphSupportChecker.inferGraphProtocol(statement, executionProfile, context);
6465
message =
6566
GraphConversions.createContinuousMessageFromGraphStatement(
6667
statement, subProtocol, executionProfile, context, graphBinaryModule);

core/src/main/java/com/datastax/dse/driver/internal/core/graph/GraphConversions.java

-19
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import java.util.Collections;
5454
import java.util.List;
5555
import java.util.Map;
56-
import java.util.Objects;
5756
import org.apache.tinkerpop.gremlin.process.traversal.Traverser;
5857
import org.apache.tinkerpop.gremlin.structure.io.Buffer;
5958
import org.apache.tinkerpop.gremlin.structure.io.BufferFactory;
@@ -83,24 +82,6 @@ public class GraphConversions extends Conversions {
8382

8483
@VisibleForTesting static final byte[] EMPTY_STRING_QUERY = "".getBytes(UTF_8);
8584

86-
public static GraphProtocol inferSubProtocol(
87-
GraphStatement<?> statement, DriverExecutionProfile config) {
88-
String graphProtocol = statement.getSubProtocol();
89-
if (graphProtocol == null) {
90-
graphProtocol =
91-
config.getString(
92-
DseDriverOption.GRAPH_SUB_PROTOCOL,
93-
// TODO pick graphson-3.0 if the target graph uses the core engine
94-
"graphson-2.0");
95-
}
96-
// should not be null because we call config.getString() with a default value
97-
Objects.requireNonNull(
98-
graphProtocol,
99-
"Could not determine the graph protocol for the query. This is a bug, please report.");
100-
101-
return GraphProtocol.fromString(graphProtocol);
102-
}
103-
10485
public static Message createContinuousMessageFromGraphStatement(
10586
GraphStatement<?> statement,
10687
GraphProtocol subProtocol,

core/src/main/java/com/datastax/dse/driver/internal/core/graph/GraphPagingSupportChecker.java

-82
This file was deleted.

core/src/main/java/com/datastax/dse/driver/internal/core/graph/GraphRequestAsyncProcessor.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class GraphRequestAsyncProcessor
3737
implements RequestProcessor<GraphStatement<?>, CompletionStage<AsyncGraphResultSet>> {
3838

3939
private final GraphBinaryModule graphBinaryModule;
40-
private final GraphPagingSupportChecker graphPagingSupportChecker;
40+
private final GraphSupportChecker graphSupportChecker;
4141

4242
public GraphRequestAsyncProcessor(
4343
DefaultDriverContext context, GraphPagingSupportChecker graphPagingSupportChecker) {
@@ -47,7 +47,7 @@ public GraphRequestAsyncProcessor(
4747
new GraphBinaryModule(
4848
new GraphBinaryReader(typeSerializerRegistry),
4949
new GraphBinaryWriter(typeSerializerRegistry));
50-
this.graphPagingSupportChecker = graphPagingSupportChecker;
50+
this.graphSupportChecker = graphSupportChecker;
5151
}
5252

5353
@NonNull
@@ -67,13 +67,23 @@ public CompletionStage<AsyncGraphResultSet> process(
6767
InternalDriverContext context,
6868
String sessionLogPrefix) {
6969

70-
if (graphPagingSupportChecker.isPagingEnabled(request, context)) {
70+
if (graphSupportChecker.isPagingEnabled(request, context)) {
7171
return new ContinuousGraphRequestHandler(
72-
request, session, context, sessionLogPrefix, getGraphBinaryModule())
72+
request,
73+
session,
74+
context,
75+
sessionLogPrefix,
76+
getGraphBinaryModule(),
77+
graphSupportChecker)
7378
.handle();
7479
} else {
7580
return new GraphRequestHandler(
76-
request, session, context, sessionLogPrefix, getGraphBinaryModule())
81+
request,
82+
session,
83+
context,
84+
sessionLogPrefix,
85+
getGraphBinaryModule(),
86+
graphSupportChecker)
7787
.handle();
7888
}
7989
}

core/src/main/java/com/datastax/dse/driver/internal/core/graph/GraphRequestHandler.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ public GraphRequestHandler(
140140
@NonNull DefaultSession dseSession,
141141
@NonNull InternalDriverContext context,
142142
@NonNull String sessionLogPrefix,
143-
@NonNull GraphBinaryModule graphBinaryModule) {
143+
@NonNull GraphBinaryModule graphBinaryModule,
144+
@NonNull GraphSupportChecker graphSupportChecker) {
144145
this.startTimeNanos = System.nanoTime();
145146
this.logPrefix = sessionLogPrefix + "|" + this.hashCode();
146147
Preconditions.checkArgument(
@@ -195,7 +196,8 @@ public GraphRequestHandler(
195196
this.inFlightCallbacks = new CopyOnWriteArrayList<>();
196197
this.graphBinaryModule = graphBinaryModule;
197198

198-
this.subProtocol = GraphConversions.inferSubProtocol(this.graphStatement, executionProfile);
199+
this.subProtocol =
200+
graphSupportChecker.inferGraphProtocol(this.graphStatement, executionProfile, this.context);
199201
LOG.debug("[{}], Graph protocol used for query: {}", logPrefix, subProtocol);
200202

201203
this.message =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* This software can be used solely with DataStax Enterprise. Please consult the license at
5+
* http://www.datastax.com/terms/datastax-dse-driver-license-terms
6+
*/
7+
package com.datastax.dse.driver.internal.core.graph;
8+
9+
import com.datastax.dse.driver.api.core.config.DseDriverOption;
10+
import com.datastax.dse.driver.api.core.graph.GraphStatement;
11+
import com.datastax.dse.driver.api.core.graph.PagingEnabledOptions;
12+
import com.datastax.dse.driver.api.core.metadata.DseNodeProperties;
13+
import com.datastax.dse.driver.internal.core.DseProtocolFeature;
14+
import com.datastax.oss.driver.api.core.ProtocolVersion;
15+
import com.datastax.oss.driver.api.core.Version;
16+
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
17+
import com.datastax.oss.driver.api.core.metadata.Node;
18+
import com.datastax.oss.driver.internal.core.context.InternalDriverContext;
19+
import com.datastax.oss.driver.internal.core.cql.Conversions;
20+
import edu.umd.cs.findbugs.annotations.NonNull;
21+
import java.util.Collection;
22+
import java.util.Objects;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
public class GraphSupportChecker {
27+
private static final Logger LOG = LoggerFactory.getLogger(GraphSupportChecker.class);
28+
static final Version MIN_DSE_VERSION_GRAPH_BINARY_AND_PAGING =
29+
Objects.requireNonNull(Version.parse("6.8.0"));
30+
31+
private volatile Boolean contextGraphPagingEnabled;
32+
private volatile Boolean isDse68OrAbove;
33+
34+
// Graph paging is available if
35+
// 1) continuous paging is generally available and
36+
// 2) all hosts are running DSE 6.8+
37+
// The computation below will be done only once when the session is initialized; if other hosts
38+
// join the cluster later and are not running DSE 6.8, the user has to manually disable graph
39+
// paging.
40+
boolean isPagingEnabled(GraphStatement<?> graphStatement, InternalDriverContext context) {
41+
DriverExecutionProfile driverExecutionProfile =
42+
Conversions.resolveExecutionProfile(graphStatement, context);
43+
PagingEnabledOptions pagingEnabledOptions =
44+
PagingEnabledOptions.valueOf(
45+
driverExecutionProfile.getString(DseDriverOption.GRAPH_PAGING_ENABLED));
46+
if (LOG.isTraceEnabled()) {
47+
LOG.trace("GRAPH_PAGING_ENABLED: {}", pagingEnabledOptions);
48+
}
49+
if (pagingEnabledOptions == PagingEnabledOptions.DISABLED) {
50+
return false;
51+
} else if (pagingEnabledOptions == PagingEnabledOptions.ENABLED) {
52+
return true;
53+
} else {
54+
return isContextGraphPagingEnabled(context);
55+
}
56+
}
57+
58+
private boolean isContextGraphPagingEnabled(InternalDriverContext context) {
59+
if (contextGraphPagingEnabled == null) {
60+
ProtocolVersion protocolVersion = context.getProtocolVersion();
61+
if (!context
62+
.getProtocolVersionRegistry()
63+
.supports(protocolVersion, DseProtocolFeature.CONTINUOUS_PAGING)) {
64+
contextGraphPagingEnabled = false;
65+
} else {
66+
if (isDse68OrAbove == null) {
67+
isDse68OrAbove = checkIsDse68OrAbove(context);
68+
}
69+
contextGraphPagingEnabled = isDse68OrAbove;
70+
}
71+
}
72+
return contextGraphPagingEnabled;
73+
}
74+
75+
/**
76+
* Determines the default {@link GraphProtocol} for the given context. When a statement is
77+
* executed, if the Graph protocol is not explicitly set on the statement (via {@link
78+
* GraphStatement#setSubProtocol(java.lang.String)}), or is not explicitly set in the config (see
79+
* dse-reference.conf), the default Graph protocol used is determined by the DSE version to which
80+
* the driver is connected. For DSE versions 6.7.x and lower, the default Graph protocol is {@link
81+
* GraphProtocol#GRAPHSON_2_0}. For DSE versions 6.8.0 and higher, the default Graph protocol is
82+
* {@link GraphProtocol#GRAPH_BINARY_1_0}.
83+
*
84+
* @return The default GraphProtocol to used based on the provided context.
85+
*/
86+
GraphProtocol getDefaultGraphProtocol(@NonNull InternalDriverContext context) {
87+
if (isDse68OrAbove == null) {
88+
isDse68OrAbove = checkIsDse68OrAbove(context);
89+
}
90+
// if the DSE version can't be determined, default to GraphSON 2.0
91+
return isDse68OrAbove ? GraphProtocol.GRAPH_BINARY_1_0 : GraphProtocol.GRAPHSON_2_0;
92+
}
93+
94+
private boolean checkIsDse68OrAbove(@NonNull InternalDriverContext context) {
95+
Collection<Node> nodes = context.getMetadataManager().getMetadata().getNodes().values();
96+
97+
for (Node node : nodes) {
98+
Version dseVersion = (Version) node.getExtras().get(DseNodeProperties.DSE_VERSION);
99+
if (dseVersion == null || dseVersion.compareTo(MIN_DSE_VERSION_GRAPH_BINARY_AND_PAGING) < 0) {
100+
return false;
101+
}
102+
}
103+
return true;
104+
}
105+
106+
GraphProtocol inferGraphProtocol(
107+
GraphStatement<?> statement, DriverExecutionProfile config, InternalDriverContext context) {
108+
String graphProtocol = statement.getSubProtocol();
109+
if (graphProtocol == null) {
110+
// use the protocol specified in configuration, otherwise get the default from the context
111+
graphProtocol =
112+
(config.isDefined(DseDriverOption.GRAPH_SUB_PROTOCOL))
113+
? config.getString(DseDriverOption.GRAPH_SUB_PROTOCOL)
114+
: getDefaultGraphProtocol(context).toInternalCode();
115+
}
116+
// should not be null because we call config.getString() with a default value
117+
Objects.requireNonNull(
118+
graphProtocol,
119+
"Could not determine the graph protocol for the query. This is a bug, please report.");
120+
121+
return GraphProtocol.fromString(graphProtocol);
122+
}
123+
}

core/src/main/resources/reference.conf

+3-2
Original file line numberDiff line numberDiff line change
@@ -1017,8 +1017,9 @@ datastax-java-driver {
10171017
# Possible values with built-in support in the driver are:
10181018
# [ "graphson-1.0", "graphson-2.0", "graph-binary-1.0"]
10191019
#
1020-
# The default value for DSE 6.7 and lower is "graphson-2.0". For DSE 6.8 and higher, the default
1021-
# value is "graphson-binary-1.0"
1020+
# IMPORTANT: The default value for the Graph sub-protocol is based only on the DSE
1021+
# version. If the version is DSE 6.7 and lower, "graphson-2.0" will be the default. For DSE 6.8
1022+
# and higher, the default value is "graphson-binary-1.0".
10221023
#
10231024
# Required: no
10241025
# Modifiable at runtime: yes, the new value will be used for requests issued after the change.

0 commit comments

Comments
 (0)