-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug] [Api] Resource files need to be checked before submitting #13887
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* 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.api.resource; | ||
|
||
import org.apache.dolphinscheduler.api.enums.Status; | ||
import org.apache.dolphinscheduler.api.exceptions.ServiceException; | ||
import org.apache.dolphinscheduler.dao.entity.Resource; | ||
import org.apache.dolphinscheduler.service.process.ProcessService; | ||
import org.apache.dolphinscheduler.spi.enums.ResourceType; | ||
|
||
import org.apache.commons.collections.CollectionUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import org.slf4j.Logger; | ||
|
||
import com.google.common.base.Strings; | ||
|
||
@Getter | ||
@Setter | ||
public class ResourceCheck { | ||
|
||
/** | ||
* logger | ||
*/ | ||
private Logger logger; | ||
/** | ||
* Resource Type | ||
*/ | ||
private ResourceType resourceType; | ||
|
||
/** | ||
* Process Service | ||
*/ | ||
private ProcessService processService; | ||
|
||
/** | ||
* need check resourceIds | ||
*/ | ||
private String resourceIds; | ||
|
||
/** | ||
* task name | ||
*/ | ||
private String taskName; | ||
|
||
/** | ||
* resource exist check | ||
* @param resourceType resource type | ||
* @param processService process service | ||
* @param resourceIds resource ids string with , combine | ||
* @param taskName task name | ||
* @param logger logger | ||
*/ | ||
public ResourceCheck(ResourceType resourceType, ProcessService processService, String resourceIds, String taskName, | ||
Logger logger) { | ||
this.resourceType = resourceType; | ||
this.processService = processService; | ||
this.resourceIds = resourceIds; | ||
this.taskName = taskName; | ||
this.logger = logger; | ||
} | ||
|
||
/** | ||
* check all resources exist, | ||
* if contains removed resource throws ServiceException | ||
*/ | ||
public void checkAllExist() throws ServiceException { | ||
switch (resourceType) { | ||
case FILE: | ||
if (Strings.isNullOrEmpty(this.resourceIds)) { | ||
logger.error("The given task definition has null resources str, taskName: {}", this.taskName); | ||
return; | ||
} | ||
|
||
Integer[] resourceIdArray = | ||
Arrays.stream(this.resourceIds.split(",")).map(Integer::parseInt).toArray(Integer[]::new); | ||
|
||
if (resourceIdArray.length > 0) { | ||
List<Resource> list = processService.listResourceByIds(resourceIdArray); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we have refactored the resource center in #12076, ResourceMapper has been deprecated. I think we should not do this way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Add this pre-check and throw an explicit error msg during task execution, which one is better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If the user deletes files in third-party storage after passing the pre check, it also cannot avoid runtime errors... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In our scenario, we are currently using the 3.1.1 version and I don't know we have refactored about resource center in dev. And for the 3.1.1 version, we encountered this situation. Here are the prerequisites:
process -> task job -> resource table -> third-party storage And if a process is maintained by a team. One member A operates offline this process. Another member B can delete resource binds in this process by ds. Member A re-online this process and got successful. Will throw an exception during runtime. And If we add a pre-check will find this exception when online. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And I agree if the user deletes a third-party storage file directly and we can do nothing about this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code only takes effect in version 3.1.X. And I'm +0 on this. cc @ruanwenjun @zhongjiajie @EricGao888 @caishunfeng There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can post this change to 3.1.6, not dev. Because dev is totally different. |
||
if (CollectionUtils.isEmpty(list) || list.size() != resourceIdArray.length) { | ||
logger.error( | ||
"The given task definition has deleted resources, taskName: {}, resourceIds: {}", | ||
this.taskName, this.resourceIds); | ||
throw new ServiceException( | ||
Status.TASK_RESOURCE_NOT_EXIST, this.taskName); | ||
} | ||
} | ||
break; | ||
default: | ||
logger.error("Error resourceType: {}", this.resourceType); | ||
throw new ServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, this.resourceType); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* 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.api.resource; | ||
|
||
import org.apache.dolphinscheduler.api.enums.Status; | ||
import org.apache.dolphinscheduler.api.exceptions.ServiceException; | ||
import org.apache.dolphinscheduler.dao.entity.Resource; | ||
import org.apache.dolphinscheduler.dao.entity.TaskDefinition; | ||
import org.apache.dolphinscheduler.service.process.ProcessService; | ||
import org.apache.dolphinscheduler.spi.enums.ResourceType; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* resource check test | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class ResourceCheckTest { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ResourceCheckTest.class); | ||
|
||
@Mock | ||
private ProcessService processService; | ||
|
||
@Test | ||
public void testResourceCheckNull() { | ||
TaskDefinition taskDefinition = new TaskDefinition(); | ||
taskDefinition.setResourceIds(null); | ||
taskDefinition.setName("taskDefinition"); | ||
|
||
new ResourceCheck(ResourceType.FILE, processService, taskDefinition.getResourceIds(), | ||
taskDefinition.getName(), logger).checkAllExist(); | ||
} | ||
|
||
@Test | ||
public void testResourceCheckNotExist() { | ||
TaskDefinition taskDefinition = new TaskDefinition(); | ||
taskDefinition.setResourceIds("1"); | ||
taskDefinition.setName("taskDefinition"); | ||
String error = null; | ||
|
||
try { | ||
new ResourceCheck(ResourceType.FILE, processService, taskDefinition.getResourceIds(), | ||
taskDefinition.getName(), logger).checkAllExist(); | ||
} catch (ServiceException e) { | ||
error = e.getMessage(); | ||
} | ||
|
||
Assertions.assertEquals(error, | ||
MessageFormat.format(Status.TASK_RESOURCE_NOT_EXIST.getMsg(), taskDefinition.getName())); | ||
} | ||
|
||
@Test | ||
public void testResourceCheckExist() { | ||
TaskDefinition taskDefinition = new TaskDefinition(); | ||
taskDefinition.setResourceIds("1"); | ||
taskDefinition.setName("taskDefinition"); | ||
String error = null; | ||
|
||
Integer[] resourceArr = {1}; | ||
List<Resource> list = new ArrayList<>(); | ||
list.add(new Resource()); | ||
|
||
Mockito.when(processService.listResourceByIds(resourceArr)).thenReturn(list); | ||
|
||
try { | ||
new ResourceCheck(ResourceType.FILE, processService, taskDefinition.getResourceIds(), | ||
taskDefinition.getName(), logger).checkAllExist(); | ||
} catch (ServiceException e) { | ||
error = e.getMessage(); | ||
} | ||
|
||
Assertions.assertNull(error); | ||
} | ||
|
||
@Test | ||
public void testResourceCheckUDF() { | ||
TaskDefinition taskDefinition = new TaskDefinition(); | ||
taskDefinition.setResourceIds(null); | ||
taskDefinition.setName("taskDefinition"); | ||
String error = null; | ||
|
||
try { | ||
new ResourceCheck(ResourceType.UDF, processService, taskDefinition.getResourceIds(), | ||
taskDefinition.getName(), logger).checkAllExist(); | ||
} catch (ServiceException e) { | ||
error = e.getMessage(); | ||
} | ||
|
||
Assertions.assertEquals(error, | ||
MessageFormat.format(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getMsg(), ResourceType.UDF)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's better to return the resource doesn't exist