|
| 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 | +} |
0 commit comments