-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Verify JVM configuration in Snowflake connector #24717
Open
hashhar
wants to merge
2
commits into
trinodb:master
Choose a base branch
from
hashhar:hashhar/snow-arrow-jvm-check
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -33,15 +33,9 @@ snowflake.role=role | |||||
snowflake.warehouse=warehouse | ||||||
``` | ||||||
|
||||||
### Arrow serialization support | ||||||
|
||||||
This is an experimental feature which introduces support for using Apache Arrow | ||||||
as the serialization format when reading from Snowflake. Please note there are | ||||||
a few caveats: | ||||||
|
||||||
- Using Apache Arrow serialization is disabled by default. In order to enable | ||||||
it, add `--add-opens=java.base/java.nio=ALL-UNNAMED` to the Trino | ||||||
{ref}`jvm-config`. | ||||||
Snowflake connector uses Apache Arrow as the serialization format when | ||||||
reading from Snowflake which requires additional JVM arguments. Add | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
And then have a codeblock with the line |
||||||
`--add-opens=java.base/java.nio=ALL-UNNAMED` to the Trino {ref}`jvm-config`. | ||||||
|
||||||
### Multiple Snowflake databases or accounts | ||||||
|
||||||
|
78 changes: 78 additions & 0 deletions
78
lib/trino-plugin-toolkit/src/main/java/io/trino/plugin/base/JdkCompatibilityChecks.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,78 @@ | ||
/* | ||
* 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 io.trino.plugin.base; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Joiner; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Multimap; | ||
import com.google.inject.Binder; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class JdkCompatibilityChecks | ||
{ | ||
private final String inputArguments; | ||
|
||
private static final JdkCompatibilityChecks INSTANCE = new JdkCompatibilityChecks( | ||
ManagementFactory.getRuntimeMXBean().getInputArguments()); | ||
|
||
@VisibleForTesting | ||
JdkCompatibilityChecks(List<String> inputArguments) | ||
{ | ||
this.inputArguments = Joiner.on(" ") | ||
.skipNulls() | ||
.join(requireNonNull(inputArguments, "inputArguments is null")); | ||
} | ||
|
||
public static void verifyConnectorAccessOpened(Binder binder, String connectorName, Multimap<String, String> modules) | ||
{ | ||
INSTANCE.verifyAccessOpened(wrap(binder), format("Connector '%s'", connectorName), modules); | ||
} | ||
|
||
@VisibleForTesting | ||
void verifyAccessOpened(ThrowableSettable throwableSettable, String description, Multimap<String, String> modules) | ||
{ | ||
ImmutableList.Builder<String> missingJvmArguments = ImmutableList.builder(); | ||
for (String fromModule : modules.keySet()) { | ||
for (String toModule : modules.get(fromModule)) { | ||
String requiredJvmArgument = format(".*?--add-opens[\\s=]%s/%s=ALL-UNNAMED.*?", Pattern.quote(fromModule), Pattern.quote(toModule)); | ||
if (!inputArguments.matches(requiredJvmArgument)) { | ||
missingJvmArguments.add(format("--add-opens=%s/%s=ALL-UNNAMED", fromModule, toModule)); | ||
} | ||
} | ||
} | ||
|
||
List<String> requiredJvmArguments = missingJvmArguments.build(); | ||
if (!requiredJvmArguments.isEmpty()) { | ||
throwableSettable.setThrowable(new IllegalStateException(format("%s requires additional JVM argument(s). Please add the following to the JVM configuration: '%s'", description, String.join(" ", requiredJvmArguments)))); | ||
} | ||
} | ||
|
||
private static ThrowableSettable wrap(Binder binder) | ||
{ | ||
return throwable -> binder.addError(throwable.getMessage()); | ||
} | ||
|
||
@VisibleForTesting | ||
interface ThrowableSettable | ||
{ | ||
void setThrowable(Throwable throwable); | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
lib/trino-plugin-toolkit/src/test/java/io/trino/plugin/base/TestJdkCompatibilityChecks.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,105 @@ | ||
/* | ||
* 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 io.trino.plugin.base; | ||
|
||
import com.google.common.collect.ImmutableMultimap; | ||
import com.google.inject.Binder; | ||
import com.google.inject.CreationException; | ||
import com.google.inject.Guice; | ||
import com.google.inject.Module; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static io.trino.plugin.base.JdkCompatibilityChecks.verifyConnectorAccessOpened; | ||
import static java.util.Objects.requireNonNull; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class TestJdkCompatibilityChecks | ||
{ | ||
@Test | ||
public void shouldThrowWhenAccessIsNotGranted() | ||
{ | ||
ThrowableSettableMock errorSink = new ThrowableSettableMock(); | ||
new JdkCompatibilityChecks(List.of()).verifyAccessOpened(errorSink, "Connector 'snowflake'", ImmutableMultimap.of( | ||
"java.base", "java.nio")); | ||
|
||
assertThat(errorSink.getThrowable()) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessage("Connector 'snowflake' requires additional JVM argument(s). Please add the following to the JVM configuration: '--add-opens=java.base/java.nio=ALL-UNNAMED'"); | ||
} | ||
|
||
@Test | ||
public void shouldThrowWhenOneOfAccessGrantsIsMissing() | ||
{ | ||
ThrowableSettableMock errorSink = new ThrowableSettableMock(); | ||
new JdkCompatibilityChecks(List.of("--add-opens", "java.base/java.nio=ALL-UNNAMED")).verifyAccessOpened(errorSink, "Connector 'snowflake'", ImmutableMultimap.of( | ||
"java.base", "java.nio", | ||
"java.base", "java.lang")); | ||
|
||
assertThat(errorSink.getThrowable()) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessage("Connector 'snowflake' requires additional JVM argument(s). Please add the following to the JVM configuration: '--add-opens=java.base/java.lang=ALL-UNNAMED'"); | ||
} | ||
|
||
@Test | ||
public void shouldSetErrorOnBinder() | ||
{ | ||
assertThatThrownBy(() -> Guice.createInjector(new TestingModule())) | ||
.isInstanceOf(CreationException.class) | ||
.hasMessageContaining("Unable to create injector, see the following errors:") | ||
.hasMessageContaining("1) Connector 'Testing' requires additional JVM argument(s). Please add the following to the JVM configuration: '--add-opens=java.base/java.non.existing=ALL-UNNAMED --add-opens=java.base/java.this.should.fail=ALL-UNNAMED'"); | ||
} | ||
|
||
@Test | ||
public void shouldNotThrowWhenAllAccessGrantsArePresent() | ||
{ | ||
ThrowableSettableMock errorSink = new ThrowableSettableMock(); | ||
new JdkCompatibilityChecks(List.of("--add-opens=java.base/java.nio=ALL-UNNAMED", "--add-opens java.base/java.lang=ALL-UNNAMED")).verifyAccessOpened(errorSink, "Connector 'snowflake'", ImmutableMultimap.of( | ||
"java.base", "java.nio", | ||
"java.base", "java.lang")); | ||
|
||
assertThat(errorSink.getThrowable()).isNull(); | ||
} | ||
|
||
private static class TestingModule | ||
implements Module | ||
{ | ||
@Override | ||
public void configure(Binder binder) | ||
{ | ||
verifyConnectorAccessOpened(binder, "Testing", ImmutableMultimap.of( | ||
"java.base", "java.non.existing", | ||
"java.base", "java.this.should.fail")); | ||
} | ||
} | ||
|
||
private static class ThrowableSettableMock | ||
implements JdkCompatibilityChecks.ThrowableSettable | ||
{ | ||
private Throwable throwable; | ||
|
||
@Override | ||
public void setThrowable(Throwable throwable) | ||
{ | ||
this.throwable = requireNonNull(throwable, "throwable is null"); | ||
} | ||
|
||
public Throwable getThrowable() | ||
{ | ||
return throwable; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
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.