-
Notifications
You must be signed in to change notification settings - Fork 11
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
Enable SPI functionality in GraalVM native mode #20
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2737b04
Enable SPI functionality in native images
pivovarit 66b4206
Add a README note
pivovarit 91836fe
Add comments for Target_ServiceLoader
pivovarit cda8a73
Add comments for ServiceLoaderUtils
pivovarit 44d4ae2
Use simple comments instead of JavaDoc
pivovarit a387d05
Add comments for Target_JCacheDetector
pivovarit e01cd46
Improve README
pivovarit a2cc065
Handle system classloader
pivovarit 90b7f17
Remove unused getters
pivovarit File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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
57 changes: 57 additions & 0 deletions
57
runtime/src/main/java/io/quarkus/hazelcast/client/runtime/graal/ServiceLoaderUtils.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,57 @@ | ||
package io.quarkus.hazelcast.client.runtime.graal; | ||
|
||
import com.hazelcast.logging.ILogger; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import static com.hazelcast.internal.nio.IOUtil.closeResource; | ||
|
||
final class ServiceLoaderUtils { | ||
private ServiceLoaderUtils() { | ||
} | ||
|
||
/* | ||
Expanded version of {@link com.hazelcast.internal.util.ServiceLoader#parse(ServiceLoader.URLDefinition) | ||
that's additionally parameterized with {@link ILogger} } | ||
*/ | ||
static Set<Target_ServiceDefinition> parse(URL url, ClassLoader classLoader, ILogger logger) { | ||
try { | ||
Set<Target_ServiceDefinition> names = new HashSet<>(); | ||
BufferedReader r = null; | ||
try { | ||
r = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)); | ||
while (true) { | ||
String line = r.readLine(); | ||
if (line == null) { | ||
break; | ||
} | ||
int comment = line.indexOf('#'); | ||
if (comment >= 0) { | ||
line = line.substring(0, comment); | ||
} | ||
String name = line.trim(); | ||
if (name.length() == 0) { | ||
continue; | ||
} | ||
names.add(new Target_ServiceDefinition(name, classLoader)); | ||
} | ||
} finally { | ||
closeResource(r); | ||
} | ||
return names; | ||
} catch (Exception e) { | ||
logger.severe(e); | ||
} | ||
return Collections.emptySet(); | ||
} | ||
|
||
static ClassLoader resolveClassloader(ClassLoader classLoader) { | ||
return classLoader == null ? Thread.currentThread().getContextClassLoader() : classLoader; | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
...ime/src/main/java/io/quarkus/hazelcast/client/runtime/graal/Target_ServiceDefinition.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,41 @@ | ||
package io.quarkus.hazelcast.client.runtime.graal; | ||
|
||
import com.oracle.svm.core.annotate.Substitute; | ||
import com.oracle.svm.core.annotate.TargetClass; | ||
|
||
import static com.hazelcast.internal.util.Preconditions.isNotNull; | ||
|
||
@Substitute | ||
@TargetClass(className = "com.hazelcast.internal.util.ServiceLoader", innerClass = "ServiceDefinition") | ||
final class Target_ServiceDefinition { | ||
private final String className; | ||
private final ClassLoader classLoader; | ||
|
||
public Target_ServiceDefinition(String className, ClassLoader classLoader) { | ||
this.className = isNotNull(className, "className"); | ||
this.classLoader = isNotNull(classLoader, "classLoader"); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
Target_ServiceDefinition that = (Target_ServiceDefinition) o; | ||
if (!classLoader.equals(that.classLoader)) { | ||
return false; | ||
} | ||
return className.equals(that.className); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = className.hashCode(); | ||
result = 31 * result + classLoader.hashCode(); | ||
return result; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
runtime/src/main/java/io/quarkus/hazelcast/client/runtime/graal/Target_ServiceLoader.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,69 @@ | ||
package io.quarkus.hazelcast.client.runtime.graal; | ||
|
||
import com.hazelcast.internal.util.ServiceLoader; | ||
import com.hazelcast.logging.ILogger; | ||
import com.hazelcast.logging.Logger; | ||
import com.oracle.svm.core.annotate.Alias; | ||
import com.oracle.svm.core.annotate.Substitute; | ||
import com.oracle.svm.core.annotate.TargetClass; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import static io.quarkus.hazelcast.client.runtime.graal.ServiceLoaderUtils.parse; | ||
import static io.quarkus.hazelcast.client.runtime.graal.ServiceLoaderUtils.resolveClassloader; | ||
|
||
@TargetClass(ServiceLoader.class) | ||
public final class Target_ServiceLoader { | ||
|
||
@Alias | ||
private static ILogger LOGGER; | ||
|
||
/* | ||
Hazelcast SPI relies on the classpath presence of multiple _META-INF/services/com.foo.Bar_ files (for example, _com.hazelcast.spi.discovery.multicast.DiscoveryStrategyFactory_). | ||
|
||
However, the internal implementation of ServiceLoader doesn't play along with Substrate since | ||
it doesn't really feature a _normal_ classpath nor classloading but a mere _simulation_ of these. | ||
|
||
URLs to classpath resources located in different jars normally look like: | ||
|
||
jar:file:/work/application/lib/com.hazelcast.hazelcast-4.0.1.jar!/META-INF/services/com.hazelcast.spi.discovery.DiscoveryStrategyFactory | ||
jar:file:/work/application/lib/com.hazelcast.hazelcast-kubernetes-2.0.1.jar!/META-INF/services/com.hazelcast.spi.discovery.DiscoveryStrategyFactory | ||
|
||
but under Substrate, they become _resources_ and share the same URL (but `URL#openStream` leads to different content) which requires special handling: | ||
|
||
resource:META-INF/services/com.hazelcast.spi.discovery.DiscoveryStrategyFactory | ||
resource:META-INF/services/com.hazelcast.spi.discovery.DiscoveryStrategyFactory | ||
|
||
In order to make it work under GraalVM, we need to give up extra URL processing and don't enforce uniqueness for URLs, but for their content instead. | ||
We need to make sure that we're properly handling system classloader represented as (null) | ||
*/ | ||
@Substitute | ||
private static Set<Target_ServiceDefinition> getServiceDefinitions(String factoryId, ClassLoader classLoader) { | ||
ClassLoader actual = resolveClassloader(classLoader); | ||
|
||
Set<Target_ServiceDefinition> services = new HashSet<>(); | ||
|
||
try { | ||
Enumeration<URL> resources = actual | ||
.getResources("META-INF/services/" + factoryId); | ||
|
||
while (resources.hasMoreElements()) { | ||
services.addAll(parse(resources.nextElement(), actual, LOGGER)); | ||
} | ||
} catch (IOException e) { | ||
LOGGER.severe(e); | ||
} | ||
|
||
if (services.isEmpty()) { | ||
Logger.getLogger(ServiceLoader.class).finest( | ||
"Service loader could not load 'META-INF/services/" + factoryId + "'. It may be empty or does not exist."); | ||
} | ||
return services; | ||
} | ||
} | ||
|
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.
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.
I will add docs around the Graal substitution and explain the problem there as well.
I'm working on tests at the moment. Unit testing is useless, we need an integration test that loads a custom extension via SPI
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.
Actually, this is covered by Hasan's test suite