-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract JDK compatibility check from BigQuery for reuse
This will be used in Snowflake connector too in the next commit. Co-authored-by: Mateusz "Serafin" Gajewski <[email protected]>
- Loading branch information
Showing
3 changed files
with
189 additions
and
35 deletions.
There are no files selected for viewing
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