Ver código fonte

[refactor] cache compiled regex pattern (#13208)

* [refactor] cache compiled regex pattern

* [refactor] cache compiled regex pattern

* [refactor] cache compiled regex pattern
youzipi 2 anos atrás
pai
commit
042ec74a24

+ 2 - 3
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/RegexUtils.java

@@ -26,7 +26,7 @@ import java.util.regex.Pattern;
  */
 public class RegexUtils {
 
-    private static final String LINUX_USERNAME_PATTERN = "^[a-zA-Z0-9_].{0,30}";
+    private static final Pattern LINUX_USERNAME_PATTERN = Pattern.compile("^[a-zA-Z0-9_].{0,30}");
 
     private RegexUtils() {
     }
@@ -37,8 +37,7 @@ public class RegexUtils {
      * @return boolean
      */
     public static boolean isValidLinuxUserName(String str) {
-        Pattern pattern = Pattern.compile(LINUX_USERNAME_PATTERN);
-        return pattern.matcher(str).matches();
+        return LINUX_USERNAME_PATTERN.matcher(str).matches();
     }
 
     public static String escapeNRT(String str) {

+ 2 - 1
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/TaskConstants.java

@@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.plugin.task.api;
 
 import java.time.Duration;
 import java.util.Set;
+import java.util.regex.Pattern;
 
 import com.google.common.collect.Sets;
 
@@ -473,7 +474,7 @@ public class TaskConstants {
     public static final int LOG_LINES = 500;
     public static final String NAMESPACE_NAME = "name";
     public static final String CLUSTER = "cluster";
-    public static final String COMMAND_SPLIT_REGEX = "[^\\s\"'`]+|\"([^\"]+)\"|'([^']+)'|`([^`]+)`";
+    public static final Pattern COMMAND_SPLIT_REGEX = Pattern.compile("[^\\s\"'`]+|\"([^\"]+)\"|'([^']+)'|`([^`]+)`");
 
     /**
      * conda config used by jupyter task plugin

+ 1 - 2
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/k8s/impl/K8sTaskExecutor.java

@@ -55,7 +55,6 @@ import java.util.Map;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 import org.slf4j.Logger;
 
@@ -116,7 +115,7 @@ public class K8sTaskExecutor extends AbstractK8sTaskExecutor {
         List<String> commands = new ArrayList<>();
 
         if (commandString != null) {
-            Matcher commandMatcher = Pattern.compile(COMMAND_SPLIT_REGEX).matcher(commandString.trim());
+            Matcher commandMatcher = COMMAND_SPLIT_REGEX.matcher(commandString.trim());
             while (commandMatcher.find()) {
                 commands.add(commandMatcher.group());
             }

+ 4 - 5
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/parser/ParameterUtils.java

@@ -47,9 +47,9 @@ public class ParameterUtils {
 
     private static final Logger logger = LoggerFactory.getLogger(ParameterUtils.class);
 
-    private static final String DATE_PARSE_PATTERN = "\\$\\[([^\\$\\]]+)]";
+    private static final Pattern DATE_PARSE_PATTERN = Pattern.compile("\\$\\[([^\\$\\]]+)]");
 
-    private static final String DATE_START_PATTERN = "^[0-9]";
+    private static final Pattern DATE_START_PATTERN = Pattern.compile("^[0-9]");
 
     private static final char PARAM_REPLACE_CHAR = '?';
 
@@ -253,15 +253,14 @@ public class ParameterUtils {
         if (templateStr == null) {
             return null;
         }
-        Pattern pattern = Pattern.compile(DATE_PARSE_PATTERN);
 
         StringBuffer newValue = new StringBuffer(templateStr.length());
 
-        Matcher matcher = pattern.matcher(templateStr);
+        Matcher matcher = DATE_PARSE_PATTERN.matcher(templateStr);
 
         while (matcher.find()) {
             String key = matcher.group(1);
-            if (Pattern.matches(DATE_START_PATTERN, key)) {
+            if (DATE_START_PATTERN.matcher(key).matches()) {
                 continue;
             }
             String value = TimePlaceholderUtils.getPlaceHolderTime(key, date);

+ 2 - 2
dolphinscheduler-task-plugin/dolphinscheduler-task-openmldb/src/main/java/org/apache/dolphinscheduler/plugin/task/openmldb/OpenmldbTask.java

@@ -49,6 +49,7 @@ public class OpenmldbTask extends PythonTask {
      */
     private static final String OPENMLDB_PYTHON = "python3";
     private static final Pattern PYTHON_PATH_PATTERN = Pattern.compile("/bin/python[\\d.]*$");
+    public static final Pattern SQL_PATTERN = Pattern.compile("\\S");
 
     /**
      * constructor
@@ -127,9 +128,8 @@ public class OpenmldbTask extends PythonTask {
 
         // split sql to list
         // skip the sql only has space characters
-        Pattern pattern = Pattern.compile("\\S");
         for (String sql : rawSqlScript.split(";")) {
-            if (pattern.matcher(sql).find()) {
+            if (SQL_PATTERN.matcher(sql).find()) {
                 sql = sql.replaceAll("\\n", "\\\\n");
                 builder.append("con.execute(\"").append(sql).append("\")\n");
             }