You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@linkis.apache.org by pe...@apache.org on 2022/08/08 08:11:54 UTC
[incubator-linkis] branch dev-1.2.0 updated: ${} 2 #{} / Fix the dead loop (#2631)
This is an automated email from the ASF dual-hosted git repository.
peacewong pushed a commit to branch dev-1.2.0
in repository https://gitbox.apache.org/repos/asf/incubator-linkis.git
The following commit(s) were added to refs/heads/dev-1.2.0 by this push:
new 60e7cf8eb ${} 2 #{} / Fix the dead loop (#2631)
60e7cf8eb is described below
commit 60e7cf8eb5fd16247e8056aa87306da4fd98ae30
Author: 野鹿 <35...@qq.com>
AuthorDate: Mon Aug 8 16:11:48 2022 +0800
${} 2 #{} / Fix the dead loop (#2631)
* ${} 2 #{} / Fix the dead loop
* del
---
.../common/utils/VariableOperationUtils.java | 111 ++-------------------
.../common/variable/VariableOperationTest.java | 43 +++++++-
2 files changed, 47 insertions(+), 107 deletions(-)
diff --git a/linkis-commons/linkis-common/src/main/java/org/apache/linkis/common/utils/VariableOperationUtils.java b/linkis-commons/linkis-common/src/main/java/org/apache/linkis/common/utils/VariableOperationUtils.java
index 170a3ba8d..657cd0bdf 100644
--- a/linkis-commons/linkis-common/src/main/java/org/apache/linkis/common/utils/VariableOperationUtils.java
+++ b/linkis-commons/linkis-common/src/main/java/org/apache/linkis/common/utils/VariableOperationUtils.java
@@ -18,8 +18,6 @@ package org.apache.linkis.common.utils;
import org.apache.linkis.common.exception.VariableOperationFailedException;
-import org.apache.commons.lang3.StringUtils;
-
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -36,9 +34,10 @@ import java.util.Map;
/** support variable operation #{yyyyMMdd%-1d}/#{yyyy-MM-01%-2M} Date: 2021/5/7 11:10 */
public class VariableOperationUtils {
- private static final String DOLLAR = "#";
+ private static final String DOLLAR = "&";
private static final String PLACEHOLDER_SPLIT = "%";
private static final String PLACEHOLDER_LEFT = "{";
+ private static final String LEFT = DOLLAR + PLACEHOLDER_LEFT;
private static final String PLACEHOLDER_RIGHT = "}";
private static final String CYCLE_YEAR = "y";
private static final String CYCLE_MONTH = "M";
@@ -106,17 +105,14 @@ public class VariableOperationUtils {
private static String replace(ZonedDateTime dateTime, String str)
throws VariableOperationFailedException {
StringBuilder buffer = new StringBuilder(str);
- int startIndex = str.indexOf(PLACEHOLDER_LEFT);
+ int startIndex = str.indexOf(LEFT);
while (startIndex != -1) {
int endIndex = buffer.indexOf(PLACEHOLDER_RIGHT, startIndex);
if (endIndex != -1) {
String placeHolder = buffer.substring(startIndex, endIndex + 1);
String content =
- placeHolder
- .replace(PLACEHOLDER_LEFT, "")
- .replace(PLACEHOLDER_RIGHT, "")
- .trim();
+ placeHolder.replace(LEFT, "").replace(PLACEHOLDER_RIGHT, "").trim();
String[] parts = content.split(PLACEHOLDER_SPLIT);
try {
ZonedDateTime ndt = dateTime;
@@ -125,8 +121,8 @@ public class VariableOperationUtils {
}
String newContent = ndt.format(DateTimeFormatter.ofPattern(parts[0]));
- if (buffer.substring(startIndex - 1, endIndex + 1).contains(DOLLAR)) {
- buffer.replace(startIndex - 1, endIndex + 1, newContent);
+ if (buffer.substring(startIndex, endIndex + 1).contains(DOLLAR)) {
+ buffer.replace(startIndex, endIndex + 1, newContent);
}
startIndex = buffer.indexOf(PLACEHOLDER_LEFT, startIndex + newContent.length());
} catch (IllegalArgumentException e1) {
@@ -178,47 +174,6 @@ public class VariableOperationUtils {
return dateTime;
}
- /**
- * @param keyValue
- * @param str
- * @return
- */
- private static String replace(Map<String, String> keyValue, String str)
- throws VariableOperationFailedException {
- StringBuilder buffer = new StringBuilder(str);
- int startIndex = str.indexOf(PLACEHOLDER_LEFT);
-
- while (startIndex != -1) {
- int endIndex = buffer.indexOf(PLACEHOLDER_RIGHT, startIndex);
- if (endIndex != -1) {
- String placeHolder = buffer.substring(startIndex, endIndex + 1);
- String content =
- placeHolder
- .replace(PLACEHOLDER_LEFT, "")
- .replace(PLACEHOLDER_RIGHT, "")
- .trim();
- try {
- String newContent = keyValue.get(content);
- if (newContent != null) {
- if (buffer.substring(startIndex - 1, endIndex + 1).contains(DOLLAR)) {
- buffer.replace(startIndex - 1, endIndex + 1, newContent);
- }
- startIndex =
- buffer.indexOf(PLACEHOLDER_LEFT, startIndex + newContent.length());
- } else {
- startIndex = buffer.indexOf(PLACEHOLDER_LEFT, endIndex);
- }
- } catch (Exception e2) {
- throw new VariableOperationFailedException(
- 20050, "variable operation expression" + e2.getMessage(), e2);
- }
- } else {
- startIndex = -1; // leave while
- }
- }
- return buffer.toString();
- }
-
/**
* json support variable operation
*
@@ -256,58 +211,4 @@ public class VariableOperationUtils {
}
}
}
-
- /**
- * @param template
- * @param map
- * @return
- */
- public static String format(CharSequence template, Map<?, ?> map) {
- return VariableOperationUtils.format(template, map, "${", "}", true);
- }
-
- /**
- * map = {a: "aValue", b: "bValue"} format("{a} and {b}", map) ---=》 aValue and bValue
- *
- * @param template
- * @param map
- * @param leftStr
- * @param rightStr
- * @param ignoreNull
- * @return
- */
- public static String format(
- CharSequence template,
- Map<?, ?> map,
- CharSequence leftStr,
- CharSequence rightStr,
- boolean ignoreNull) {
- if (null == template) {
- return null;
- }
- if (null == map || map.isEmpty()) {
- return template.toString();
- }
-
- if (StringUtils.isBlank(leftStr)) {
- leftStr = "";
- }
-
- if (StringUtils.isBlank(rightStr)) {
- rightStr = "";
- }
-
- String template2 = template.toString();
- String value;
- for (Map.Entry<?, ?> entry : map.entrySet()) {
- value = entry.getValue().toString();
- if (null == value && ignoreNull) {
- continue;
- }
- template2 =
- StringUtils.replace(
- template2, leftStr.toString() + entry.getKey() + rightStr, value);
- }
- return template2;
- }
}
diff --git a/linkis-commons/linkis-common/src/test/java/org/apache/linkis/common/variable/VariableOperationTest.java b/linkis-commons/linkis-common/src/test/java/org/apache/linkis/common/variable/VariableOperationTest.java
index 1cb9f8485..eaaefaccf 100644
--- a/linkis-commons/linkis-common/src/test/java/org/apache/linkis/common/variable/VariableOperationTest.java
+++ b/linkis-commons/linkis-common/src/test/java/org/apache/linkis/common/variable/VariableOperationTest.java
@@ -23,6 +23,8 @@ import org.junit.jupiter.api.Test;
import java.time.ZonedDateTime;
import java.util.Date;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -34,7 +36,7 @@ public class VariableOperationTest {
@Test
public void testJsonFormat() throws VariableOperationFailedException {
String jsonOld =
- "{\"name\":\"#{yyyyMMdd%-1d}\",\"address\":{\"street\":\"#{yyyyMMdd%-1y}\"},\"links\":[{\"name\":\"#{yyyyMMdd%-1M}\"}]}";
+ "{\"name\":\"&{yyyyMMdd%-1d}\",\"address\":{\"street\":\"&{yyyyMMdd%-1y}\"},\"links\":[{\"name\":\"&{yyyyMMdd%-1M}\"}]}";
String jsonNew = VariableOperationUtils.replaces(zonedDateTime, jsonOld);
System.out.println(jsonOld + "\n" + jsonNew);
assertEquals(
@@ -44,7 +46,7 @@ public class VariableOperationTest {
@Test
public void testTextFormat() throws VariableOperationFailedException {
- String strOld = "abc#{yyyyMMdd%-1d}def";
+ String strOld = "abc&{yyyyMMdd%-1d}def";
String strNew = VariableOperationUtils.replaces(zonedDateTime, strOld);
System.out.println(strOld + "\n" + strNew);
assertEquals(strNew, "abc20220401def");
@@ -63,4 +65,41 @@ public class VariableOperationTest {
String strNew = VariableOperationUtils.replaces(zonedDateTime, str);
assertEquals(strNew, str);
}
+
+ @Test
+ public void testJsonFormatThread() throws Exception {
+ String jsonOld =
+ "hql|show tables\n"
+ + "hql|show tables\n"
+ + "hql|show tables\n"
+ + "hql|show tables\n"
+ + "hql|show tables\n"
+ + "scala|val s=sqlContext.sql(\\\"show tables\\\")\\nshow(s)\\n\n"
+ + "shell|sleep 100\\nfunction example {\\n echo $[$(date +%s%N)/1000000]\\n}\n"
+ + "shell|ifconfig\n"
+ + "shell|echo ${f}|\"variable\":{\"f\":\"linkis\"}\n"
+ + "python|print(\\\":hello world\\\") \\ndef world(id):\\n print(id); \\n world(${f})|\"variable\":{\"f\":\"36\"}\n"
+ + "python|#!/usr/bin/python\\n# -*- coding:utf-8 -*-\\nimport time\\nimport sys,os\\nimport json\\n\\nargs='{\\\"user_name\\\": \\\"zychen\\\"}'\\nprint(args)\\ndict = json.loads(args)\\nusername = dict.get(\\\"user_name\\\")\\nprint(username)\n"
+ + "python|import sys\\nprint (\\\"Python Version {}\\\".format(str(sys.version).replace('\\\\n', '')))";
+
+ ExecutorService threadPool = Executors.newFixedThreadPool(10);
+ for (int i = 0; i < 10; i++) {
+ threadPool.execute(
+ () -> {
+ try {
+ String jsonNew =
+ VariableOperationUtils.replaces(zonedDateTime, jsonOld);
+ assertEquals(jsonNew, jsonOld);
+ } catch (VariableOperationFailedException e) {
+ e.printStackTrace();
+ throw new RuntimeException(e);
+ }
+ });
+ }
+
+ threadPool.shutdown();
+ while (!threadPool.isTerminated()) {
+ Thread.sleep(1000);
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@linkis.apache.org
For additional commands, e-mail: commits-help@linkis.apache.org