github-code-scanning[bot] commented on code in PR #12552: URL: https://github.com/apache/dolphinscheduler/pull/12552#discussion_r1006799664
########## dolphinscheduler-worker/src/test/java/org/apache/dolphinscheduler/server/worker/utils/TaskFilesTransferUtilsTest.java: ########## @@ -0,0 +1,256 @@ +/* + * 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.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.curator.shaded.com.google.common.io.Files; + +import java.io.File; +import java.io.IOException; +import java.time.format.DateTimeFormatter; +import java.util.List; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.zeroturnaround.zip.ZipUtil; + +public class TaskFilesTransferUtilsTest { + + final private long processDefineCode = 123; + final private int processDefineVersion = 456; + final private int processInstanceId = 678; + final private int taskInstanceId = 789; + final private String taskName = "test"; + + final private String tenantCode = "ubuntu"; + + private String date; + + private long endTime; + + private String exceptTemplate; + + @BeforeEach + public void Tas() { + endTime = System.currentTimeMillis(); + date = DateUtils.formatTimeStamp(endTime, DateTimeFormatter.ofPattern("yyyyMMdd")); + exceptTemplate = String.format("%s/%s/%d/%d_%d/%s_%d", + TaskFilesTransferUtils.RESOURCE_TAG, + date, + processDefineCode, + processDefineVersion, + processInstanceId, + taskName, + taskInstanceId); + } + + @Test + public void testUploadOutputFiles() throws IOException { + File executePath = Files.createTempDir(); + File folderPath = new File(executePath, "data"); + folderPath.mkdirs(); + File file = new File(folderPath.getPath() + "/test.txt"); + file.createNewFile(); Review Comment: ## Ignored error status of call Method testUploadOutputFiles ignores exceptional return value of File.createNewFile. [Show more details](https://github.com/apache/dolphinscheduler/security/code-scanning/2133) ########## dolphinscheduler-worker/src/main/java/org/apache/dolphinscheduler/server/worker/utils/TaskFilesTransferUtils.java: ########## @@ -0,0 +1,268 @@ +/* + * 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)); + + // root path in resource storage + final static String RESOURCE_TAG = "DATA_TRANSFER"; + + // 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"; + + /** + * 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); + logger.info("123123123123"); + 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(String.format("%s not in %s", 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); + } + + // unpack if the data is packaged + if (isPack) { + File downloadFile = new File(downloadPath); + logger.info("Unpack {} to {}", downloadPath, targetPath); + ZipUtil.unpack(downloadFile, new File(targetPath)); + } + } + + // delete DownloadTmp Folder if DownloadTmpPath exists + try { + org.apache.commons.io.FileUtils.deleteDirectory(new File(DownloadTmpPath)); + } catch (IOException e) { + logger.error( + "Delete DownloadTmpPath {} failed, this will not affect the task status", DownloadTmpPath, e); + } + } + + /** + * get local parameters property which type is FILE and direction is equal to direct + * + * @param taskExecutionContext is the context of task + * @param direct, may be Direct.IN or Direct.OUT. Review Comment: ## Spurious Javadoc @param tags @param tag "direct," does not match any actual parameter of method "getFileLocalParams()". [Show more details](https://github.com/apache/dolphinscheduler/security/code-scanning/2132) -- 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]
