You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@hertzbeat.apache.org by zh...@apache.org on 2024/09/02 08:13:37 UTC
(hertzbeat) 01/02: refactor(script): switch to GraalVM for script execution
This is an automated email from the ASF dual-hosted git repository.
zhaoqingran pushed a commit to branch script
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
commit c07549d22ee32464aeafd3926e9566547005b5d0
Author: Logic <zq...@126.com>
AuthorDate: Mon Sep 2 09:45:58 2024 +0800
refactor(script): switch to GraalVM for script execution
Remove the usage of the Nashorn JavaScript engine and transition to the GraalVM JavaScriptengine for executing scripts in both the collector and common modules. This change involvesAPI modifications and dependency updates to utilize the more performant and feature-rich
GraalVM ecosystem.
---
collector/pom.xml | 11 ---------
.../collector/script/GraalJavaScriptExecutor.java | 26 +++++++--------------
.../collector/script/PluginScriptTest.java | 13 ++++++-----
common/pom.xml | 10 ++++++++
.../hertzbeat/common/script/ScriptExecutor.java | 12 ++++------
.../script/executor/JSR223ScriptExecutor.java | 27 ++++++----------------
6 files changed, 36 insertions(+), 63 deletions(-)
diff --git a/collector/pom.xml b/collector/pom.xml
index 5adb8164e..fea17e384 100644
--- a/collector/pom.xml
+++ b/collector/pom.xml
@@ -212,17 +212,6 @@
<artifactId>hivemq-mqtt-client</artifactId>
<version>1.3.3</version>
</dependency>
- <dependency>
- <groupId>org.graalvm.js</groupId>
- <artifactId>js</artifactId>
- <version>23.0.5</version>
- </dependency>
-
- <dependency>
- <groupId>org.graalvm.js</groupId>
- <artifactId>js-scriptengine</artifactId>
- <version>23.0.5</version>
- </dependency>
</dependencies>
<build>
diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/script/GraalJavaScriptExecutor.java b/collector/src/main/java/org/apache/hertzbeat/collector/script/GraalJavaScriptExecutor.java
index b3d4c22a4..59eab738e 100644
--- a/collector/src/main/java/org/apache/hertzbeat/collector/script/GraalJavaScriptExecutor.java
+++ b/collector/src/main/java/org/apache/hertzbeat/collector/script/GraalJavaScriptExecutor.java
@@ -44,24 +44,14 @@ public class GraalJavaScriptExecutor extends ScriptExecutor {
}
@Override
- public void load(String nodeId, String script) {
- try {
- scriptMap.put(nodeId, Source.create("js", (CharSequence) compile(script)));
- } catch (Exception e) {
- String errorMsg = String.format("Script loading error for node[%s], error msg: %s", nodeId, e.getMessage());
- throw new ScriptLoadException(errorMsg);
+ public Object executeScript(String scriptKey) {
+ Source source = scriptMap.get(scriptKey);
+ if (source == null) {
+ throw new RuntimeException("Script not found in cache");
}
- }
- @Override
- public void unLoad(String nodeId) {
- scriptMap.remove(nodeId);
- }
-
- @Override
- public Object executeScript(ScriptExecuteWrap wrap) {
try (Context context = Context.newBuilder().allowAllAccess(true).engine(this.engine).build()) {
- Value value = context.eval(scriptMap.get(wrap.getNodeId()));
+ Value value = context.eval(source);
if (value.isBoolean()) {
return value.asBoolean();
} else if (value.isNumber()) {
@@ -88,9 +78,9 @@ public class GraalJavaScriptExecutor extends ScriptExecutor {
@Override
public Object compile(String script) throws Exception {
String wrapScript = String.format("function process(){ %s } process();", script);
- try (Context context = Context.newBuilder().allowAllAccess(true).engine(engine).build()) {
- context.parse(Source.create("js", wrapScript));
- }
+ Source source = Source.create("js", wrapScript);
+ // Cache the parsed script
+ scriptMap.put(script, source);
return wrapScript;
}
}
diff --git a/collector/src/test/java/org/apache/hertzbeat/collector/script/PluginScriptTest.java b/collector/src/test/java/org/apache/hertzbeat/collector/script/PluginScriptTest.java
index e2049660b..48748e8ac 100644
--- a/collector/src/test/java/org/apache/hertzbeat/collector/script/PluginScriptTest.java
+++ b/collector/src/test/java/org/apache/hertzbeat/collector/script/PluginScriptTest.java
@@ -22,6 +22,8 @@ package org.apache.hertzbeat.collector.script;
import org.apache.hertzbeat.common.constants.ScriptTypeEnum;
import org.apache.hertzbeat.common.script.ScriptExecutor;
import org.apache.hertzbeat.common.support.valid.ScriptValidator;
+import org.graalvm.polyglot.Context;
+import org.graalvm.polyglot.Source;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -97,13 +99,12 @@ class PluginScriptTest {
}
var result = addByArray([a,b,c,d]);
- if (result == 11) {
- return true;
- } else{
- return false;
+ return result;
""";
- Object compile = scriptExecutor.compile(script);
- scriptExecutor.
+ scriptExecutor.compile(script);
+ Object object = scriptExecutor.executeScript(script);
+ Assertions.assertEquals(11, object);
+ Assertions.assertNotEquals(12, object);
}
}
diff --git a/common/pom.xml b/common/pom.xml
index 770733af8..de8768bba 100644
--- a/common/pom.xml
+++ b/common/pom.xml
@@ -128,5 +128,15 @@
<artifactId>jutf7</artifactId>
<version>${jutf7.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.graalvm.js</groupId>
+ <artifactId>js</artifactId>
+ <version>23.0.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.graalvm.js</groupId>
+ <artifactId>js-scriptengine</artifactId>
+ <version>23.0.5</version>
+ </dependency>
</dependencies>
</project>
diff --git a/common/src/main/java/org/apache/hertzbeat/common/script/ScriptExecutor.java b/common/src/main/java/org/apache/hertzbeat/common/script/ScriptExecutor.java
index 9c63844e7..74986f70f 100644
--- a/common/src/main/java/org/apache/hertzbeat/common/script/ScriptExecutor.java
+++ b/common/src/main/java/org/apache/hertzbeat/common/script/ScriptExecutor.java
@@ -18,6 +18,7 @@
package org.apache.hertzbeat.common.script;
import org.apache.hertzbeat.common.constants.ScriptTypeEnum;
+import org.graalvm.polyglot.Source;
/**
* script executor interface
@@ -25,16 +26,11 @@ import org.apache.hertzbeat.common.constants.ScriptTypeEnum;
*/
public abstract class ScriptExecutor {
- public abstract void load(String nodeId, String script);
-
- public abstract void unLoad(String nodeId);
-
-
- public Object execute(ScriptExecuteWrap wrap) throws Exception{
- return executeScript(wrap);
+ public Object execute(String scriptKey) throws Exception{
+ return executeScript(scriptKey);
}
- public abstract Object executeScript(ScriptExecuteWrap wrap) throws Exception;
+ public abstract Object executeScript(String scriptKey) throws Exception;
public abstract void cleanCache();
diff --git a/common/src/main/java/org/apache/hertzbeat/common/script/executor/JSR223ScriptExecutor.java b/common/src/main/java/org/apache/hertzbeat/common/script/executor/JSR223ScriptExecutor.java
index a4284140d..092c3503e 100644
--- a/common/src/main/java/org/apache/hertzbeat/common/script/executor/JSR223ScriptExecutor.java
+++ b/common/src/main/java/org/apache/hertzbeat/common/script/executor/JSR223ScriptExecutor.java
@@ -29,6 +29,7 @@ import javax.script.SimpleBindings;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.common.script.ScriptExecutor;
import org.apache.hertzbeat.common.support.exception.ScriptLoadException;
+import org.graalvm.polyglot.Source;
/**
* JSR223 script engine
@@ -53,31 +54,17 @@ public abstract class JSR223ScriptExecutor extends ScriptExecutor {
return script;
}
- @Override
- public void load(String nodeId, String script) {
- try {
- compiledScriptMap.put(nodeId, (CompiledScript) compile(script));
- }
- catch (Exception e) {
- String errorMsg = String.format("script loading error for node[%s], error msg:%s", nodeId, e.getMessage());
- throw new ScriptLoadException(errorMsg);
- }
- }
- @Override
- public void unLoad(String nodeId) {
- compiledScriptMap.remove(nodeId);
- }
@Override
- public Object executeScript(ScriptExecuteWrap wrap) throws Exception {
- if (!compiledScriptMap.containsKey(wrap.getNodeId())) {
- String errorMsg = String.format("script for node[%s] is not loaded", wrap.getNodeId());
- throw new ScriptLoadException(errorMsg);
- }
+ public Object executeScript(String scriptKey) throws Exception {
+// if (!compiledScriptMap.containsKey(wrap.getNodeId())) {
+// String errorMsg = String.format("script for node[%s] is not loaded", wrap.getNodeId());
+// throw new ScriptLoadException(errorMsg);
+// }
- CompiledScript compiledScript = compiledScriptMap.get(wrap.getNodeId());
+ CompiledScript compiledScript = compiledScriptMap.get(scriptKey);
Bindings bindings = new SimpleBindings();
return compiledScript.eval(bindings);
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@hertzbeat.apache.org
For additional commands, e-mail: notifications-help@hertzbeat.apache.org