@ -35,6 +35,7 @@ import org.apache.dolphinscheduler.dao.mapper.ProcessTaskRelationMapper;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper ;
import org.apache.dolphinscheduler.dao.mapper.TaskDefinitionLogMapper ;
import org.apache.dolphinscheduler.dao.mapper.TaskDefinitionMapper ;
import org.apache.dolphinscheduler.spi.utils.StringUtils ;
import org.apache.commons.collections.CollectionUtils ;
@ -50,6 +51,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import com.google.common.collect.Lists ;
/ * *
* process task relation service impl
* /
@ -202,29 +205,72 @@ public class ProcessTaskRelationServiceImpl extends BaseServiceImpl implements P
/ * *
* delete task upstream relation
*
* @param loginUser login user
* @param projectCode project code
* @param loginUser login user
* @param projectCode project code
* @param preTaskCodes the pre task codes , sep ','
* @param taskCode the post task code
* @param taskCode the post task code
* @return delete result code
* /
@Override
public Map < String , Object > deleteUpstreamRelation ( User loginUser , long projectCode , String preTaskCodes , long taskCode ) {
return null ;
Project project = projectMapper . queryByCode ( projectCode ) ;
//check user access for project
Map < String , Object > result = projectService . checkProjectAndAuth ( loginUser , project , projectCode ) ;
if ( result . get ( Constants . STATUS ) ! = Status . SUCCESS ) {
return result ;
}
if ( StringUtils . isEmpty ( preTaskCodes ) ) {
putMsg ( result , Status . DATA_IS_NULL , "preTaskCodes" ) ;
return result ;
}
Set < Long > preTaskCodesSet = Lists . newArrayList ( preTaskCodes . split ( Constants . COMMA ) ) . stream ( ) . map ( Long : : parseLong ) . collect ( Collectors . toSet ( ) ) ;
Status status = deleteUpstreamRelation ( projectCode , preTaskCodesSet . toArray ( new Long [ 0 ] ) , taskCode ) ;
if ( status ! = Status . SUCCESS ) {
putMsg ( result , status ) ;
}
return result ;
}
/ * *
* delete task downstream relation
*
* @param loginUser login user
* @param projectCode project code
* @param loginUser login user
* @param projectCode project code
* @param postTaskCodes the post task codes , sep ','
* @param taskCode the pre task code
* @param taskCode the pre task code
* @return delete result code
* /
@Override
public Map < String , Object > deleteDownstreamRelation ( User loginUser , long projectCode , String postTaskCodes , long taskCode ) {
return null ;
Project project = projectMapper . queryByCode ( projectCode ) ;
//check user access for project
Map < String , Object > result = projectService . checkProjectAndAuth ( loginUser , project , projectCode ) ;
if ( result . get ( Constants . STATUS ) ! = Status . SUCCESS ) {
return result ;
}
if ( StringUtils . isEmpty ( postTaskCodes ) ) {
putMsg ( result , Status . DATA_IS_NULL , "postTaskCodes" ) ;
return result ;
}
Set < Long > postTaskCodesSet = Lists . newArrayList ( postTaskCodes . split ( Constants . COMMA ) ) . stream ( ) . map ( Long : : parseLong ) . collect ( Collectors . toSet ( ) ) ;
List < Long > deleteFailedCodeList = new ArrayList < > ( ) ;
postTaskCodesSet . stream ( ) . forEach (
postTaskCode - > {
try {
Status status = deleteUpstreamRelation ( projectCode , new Long [ ] { taskCode } , postTaskCode ) ;
if ( Status . SUCCESS ! = status ) {
deleteFailedCodeList . add ( postTaskCode ) ;
}
} catch ( Exception e ) {
deleteFailedCodeList . add ( postTaskCode ) ;
}
}
) ;
if ( ! deleteFailedCodeList . isEmpty ( ) ) {
putMsg ( result , Status . DELETE_TASK_PROCESS_RELATION_ERROR , String . join ( "," , deleteFailedCodeList . stream ( ) . map ( o - > o + "" ) . collect ( Collectors . toList ( ) ) ) ) ;
}
return result ;
}
/ * *
@ -328,4 +374,61 @@ public class ProcessTaskRelationServiceImpl extends BaseServiceImpl implements P
} ;
}
/ * *
* delete upstream relation
*
* @param projectCode project code
* @param preTaskCodes pre task codes
* @param taskCode pre task code
* @return status
* /
private Status deleteUpstreamRelation ( long projectCode , Long [ ] preTaskCodes , long taskCode ) {
List < ProcessTaskRelation > upstreamList = processTaskRelationMapper . queryUpstreamByCodes ( projectCode , taskCode , preTaskCodes ) ;
if ( CollectionUtils . isEmpty ( upstreamList ) ) {
return Status . SUCCESS ;
}
Map < Long , List < ProcessTaskRelation > > processTaskRelationListGroupByProcessDefinitionCode = upstreamList . stream ( )
. collect ( Collectors . groupingBy ( ProcessTaskRelation : : getProcessDefinitionCode ) ) ;
// count upstream relation group by process definition code
List < Map < Long , Integer > > countListGroupByProcessDefinitionCode = processTaskRelationMapper
. countUpstreamByCodeGroupByProcessDefinitionCode ( projectCode , processTaskRelationListGroupByProcessDefinitionCode . keySet ( ) . toArray ( new Long [ 0 ] ) , taskCode ) ;
List < ProcessTaskRelation > deletes = new ArrayList < > ( ) ;
List < ProcessTaskRelation > updates = new ArrayList < > ( ) ;
countListGroupByProcessDefinitionCode . stream ( ) . forEach (
processDefinitionCodeUpstreamCountMap - >
processDefinitionCodeUpstreamCountMap . entrySet ( ) . stream ( ) . forEach (
o - > {
Long processDefinitionCode = o . getKey ( ) ;
Integer count = o . getValue ( ) ;
List < ProcessTaskRelation > processTaskRelationList = processTaskRelationListGroupByProcessDefinitionCode . get ( processDefinitionCode ) ;
if ( count < = processTaskRelationList . size ( ) ) {
ProcessTaskRelation processTaskRelation = processTaskRelationList . remove ( 0 ) ;
if ( processTaskRelation . getPreTaskCode ( ) ! = 0 ) {
processTaskRelation . setPreTaskCode ( 0 ) ;
processTaskRelation . setPreTaskVersion ( 0 ) ;
updates . add ( processTaskRelation ) ;
}
}
if ( ! processTaskRelationList . isEmpty ( ) ) {
deletes . addAll ( processTaskRelationList ) ;
}
}
)
) ;
int update = 0 ;
if ( ! updates . isEmpty ( ) ) {
update = processTaskRelationMapper . batchUpdateProcessTaskRelationPreTask ( updates ) ;
}
int delete = 0 ;
if ( ! deletes . isEmpty ( ) ) {
delete = processTaskRelationMapper . deleteBatchIds ( deletes . stream ( ) . map ( ProcessTaskRelation : : getId ) . collect ( Collectors . toList ( ) ) ) ;
}
if ( update < 0 | | delete < 0 ) {
return Status . DELETE_TASK_PROCESS_RELATION_ERROR ;
}
return Status . SUCCESS ;
}
}