Unverified Commit b8550805 authored by ZackYoung's avatar ZackYoung Committed by GitHub

[Optimize] You can submit Yarn-Application (Linux) on Windows (#2541)

* fix_app * fix_web_some_bug * fix_app * fix_app * fix_app * fix_app_some_bug * fix_app_some_bug * fix_app_some_bug
parent 4f6c1c14
......@@ -44,11 +44,6 @@
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.12.481</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
......
......@@ -26,6 +26,7 @@ import org.dinky.data.exception.BusException;
import org.dinky.data.model.FlinkUdfManifest;
import org.dinky.function.constant.PathConstant;
import org.dinky.function.util.ZipWriter;
import org.dinky.service.resource.BaseResourceManager;
import java.io.File;
import java.io.InputStream;
......@@ -50,7 +51,9 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
/** @since 0.7.0 */
/**
* @since 0.7.0
*/
@Slf4j
@RestController
@Api(tags = "UDF & App Jar Controller")
......@@ -114,4 +117,12 @@ public class DownloadController {
ServletUtil.write(resp, files.get(0));
}
}
@GetMapping("downloadFromRs")
@Log(title = "Download From Resource", businessType = BusinessType.DOWNLOAD)
@ApiOperation("Download From Resource")
public void downloadJavaUDF(String path, HttpServletResponse resp) {
InputStream inputStream = BaseResourceManager.getInstance().readFile(path);
ServletUtil.write(resp, inputStream);
}
}
......@@ -34,6 +34,7 @@ import org.dinky.data.properties.OssProperties;
import org.dinky.function.constant.PathConstant;
import org.dinky.function.pool.UdfCodePool;
import org.dinky.job.FlinkJobTask;
import org.dinky.oss.OssTemplate;
import org.dinky.scheduler.client.ProjectClient;
import org.dinky.scheduler.exception.SchedulerException;
import org.dinky.scheduler.model.Project;
......@@ -46,7 +47,6 @@ import org.dinky.service.resource.impl.HdfsResourceManager;
import org.dinky.service.resource.impl.OssResourceManager;
import org.dinky.url.RsURLStreamHandlerFactory;
import org.dinky.utils.JsonUtils;
import org.dinky.utils.OssTemplate;
import org.dinky.utils.UDFUtils;
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
......@@ -98,9 +98,9 @@ public class SystemInit implements ApplicationRunner {
public void run(ApplicationArguments args) {
TenantContextHolder.ignoreTenant();
initResources();
List<Tenant> tenants = tenantService.list();
sysConfigService.initSysConfig();
for (Tenant tenant : tenants) {
taskService.initDefaultFlinkSQLEnv(tenant.getId());
}
......
......@@ -21,8 +21,8 @@ package org.dinky.service.resource.impl;
import org.dinky.data.exception.BusException;
import org.dinky.data.exception.DinkyException;
import org.dinky.oss.OssTemplate;
import org.dinky.service.resource.BaseResourceManager;
import org.dinky.utils.OssTemplate;
import java.io.File;
import java.io.InputStream;
......
......@@ -40,10 +40,6 @@
<artifactId>dinky-app-base</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-client-1.14</artifactId>
......@@ -53,14 +49,6 @@
<artifactId>dinky-flink-1.14</artifactId>
<scope>${scope.runtime}</scope>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-client-base</artifactId>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-executor</artifactId>
</dependency>
</dependencies>
<build>
......
......@@ -22,7 +22,6 @@ package org.dinky.app;
import org.dinky.app.constant.AppParamConstant;
import org.dinky.app.db.DBUtil;
import org.dinky.app.flinksql.Submitter;
import org.dinky.app.util.FlinkAppUtil;
import org.dinky.data.app.AppParamConfig;
import org.dinky.utils.JsonUtils;
......@@ -56,9 +55,6 @@ public class MainApp {
} catch (Exception e) {
log.error("exectue app failed with config: {}", appConfig);
throw e;
} finally {
log.info("Start Monitor Job");
FlinkAppUtil.monitorFlinkTask(appConfig.getTaskId());
}
}
}
......@@ -39,10 +39,6 @@
<groupId>org.dinky</groupId>
<artifactId>dinky-app-base</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-client-1.15</artifactId>
......@@ -52,14 +48,6 @@
<artifactId>dinky-flink-1.15</artifactId>
<scope>${scope.runtime}</scope>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-client-base</artifactId>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-executor</artifactId>
</dependency>
</dependencies>
<build>
......
......@@ -22,7 +22,6 @@ package org.dinky.app;
import org.dinky.app.constant.AppParamConstant;
import org.dinky.app.db.DBUtil;
import org.dinky.app.flinksql.Submitter;
import org.dinky.app.util.FlinkAppUtil;
import org.dinky.data.app.AppParamConfig;
import org.dinky.utils.JsonUtils;
......@@ -56,9 +55,6 @@ public class MainApp {
} catch (Exception e) {
log.error("exectue app failed with config: {}", appConfig);
throw e;
} finally {
log.info("Start Monitor Job");
FlinkAppUtil.monitorFlinkTask(appConfig.getTaskId());
}
}
}
......@@ -25,12 +25,11 @@
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-client-${dinky.flink.version}</artifactId>
<scope>${scope.runtime}</scope>
<artifactId>dinky-client-1.16</artifactId>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-flink-${dinky.flink.version}</artifactId>
<artifactId>dinky-flink-1.16</artifactId>
<scope>${scope.runtime}</scope>
</dependency>
</dependencies>
......
......@@ -22,7 +22,6 @@ package org.dinky.app;
import org.dinky.app.constant.AppParamConstant;
import org.dinky.app.db.DBUtil;
import org.dinky.app.flinksql.Submitter;
import org.dinky.app.util.FlinkAppUtil;
import org.dinky.data.app.AppParamConfig;
import org.dinky.utils.JsonUtils;
......@@ -55,9 +54,6 @@ public class MainApp {
Submitter.submit(appConfig);
} catch (Exception e) {
log.error("exectue app failed : ", e);
} finally {
log.info("Start Monitor Job");
FlinkAppUtil.monitorFlinkTask(appConfig.getTaskId());
}
}
}
......@@ -25,12 +25,11 @@
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-client-${dinky.flink.version}</artifactId>
<scope>${scope.runtime}</scope>
<artifactId>dinky-client-1.17</artifactId>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-flink-${dinky.flink.version}</artifactId>
<artifactId>dinky-flink-1.17</artifactId>
<scope>${scope.runtime}</scope>
</dependency>
</dependencies>
......
......@@ -22,7 +22,6 @@ package org.dinky.app;
import org.dinky.app.constant.AppParamConstant;
import org.dinky.app.db.DBUtil;
import org.dinky.app.flinksql.Submitter;
import org.dinky.app.util.FlinkAppUtil;
import org.dinky.data.app.AppParamConfig;
import org.dinky.utils.JsonUtils;
......@@ -56,9 +55,6 @@ public class MainApp {
} catch (Exception e) {
log.error("exectue app failed with config: {}", appConfig);
throw e;
} finally {
log.info("Start Monitor Job");
FlinkAppUtil.monitorFlinkTask(appConfig.getTaskId());
}
}
}
......@@ -25,12 +25,11 @@
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-client-${dinky.flink.version}</artifactId>
<scope>${scope.runtime}</scope>
<artifactId>dinky-client-1.18</artifactId>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-flink-${dinky.flink.version}</artifactId>
<artifactId>dinky-flink-1.18</artifactId>
<scope>${scope.runtime}</scope>
</dependency>
</dependencies>
......
......@@ -22,7 +22,6 @@ package org.dinky.app;
import org.dinky.app.constant.AppParamConstant;
import org.dinky.app.db.DBUtil;
import org.dinky.app.flinksql.Submitter;
import org.dinky.app.util.FlinkAppUtil;
import org.dinky.data.app.AppParamConfig;
import org.dinky.utils.JsonUtils;
......@@ -56,9 +55,6 @@ public class MainApp {
} catch (Exception e) {
log.error("exectue app failed with config: {}", appConfig);
throw e;
} finally {
log.info("Start Monitor Job");
FlinkAppUtil.monitorFlinkTask(appConfig.getTaskId());
}
}
}
......@@ -31,6 +31,12 @@
<name>Dinky : App : Base</name>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
......@@ -38,6 +44,44 @@
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-executor</artifactId>
<exclusions>
<exclusion>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-transport-httpclient5</artifactId>
</exclusion>
<exclusion>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-spel</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit.archive</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit.ssh.jsch</artifactId>
</exclusion>
<exclusion>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.dinky</groupId>
<artifactId>dinky-gateway</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
......
......@@ -19,6 +19,7 @@
package org.dinky.app.db;
import org.dinky.app.model.SysConfig;
import org.dinky.data.app.AppDatabase;
import org.dinky.data.app.AppParamConfig;
import org.dinky.data.app.AppTask;
......@@ -68,14 +69,8 @@ public class DBUtil {
return sb.toString();
}
public static String getSysConfig(String key) throws SQLException {
Entity option = Entity.create("dinky_sys_config").set("name", key);
List<Entity> entities = db.find(option);
if (entities.size() <= 0) {
throw new IllegalArgumentException(
StrFormatter.format("The system conifg is not found: {}, please check! ", key));
} else {
return entities.get(0).getStr("value");
}
public static List<SysConfig> getSysConfigList() throws SQLException {
Entity option = Entity.create("dinky_sys_config");
return db.find(option, SysConfig.class);
}
}
......@@ -19,19 +19,33 @@
package org.dinky.app.flinksql;
import static org.apache.hadoop.fs.FileSystem.getDefaultUri;
import org.dinky.app.db.DBUtil;
import org.dinky.app.model.StatementParam;
import org.dinky.app.model.SysConfig;
import org.dinky.app.resource.impl.HdfsResourceManager;
import org.dinky.app.resource.impl.OssResourceManager;
import org.dinky.app.url.RsURLStreamHandlerFactory;
import org.dinky.app.util.FlinkAppUtil;
import org.dinky.assertion.Asserts;
import org.dinky.config.Dialect;
import org.dinky.constant.FlinkSQLConstant;
import org.dinky.data.app.AppParamConfig;
import org.dinky.data.app.AppTask;
import org.dinky.data.enums.Status;
import org.dinky.data.exception.DinkyException;
import org.dinky.data.model.SystemConfiguration;
import org.dinky.data.properties.OssProperties;
import org.dinky.executor.Executor;
import org.dinky.executor.ExecutorConfig;
import org.dinky.executor.ExecutorFactory;
import org.dinky.interceptor.FlinkInterceptor;
import org.dinky.oss.OssTemplate;
import org.dinky.parser.SqlType;
import org.dinky.trans.Operations;
import org.dinky.trans.dml.ExecuteJarOperation;
import org.dinky.trans.parse.AddJarSqlParseStrategy;
import org.dinky.trans.parse.ExecuteJarParseStrategy;
import org.dinky.utils.SqlUtil;
import org.dinky.utils.ZipUtils;
......@@ -39,6 +53,8 @@ import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.configuration.PipelineOptions;
import org.apache.flink.python.PythonOptions;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import java.io.File;
import java.io.FileOutputStream;
......@@ -53,15 +69,20 @@ import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Singleton;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.URLUtil;
import lombok.SneakyThrows;
/**
* FlinkSQLFactory
......@@ -71,7 +92,52 @@ import cn.hutool.core.util.URLUtil;
public class Submitter {
private static final Logger log = LoggerFactory.getLogger(Submitter.class);
private static void initSystemConfiguration() throws SQLException {
SystemConfiguration systemConfiguration = SystemConfiguration.getInstances();
List<SysConfig> sysConfigList = DBUtil.getSysConfigList();
Map<String, String> configMap =
CollUtil.toMap(sysConfigList, new HashMap<>(), SysConfig::getName, SysConfig::getValue);
systemConfiguration.initSetConfiguration(configMap);
}
private static void initResource() throws SQLException {
SystemConfiguration systemConfiguration = SystemConfiguration.getInstances();
switch (systemConfiguration.getResourcesModel().getValue()) {
case OSS:
OssProperties ossProperties = new OssProperties();
ossProperties.setAccessKey(
systemConfiguration.getResourcesOssAccessKey().getValue());
ossProperties.setSecretKey(
systemConfiguration.getResourcesOssSecretKey().getValue());
ossProperties.setEndpoint(
systemConfiguration.getResourcesOssEndpoint().getValue());
ossProperties.setBucketName(
systemConfiguration.getResourcesOssBucketName().getValue());
ossProperties.setRegion(
systemConfiguration.getResourcesOssRegion().getValue());
Singleton.get(OssResourceManager.class).setOssTemplate(new OssTemplate(ossProperties));
break;
case HDFS:
final Configuration configuration = new Configuration();
configuration.set(
"fs.defaultFS",
systemConfiguration.getResourcesHdfsDefaultFS().getValue());
try {
FileSystem fileSystem = FileSystem.get(
getDefaultUri(configuration),
configuration,
systemConfiguration.getResourcesHdfsUser().getValue());
Singleton.get(HdfsResourceManager.class).setHdfs(fileSystem);
} catch (Exception e) {
throw new DinkyException(e);
}
}
}
public static void submit(AppParamConfig config) throws SQLException {
initSystemConfiguration();
initResource();
URL.setURLStreamHandlerFactory(new RsURLStreamHandlerFactory());
log.info("{} Start Submit Job:{}", LocalDateTime.now(), config.getTaskId());
AppTask appTask = DBUtil.getTask(config.getTaskId());
......@@ -89,15 +155,23 @@ public class Submitter {
// .config(JsonUtils.toMap(appTask.getConfigJson()))
.build();
Executor executor = ExecutorFactory.buildAppStreamExecutor(executorConfig);
log.info("Start Monitor Job");
FlinkAppUtil.monitorFlinkTask(config.getTaskId());
// 加载第三方jar //TODO 这里有问题,需要修一修
// loadDep(appTask.getType(),
// config.getTaskId(),DBUtil.getSysConfig(Status.SYS_ENV_SETTINGS_DINKYADDR.getKey()), executorConfig);
log.info("The job configuration is as follows: {}", executorConfig);
String[] statements =
SqlUtil.getStatements(sql, DBUtil.getSysConfig(Status.SYS_FLINK_SETTINGS_SQLSEPARATOR.getKey()));
excuteJob(executorConfig, statements);
SqlUtil.getStatements(sql, SystemConfiguration.getInstances().getSqlSeparator());
if (Dialect.FLINK_JAR == appTask.getDialect()) {
executeJarJob(appTask.getType(), executor, statements);
} else {
executeJob(executor, statements);
}
}
public static String buildSql(AppTask appTask) throws SQLException {
......@@ -209,9 +283,24 @@ public class Submitter {
}
}
public static void excuteJob(ExecutorConfig executorConfig, String[] statements) {
@SneakyThrows
public static void executeJarJob(String type, Executor executor, String[] statements) {
for (int i = 0; i < statements.length; i++) {
String sqlStatement = executor.pretreatStatement(statements[i]);
if (ExecuteJarParseStrategy.INSTANCE.match(sqlStatement)) {
ExecuteJarOperation executeJarOperation = new ExecuteJarOperation(sqlStatement);
executeJarOperation.execute(executor.getCustomTableEnvironment());
break;
} else if (Operations.getOperationType(sqlStatement) == SqlType.ADD
&& "kubernetes-application".equals(type)) {
executor.addJar(AddJarSqlParseStrategy.getInfo(sqlStatement));
}
}
}
public static void executeJob(Executor executor, String[] statements) {
Executor executor = ExecutorFactory.buildAppStreamExecutor(executorConfig);
ExecutorConfig executorConfig = executor.getExecutorConfig();
List<StatementParam> ddl = new ArrayList<>();
List<StatementParam> trans = new ArrayList<>();
List<StatementParam> execute = new ArrayList<>();
......@@ -228,11 +317,6 @@ public class Submitter {
if (!executorConfig.isUseStatementSet()) {
break;
}
} else if (operationType.equals(SqlType.EXECUTE)) {
execute.add(new StatementParam(statement, operationType));
if (!executorConfig.isUseStatementSet()) {
break;
}
} else {
ddl.add(new StatementParam(statement, operationType));
}
......
/*
*
* 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.dinky.app.model;
import java.io.Serializable;
import lombok.Data;
@Data
public class SysConfig implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String value;
}
/*
*
* 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.dinky.app.resource;
import org.dinky.app.resource.impl.HdfsResourceManager;
import org.dinky.app.resource.impl.OssResourceManager;
import org.dinky.data.model.SystemConfiguration;
import java.io.InputStream;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Singleton;
public interface BaseResourceManager {
SystemConfiguration instances = SystemConfiguration.getInstances();
InputStream readFile(String path);
static BaseResourceManager getInstance() {
switch (SystemConfiguration.getInstances().getResourcesModel().getValue()) {
case HDFS:
return Singleton.get(HdfsResourceManager.class);
case OSS:
return Singleton.get(OssResourceManager.class);
default:
return null;
}
}
default String getFilePath(String path) {
return FileUtil.normalize(
FileUtil.file(instances.getResourcesUploadBasePath().getValue(), path)
.toString());
}
}
/*
*
* 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.dinky.app.resource.impl;
import org.dinky.app.resource.BaseResourceManager;
import org.dinky.data.exception.BusException;
import org.dinky.data.model.SystemConfiguration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import cn.hutool.core.util.URLUtil;
public class HdfsResourceManager implements BaseResourceManager {
FileSystem hdfs;
SystemConfiguration systemConfiguration = SystemConfiguration.getInstances();
@Override
public InputStream readFile(String path) {
try {
if (systemConfiguration.getResourcesHdfsDefaultFS().getValue().contains("file:/")) {
return new URL("http://" + systemConfiguration.getDinkyAddr().getValue()
+ "/download/downloadFromRs?path=" + URLUtil.encode(path))
.openStream();
}
return getHdfs().open(new Path(getFilePath(path)));
} catch (IOException e) {
throw BusException.valueOf("file.read.failed", e);
}
}
public FileSystem getHdfs() {
if (hdfs == null && instances.getResourcesEnable().getValue()) {
throw BusException.valueOf("Resource configuration error, HDFS is not enabled");
}
return hdfs;
}
public synchronized void setHdfs(FileSystem hdfs) {
this.hdfs = hdfs;
}
}
/*
*
* 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.dinky.app.resource.impl;
import org.dinky.app.resource.BaseResourceManager;
import org.dinky.data.exception.BusException;
import org.dinky.oss.OssTemplate;
import java.io.InputStream;
public class OssResourceManager implements BaseResourceManager {
OssTemplate ossTemplate;
@Override
public InputStream readFile(String path) {
return getOssTemplate()
.getObject(getOssTemplate().getBucketName(), getFilePath(path))
.getObjectContent();
}
public OssTemplate getOssTemplate() {
if (ossTemplate == null && instances.getResourcesEnable().getValue()) {
throw BusException.valueOf("Resource configuration error, OSS is not enabled");
}
return ossTemplate;
}
public void setOssTemplate(OssTemplate ossTemplate) {
this.ossTemplate = ossTemplate;
}
}
/*
*
* 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.dinky.app.url;
import org.dinky.app.resource.BaseResourceManager;
import org.dinky.data.exception.BusException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class RsURLConnection extends URLConnection {
private InputStream inputStream;
@Override
public void connect() {
BaseResourceManager instance = BaseResourceManager.getInstance();
if (instance == null) {
throw BusException.valueOf("ResourceManager is disabled");
}
inputStream = instance.readFile(getURL().getPath());
}
@Override
public InputStream getInputStream() {
connect();
return inputStream;
}
public RsURLConnection(URL url) {
super(url);
}
}
/*
*
* 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.dinky.app.url;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
public class RsURLStreamHandler extends URLStreamHandler {
@Override
protected URLConnection openConnection(URL u) {
return new RsURLConnection(u);
}
}
/*
*
* 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.dinky.app.url;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
public class RsURLStreamHandlerFactory implements URLStreamHandlerFactory {
private static final String PREFIX = "sun.net.www.protocol";
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if ("rs".equals(protocol)) {
return new RsURLStreamHandler();
}
try {
Class.forName("org.apache.hadoop.fs.FsUrlStreamHandlerFactory");
} catch (Exception e) {
return null;
}
String name = PREFIX + "." + protocol + ".Handler";
try {
@SuppressWarnings("deprecation")
Object o = Class.forName(name).newInstance();
return (URLStreamHandler) o;
} catch (ClassNotFoundException x) {
// ignore
} catch (Exception e) {
// For compatibility, all Exceptions are ignored.
// any number of exceptions can get thrown here
}
return null;
}
}
......@@ -19,19 +19,20 @@
package org.dinky.app.util;
import org.dinky.app.db.DBUtil;
import org.dinky.constant.FlinkConstant;
import org.dinky.data.enums.JobStatus;
import org.dinky.data.enums.Status;
import org.dinky.context.CustomTableEnvironmentContext;
import org.dinky.data.model.SystemConfiguration;
import org.dinky.utils.JsonUtils;
import org.apache.flink.api.common.JobExecutionResult;
import org.apache.flink.api.common.JobStatus;
import org.apache.flink.client.deployment.StandaloneClusterId;
import org.apache.flink.client.program.rest.RestClusterClient;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.GlobalConfiguration;
import org.apache.flink.runtime.client.JobStatusMessage;
import java.util.Collection;
import org.apache.flink.configuration.ReadableConfig;
import org.apache.flink.core.execution.JobClient;
import org.apache.flink.core.execution.JobListener;
import org.apache.flink.runtime.client.JobCancellationException;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.http.HttpUtil;
......@@ -46,27 +47,36 @@ public class FlinkAppUtil {
* If the task is completed, it sends a hook notification and stops monitoring.
*/
public static void monitorFlinkTask(int taskId) {
boolean isRun = true;
try (RestClusterClient<StandaloneClusterId> client = createClient()) {
while (isRun) {
Collection<JobStatusMessage> jobs = client.listJobs().get();
if (jobs.isEmpty()) {
log.error("No Flink task found, try again in 2 seconds.....");
StreamExecutionEnvironment streamExecutionEnvironment =
CustomTableEnvironmentContext.get().getStreamExecutionEnvironment();
streamExecutionEnvironment.registerJobListener(new JobListener() {
@Override
public void onJobSubmitted(JobClient jobClient, Throwable throwable) {
jobClient.getJobExecutionResult().thenAccept(jobExecutionResult -> finshedHook(jobClient, taskId));
jobClient.getJobStatus().thenAccept(job -> {
if (job == JobStatus.FINISHED) {
finshedHook(jobClient, taskId);
}
for (JobStatusMessage job : jobs) {
if (JobStatus.isDone(job.getJobState().toString())) {
sendHook(taskId, job.getJobId().toHexString(), 0);
log.info("hook {} finished.", job.getJobName());
// There should be only one in application mode, so stop monitoring here
isRun = false;
});
}
@Override
public void onJobExecuted(JobExecutionResult jobExecutionResult, Throwable throwable) {
if (throwable instanceof JobCancellationException) {
// todo cancel task
} else {
// other exception
}
Thread.sleep(5000);
}
} catch (Exception e) {
// If an exception is thrown, it will cause the k8s pod to trigger a restart,
// resulting in an inability to exit normally
log.error("hook failed:", e);
});
}
private static void finshedHook(JobClient jobClient, int taskId) {
try {
sendHook(taskId, jobClient.getJobID().toHexString(), 0);
log.info("hook finished.");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
......@@ -79,9 +89,9 @@ public class FlinkAppUtil {
*/
private static void sendHook(int taskId, String jobId, int reTryCount) throws InterruptedException {
try {
String dinkyAddr = DBUtil.getSysConfig(Status.SYS_ENV_SETTINGS_DINKYADDR.getKey());
String url = StrFormatter.format(
"http://{}/api/jobInstance/hookJobDone?taskId={}&jobId={}", dinkyAddr, taskId, jobId);
String dinkyAddr = SystemConfiguration.getInstances().getDinkyAddr().getValue();
String url =
StrFormatter.format("{}/api/jobInstance/hookJobDone?taskId={}&jobId={}", dinkyAddr, taskId, jobId);
String resultStr = HttpUtil.get(url);
// TODO 这里应该使用Result实体类,但是Result.class不在comm里,迁移改动太大,暂时不搞
String code = JsonUtils.parseObject(resultStr).get("code").toString();
......@@ -101,17 +111,16 @@ public class FlinkAppUtil {
/**
* Create a REST cluster client for Flink.
*
* @return
* @throws Exception
*/
private static RestClusterClient<StandaloneClusterId> createClient() throws Exception {
Configuration config;
Configuration fromEnvConfig = GlobalConfiguration.loadConfiguration();
if (!fromEnvConfig.keySet().isEmpty()) {
config = fromEnvConfig;
} else {
config = GlobalConfiguration.loadConfiguration(FlinkConstant.DEFAULT_FLINK_HOME);
}
return new RestClusterClient<>(config, StandaloneClusterId.getInstance());
ReadableConfig config = CustomTableEnvironmentContext.get()
.getStreamExecutionEnvironment()
.getConfiguration();
Configuration configuration = new Configuration((Configuration) config);
return new RestClusterClient<>(configuration, StandaloneClusterId.getInstance());
}
}
......@@ -35,10 +35,6 @@
<groupId>org.dinky</groupId>
<artifactId>dinky-client-base</artifactId>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-common</artifactId>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-flink-1.14</artifactId>
......
......@@ -22,6 +22,7 @@ package org.dinky.executor;
import org.dinky.assertion.Asserts;
import org.dinky.data.model.LineageRel;
import org.dinky.data.result.SqlExplainResult;
import org.dinky.utils.JsonUtils;
import org.dinky.utils.LineageContext;
import org.apache.flink.api.common.RuntimeExecutionMode;
......@@ -73,6 +74,7 @@ import org.apache.flink.types.Row;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
......@@ -81,18 +83,19 @@ import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.URLUtil;
import lombok.extern.slf4j.Slf4j;
/**
* 定制TableEnvironmentImpl
*
* @since 2021/10/22 10:02
*/
@Slf4j
public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
public CustomTableEnvironmentImpl(
......@@ -212,21 +215,26 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
((DefaultExecutor) executor).getExecutionEnvironment().generateStreamGraph(trans);
JSONGenerator jsonGenerator = new JSONGenerator(streamGraph);
String json = jsonGenerator.getJSON();
ObjectMapper mapper = new ObjectMapper();
ObjectNode objectNode = mapper.createObjectNode();
try {
objectNode = (ObjectNode) mapper.readTree(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
} finally {
return objectNode;
}
return JsonUtils.parseObject(json);
} else {
throw new TableException("Unsupported SQL query! explainSql() need a single SQL to query.");
}
}
}
@Override
public void addJar(File... jarPath) {
Configuration configuration = new Configuration(this.getRootConfiguration());
List<String> pathList =
Arrays.stream(URLUtil.getURLs(jarPath)).map(URL::toString).collect(Collectors.toList());
List<String> jars = configuration.get(PipelineOptions.JARS);
if (jars == null) {
configuration.set(PipelineOptions.JARS, pathList);
} else {
CollUtil.addAll(jars, pathList);
}
}
@Override
public JobPlanInfo getJobPlanInfo(List<String> statements) {
return new JobPlanInfo(JsonPlanGenerator.generatePlan(getJobGraphFromInserts(statements)));
......@@ -374,19 +382,6 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
}
}
@Override
public void addJar(File... jarPath) {
Configuration configuration = this.getRootConfiguration();
List<String> jars = configuration.get(PipelineOptions.JARS);
if (jars == null) {
configuration.set(
PipelineOptions.JARS,
Arrays.stream(jarPath).map(File::getAbsolutePath).collect(Collectors.toList()));
} else {
CollUtil.addAll(jars, jarPath);
}
}
@Override
public <T> void createTemporaryView(String path, DataStream<T> dataStream, Expression... fields) {
createTemporaryView(path, fromDataStream(dataStream, fields));
......
......@@ -36,10 +36,6 @@
<artifactId>dinky-client-base</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-common</artifactId>
</dependency>
<dependency>
<groupId>org.dinky</groupId>
<artifactId>dinky-flink-1.15</artifactId>
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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