-
Notifications
You must be signed in to change notification settings - Fork 888
JAVA-1932: Send DRIVER_NAME and DRIVER_VERSION in Startup #1087
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
80 changes: 80 additions & 0 deletions
80
core/src/main/java/com/datastax/oss/driver/internal/core/context/StartupOptionsBuilder.java
This file contains hidden or 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,80 @@ | ||
| /* | ||
| * Copyright DataStax, Inc. | ||
| * | ||
| * 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 com.datastax.oss.driver.internal.core.context; | ||
|
|
||
| import com.datastax.oss.driver.api.core.session.Session; | ||
| import com.datastax.oss.protocol.internal.request.Startup; | ||
| import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableMap; | ||
| import java.util.Map; | ||
| import net.jcip.annotations.Immutable; | ||
|
|
||
| @Immutable | ||
| public class StartupOptionsBuilder { | ||
|
|
||
| public static final String DRIVER_NAME_KEY = "DRIVER_NAME"; | ||
| public static final String DRIVER_VERSION_KEY = "DRIVER_VERSION"; | ||
|
|
||
| protected final InternalDriverContext context; | ||
|
|
||
| public StartupOptionsBuilder(InternalDriverContext context) { | ||
| this.context = context; | ||
| } | ||
|
|
||
| /** | ||
| * Builds a map of options to send in a Startup message. | ||
| * | ||
| * <p>The default set of options are built here and include {@link | ||
| * com.datastax.oss.protocol.internal.request.Startup#COMPRESSION_KEY} (if the context passed in | ||
| * has a compressor/algorithm set), and the driver's {@link #DRIVER_NAME_KEY} and {@link | ||
| * #DRIVER_VERSION_KEY}. The {@link com.datastax.oss.protocol.internal.request.Startup} | ||
| * constructor will add {@link | ||
| * com.datastax.oss.protocol.internal.request.Startup#CQL_VERSION_KEY}. | ||
| * | ||
| * @return Map of Startup Options. | ||
| */ | ||
| public Map<String, String> build() { | ||
| NullAllowingImmutableMap.Builder<String, String> builder = NullAllowingImmutableMap.builder(3); | ||
| // add compression (if configured) and driver name and version | ||
| String compressionAlgorithm = context.getCompressor().algorithm(); | ||
| if (compressionAlgorithm != null && !compressionAlgorithm.trim().isEmpty()) { | ||
| builder.put(Startup.COMPRESSION_KEY, compressionAlgorithm.trim()); | ||
| } | ||
| return builder | ||
| .put(DRIVER_NAME_KEY, getDriverName()) | ||
| .put(DRIVER_VERSION_KEY, getDriverVersion()) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns this driver's name. | ||
| * | ||
| * <p>By default, this method will pull from the bundled Driver.properties file. Subclasses should | ||
| * override this method if they need to report a different Driver name on Startup. | ||
| */ | ||
| protected String getDriverName() { | ||
| return Session.OSS_DRIVER_COORDINATES.getName(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns this driver's version. | ||
| * | ||
| * <p>By default, this method will pull from the bundled Driver.properties file. Subclasses should | ||
| * override this method if they need to report a different Driver version on Startup. | ||
| */ | ||
| protected String getDriverVersion() { | ||
| return Session.OSS_DRIVER_COORDINATES.getVersion().toString(); | ||
| } | ||
| } |
This file contains hidden or 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
107 changes: 107 additions & 0 deletions
107
...rc/test/java/com/datastax/oss/driver/internal/core/context/StartupOptionsBuilderTest.java
This file contains hidden or 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,107 @@ | ||
| /* | ||
| * Copyright DataStax, Inc. | ||
| * | ||
| * 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 com.datastax.oss.driver.internal.core.context; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import com.datastax.oss.driver.api.core.Version; | ||
| import com.datastax.oss.driver.api.core.config.DefaultDriverOption; | ||
| import com.datastax.oss.driver.api.core.config.DriverConfig; | ||
| import com.datastax.oss.driver.api.core.config.DriverConfigLoader; | ||
| import com.datastax.oss.driver.api.core.config.DriverExecutionProfile; | ||
| import com.datastax.oss.driver.api.core.metadata.Node; | ||
| import com.datastax.oss.driver.api.core.metadata.NodeStateListener; | ||
| import com.datastax.oss.driver.api.core.metadata.schema.SchemaChangeListener; | ||
| import com.datastax.oss.driver.api.core.session.Session; | ||
| import com.datastax.oss.driver.api.core.tracker.RequestTracker; | ||
| import com.datastax.oss.driver.api.core.type.codec.TypeCodec; | ||
| import com.datastax.oss.driver.shaded.guava.common.collect.Lists; | ||
| import com.datastax.oss.driver.shaded.guava.common.collect.Maps; | ||
| import com.datastax.oss.protocol.internal.request.Startup; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Predicate; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Mockito; | ||
| import org.mockito.MockitoAnnotations; | ||
|
|
||
| public class StartupOptionsBuilderTest { | ||
|
|
||
| private DefaultDriverContext defaultDriverContext; | ||
|
|
||
| // Mocks for instantiating the default driver context | ||
| @Mock private DriverConfigLoader configLoader; | ||
| private List<TypeCodec<?>> typeCodecs = Lists.newArrayList(); | ||
| @Mock private NodeStateListener nodeStateListener; | ||
| @Mock private SchemaChangeListener schemaChangeListener; | ||
| @Mock private RequestTracker requestTracker; | ||
| private Map<String, Predicate<Node>> nodeFilters = Maps.newHashMap(); | ||
| @Mock private ClassLoader classLoader; | ||
| @Mock private DriverConfig driverConfig; | ||
| @Mock private DriverExecutionProfile defaultProfile; | ||
|
|
||
| @Before | ||
| public void before() { | ||
| MockitoAnnotations.initMocks(this); | ||
| Mockito.when(configLoader.getInitialConfig()).thenReturn(driverConfig); | ||
| Mockito.when(driverConfig.getDefaultProfile()).thenReturn(defaultProfile); | ||
| } | ||
|
|
||
| private void buildDriverContext() { | ||
| defaultDriverContext = | ||
| new DefaultDriverContext( | ||
| configLoader, | ||
| typeCodecs, | ||
| nodeStateListener, | ||
| schemaChangeListener, | ||
| requestTracker, | ||
| nodeFilters, | ||
| classLoader); | ||
| } | ||
|
|
||
| private void assertDefaultStartupOptions(Startup startup) { | ||
| assertThat(startup.options).containsEntry(Startup.CQL_VERSION_KEY, "3.0.0"); | ||
| assertThat(startup.options) | ||
| .containsEntry( | ||
| StartupOptionsBuilder.DRIVER_NAME_KEY, Session.OSS_DRIVER_COORDINATES.getName()); | ||
| assertThat(startup.options).containsKey(StartupOptionsBuilder.DRIVER_VERSION_KEY); | ||
| Version version = Version.parse(startup.options.get(StartupOptionsBuilder.DRIVER_VERSION_KEY)); | ||
| assertThat(version).isEqualByComparingTo(Session.OSS_DRIVER_COORDINATES.getVersion()); | ||
| } | ||
|
|
||
| @Test | ||
| public void should_build_minimal_startup_options() { | ||
| buildDriverContext(); | ||
| Startup startup = new Startup(defaultDriverContext.getStartupOptions()); | ||
| assertThat(startup.options).doesNotContainKey(Startup.COMPRESSION_KEY); | ||
| assertDefaultStartupOptions(startup); | ||
| } | ||
|
|
||
| @Test | ||
| public void should_build_startup_options_with_compression() { | ||
| Mockito.when(defaultProfile.isDefined(DefaultDriverOption.PROTOCOL_COMPRESSION)) | ||
| .thenReturn(Boolean.TRUE); | ||
| Mockito.when(defaultProfile.getString(DefaultDriverOption.PROTOCOL_COMPRESSION)) | ||
| .thenReturn("lz4"); | ||
| buildDriverContext(); | ||
| Startup startup = new Startup(defaultDriverContext.getStartupOptions()); | ||
| // assert the compression option is present | ||
| assertThat(startup.options).containsEntry(Startup.COMPRESSION_KEY, "lz4"); | ||
| assertDefaultStartupOptions(startup); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 that makes sense, the only thing this was testing was the options map, and since it's now built outside of the handler, covering it here wouldn't be very useful.