caishunfeng commented on code in PR #12552: URL: https://github.com/apache/dolphinscheduler/pull/12552#discussion_r1010240224
########## dolphinscheduler-worker/src/main/java/org/apache/dolphinscheduler/server/worker/utils/TaskFilesTransferUtils.java: ########## @@ -0,0 +1,270 @@ +/* + * 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.apache.dolphinscheduler.server.worker.utils; + +import org.apache.dolphinscheduler.common.utils.DateUtils; +import org.apache.dolphinscheduler.common.utils.JSONUtils; +import org.apache.dolphinscheduler.plugin.task.api.TaskConstants; +import org.apache.dolphinscheduler.plugin.task.api.TaskException; +import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext; +import org.apache.dolphinscheduler.plugin.task.api.enums.DataType; +import org.apache.dolphinscheduler.plugin.task.api.enums.Direct; +import org.apache.dolphinscheduler.plugin.task.api.model.Property; +import org.apache.dolphinscheduler.service.storage.StorageOperate; + +import org.apache.commons.lang3.StringUtils; + +import java.io.File; +import java.io.IOException; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.zeroturnaround.zip.ZipUtil; + +import com.fasterxml.jackson.databind.JsonNode; + +public class TaskFilesTransferUtils { + + protected final static Logger logger = LoggerFactory + .getLogger(String.format(TaskConstants.TASK_LOG_LOGGER_NAME_FORMAT, TaskFilesTransferUtils.class)); + + // tmp path in local path for transfer + final static String DOWNLOAD_TMP = ".DT_TMP"; + + // suffix of the package file + final static String PACK_SUFFIX = "_ds_pack.zip"; + + // root path in resource storage + final static String RESOURCE_TAG = "DATA_TRANSFER"; + + private TaskFilesTransferUtils() { + throw new IllegalStateException("Utility class"); + } + + /** + * upload output files to resource storage + * + * @param taskExecutionContext is the context of task + * @param storageOperate is the storage operate + * @throws TaskException TaskException + */ + public static void uploadOutputFiles(TaskExecutionContext taskExecutionContext, + StorageOperate storageOperate) throws TaskException { + List<Property> varPools = getVarPools(taskExecutionContext); + // get map of varPools for quick search + Map<String, Property> varPoolsMap = varPools.stream().collect(Collectors.toMap(Property::getProp, x -> x)); + + // get OUTPUT FILE parameters + List<Property> localParamsProperty = getFileLocalParams(taskExecutionContext, Direct.OUT); + + if (localParamsProperty.isEmpty()) { + return; + } + + logger.info("Upload output files ..."); + for (Property property : localParamsProperty) { + // get local file path + String srcPath = + packIfDir(String.format("%s/%s", taskExecutionContext.getExecutePath(), property.getValue())); + // get remote file path + String resourcePath = getResourcePath(taskExecutionContext, new File(srcPath).getName()); + try { + // upload file to storage + String resourceWholePath = + storageOperate.getResourceFileName(taskExecutionContext.getTenantCode(), resourcePath); + logger.info("{} --- Local:{} to Remote:{}", property, srcPath, resourceWholePath); + storageOperate.upload(taskExecutionContext.getTenantCode(), srcPath, resourceWholePath, false, true); + } catch (IOException ex) { + throw new TaskException(ex.getMessage(), ex); + } + + // update varPool + Property oriProperty; + // if the property is not in varPool, add it + if (varPoolsMap.containsKey(property.getProp())) { + oriProperty = varPoolsMap.get(property.getProp()); + } else { + oriProperty = new Property(property.getProp(), Direct.OUT, DataType.FILE, property.getValue()); + varPools.add(oriProperty); + } + oriProperty.setProp(String.format("%s.%s", taskExecutionContext.getTaskName(), oriProperty.getProp())); + oriProperty.setValue(resourcePath); + } + taskExecutionContext.setVarPool(JSONUtils.toJsonString(varPools)); + } + + /** + * download upstream files from storage + * only download files which are defined in the task parameters + * + * @param taskExecutionContext is the context of task + * @param storageOperate is the storage operate + * @throws TaskException task exception + */ + public static void downloadUpstreamFiles(TaskExecutionContext taskExecutionContext, StorageOperate storageOperate) { + List<Property> varPools = getVarPools(taskExecutionContext); + // get map of varPools for quick search + Map<String, Property> varPoolsMap = varPools.stream().collect(Collectors.toMap(Property::getProp, x -> x)); + + // get "IN FILE" parameters + List<Property> localParamsProperty = getFileLocalParams(taskExecutionContext, Direct.IN); + + if (localParamsProperty.isEmpty()) { + return; + } + + String executePath = taskExecutionContext.getExecutePath(); + // data path to download packaged data + String downloadTmpPath = String.format("%s/%s", executePath, DOWNLOAD_TMP); + + logger.info("Download upstream files..."); + for (Property property : localParamsProperty) { + Property inVarPool = varPoolsMap.get(property.getValue()); + if (inVarPool == null) { + logger.error("{} not in {}", property.getValue(), varPoolsMap.keySet()); + throw new TaskException(String.format("Can not find upstream file using %s, please check the key", + property.getValue())); + } + + String resourcePath = inVarPool.getValue(); + String targetPath = String.format("%s/%s", executePath, property.getProp()); + + String downloadPath; + // If the data is packaged, download it to a special directory (DOWNLOAD_TMP) and unpack it to the + // targetPath + boolean isPack = resourcePath.endsWith(PACK_SUFFIX); + if (isPack) { + downloadPath = String.format("%s/%s", downloadTmpPath, new File(resourcePath).getName()); + } else { + downloadPath = targetPath; + } + + try { + String resourceWholePath = + storageOperate.getResourceFileName(taskExecutionContext.getTenantCode(), resourcePath); + logger.info("{} --- Remote:{} to Local:{}", property, resourceWholePath, downloadPath); + storageOperate.download(taskExecutionContext.getTenantCode(), resourceWholePath, downloadPath, false, + true); + } catch (IOException ex) { + throw new TaskException(ex.getMessage(), ex); Review Comment: same here. ########## dolphinscheduler-worker/src/main/java/org/apache/dolphinscheduler/server/worker/utils/TaskFilesTransferUtils.java: ########## @@ -0,0 +1,270 @@ +/* + * 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.apache.dolphinscheduler.server.worker.utils; + +import org.apache.dolphinscheduler.common.utils.DateUtils; +import org.apache.dolphinscheduler.common.utils.JSONUtils; +import org.apache.dolphinscheduler.plugin.task.api.TaskConstants; +import org.apache.dolphinscheduler.plugin.task.api.TaskException; +import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext; +import org.apache.dolphinscheduler.plugin.task.api.enums.DataType; +import org.apache.dolphinscheduler.plugin.task.api.enums.Direct; +import org.apache.dolphinscheduler.plugin.task.api.model.Property; +import org.apache.dolphinscheduler.service.storage.StorageOperate; + +import org.apache.commons.lang3.StringUtils; + +import java.io.File; +import java.io.IOException; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.zeroturnaround.zip.ZipUtil; + +import com.fasterxml.jackson.databind.JsonNode; + +public class TaskFilesTransferUtils { + + protected final static Logger logger = LoggerFactory + .getLogger(String.format(TaskConstants.TASK_LOG_LOGGER_NAME_FORMAT, TaskFilesTransferUtils.class)); + + // tmp path in local path for transfer + final static String DOWNLOAD_TMP = ".DT_TMP"; + + // suffix of the package file + final static String PACK_SUFFIX = "_ds_pack.zip"; + + // root path in resource storage + final static String RESOURCE_TAG = "DATA_TRANSFER"; + + private TaskFilesTransferUtils() { + throw new IllegalStateException("Utility class"); + } + + /** + * upload output files to resource storage + * + * @param taskExecutionContext is the context of task + * @param storageOperate is the storage operate + * @throws TaskException TaskException + */ + public static void uploadOutputFiles(TaskExecutionContext taskExecutionContext, + StorageOperate storageOperate) throws TaskException { + List<Property> varPools = getVarPools(taskExecutionContext); + // get map of varPools for quick search + Map<String, Property> varPoolsMap = varPools.stream().collect(Collectors.toMap(Property::getProp, x -> x)); + + // get OUTPUT FILE parameters + List<Property> localParamsProperty = getFileLocalParams(taskExecutionContext, Direct.OUT); + + if (localParamsProperty.isEmpty()) { + return; + } + + logger.info("Upload output files ..."); + for (Property property : localParamsProperty) { + // get local file path + String srcPath = + packIfDir(String.format("%s/%s", taskExecutionContext.getExecutePath(), property.getValue())); + // get remote file path + String resourcePath = getResourcePath(taskExecutionContext, new File(srcPath).getName()); + try { + // upload file to storage + String resourceWholePath = + storageOperate.getResourceFileName(taskExecutionContext.getTenantCode(), resourcePath); + logger.info("{} --- Local:{} to Remote:{}", property, srcPath, resourceWholePath); + storageOperate.upload(taskExecutionContext.getTenantCode(), srcPath, resourceWholePath, false, true); + } catch (IOException ex) { + throw new TaskException(ex.getMessage(), ex); Review Comment: ```suggestion throw new TaskException("Upload file to storage error", ex); ``` ########## dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ResourcesController.java: ########## @@ -270,6 +270,25 @@ public Result<Object> deleteResource(@Parameter(hidden = true) @RequestAttribute return resourceService.delete(loginUser, fullName, tenantCode); } + /** + * delete DATA_TRANSFER data + * + * @param loginUser login user + * @return delete result code + */ + @Operation(summary = "deleteDataTransferData", description = "Delete the N days ago data of DATA_TRANSFER ") + @Parameters({ + @Parameter(name = "days", description = "N days ago", required = true, schema = @Schema(implementation = Integer.class)) + }) + @DeleteMapping(value = "/data-transfer-delete") Review Comment: Don't need delete action due to the request method is `DeleteMapping`. ```suggestion @DeleteMapping(value = "/data-transfer") ``` ########## dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java: ########## @@ -1871,6 +1874,61 @@ public Resource queryResourcesFileInfo(String userName, String fileName) { return (Resource) resourceResponse.getData(); } + @Override + public Map<String, Object> deleteDataTransferData(User loginUser, Integer days) { Review Comment: We should avoid to use `Map<String, Object>` as result object, because it's not clearly. ```suggestion public DeleteDataTransferResponse deleteDataTransferData(User loginUser, Integer days) { ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
