@ -30,7 +30,9 @@ import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.MI;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.NAME_LABEL ;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.NAME_LABEL ;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.RESTART_POLICY ;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.RESTART_POLICY ;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.TASK_INSTANCE_ID ;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.TASK_INSTANCE_ID ;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.UNIQUE_LABEL_NAME ;
import org.apache.dolphinscheduler.common.thread.ThreadUtils ;
import org.apache.dolphinscheduler.common.utils.JSONUtils ;
import org.apache.dolphinscheduler.common.utils.JSONUtils ;
import org.apache.dolphinscheduler.plugin.task.api.K8sTaskExecutionContext ;
import org.apache.dolphinscheduler.plugin.task.api.K8sTaskExecutionContext ;
import org.apache.dolphinscheduler.plugin.task.api.TaskConstants ;
import org.apache.dolphinscheduler.plugin.task.api.TaskConstants ;
@ -41,16 +43,23 @@ import org.apache.dolphinscheduler.plugin.task.api.enums.TaskTimeoutStrategy;
import org.apache.dolphinscheduler.plugin.task.api.k8s.AbstractK8sTaskExecutor ;
import org.apache.dolphinscheduler.plugin.task.api.k8s.AbstractK8sTaskExecutor ;
import org.apache.dolphinscheduler.plugin.task.api.k8s.K8sTaskMainParameters ;
import org.apache.dolphinscheduler.plugin.task.api.k8s.K8sTaskMainParameters ;
import org.apache.dolphinscheduler.plugin.task.api.model.TaskResponse ;
import org.apache.dolphinscheduler.plugin.task.api.model.TaskResponse ;
import org.apache.dolphinscheduler.plugin.task.api.utils.LogUtils ;
import org.apache.dolphinscheduler.plugin.task.api.utils.MapUtils ;
import org.apache.dolphinscheduler.plugin.task.api.utils.MapUtils ;
import org.apache.dolphinscheduler.plugin.task.api.utils.ProcessUtils ;
import org.apache.commons.lang3.StringUtils ;
import org.apache.commons.lang3.StringUtils ;
import java.io.BufferedReader ;
import java.io.InputStreamReader ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.HashMap ;
import java.util.List ;
import java.util.List ;
import java.util.Locale ;
import java.util.Locale ;
import java.util.Map ;
import java.util.Map ;
import java.util.concurrent.CountDownLatch ;
import java.util.concurrent.CountDownLatch ;
import java.util.concurrent.ExecutionException ;
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.Future ;
import java.util.concurrent.TimeUnit ;
import java.util.concurrent.TimeUnit ;
import org.slf4j.Logger ;
import org.slf4j.Logger ;
@ -67,6 +76,7 @@ import io.fabric8.kubernetes.api.model.batch.v1.JobStatus;
import io.fabric8.kubernetes.client.Watch ;
import io.fabric8.kubernetes.client.Watch ;
import io.fabric8.kubernetes.client.Watcher ;
import io.fabric8.kubernetes.client.Watcher ;
import io.fabric8.kubernetes.client.WatcherException ;
import io.fabric8.kubernetes.client.WatcherException ;
import io.fabric8.kubernetes.client.dsl.LogWatch ;
/ * *
/ * *
* K8sTaskExecutor used to submit k8s task to K8S
* K8sTaskExecutor used to submit k8s task to K8S
@ -74,6 +84,9 @@ import io.fabric8.kubernetes.client.WatcherException;
public class K8sTaskExecutor extends AbstractK8sTaskExecutor {
public class K8sTaskExecutor extends AbstractK8sTaskExecutor {
private Job job ;
private Job job ;
protected boolean podLogOutputIsFinished = false ;
protected Future < ? > podLogOutputFuture ;
public K8sTaskExecutor ( Logger logger , TaskExecutionContext taskRequest ) {
public K8sTaskExecutor ( Logger logger , TaskExecutionContext taskRequest ) {
super ( logger , taskRequest ) ;
super ( logger , taskRequest ) ;
}
}
@ -100,6 +113,8 @@ public class K8sTaskExecutor extends AbstractK8sTaskExecutor {
Map < String , String > labelMap = k8STaskMainParameters . getLabelMap ( ) ;
Map < String , String > labelMap = k8STaskMainParameters . getLabelMap ( ) ;
labelMap . put ( LAYER_LABEL , LAYER_LABEL_VALUE ) ;
labelMap . put ( LAYER_LABEL , LAYER_LABEL_VALUE ) ;
labelMap . put ( NAME_LABEL , k8sJobName ) ;
labelMap . put ( NAME_LABEL , k8sJobName ) ;
Map < String , String > podLabelMap = new HashMap < > ( ) ;
podLabelMap . put ( UNIQUE_LABEL_NAME , taskRequest . getTaskAppId ( ) ) ;
EnvVar taskInstanceIdVar = new EnvVar ( TASK_INSTANCE_ID , taskInstanceId , null ) ;
EnvVar taskInstanceIdVar = new EnvVar ( TASK_INSTANCE_ID , taskInstanceId , null ) ;
List < EnvVar > envVars = new ArrayList < > ( ) ;
List < EnvVar > envVars = new ArrayList < > ( ) ;
envVars . add ( taskInstanceIdVar ) ;
envVars . add ( taskInstanceIdVar ) ;
@ -150,6 +165,9 @@ public class K8sTaskExecutor extends AbstractK8sTaskExecutor {
. withNewSpec ( )
. withNewSpec ( )
. withTtlSecondsAfterFinished ( JOB_TTL_SECONDS )
. withTtlSecondsAfterFinished ( JOB_TTL_SECONDS )
. withNewTemplate ( )
. withNewTemplate ( )
. withNewMetadata ( )
. withLabels ( podLabelMap )
. endMetadata ( )
. withNewSpec ( )
. withNewSpec ( )
. addNewContainer ( )
. addNewContainer ( )
. withName ( k8sJobName )
. withName ( k8sJobName )
@ -170,36 +188,36 @@ public class K8sTaskExecutor extends AbstractK8sTaskExecutor {
return jobBuilder . build ( ) ;
return jobBuilder . build ( ) ;
}
}
public void registerBatchJobWatcher ( Job job , String taskInstanceId , TaskResponse taskResponse ,
public void registerBatchJobWatcher ( Job job , String taskInstanceId , TaskResponse taskResponse ) {
K8sTaskMainParameters k8STaskMainParameters ) {
CountDownLatch countDownLatch = new CountDownLatch ( 1 ) ;
CountDownLatch countDownLatch = new CountDownLatch ( 1 ) ;
Watcher < Job > watcher = new Watcher < Job > ( ) {
Watcher < Job > watcher = new Watcher < Job > ( ) {
@Override
@Override
public void eventReceived ( Action action , Job job ) {
public void eventReceived ( Action action , Job job ) {
log . info ( "event received : job:{} action:{}" , job . getMetadata ( ) . getName ( ) , action ) ;
try (
if ( action ! = Action . ADDED ) {
final LogUtils . MDCAutoClosableContext mdcAutoClosableContext =
int jobStatus = getK8sJobStatus ( job ) ;
LogUtils . setTaskInstanceLogFullPathMDC ( taskRequest . getLogPath ( ) ) ) {
log . info ( "job {} status {}" , job . getMetadata ( ) . getName ( ) , jobStatus ) ;
log . info ( "event received : job:{} action:{}" , job . getMetadata ( ) . getName ( ) , action ) ;
if ( jobStatus = = TaskConstants . RUNNING_CODE ) {
if ( action ! = Action . ADDED ) {
return ;
int jobStatus = getK8sJobStatus ( job ) ;
log . info ( "job {} status {}" , job . getMetadata ( ) . getName ( ) , jobStatus ) ;
if ( jobStatus = = TaskConstants . RUNNING_CODE ) {
return ;
}
setTaskStatus ( jobStatus , taskInstanceId , taskResponse ) ;
countDownLatch . countDown ( ) ;
}
}
setTaskStatus ( jobStatus , taskInstanceId , taskResponse , k8STaskMainParameters ) ;
countDownLatch . countDown ( ) ;
}
}
}
}
@Override
@Override
public void onClose ( WatcherException e ) {
public void onClose ( WatcherException e ) {
logStringBuffer . append ( String . format ( "[K8sJobExecutor-%s] fail in k8s: %s" , job . getMetadata ( ) . getName ( ) ,
log . error ( "[K8sJobExecutor-{}] fail in k8s: {}" , job . getMetadata ( ) . getName ( ) , e . getMessage ( ) ) ;
e . getMessage ( ) ) ) ;
taskResponse . setExitStatusCode ( EXIT_CODE_FAILURE ) ;
taskResponse . setExitStatusCode ( EXIT_CODE_FAILURE ) ;
countDownLatch . countDown ( ) ;
countDownLatch . countDown ( ) ;
}
}
} ;
} ;
Watch watch = null ;
try ( Watch watch = k8sUtils . createBatchJobWatcher ( job . getMetadata ( ) . getName ( ) , watcher ) ) {
try {
watch = k8sUtils . createBatchJobWatcher ( job . getMetadata ( ) . getName ( ) , watcher ) ;
boolean timeoutFlag = taskRequest . getTaskTimeoutStrategy ( ) = = TaskTimeoutStrategy . FAILED
boolean timeoutFlag = taskRequest . getTaskTimeoutStrategy ( ) = = TaskTimeoutStrategy . FAILED
| | taskRequest . getTaskTimeoutStrategy ( ) = = TaskTimeoutStrategy . WARNFAILED ;
| | taskRequest . getTaskTimeoutStrategy ( ) = = TaskTimeoutStrategy . WARNFAILED ;
if ( timeoutFlag ) {
if ( timeoutFlag ) {
@ -208,7 +226,6 @@ public class K8sTaskExecutor extends AbstractK8sTaskExecutor {
} else {
} else {
countDownLatch . await ( ) ;
countDownLatch . await ( ) ;
}
}
flushLog ( taskResponse ) ;
} catch ( InterruptedException e ) {
} catch ( InterruptedException e ) {
log . error ( "job failed in k8s: {}" , e . getMessage ( ) , e ) ;
log . error ( "job failed in k8s: {}" , e . getMessage ( ) , e ) ;
Thread . currentThread ( ) . interrupt ( ) ;
Thread . currentThread ( ) . interrupt ( ) ;
@ -216,19 +233,42 @@ public class K8sTaskExecutor extends AbstractK8sTaskExecutor {
} catch ( Exception e ) {
} catch ( Exception e ) {
log . error ( "job failed in k8s: {}" , e . getMessage ( ) , e ) ;
log . error ( "job failed in k8s: {}" , e . getMessage ( ) , e ) ;
taskResponse . setExitStatusCode ( EXIT_CODE_FAILURE ) ;
taskResponse . setExitStatusCode ( EXIT_CODE_FAILURE ) ;
} finally {
if ( watch ! = null ) {
watch . close ( ) ;
}
}
}
}
}
private void parsePodLogOutput ( ) {
ExecutorService collectPodLogExecutorService = ThreadUtils
. newSingleDaemonScheduledExecutorService ( "CollectPodLogOutput-thread-" + taskRequest . getTaskName ( ) ) ;
String taskInstanceId = String . valueOf ( taskRequest . getTaskInstanceId ( ) ) ;
String taskName = taskRequest . getTaskName ( ) . toLowerCase ( Locale . ROOT ) ;
String containerName = String . format ( "%s-%s" , taskName , taskInstanceId ) ;
podLogOutputFuture = collectPodLogExecutorService . submit ( ( ) - > {
try (
final LogUtils . MDCAutoClosableContext mdcAutoClosableContext =
LogUtils . setTaskInstanceLogFullPathMDC ( taskRequest . getLogPath ( ) ) ;
LogWatch watcher = ProcessUtils . getPodLogWatcher ( taskRequest . getK8sTaskExecutionContext ( ) ,
taskRequest . getTaskAppId ( ) , containerName ) ) {
String line ;
try ( BufferedReader reader = new BufferedReader ( new InputStreamReader ( watcher . getOutput ( ) ) ) ) {
while ( ( line = reader . readLine ( ) ) ! = null ) {
log . info ( "[K8S-pod-log] {}" , line ) ;
}
}
} catch ( Exception e ) {
throw new RuntimeException ( e ) ;
} finally {
podLogOutputIsFinished = true ;
}
} ) ;
collectPodLogExecutorService . shutdown ( ) ;
}
@Override
@Override
public TaskResponse run ( String k8sParameterStr ) throws Exception {
public TaskResponse run ( String k8sParameterStr ) throws Exception {
TaskResponse result = new TaskResponse ( ) ;
TaskResponse result = new TaskResponse ( ) ;
int taskInstanceId = taskRequest . getTaskInstanceId ( ) ;
int taskInstanceId = taskRequest . getTaskInstanceId ( ) ;
K8sTaskMainParameters k8STaskMainParameters =
JSONUtils . parseObject ( k8sParameterStr , K8sTaskMainParameters . class ) ;
try {
try {
if ( null = = TaskExecutionContextCacheManager . getByTaskInstanceId ( taskInstanceId ) ) {
if ( null = = TaskExecutionContextCacheManager . getByTaskInstanceId ( taskInstanceId ) ) {
result . setExitStatusCode ( EXIT_CODE_KILL ) ;
result . setExitStatusCode ( EXIT_CODE_KILL ) ;
@ -242,9 +282,20 @@ public class K8sTaskExecutor extends AbstractK8sTaskExecutor {
String configYaml = k8sTaskExecutionContext . getConfigYaml ( ) ;
String configYaml = k8sTaskExecutionContext . getConfigYaml ( ) ;
k8sUtils . buildClient ( configYaml ) ;
k8sUtils . buildClient ( configYaml ) ;
submitJob2k8s ( k8sParameterStr ) ;
submitJob2k8s ( k8sParameterStr ) ;
registerBatchJobWatcher ( job , Integer . toString ( taskInstanceId ) , result , k8STaskMainParameters ) ;
parsePodLogOutput ( ) ;
registerBatchJobWatcher ( job , Integer . toString ( taskInstanceId ) , result ) ;
if ( podLogOutputFuture ! = null ) {
try {
// Wait kubernetes pod log collection finished
podLogOutputFuture . get ( ) ;
} catch ( ExecutionException e ) {
log . error ( "Handle pod log error" , e ) ;
}
}
} catch ( Exception e ) {
} catch ( Exception e ) {
cancelApplication ( k8sParameterStr ) ;
cancelApplication ( k8sParameterStr ) ;
Thread . currentThread ( ) . interrupt ( ) ;
result . setExitStatusCode ( EXIT_CODE_FAILURE ) ;
result . setExitStatusCode ( EXIT_CODE_FAILURE ) ;
throw e ;
throw e ;
}
}
@ -270,9 +321,9 @@ public class K8sTaskExecutor extends AbstractK8sTaskExecutor {
stopJobOnK8s ( k8sParameterStr ) ;
stopJobOnK8s ( k8sParameterStr ) ;
String namespaceName = k8STaskMainParameters . getNamespaceName ( ) ;
String namespaceName = k8STaskMainParameters . getNamespaceName ( ) ;
k8sUtils . createJob ( namespaceName , job ) ;
k8sUtils . createJob ( namespaceName , job ) ;
log . info ( "[K8sJobExecutor-{}-{}] submitted job successfully" , taskName , taskInstanceId ) ;
log . info ( "[K8sJobExecutor-{}-{}] submitted job successfully" , taskName , taskInstanceId ) ;
} catch ( Exception e ) {
} catch ( Exception e ) {
log . error ( "[K8sJobExecutor-{}-{}] fail to submit job" , taskName , taskInstanceId ) ;
log . error ( "[K8sJobExecutor-{}-{}] fail to submit job" , taskName , taskInstanceId ) ;
throw new TaskException ( "K8sJobExecutor fail to submit job" , e ) ;
throw new TaskException ( "K8sJobExecutor fail to submit job" , e ) ;
}
}
}
}
@ -288,7 +339,7 @@ public class K8sTaskExecutor extends AbstractK8sTaskExecutor {
k8sUtils . deleteJob ( jobName , namespaceName ) ;
k8sUtils . deleteJob ( jobName , namespaceName ) ;
}
}
} catch ( Exception e ) {
} catch ( Exception e ) {
log . error ( "[K8sJobExecutor-{}] fail to stop job" , jobName ) ;
log . error ( "[K8sJobExecutor-{}] fail to stop job" , jobName ) ;
throw new TaskException ( "K8sJobExecutor fail to stop job" , e ) ;
throw new TaskException ( "K8sJobExecutor fail to stop job" , e ) ;
}
}
}
}
@ -304,21 +355,16 @@ public class K8sTaskExecutor extends AbstractK8sTaskExecutor {
}
}
}
}
public void setTaskStatus ( int jobStatus , String taskInstanceId , TaskResponse taskResponse ,
public void setTaskStatus ( int jobStatus , String taskInstanceId , TaskResponse taskResponse ) {
K8sTaskMainParameters k8STaskMainParameters ) {
if ( jobStatus = = EXIT_CODE_SUCCESS | | jobStatus = = EXIT_CODE_FAILURE ) {
if ( jobStatus = = EXIT_CODE_SUCCESS | | jobStatus = = EXIT_CODE_FAILURE ) {
if ( null = = TaskExecutionContextCacheManager . getByTaskInstanceId ( Integer . valueOf ( taskInstanceId ) ) ) {
if ( null = = TaskExecutionContextCacheManager . getByTaskInstanceId ( Integer . valueOf ( taskInstanceId ) ) ) {
logStringBuffer . append ( Str ing . format ( "[K8sJobExecutor-%s ] killed" , job . getMetadata ( ) . getName ( ) ) ) ;
log . info ( "[K8sJobExecutor-{} ] killed" , job . getMetadata ( ) . getName ( ) ) ;
taskResponse . setExitStatusCode ( EXIT_CODE_KILL ) ;
taskResponse . setExitStatusCode ( EXIT_CODE_KILL ) ;
} else if ( jobStatus = = EXIT_CODE_SUCCESS ) {
} else if ( jobStatus = = EXIT_CODE_SUCCESS ) {
logStringBuffer
log . info ( "[K8sJobExecutor-{}] succeed in k8s" , job . getMetadata ( ) . getName ( ) ) ;
. append ( String . format ( "[K8sJobExecutor-%s] succeed in k8s" , job . getMetadata ( ) . getName ( ) ) ) ;
taskResponse . setExitStatusCode ( EXIT_CODE_SUCCESS ) ;
taskResponse . setExitStatusCode ( EXIT_CODE_SUCCESS ) ;
} else {
} else {
String errorMessage =
log . error ( "[K8sJobExecutor-{}] fail in k8s" , job . getMetadata ( ) . getName ( ) ) ;
k8sUtils . getPodLog ( job . getMetadata ( ) . getName ( ) , k8STaskMainParameters . getNamespaceName ( ) ) ;
logStringBuffer . append ( String . format ( "[K8sJobExecutor-%s] fail in k8s: %s" , job . getMetadata ( ) . getName ( ) ,
errorMessage ) ) ;
taskResponse . setExitStatusCode ( EXIT_CODE_FAILURE ) ;
taskResponse . setExitStatusCode ( EXIT_CODE_FAILURE ) ;
}
}
}
}