Unverified Commit e9c59e24 authored by gaoyan's avatar gaoyan Committed by GitHub

Add local resource (#2717)

* fix log error * add local resoucrce manager * fix app * formte code
parent 3e1cbf48
......@@ -19,8 +19,6 @@
package org.dinky.init;
import static org.apache.hadoop.fs.FileSystem.getDefaultUri;
import org.dinky.assertion.Asserts;
import org.dinky.context.TenantContextHolder;
import org.dinky.daemon.constant.FlinkTaskConstant;
......@@ -35,14 +33,12 @@ import org.dinky.data.model.SystemConfiguration;
import org.dinky.data.model.Task;
import org.dinky.data.model.job.JobInstance;
import org.dinky.data.model.rbac.Tenant;
import org.dinky.data.properties.OssProperties;
import org.dinky.function.constant.PathConstant;
import org.dinky.function.pool.UdfCodePool;
import org.dinky.job.ClearJobHistoryTask;
import org.dinky.job.DynamicResizeFlinkJobPoolTask;
import org.dinky.job.FlinkJobTask;
import org.dinky.job.SystemMetricsTask;
import org.dinky.oss.OssTemplate;
import org.dinky.scheduler.client.ProjectClient;
import org.dinky.scheduler.exception.SchedulerException;
import org.dinky.scheduler.model.Project;
......@@ -51,14 +47,12 @@ import org.dinky.service.JobInstanceService;
import org.dinky.service.SysConfigService;
import org.dinky.service.TaskService;
import org.dinky.service.TenantService;
import org.dinky.service.resource.impl.HdfsResourceManager;
import org.dinky.service.resource.impl.OssResourceManager;
import org.dinky.service.resource.BaseResourceManager;
import org.dinky.url.RsURLStreamHandlerFactory;
import org.dinky.utils.JsonUtils;
import org.dinky.utils.UDFUtils;
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
import org.apache.hadoop.fs.FileSystem;
import java.util.List;
import java.util.concurrent.TimeUnit;
......@@ -77,7 +71,6 @@ import com.baomidou.mybatisplus.extension.activerecord.Model;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Singleton;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
......@@ -140,49 +133,7 @@ public class SystemInit implements ApplicationRunner {
.forEach(x -> x.addParameterCheck(y -> {
if (Boolean.TRUE.equals(
systemConfiguration.getResourcesEnable().getValue())) {
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());
ossProperties.setPathStyleAccess(systemConfiguration
.getResourcesPathStyleAccess()
.getValue());
Singleton.get(OssResourceManager.class).setOssTemplate(new OssTemplate(ossProperties));
break;
case HDFS:
final org.apache.hadoop.conf.Configuration configuration =
new org.apache.hadoop.conf.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);
}
}
BaseResourceManager.initResourceManager();
}
}));
}
......
......@@ -19,10 +19,16 @@
package org.dinky.service.resource;
import org.dinky.data.exception.DinkyException;
import org.dinky.data.model.SystemConfiguration;
import org.dinky.oss.OssTemplate;
import org.dinky.service.resource.impl.HdfsResourceManager;
import org.dinky.service.resource.impl.LocalResourceManager;
import org.dinky.service.resource.impl.OssResourceManager;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import java.io.File;
import java.io.InputStream;
......@@ -52,11 +58,37 @@ public interface BaseResourceManager {
return Singleton.get(HdfsResourceManager.class);
case OSS:
return Singleton.get(OssResourceManager.class);
case LOCAL:
return Singleton.get(LocalResourceManager.class);
default:
return null;
}
}
static void initResourceManager() {
switch (instances.getResourcesModel().getValue()) {
case LOCAL:
Singleton.get(LocalResourceManager.class);
case OSS:
OssTemplate template = new OssTemplate(instances.getOssProperties());
Singleton.get(OssResourceManager.class).setOssTemplate(template);
break;
case HDFS:
final Configuration configuration = new Configuration();
configuration.set(
"fs.defaultFS", instances.getResourcesHdfsDefaultFS().getValue());
try {
FileSystem fileSystem = FileSystem.get(
FileSystem.getDefaultUri(configuration),
configuration,
instances.getResourcesHdfsUser().getValue());
Singleton.get(HdfsResourceManager.class).setHdfs(fileSystem);
} catch (Exception e) {
throw new DinkyException(e);
}
}
}
default String getFilePath(String path) {
return FileUtil.normalize(
FileUtil.file(instances.getResourcesUploadBasePath().getValue(), path)
......
/*
*
* 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.service.resource.impl;
import org.dinky.data.exception.BusException;
import org.dinky.service.resource.BaseResourceManager;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LocalResourceManager implements BaseResourceManager {
@Override
public void remove(String path) {
try {
boolean isSuccess = FileUtil.del(getFilePath(path));
if (!isSuccess) {
throw new BusException("remove file failed,reason unknown");
}
} catch (IORuntimeException e) {
log.error("remove file failed", e);
throw new BusException(e.getMessage());
}
}
@Override
public void rename(String path, String newPath) {
try {
String newName = FileUtil.getName(newPath);
FileUtil.rename(new File(getFilePath(path)), newName, true);
} catch (Exception e) {
log.error("rename file failed", e);
throw new BusException(e.getMessage());
}
}
@Override
public void putFile(String path, MultipartFile file) {
try {
FileUtil.writeFromStream(file.getInputStream(), getFilePath(path));
} catch (IOException e) {
log.error("putFile file failed", e);
throw new BusException(e.getMessage());
}
}
@Override
public void putFile(String path, File file) {
BufferedInputStream inputStream = FileUtil.getInputStream(file);
FileUtil.writeFromStream(inputStream, getFilePath(path));
}
@Override
public String getFileContent(String path) {
return IoUtil.readUtf8(readFile(path));
}
@Override
public InputStream readFile(String path) {
return FileUtil.getInputStream(getFilePath(path));
}
}
......@@ -276,13 +276,11 @@ public class ResourceServiceImpl extends ServiceImpl<ResourcesMapper, Resources>
() -> new BusException(Status.ROOT_DIR_NOT_ALLOW_DELETE));
try {
if (id < 1) {
getBaseResourceManager().remove("/");
// todo 删除主目录,实际是清空
remove(new LambdaQueryWrapper<Resources>().ne(Resources::getId, 0));
}
Resources byId = getById(id);
if (isExistsChildren(id)) {
getBaseResourceManager().remove(byId.getFullName());
if (byId.getIsDirectory()) {
List<Resources> resourceByPidToChildren =
getResourceByPidToChildren(new ArrayList<>(), byId.getId());
......@@ -291,7 +289,6 @@ public class ResourceServiceImpl extends ServiceImpl<ResourcesMapper, Resources>
List<Resources> resourceByPidToParent = getResourceByPidToParent(new ArrayList<>(), byId.getPid());
resourceByPidToParent.forEach(x -> x.setSize(x.getSize() - byId.getSize()));
updateBatchById(resourceByPidToParent);
getBaseResourceManager().remove(byId.getFullName());
return removeById(id);
}
return removeById(id);
......
......@@ -19,13 +19,10 @@
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.resource.BaseResourceManager;
import org.dinky.app.url.RsURLStreamHandlerFactory;
import org.dinky.assertion.Asserts;
import org.dinky.classloader.DinkyClassLoader;
......@@ -33,14 +30,11 @@ 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.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;
......@@ -53,8 +47,6 @@ 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;
......@@ -80,7 +72,6 @@ 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;
......@@ -102,43 +93,9 @@ public class Submitter {
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();
BaseResourceManager.initResourceManager();
URL.setURLStreamHandlerFactory(new RsURLStreamHandlerFactory());
log.info("{} Start Submit Job:{}", LocalDateTime.now(), config.getTaskId());
......
......@@ -20,8 +20,14 @@
package org.dinky.app.resource;
import org.dinky.app.resource.impl.HdfsResourceManager;
import org.dinky.app.resource.impl.LocalResourceManager;
import org.dinky.app.resource.impl.OssResourceManager;
import org.dinky.data.exception.DinkyException;
import org.dinky.data.model.SystemConfiguration;
import org.dinky.oss.OssTemplate;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import java.io.InputStream;
......@@ -39,11 +45,37 @@ public interface BaseResourceManager {
return Singleton.get(HdfsResourceManager.class);
case OSS:
return Singleton.get(OssResourceManager.class);
case LOCAL:
return Singleton.get(LocalResourceManager.class);
default:
return null;
}
}
static void initResourceManager() {
switch (instances.getResourcesModel().getValue()) {
case LOCAL:
Singleton.get(LocalResourceManager.class);
case OSS:
OssTemplate template = new OssTemplate(instances.getOssProperties());
Singleton.get(OssResourceManager.class).setOssTemplate(template);
break;
case HDFS:
final Configuration configuration = new Configuration();
configuration.set(
"fs.defaultFS", instances.getResourcesHdfsDefaultFS().getValue());
try {
FileSystem fileSystem = FileSystem.get(
FileSystem.getDefaultUri(configuration),
configuration,
instances.getResourcesHdfsUser().getValue());
Singleton.get(HdfsResourceManager.class).setHdfs(fileSystem);
} catch (Exception e) {
throw new DinkyException(e);
}
}
}
default String getFilePath(String path) {
return FileUtil.normalize(
FileUtil.file(instances.getResourcesUploadBasePath().getValue(), path)
......
......@@ -21,29 +21,19 @@ 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);
......
/*
*
* 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.model.SystemConfiguration;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import cn.hutool.core.util.URLUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LocalResourceManager implements BaseResourceManager {
SystemConfiguration systemConfiguration = SystemConfiguration.getInstances();
@Override
public InputStream readFile(String path) {
try {
return new URL("http://" + systemConfiguration.getDinkyAddr().getValue() + "/download/downloadFromRs?path="
+ URLUtil.encode(path))
.openStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
......@@ -20,6 +20,7 @@
package org.dinky.data.model;
public enum ResourcesModelEnum {
LOCAL,
HDFS,
OSS
}
......@@ -20,6 +20,7 @@
package org.dinky.data.model;
import org.dinky.data.enums.Status;
import org.dinky.data.properties.OssProperties;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -211,14 +212,15 @@ public class SystemConfiguration {
.defaultValue(true)
.note(Status.SYS_RESOURCE_SETTINGS_ENABLE_NOTE);
private final Configuration<ResourcesModelEnum> resourcesModel = key(Status.SYS_RESOURCE_SETTINGS_MODEL)
.enumType(ResourcesModelEnum.class)
.defaultValue(ResourcesModelEnum.LOCAL)
.note(Status.SYS_RESOURCE_SETTINGS_MODEL_NOTE);
private final Configuration<String> resourcesUploadBasePath = key(Status.SYS_RESOURCE_SETTINGS_UPLOAD_BASE_PATH)
.stringType()
.defaultValue("/dinky")
.note(Status.SYS_RESOURCE_SETTINGS_UPLOAD_BASE_PATH_NOTE);
private final Configuration<ResourcesModelEnum> resourcesModel = key(Status.SYS_RESOURCE_SETTINGS_MODEL)
.enumType(ResourcesModelEnum.class)
.defaultValue(ResourcesModelEnum.HDFS)
.note(Status.SYS_RESOURCE_SETTINGS_MODEL_NOTE);
private final Configuration<String> resourcesOssEndpoint = key(Status.SYS_RESOURCE_SETTINGS_OSS_ENDPOINT)
.stringType()
......@@ -343,4 +345,16 @@ public class SystemConfiguration {
public String getPythonHome() {
return pythonHome.getValue();
}
public OssProperties getOssProperties() {
return OssProperties.builder()
.enable(true)
.endpoint(resourcesOssEndpoint.getValue())
.accessKey(resourcesOssAccessKey.getValue())
.secretKey(resourcesOssSecretKey.getValue())
.bucketName(resourcesOssBucketName.getValue())
.region(resourcesOssRegion.getValue())
.pathStyleAccess(resourcesPathStyleAccess.getValue())
.build();
}
}
......@@ -21,12 +21,14 @@ package org.dinky.data.properties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@ApiModel(value = "OssProperties", description = "Configuration Properties for Object Storage Service (OSS)")
@Builder
public class OssProperties {
@ApiModelProperty(
......@@ -34,7 +36,7 @@ public class OssProperties {
dataType = "boolean",
notes = "Whether to enable OSS (Object Storage Service)",
example = "true")
private boolean enable = true;
private boolean enable;
@ApiModelProperty(
value = "OSS Endpoint",
......@@ -43,19 +45,12 @@ public class OssProperties {
example = "https://example.oss-cn-hangzhou.aliyuncs.com")
private String endpoint;
@ApiModelProperty(
value = "Custom Domain",
dataType = "String",
notes = "Custom domain for OSS",
example = "https://custom-domain.com")
private String customDomain;
@ApiModelProperty(
value = "Path Style Access",
dataType = "Boolean",
notes = "Path style access configuration (true for path-style, false for virtual-hosted-style)",
example = "true")
private Boolean pathStyleAccess = true;
private Boolean pathStyleAccess;
@ApiModelProperty(value = "Region", dataType = "String", notes = "Region for OSS", example = "oss-cn-hangzhou")
private String region;
......
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