Unverified Commit 4fb43039 authored by Licho's avatar Licho Committed by GitHub

[Fix][Flink]Fix local model bug and FlinkSql task type selected by default (#2403)

* refactor: build pattern * feat: the first task type selected by default. * feat: detect remote by run model * fix: use GatewayType detection remmote --------- Co-authored-by: 's avatarleechor <leechor@users.noreply.github.com>
parent 0d2d3d5b
...@@ -99,7 +99,7 @@ public class Submitter { ...@@ -99,7 +99,7 @@ public class Submitter {
"{} --> 获取 FlinkSQL 配置异常,ID 为 {}, 连接信息为:{} ,异常信息为:{} ", "{} --> 获取 FlinkSQL 配置异常,ID 为 {}, 连接信息为:{} ,异常信息为:{} ",
LocalDateTime.now(), LocalDateTime.now(),
id, id,
config.toString(), config,
e.getMessage(), e.getMessage(),
e); e);
} }
...@@ -115,7 +115,7 @@ public class Submitter { ...@@ -115,7 +115,7 @@ public class Submitter {
"{} --> 获取 FlinkSQL 配置异常,ID 为 {}, 连接信息为:{} ,异常信息为:{} ", "{} --> 获取 FlinkSQL 配置异常,ID 为 {}, 连接信息为:{} ,异常信息为:{} ",
LocalDateTime.now(), LocalDateTime.now(),
id, id,
config.toString(), config,
e.getMessage(), e.getMessage(),
e); e);
} }
...@@ -134,25 +134,20 @@ public class Submitter { ...@@ -134,25 +134,20 @@ public class Submitter {
String fragment = DBUtil.getOneByID(sqlCheck, dbConfig); String fragment = DBUtil.getOneByID(sqlCheck, dbConfig);
if ("1".equals(fragment)) { if ("1".equals(fragment)) {
return DBUtil.getDbSourceSQLStatement(sql, dbConfig); return DBUtil.getDbSourceSQLStatement(sql, dbConfig);
} else {
// 全局变量未开启,返回空字符串
logger.info("任务 {} 未开启全局变量,不进行变量加载。");
return "";
} }
// 全局变量未开启,返回空字符串
logger.info("任务 {} 未开启全局变量,不进行变量加载。", id);
} catch (IOException | SQLException e) { } catch (IOException | SQLException e) {
logger.error( logger.error(
"{} --> 获取 数据源信息异常,请检查数据库连接,连接信息为:{} ,异常信息为:{}", "{} --> 获取 数据源信息异常,请检查数据库连接,连接信息为:{} ,异常信息为:{}", LocalDateTime.now(), dbConfig, e.getMessage(), e);
LocalDateTime.now(),
dbConfig.toString(),
e.getMessage(),
e);
} }
return ""; return "";
} }
public static void submit(Integer id, DBConfig dbConfig, String dinkyAddr) { public static void submit(Integer id, DBConfig dbConfig, String dinkyAddr) {
logger.info(LocalDateTime.now() + "开始提交作业 -- " + id); logger.info("{}开始提交作业 -- {}", LocalDateTime.now(), id);
if (NULL.equals(dinkyAddr)) { if (NULL.equals(dinkyAddr)) {
dinkyAddr = ""; dinkyAddr = "";
} }
...@@ -166,13 +161,13 @@ public class Submitter { ...@@ -166,13 +161,13 @@ public class Submitter {
} }
sb.append("\n"); sb.append("\n");
} }
// 添加数据源全局变量 // 添加数据源全局变量
sb.append(getDbSourceSqlStatements(dbConfig, id)); sb.append(getDbSourceSqlStatements(dbConfig, id));
// 添加自定义全局变量信息 // 添加自定义全局变量信息
sb.append(getFlinkSQLStatement(id, dbConfig)); sb.append(getFlinkSQLStatement(id, dbConfig));
List<String> statements = Submitter.getStatements(sb.toString());
ExecutorConfig executorConfig = ExecutorConfig.buildFromMap(taskConfig);
ExecutorConfig executorConfig = ExecutorConfig.buildFromMap(taskConfig);
// 加载第三方jar // 加载第三方jar
loadDep(taskConfig.get("type"), id, dinkyAddr, executorConfig); loadDep(taskConfig.get("type"), id, dinkyAddr, executorConfig);
...@@ -181,11 +176,14 @@ public class Submitter { ...@@ -181,11 +176,14 @@ public class Submitter {
List<StatementParam> ddl = new ArrayList<>(); List<StatementParam> ddl = new ArrayList<>();
List<StatementParam> trans = new ArrayList<>(); List<StatementParam> trans = new ArrayList<>();
List<StatementParam> execute = new ArrayList<>(); List<StatementParam> execute = new ArrayList<>();
List<String> statements = Submitter.getStatements(sb.toString());
for (String item : statements) { for (String item : statements) {
String statement = FlinkInterceptor.pretreatStatement(executor, item); String statement = FlinkInterceptor.pretreatStatement(executor, item);
if (statement.isEmpty()) { if (statement.isEmpty()) {
continue; continue;
} }
SqlType operationType = Operations.getOperationType(statement); SqlType operationType = Operations.getOperationType(statement);
if (operationType.equals(SqlType.INSERT) || operationType.equals(SqlType.SELECT)) { if (operationType.equals(SqlType.INSERT) || operationType.equals(SqlType.SELECT)) {
trans.add(new StatementParam(statement, operationType)); trans.add(new StatementParam(statement, operationType));
...@@ -201,12 +199,14 @@ public class Submitter { ...@@ -201,12 +199,14 @@ public class Submitter {
ddl.add(new StatementParam(statement, operationType)); ddl.add(new StatementParam(statement, operationType));
} }
} }
for (StatementParam item : ddl) { for (StatementParam item : ddl) {
logger.info("Executing FlinkSQL: " + item.getValue()); logger.info("Executing FlinkSQL: {}", item.getValue());
executor.executeSql(item.getValue()); executor.executeSql(item.getValue());
logger.info("Execution succeeded."); logger.info("Execution succeeded.");
} }
if (trans.size() > 0) {
if (!trans.isEmpty()) {
if (executorConfig.isUseStatementSet()) { if (executorConfig.isUseStatementSet()) {
List<String> inserts = new ArrayList<>(); List<String> inserts = new ArrayList<>();
for (StatementParam item : trans) { for (StatementParam item : trans) {
...@@ -214,19 +214,18 @@ public class Submitter { ...@@ -214,19 +214,18 @@ public class Submitter {
inserts.add(item.getValue()); inserts.add(item.getValue());
} }
} }
logger.info("Executing FlinkSQL statement set: " + String.join(FlinkSQLConstant.SEPARATOR, inserts)); logger.info("Executing FlinkSQL statement set: {}", String.join(FlinkSQLConstant.SEPARATOR, inserts));
executor.executeStatementSet(inserts); executor.executeStatementSet(inserts);
logger.info("Execution succeeded."); logger.info("Execution succeeded.");
} else { } else {
for (StatementParam item : trans) { StatementParam item = trans.get(0);
logger.info("Executing FlinkSQL: " + item.getValue()); logger.info("Executing FlinkSQL: {}", item.getValue());
executor.executeSql(item.getValue()); executor.executeSql(item.getValue());
logger.info("Execution succeeded."); logger.info("Execution succeeded.");
break;
}
} }
} }
if (execute.size() > 0) {
if (!execute.isEmpty()) {
List<String> executes = new ArrayList<>(); List<String> executes = new ArrayList<>();
for (StatementParam item : execute) { for (StatementParam item : execute) {
executes.add(item.getValue()); executes.add(item.getValue());
...@@ -235,7 +234,8 @@ public class Submitter { ...@@ -235,7 +234,8 @@ public class Submitter {
break; break;
} }
} }
logger.info("正在执行 FlinkSQL 语句集: " + String.join(FlinkSQLConstant.SEPARATOR, executes));
logger.info("正在执行 FlinkSQL 语句集: {}", String.join(FlinkSQLConstant.SEPARATOR, executes));
try { try {
executor.execute(executorConfig.getJobName()); executor.execute(executorConfig.getJobName());
logger.info("执行成功"); logger.info("执行成功");
...@@ -250,6 +250,7 @@ public class Submitter { ...@@ -250,6 +250,7 @@ public class Submitter {
if (StringUtils.isBlank(dinkyAddr)) { if (StringUtils.isBlank(dinkyAddr)) {
return; return;
} }
if ("kubernetes-application".equals(type)) { if ("kubernetes-application".equals(type)) {
try { try {
String httpJar = "http://" + dinkyAddr + "/download/downloadDepJar/" + taskId; String httpJar = "http://" + dinkyAddr + "/download/downloadDepJar/" + taskId;
...@@ -301,9 +302,8 @@ public class Submitter { ...@@ -301,9 +302,8 @@ public class Submitter {
private static void addURLs(URL[] jarUrls) { private static void addURLs(URL[] jarUrls) {
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Method add = null;
try { try {
add = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); Method add = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
add.setAccessible(true); add.setAccessible(true);
for (URL jarUrl : jarUrls) { for (URL jarUrl : jarUrls) {
add.invoke(urlClassLoader, jarUrl); add.invoke(urlClassLoader, jarUrl);
...@@ -319,18 +319,18 @@ public class Submitter { ...@@ -319,18 +319,18 @@ public class Submitter {
// 设置超时间为3秒 // 设置超时间为3秒
conn.setConnectTimeout(3 * 1000); conn.setConnectTimeout(3 * 1000);
// 获取输入流 // 获取输入流
InputStream inputStream = conn.getInputStream(); try (InputStream inputStream = conn.getInputStream()) {
// 获取输出流 // 获取输出流
FileOutputStream outputStream = new FileOutputStream(path); try (FileOutputStream outputStream = new FileOutputStream(path)) {
// 每次下载1024位 // 每次下载1024位
byte[] b = new byte[1024]; byte[] b = new byte[1024];
int len = -1; int len = -1;
while ((len = inputStream.read(b)) != -1) { while ((len = inputStream.read(b)) != -1) {
outputStream.write(b, 0, len); outputStream.write(b, 0, len);
}
return true;
}
} }
inputStream.close();
outputStream.close();
return true;
} catch (Exception e) { } catch (Exception e) {
return false; return false;
} }
......
...@@ -36,7 +36,7 @@ import java.util.Map; ...@@ -36,7 +36,7 @@ import java.util.Map;
public class LineageBuilder { public class LineageBuilder {
public static LineageResult getColumnLineageByLogicalPlan(String statement) { public static LineageResult getColumnLineageByLogicalPlan(String statement) {
Explainer explainer = new Explainer(ExecutorFactory.getExecutor(), false); Explainer explainer = new Explainer(ExecutorFactory.getDefaultExecutor(), false);
List<LineageRel> lineageRelList = explainer.getLineage(statement); List<LineageRel> lineageRelList = explainer.getLineage(statement);
List<LineageRelation> relations = new ArrayList<>(); List<LineageRelation> relations = new ArrayList<>();
Map<String, LineageTable> tableMap = new HashMap<>(); Map<String, LineageTable> tableMap = new HashMap<>();
......
...@@ -51,7 +51,11 @@ import lombok.Data; ...@@ -51,7 +51,11 @@ import lombok.Data;
@ApiModel(value = "JobConfig", description = "Configuration details of a job") @ApiModel(value = "JobConfig", description = "Configuration details of a job")
public class JobConfig { public class JobConfig {
@ApiModelProperty(value = "Flink run mode", dataType = "String", example = "batch", notes = "Flink run mode") @ApiModelProperty(
value = "Flink run mode",
dataType = "String",
example = "local standalone",
notes = "Flink run mode")
private String type; private String type;
@ApiModelProperty(value = "Check Point", dataType = "Integer", example = "1", notes = "Check point for the task") @ApiModelProperty(value = "Check Point", dataType = "Integer", example = "1", notes = "Check point for the task")
...@@ -217,6 +221,7 @@ public class JobConfig { ...@@ -217,6 +221,7 @@ public class JobConfig {
public ExecutorConfig getExecutorSetting() { public ExecutorConfig getExecutorSetting() {
return ExecutorConfig.build( return ExecutorConfig.build(
type,
address, address,
checkpoint, checkpoint,
parallelism, parallelism,
......
...@@ -139,6 +139,10 @@ ...@@ -139,6 +139,10 @@
<groupId>org.dinky</groupId> <groupId>org.dinky</groupId>
<artifactId>dinky-cdc-core</artifactId> <artifactId>dinky-cdc-core</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-gateway</artifactId>
</dependency>
</dependencies> </dependencies>
<profiles> <profiles>
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
package org.dinky.executor; package org.dinky.executor;
import org.dinky.assertion.Asserts; import org.dinky.assertion.Asserts;
import org.dinky.gateway.enums.GatewayType;
import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.math.NumberUtils;
...@@ -36,6 +37,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -36,6 +37,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
...@@ -46,12 +49,21 @@ import lombok.Setter; ...@@ -46,12 +49,21 @@ import lombok.Setter;
*/ */
@Setter @Setter
@Getter @Getter
@Builder
@AllArgsConstructor
@ApiModel(value = "ExecutorConfig", description = "Executor config for a job") @ApiModel(value = "ExecutorConfig", description = "Executor config for a job")
public class ExecutorConfig { public class ExecutorConfig {
private static final Logger log = LoggerFactory.getLogger(ExecutorConfig.class); private static final Logger log = LoggerFactory.getLogger(ExecutorConfig.class);
public static final ExecutorConfig DEFAULT = new ExecutorConfig(0, 1, true); private static final ObjectMapper mapper = new ObjectMapper();
public static final ExecutorConfig DEFAULT = ExecutorConfig.builder()
.checkpoint(0)
.parallelism(1)
.useSqlFragment(true)
.build();
public static final String TYPE_CONST = "type";
public static final String CHECKPOINT_CONST = "checkpoint"; public static final String CHECKPOINT_CONST = "checkpoint";
public static final String PARALLELISM_CONST = "parallelism"; public static final String PARALLELISM_CONST = "parallelism";
public static final String USE_SQL_FRAGMENT = "useSqlFragment"; public static final String USE_SQL_FRAGMENT = "useSqlFragment";
...@@ -61,7 +73,13 @@ public class ExecutorConfig { ...@@ -61,7 +73,13 @@ public class ExecutorConfig {
public static final String JOB_NAME = "jobName"; public static final String JOB_NAME = "jobName";
public static final String CONFIG_CONST = "config"; public static final String CONFIG_CONST = "config";
private static final ObjectMapper mapper = new ObjectMapper(); // after unique all run model to remote, this field could discard
@ApiModelProperty(
value = "Flink run mode",
dataType = "String",
example = "local standalone",
notes = "Flink run mode")
private String type;
@ApiModelProperty( @ApiModelProperty(
value = "Job manager rest host", value = "Job manager rest host",
...@@ -139,67 +157,8 @@ public class ExecutorConfig { ...@@ -139,67 +157,8 @@ public class ExecutorConfig {
notes = "List of JAR files") notes = "List of JAR files")
private String[] jarFiles; private String[] jarFiles;
public ExecutorConfig(boolean useSqlFragment) {
this(null, useSqlFragment);
}
public ExecutorConfig(Integer checkpoint) {
this(checkpoint, false);
}
public ExecutorConfig(Integer checkpoint, boolean useSqlFragment) {
this(checkpoint, null, useSqlFragment, null, null);
}
public ExecutorConfig(Integer checkpoint, Integer parallelism, boolean useSqlFragment) {
this(checkpoint, parallelism, useSqlFragment, null, null);
}
public ExecutorConfig(
Integer checkpoint, Integer parallelism, boolean useSqlFragment, String savePointPath, String jobName) {
this(checkpoint, parallelism, useSqlFragment, savePointPath, jobName, null);
}
public ExecutorConfig(Integer checkpoint, Integer parallelism, boolean useSqlFragment, String savePointPath) {
this(checkpoint, parallelism, useSqlFragment, savePointPath, null, null);
}
public ExecutorConfig(
Integer checkpoint,
Integer parallelism,
boolean useSqlFragment,
String savePointPath,
String jobName,
Map<String, String> config) {
this(null, null, checkpoint, parallelism, useSqlFragment, false, false, savePointPath, jobName, config, null);
}
private ExecutorConfig(
String host,
Integer port,
Integer checkpoint,
Integer parallelism,
boolean useSqlFragment,
boolean useStatementSet,
boolean useBatchModel,
String savePointPath,
String jobName,
Map<String, String> config,
Map<String, String> variables) {
this.host = host;
this.port = port;
this.checkpoint = checkpoint;
this.parallelism = parallelism;
this.useSqlFragment = useSqlFragment;
this.useStatementSet = useStatementSet;
this.useBatchModel = useBatchModel;
this.savePointPath = savePointPath;
this.jobName = jobName;
this.config = config;
this.variables = variables;
}
public static ExecutorConfig build( public static ExecutorConfig build(
String type,
String address, String address,
Integer checkpoint, Integer checkpoint,
Integer parallelism, Integer parallelism,
...@@ -224,21 +183,24 @@ public class ExecutorConfig { ...@@ -224,21 +183,24 @@ public class ExecutorConfig {
} }
} }
return new ExecutorConfig( return ExecutorConfig.builder()
host, .type(type)
port, .host(host)
checkpoint, .port(port)
parallelism, .checkpoint(checkpoint)
useSqlFragment, .parallelism(parallelism)
useStatementSet, .useSqlFragment(useSqlFragment)
useBatchModel, .useStatementSet(useStatementSet)
savePointPath, .useBatchModel(useBatchModel)
jobName, .savePointPath(savePointPath)
config, .jobName(jobName)
variables); .config(config)
.variables(variables)
.build();
} }
public static ExecutorConfig build( public static ExecutorConfig build(
String type,
Integer checkpoint, Integer checkpoint,
Integer parallelism, Integer parallelism,
boolean useSqlFragment, boolean useSqlFragment,
...@@ -262,8 +224,9 @@ public class ExecutorConfig { ...@@ -262,8 +224,9 @@ public class ExecutorConfig {
config.put(item.get("key"), item.get("value")); config.put(item.get("key"), item.get("value"));
} }
} }
return new ExecutorConfig(
null, return build(
type,
null, null,
checkpoint, checkpoint,
parallelism, parallelism,
...@@ -279,7 +242,9 @@ public class ExecutorConfig { ...@@ -279,7 +242,9 @@ public class ExecutorConfig {
public static ExecutorConfig buildFromMap(Map<String, String> settingMap) { public static ExecutorConfig buildFromMap(Map<String, String> settingMap) {
Integer checkpoint = NumberUtils.createInteger(settingMap.get(CHECKPOINT_CONST)); Integer checkpoint = NumberUtils.createInteger(settingMap.get(CHECKPOINT_CONST));
Integer parallelism = NumberUtils.createInteger(settingMap.get(PARALLELISM_CONST)); Integer parallelism = NumberUtils.createInteger(settingMap.get(PARALLELISM_CONST));
String type = settingMap.get(TYPE_CONST);
return build( return build(
type,
checkpoint, checkpoint,
parallelism, parallelism,
"1".equals(settingMap.get(USE_SQL_FRAGMENT)), "1".equals(settingMap.get(USE_SQL_FRAGMENT)),
...@@ -295,7 +260,7 @@ public class ExecutorConfig { ...@@ -295,7 +260,7 @@ public class ExecutorConfig {
} }
public boolean isRemote() { public boolean isRemote() {
return Asserts.isNotNullString(this.getHost()); return !GatewayType.get(type).isLocalExecute();
} }
public boolean isValidParallelism() { public boolean isValidParallelism() {
......
...@@ -27,7 +27,9 @@ package org.dinky.executor; ...@@ -27,7 +27,9 @@ package org.dinky.executor;
**/ **/
public final class ExecutorFactory { public final class ExecutorFactory {
public static Executor getExecutor() { private ExecutorFactory() {}
public static Executor getDefaultExecutor() {
return new LocalStreamExecutor(ExecutorConfig.DEFAULT); return new LocalStreamExecutor(ExecutorConfig.DEFAULT);
} }
......
...@@ -30,7 +30,7 @@ import java.util.stream.Stream; ...@@ -30,7 +30,7 @@ import java.util.stream.Stream;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
/** /**
* LocalStreamExecuter * LocalStreamExecutor
* *
* @since 2021/5/25 13:48 * @since 2021/5/25 13:48
*/ */
......
...@@ -38,7 +38,7 @@ public class FlinkInterceptorTest { ...@@ -38,7 +38,7 @@ public class FlinkInterceptorTest {
String statement = "nullif1:=NULLIF(1, 0) as val;\n" String statement = "nullif1:=NULLIF(1, 0) as val;\n"
+ "nullif2:=NULLIF(0, 0) as val$null;\n" + "nullif2:=NULLIF(0, 0) as val$null;\n"
+ "select ${nullif1},${nullif2}"; + "select ${nullif1},${nullif2}";
String pretreatStatement = FlinkInterceptor.pretreatStatement(ExecutorFactory.getExecutor(), statement); String pretreatStatement = FlinkInterceptor.pretreatStatement(ExecutorFactory.getDefaultExecutor(), statement);
Assert.assertEquals("select NULLIF(1, 0) as val,NULLIF(0, 0) as val$null", pretreatStatement); Assert.assertEquals("select NULLIF(1, 0) as val,NULLIF(0, 0) as val$null", pretreatStatement);
} }
} }
...@@ -70,7 +70,7 @@ const JobModal: React.FC<JobModalProps> = (props) => { ...@@ -70,7 +70,7 @@ const JobModal: React.FC<JobModalProps> = (props) => {
}; };
useEffect(() => { useEffect(() => {
isUDF(jobType) && queryUdfTemplate(); if (isUDF(jobType)) queryUdfTemplate();
}, [jobType, form]); }, [jobType, form]);
/** /**
...@@ -110,6 +110,7 @@ const JobModal: React.FC<JobModalProps> = (props) => { ...@@ -110,6 +110,7 @@ const JobModal: React.FC<JobModalProps> = (props) => {
label={l('catalog.type')} label={l('catalog.type')}
tooltip={l('catalog.type.tip')} tooltip={l('catalog.type.tip')}
options={JOB_TYPE} options={JOB_TYPE}
initialValue={JOB_TYPE[0]['options'][0]['value']}
disabled={!!values.id} disabled={!!values.id}
placeholder={l('catalog.type.placeholder')} placeholder={l('catalog.type.placeholder')}
rules={[{ required: true, message: l('catalog.type.placeholder') }]} rules={[{ required: true, message: l('catalog.type.placeholder') }]}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment