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 ca571edb4a7917565b77f6f8d28acc6a1def06a7
Author: zqr10159 <[email protected]>
AuthorDate: Thu Sep 5 17:19:47 2024 +0800

    refactor(script-executor): enhance caching and compilation logic
    
    Improve the GraalJavaScriptExecutor by adding a compile method that 
utilizes script caching.
    This change enhances the script execution workflow by checking the cache 
for pre-compiled
    scripts before attempting to compile and cache them. Additionally, 
refactored JavaScriptExecutor
    and Jsr223ScriptExecutor for clarity and maintainability. 
ScriptExecutorPluginImpl has been
    updated to include logging and more robust handling of script type and 
script null checks.
---
 .../script/executor/GraalJavaScriptExecutor.java   | 26 ++++++--
 .../script/executor/Jsr223ScriptExecutor.java      | 69 ----------------------
 .../ScriptUtil.java}                               | 34 +++++++----
 .../plugin/impl/ScriptExecutorPluginImpl.java      | 56 ++++++++++++------
 4 files changed, 80 insertions(+), 105 deletions(-)

diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/script/executor/GraalJavaScriptExecutor.java
 
b/common/src/main/java/org/apache/hertzbeat/common/script/executor/GraalJavaScriptExecutor.java
index bc0d68d75..33738883a 100644
--- 
a/common/src/main/java/org/apache/hertzbeat/common/script/executor/GraalJavaScriptExecutor.java
+++ 
b/common/src/main/java/org/apache/hertzbeat/common/script/executor/GraalJavaScriptExecutor.java
@@ -19,9 +19,13 @@
 
 package org.apache.hertzbeat.common.script.executor;
 
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.hertzbeat.common.cache.CacheFactory;
 import org.apache.hertzbeat.common.constants.ScriptTypeConstants;
 import org.apache.hertzbeat.common.script.ScriptExecutor;
+import org.apache.hertzbeat.common.util.ScriptUtil;
 import org.graalvm.polyglot.Context;
 import org.graalvm.polyglot.Engine;
 import org.graalvm.polyglot.Source;
@@ -34,19 +38,28 @@ import org.springframework.stereotype.Component;
  */
 
 @Component
+@Slf4j
 public class GraalJavaScriptExecutor extends ScriptExecutor {
 
     private final Engine engine;
 
     public GraalJavaScriptExecutor() {
+        log.info("Initializing GraalJavaScriptExecutor");
         this.engine = Engine.create();
     }
 
     @Override
-    public Object executeScript(String scriptKey) {
+    public Object executeScript(String script) {
+        String scriptKey = ScriptUtil.generateScriptKey(script);
         Source source = (Source) CacheFactory.getScriptCache().get(scriptKey);
         if (source == null) {
-            throw new RuntimeException("Script not found in cache");
+            log.info("Script not found in cache, compiling...");
+            try {
+                compile(script);
+                source = (Source) CacheFactory.getScriptCache().get(scriptKey);
+            } catch (Exception e) {
+                throw new RuntimeException("Error compiling script", e);
+            }
         }
 
         try (Context context = 
Context.newBuilder().allowAllAccess(true).engine(this.engine).build()) {
@@ -76,12 +89,15 @@ public class GraalJavaScriptExecutor extends ScriptExecutor 
{
 
     @Override
     public Object compile(String script) throws Exception {
+        String scriptKey = ScriptUtil.generateScriptKey(script);
         String wrapScript = String.format("function process(){ %s } 
process();", script);
         Context context = 
Context.newBuilder().allowAllAccess(true).engine(engine).build();
         Source source = Source.create("js", wrapScript);
         context.parse(source);
-        // Cache the parsed script
-        CacheFactory.getScriptCache().put(script, source);
+        CacheFactory.getScriptCache().put(scriptKey, source);
+        log.info("Javascript script has been cached with key: {}", scriptKey);
         return wrapScript;
     }
-}
+
+
+}
\ No newline at end of file
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
deleted file mode 100644
index ff8b42c5d..000000000
--- 
a/common/src/main/java/org/apache/hertzbeat/common/script/executor/Jsr223ScriptExecutor.java
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.hertzbeat.common.script.executor;
-
-
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import javax.script.Bindings;
-import javax.script.Compilable;
-import javax.script.CompiledScript;
-import javax.script.ScriptEngine;
-import javax.script.ScriptException;
-import javax.script.SimpleBindings;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.hertzbeat.common.script.ScriptExecutor;
-
-/**
- * JSR223 script engine
- * This code references dromara/liteflow, thanks to the author Bryan.
- */
-
-@Slf4j
-public abstract class Jsr223ScriptExecutor extends ScriptExecutor {
-
-    private ScriptEngine scriptEngine;
-
-    private final Map<String, CompiledScript> compiledScriptMap = new 
ConcurrentHashMap<>();
-
-    protected String convertScript(String script) {
-        return script;
-    }
-
-    @Override
-    public Object executeScript(String scriptKey) throws Exception {
-
-        CompiledScript compiledScript = compiledScriptMap.get(scriptKey);
-        Bindings bindings = new SimpleBindings();
-
-        return compiledScript.eval(bindings);
-    }
-
-    @Override
-    public void cleanCache() {
-        compiledScriptMap.clear();
-    }
-
-    @Override
-    public Object compile(String script) throws ScriptException {
-        if (scriptEngine == null) {
-            log.error("script engine has not init");
-        }
-        return ((Compilable) scriptEngine).compile(convertScript(script));
-    }
-}
diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/script/executor/JavaScriptExecutor.java
 b/common/src/main/java/org/apache/hertzbeat/common/util/ScriptUtil.java
similarity index 52%
rename from 
common/src/main/java/org/apache/hertzbeat/common/script/executor/JavaScriptExecutor.java
rename to common/src/main/java/org/apache/hertzbeat/common/util/ScriptUtil.java
index 78f997b1b..4f167d849 100644
--- 
a/common/src/main/java/org/apache/hertzbeat/common/script/executor/JavaScriptExecutor.java
+++ b/common/src/main/java/org/apache/hertzbeat/common/util/ScriptUtil.java
@@ -15,22 +15,32 @@
  * limitations under the License.
  */
 
-package org.apache.hertzbeat.common.script.executor;
+package org.apache.hertzbeat.common.util;
 
-import org.apache.hertzbeat.common.constants.ScriptTypeConstants;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 
 /**
- * This class is used to execute JavaScript code in the plugin.
- * This code references liteflow, thanks to the author Bryan.
+ * Script Util
  */
-public class JavaScriptExecutor extends Jsr223ScriptExecutor {
-    @Override
-    protected String convertScript(String script) {
-        return String.format("function process(){%s} process();", script);
-    }
+public class ScriptUtil {
 
-    @Override
-    public String scriptType() {
-        return ScriptTypeConstants.JAVASCRIPT;
+    /**
+     *
+     * @param script script
+     * @return key
+     */
+    public static String generateScriptKey(String script) {
+        try {
+            MessageDigest md = MessageDigest.getInstance("MD5");
+            byte[] hashBytes = md.digest(script.getBytes());
+            StringBuilder sb = new StringBuilder();
+            for (byte b : hashBytes) {
+                sb.append(String.format("%02x", b));
+            }
+            return sb.toString();
+        } catch (NoSuchAlgorithmException e) {
+            throw new RuntimeException("MD5 algorithm not found", e);
+        }
     }
 }
diff --git 
a/plugin/src/main/java/org/apache/hertzbeat/plugin/impl/ScriptExecutorPluginImpl.java
 
b/plugin/src/main/java/org/apache/hertzbeat/plugin/impl/ScriptExecutorPluginImpl.java
index 8a0c50cad..c1253a763 100644
--- 
a/plugin/src/main/java/org/apache/hertzbeat/plugin/impl/ScriptExecutorPluginImpl.java
+++ 
b/plugin/src/main/java/org/apache/hertzbeat/plugin/impl/ScriptExecutorPluginImpl.java
@@ -19,7 +19,6 @@
 
 package org.apache.hertzbeat.plugin.impl;
 
-import java.util.List;
 import java.util.Map;
 import java.util.function.Function;
 import java.util.stream.Collectors;
@@ -27,39 +26,58 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.hertzbeat.common.entity.alerter.Alert;
 import org.apache.hertzbeat.common.entity.plugin.PluginContext;
 import org.apache.hertzbeat.common.script.ScriptExecutor;
+import org.apache.hertzbeat.common.support.SpringContextHolder;
 import org.apache.hertzbeat.plugin.PostAlertPlugin;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
 import org.springframework.stereotype.Component;
 
 @Component
 @Slf4j
 public class ScriptExecutorPluginImpl implements PostAlertPlugin {
 
-    private final Map<String, ScriptExecutor> scriptExecutors;
+    public ScriptExecutorPluginImpl() {}
 
-    @Autowired
-    public ScriptExecutorPluginImpl(List<ScriptExecutor> executors) {
-        this.scriptExecutors = executors.stream()
+    private Map<String, ScriptExecutor> getScriptExecutors() {
+        ApplicationContext context = 
SpringContextHolder.getApplicationContext();
+        Map<String, ScriptExecutor> beansOfType = 
context.getBeansOfType(ScriptExecutor.class);
+        return beansOfType.values().stream()
                 .collect(Collectors.toMap(ScriptExecutor::scriptType, 
Function.identity()));
     }
 
+    public ScriptExecutor getScriptExecutorByType(String type) {
+        return getScriptExecutors().get(type);
+    }
+
+
     @Override
     public void execute(Alert alert, PluginContext pluginContext) {
-        String scriptType = pluginContext.param().getString("type", null);
+        log.info("Executing ScriptExecutorPluginImpl");
+        String type = pluginContext.param().getString("type", null);
+        if (type == null) {
+            log.warn("Script type is null");
+            return;
+        }
+
+        ScriptExecutor scriptExecutor = getScriptExecutorByType(type);
+        if (scriptExecutor == null) {
+            log.warn("No executor found for script type: {}", type);
+            return;
+        }
 
         String script = pluginContext.param().getString("script", null);
-        if (script != null && scriptType != null) {
-            ScriptExecutor scriptExecutor = scriptExecutors.get(scriptType);
-            if (scriptExecutor == null) {
-                throw new IllegalArgumentException("Unsupported script type: " 
+ scriptType);
-            }
-            try {
-                Object result = scriptExecutor.execute(script);
-                log.info("Script execution result: {}", result);
-            } catch (Exception e) {
-                log.error("Failed to execute script: {}", script, e);
-                throw new RuntimeException(e);
-            }
+        if (script == null) {
+            log.warn("Script is null");
+            return;
+        }
+
+        try {
+            Object result = scriptExecutor.execute(script);
+            log.info("Script execution result: {}", result);
+        } catch (Exception e) {
+            log.error("Failed to execute script: {}", script, e);
+            throw new RuntimeException("Script execution failed", e);
         }
     }
+
 }
+


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to