forked from opensearch-project/opensearch-migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade JDK toolchain and target version for most packages to 17 to g…
…et GraalJS+LiquidJS working via a transformer. There's a rough test in place that shows the relative performance between Liquid.js and a raw javascript call. The results on my mac were about 300x slower to use the templating solution than to just run JSON.stringify(). /Library/Java/JavaVirtualMachines/amazon-corretto-17.jdk/Contents/Home/bin/java -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI ... org.opensearch.migrations.transform.LiquidJsTransformerTest [To redirect Truffle log output to a file use one of the following options: * '--log.file=<path>' if the option is passed using a guest language launcher. * '-Dpolyglot.log.file=<path>' if the option is passed using the host Java launcher. * Configure logging using the polyglot embedding API.] [engine] WARNING: The polyglot engine uses a fallback runtime that does not support runtime compilation to native code. Execution without runtime compilation will negatively impact the guest application performance. The following cause was found: Libgraal compilation is not available on this JVM. Alternatively, the org.graalvm.compiler:compiler module can be put on the --upgrade-module-path. For more information see: https://www.graalvm.org/latest/reference-manual/embed-languages/#runtime-optimization-support. To disable this warning use the '--engine.WarnInterpreterOnly=false' option or the '-Dpolyglot.engine.WarnInterpreterOnly=false' system property. [To redirect Truffle log output to a file use one of the following options: * '--log.file=<path>' if the option is passed using a guest language launcher. * '-Dpolyglot.log.file=<path>' if the option is passed using the host Java launcher. * Configure logging using the polyglot embedding API.] [engine] WARNING: The polyglot engine uses a fallback runtime that does not support runtime compilation to native code. Execution without runtime compilation will negatively impact the guest application performance. The following cause was found: Libgraal compilation is not available on this JVM. Alternatively, the org.graalvm.compiler:compiler module can be put on the --upgrade-module-path. For more information see: https://www.graalvm.org/latest/reference-manual/embed-languages/#runtime-optimization-support. To disable this warning use the '--engine.WarnInterpreterOnly=false' option or the '-Dpolyglot.engine.WarnInterpreterOnly=false' system property. [INFO ] 2024-12-20 04:11:56,654825 [main] LiquidJsTransformerTest - Run 0: PT4.052694709S [INFO ] 2024-12-20 04:11:59,694596 [main] LiquidJsTransformerTest - Run 1: PT3.039395S [INFO ] 2024-12-20 04:12:02,747087 [main] LiquidJsTransformerTest - Run 2: PT3.052563917S [INFO ] 2024-12-20 04:12:05,823644 [main] LiquidJsTransformerTest - Run 3: PT3.076653792S [INFO ] 2024-12-20 04:12:08,833241 [main] LiquidJsTransformerTest - Run 4: PT3.009687375S [To redirect Truffle log output to a file use one of the following options: * '--log.file=<path>' if the option is passed using a guest language launcher. * '-Dpolyglot.log.file=<path>' if the option is passed using the host Java launcher. * Configure logging using the polyglot embedding API.] [engine] WARNING: The polyglot engine uses a fallback runtime that does not support runtime compilation to native code. Execution without runtime compilation will negatively impact the guest application performance. The following cause was found: Libgraal compilation is not available on this JVM. Alternatively, the org.graalvm.compiler:compiler module can be put on the --upgrade-module-path. For more information see: https://www.graalvm.org/latest/reference-manual/embed-languages/#runtime-optimization-support. To disable this warning use the '--engine.WarnInterpreterOnly=false' option or the '-Dpolyglot.engine.WarnInterpreterOnly=false' system property. [INFO ] 2024-12-20 04:12:08,906295 [main] LiquidJsTransformerTest - Run 0: PT0.021998S [INFO ] 2024-12-20 04:12:08,916581 [main] LiquidJsTransformerTest - Run 1: PT0.010198S [INFO ] 2024-12-20 04:12:08,927812 [main] LiquidJsTransformerTest - Run 2: PT0.011137708S [INFO ] 2024-12-20 04:12:08,938391 [main] LiquidJsTransformerTest - Run 3: PT0.010506958S [INFO ] 2024-12-20 04:12:08,948759 [main] LiquidJsTransformerTest - Run 4: PT0.010288125S [INFO ] 2024-12-20 04:12:08,959167 [main] LiquidJsTransformerTest - Run 5: PT0.010324625S Signed-off-by: Greg Schohn <greg.schohn@gmail.com>
- Loading branch information
1 parent
d1cf844
commit b00c63b
Showing
21 changed files
with
286 additions
and
27 deletions.
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
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
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
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
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
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
38 changes: 38 additions & 0 deletions
38
...mation/transformationPlugins/jsonMessageTransformers/jsonLiquidJSTransformer/build.gradle
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,38 @@ | ||
plugins { | ||
id 'org.opensearch.migrations.java-library-conventions' | ||
id 'io.freefair.lombok' | ||
id 'de.undercouch.download' version '5.4.0' // For downloading LiquidJS | ||
} | ||
|
||
def graalVersion = '24.1.1' | ||
|
||
dependencies { | ||
implementation project(':transformation:transformationPlugins:jsonMessageTransformers:jsonMessageTransformerInterface') | ||
|
||
implementation group: 'com.google.guava', name: 'guava' | ||
implementation group: 'org.slf4j', name: 'slf4j-api' | ||
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind' | ||
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core' | ||
implementation group: 'org.codehaus.groovy', name: 'groovy-all', version: '3.0.13' | ||
implementation group: "org.graalvm.js", name: 'js', version: "${graalVersion}" | ||
implementation group: "org.graalvm.js", name: 'js-scriptengine', "version": "${graalVersion}" | ||
|
||
testImplementation project(':TrafficCapture:trafficReplayer') | ||
testImplementation project(':transformation:transformationPlugins:jsonMessageTransformers:jsonMessageTransformerLoaders') | ||
testImplementation testFixtures(project(path: ':testHelperFixtures')) | ||
testImplementation testFixtures(project(path: ':TrafficCapture:trafficReplayer')) | ||
|
||
testImplementation group:'org.junit.jupiter', name:'junit-jupiter-api' | ||
testImplementation group:'org.junit.jupiter', name:'junit-jupiter-params' | ||
testImplementation group:'org.slf4j', name: 'slf4j-api' | ||
testRuntimeOnly group:'org.junit.jupiter', name:'junit-jupiter-engine' | ||
} | ||
|
||
// Task to download LiquidJS | ||
task downloadLiquidJs(type: Download) { | ||
src 'https://cdn.jsdelivr.net/npm/liquidjs/dist/liquid.browser.min.js' | ||
dest new File(projectDir, 'src/main/resources/liquid.min.js') | ||
overwrite true | ||
} | ||
|
||
processResources.dependsOn downloadLiquidJs |
100 changes: 100 additions & 0 deletions
100
...dJSTransformer/src/main/java/org/opensearch/migrations/transform/LiquidJSTransformer.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,100 @@ | ||
package org.opensearch.migrations.transform; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.UnaryOperator; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.io.Resources; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.graalvm.polyglot.Context; | ||
import org.graalvm.polyglot.Value; | ||
import org.graalvm.polyglot.proxy.ProxyExecutable; | ||
import org.graalvm.polyglot.proxy.ProxyObject; | ||
|
||
@Slf4j | ||
public class LiquidJSTransformer implements IJsonTransformer { | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
private final Context context; | ||
private final UnaryOperator<Map<String, Object>> bindingsProvider; | ||
|
||
public LiquidJSTransformer(String templateString, | ||
UnaryOperator<Map<String, Object>> bindingsProvider) throws IOException { | ||
this.bindingsProvider = bindingsProvider; | ||
this.context = Context.newBuilder("js") | ||
.allowAllAccess(true) | ||
.build(); | ||
|
||
context.getBindings("js").putMember("userProvidedTemplate", templateString); | ||
|
||
// Load LiquidJS javascript script | ||
var liquidJsContent = Resources.toString(Resources.getResource("liquid.min.js"), StandardCharsets.UTF_8); | ||
|
||
// First evaluate the library | ||
context.eval("js", liquidJsContent); | ||
|
||
// Then use the global Liquid object that was created | ||
context.eval("js", "" + | ||
"const engine = new liquidjs.Liquid();" | ||
+ "var parsedTemplate = engine.parse(userProvidedTemplate);" | ||
); | ||
} | ||
|
||
public CompletableFuture<Object> transformJsonFuture(Map<String, Object> incomingJson) { | ||
var userBindings = bindingsProvider.apply(incomingJson); | ||
context.getBindings("js").putMember("incomingJson", ProxyObject.fromMap(userBindings)); | ||
Value promise = context.eval("js", "engine.render(parsedTemplate, incomingJson);"); | ||
return fromPromise(promise); | ||
} | ||
|
||
@SneakyThrows | ||
public String runJavascript(Map<String, Object> incomingJson) { | ||
context.getBindings("js").putMember("incomingJson", ProxyObject.fromMap(incomingJson)); | ||
var resultPromise = context.eval("js", "" + | ||
"JSON.stringify(incomingJson);\n"); | ||
var resultStr = (String) fromPromise(resultPromise).get(); | ||
log.atTrace().setMessage("resultStr={}").addArgument(resultStr).log(); | ||
return resultStr; | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public Map<String, Object> transformJson(Map<String, Object> incomingJson) { | ||
var resultStr = (String) transformJsonFuture(incomingJson).get(); | ||
log.atTrace().setMessage("resultStr={}").addArgument(resultStr).log(); | ||
var parsedObj = (Map<String,Object>) objectMapper.readValue(resultStr, LinkedHashMap.class); | ||
return parsedObj; | ||
} | ||
|
||
@SuppressWarnings("Unchecked") | ||
private static <T> CompletableFuture<T> fromPromise(Value value) { | ||
CompletableFuture<T> future = new CompletableFuture<>(); | ||
|
||
if (value.canInvokeMember("then")) { | ||
// It's a Promise - handle with then() | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then | ||
value.invokeMember("then", | ||
(ProxyExecutable) onFulfilledArg -> { | ||
T result = (T) onFulfilledArg[0].as(Object.class); | ||
future.complete(result); | ||
return null; | ||
}, | ||
(ProxyExecutable) failureArgs -> { | ||
Throwable error = new RuntimeException(failureArgs[0].toString()); | ||
future.completeExceptionally(error); | ||
return null; | ||
} | ||
); | ||
} else { | ||
// It's a direct value - complete immediately | ||
T result = (T) value.as(Object.class); | ||
future.complete(result); | ||
} | ||
return future; | ||
} | ||
} |
Oops, something went wrong.