@ -45,8 +45,10 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.web.multipart.MultipartFile ;
import org.springframework.web.multipart.MultipartFile ;
import java.io.IOException ;
import java.text.MessageFormat ;
import java.text.MessageFormat ;
import java.util.* ;
import java.util.* ;
import java.util.regex.Matcher ;
import java.util.stream.Collectors ;
import java.util.stream.Collectors ;
import static org.apache.dolphinscheduler.common.Constants.* ;
import static org.apache.dolphinscheduler.common.Constants.* ;
@ -315,7 +317,6 @@ public class ResourcesService extends BaseService {
return result ;
return result ;
}
}
if ( name . equals ( resource . getAlias ( ) ) & & desc . equals ( resource . getDescription ( ) ) ) {
if ( name . equals ( resource . getAlias ( ) ) & & desc . equals ( resource . getDescription ( ) ) ) {
putMsg ( result , Status . SUCCESS ) ;
putMsg ( result , Status . SUCCESS ) ;
return result ;
return result ;
@ -323,9 +324,10 @@ public class ResourcesService extends BaseService {
//check resource aleady exists
//check resource aleady exists
String originFullName = resource . getFullName ( ) ;
String originFullName = resource . getFullName ( ) ;
String originResourceName = resource . getAlias ( ) ;
String fullName = String . format ( "%s%s" , originFullName . substring ( 0 , originFullName . lastIndexOf ( "/" ) + 1 ) , name ) ;
String fullName = String . format ( "%s%s" , originFullName . substring ( 0 , originFullName . lastIndexOf ( "/" ) + 1 ) , name ) ;
if ( ! resource . getAlias ( ) . equals ( name ) & & checkResourceExists ( fullName , 0 , type . ordinal ( ) ) ) {
if ( ! originResourceName . equals ( name ) & & checkResourceExists ( fullName , 0 , type . ordinal ( ) ) ) {
logger . error ( "resource {} already exists, can't recreate" , name ) ;
logger . error ( "resource {} already exists, can't recreate" , name ) ;
putMsg ( result , Status . RESOURCE_EXIST ) ;
putMsg ( result , Status . RESOURCE_EXIST ) ;
return result ;
return result ;
@ -336,8 +338,22 @@ public class ResourcesService extends BaseService {
if ( StringUtils . isEmpty ( tenantCode ) ) {
if ( StringUtils . isEmpty ( tenantCode ) ) {
return result ;
return result ;
}
}
// verify whether the resource exists in storage
// get the path of origin file in storage
String originHdfsFileName = HadoopUtils . getHdfsFileName ( resource . getType ( ) , tenantCode , originFullName ) ;
try {
if ( ! HadoopUtils . getInstance ( ) . exists ( originHdfsFileName ) ) {
logger . error ( "{} not exist" , originHdfsFileName ) ;
putMsg ( result , Status . RESOURCE_NOT_EXIST ) ;
return result ;
}
} catch ( IOException e ) {
logger . error ( e . getMessage ( ) , e ) ;
throw new ServiceException ( Status . HDFS_OPERATION_ERROR ) ;
}
String nameWithSuffix = name ;
String nameWithSuffix = name ;
String originResourceName = resource . getAlias ( ) ;
if ( ! resource . isDirectory ( ) ) {
if ( ! resource . isDirectory ( ) ) {
//get the file suffix
//get the file suffix
String suffix = originResourceName . substring ( originResourceName . lastIndexOf ( "." ) ) ;
String suffix = originResourceName . substring ( originResourceName . lastIndexOf ( "." ) ) ;
@ -361,10 +377,11 @@ public class ResourcesService extends BaseService {
try {
try {
resourcesMapper . updateById ( resource ) ;
resourcesMapper . updateById ( resource ) ;
if ( resource . isDirectory ( ) & & CollectionUtils . isNotEmpty ( childrenResource ) ) {
if ( resource . isDirectory ( ) & & CollectionUtils . isNotEmpty ( childrenResource ) ) {
String matcherFullName = Matcher . quoteReplacement ( fullName ) ;
List < Resource > childResourceList = new ArrayList < > ( ) ;
List < Resource > childResourceList = new ArrayList < > ( ) ;
List < Resource > resourceList = resourcesMapper . listResourceByIds ( childrenResource . toArray ( new Integer [ childrenResource . size ( ) ] ) ) ;
List < Resource > resourceList = resourcesMapper . listResourceByIds ( childrenResource . toArray ( new Integer [ childrenResource . size ( ) ] ) ) ;
childResourceList = resourceList . stream ( ) . map ( t - > {
childResourceList = resourceList . stream ( ) . map ( t - > {
t . setFullName ( t . getFullName ( ) . replaceFirst ( oldFullName , f ullName) ) ;
t . setFullName ( t . getFullName ( ) . replaceFirst ( oldFullName , matcherF ullName) ) ;
t . setUpdateTime ( now ) ;
t . setUpdateTime ( now ) ;
return t ;
return t ;
} ) . collect ( Collectors . toList ( ) ) ;
} ) . collect ( Collectors . toList ( ) ) ;
@ -389,19 +406,13 @@ public class ResourcesService extends BaseService {
return result ;
return result ;
}
}
// get the path of origin file in hdfs
String originHdfsFileName = HadoopUtils . getHdfsFileName ( resource . getType ( ) , tenantCode , originFullName ) ;
// get the path of dest file in hdfs
// get the path of dest file in hdfs
String destHdfsFileName = HadoopUtils . getHdfsFileName ( resource . getType ( ) , tenantCode , fullName ) ;
String destHdfsFileName = HadoopUtils . getHdfsFileName ( resource . getType ( ) , tenantCode , fullName ) ;
try {
try {
if ( HadoopUtils . getInstance ( ) . exists ( originHdfsFileName ) ) {
logger . info ( "start hdfs copy {} -> {}" , originHdfsFileName , destHdfsFileName ) ;
logger . info ( "hdfs copy {} -> {}" , originHdfsFileName , destHdfsFileName ) ;
HadoopUtils . getInstance ( ) . copy ( originHdfsFileName , destHdfsFileName , true , true ) ;
HadoopUtils . getInstance ( ) . copy ( originHdfsFileName , destHdfsFileName , true , true ) ;
} else {
logger . error ( "{} not exist" , originHdfsFileName ) ;
putMsg ( result , Status . RESOURCE_NOT_EXIST ) ;
}
} catch ( Exception e ) {
} catch ( Exception e ) {
logger . error ( MessageFormat . format ( "hdfs copy {0} -> {1} fail" , originHdfsFileName , destHdfsFileName ) , e ) ;
logger . error ( MessageFormat . format ( "hdfs copy {0} -> {1} fail" , originHdfsFileName , destHdfsFileName ) , e ) ;
putMsg ( result , Status . HDFS_COPY_FAIL ) ;
putMsg ( result , Status . HDFS_COPY_FAIL ) ;