zhuzhurk commented on code in PR #27834:
URL: https://github.com/apache/flink/pull/27834#discussion_r3014376716


##########
flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java:
##########
@@ -673,49 +636,145 @@ private void runJobWithCleanupRunner(
         }
     }
 
-    private void startApplicationCleanup() {
+    private void startApplicationsCleanup() {
         for (ApplicationResult applicationResult : 
recoveredDirtyApplicationResults) {
-            ApplicationID applicationId = applicationResult.getApplicationId();
-            ApplicationState applicationState = 
applicationResult.getApplicationState();
-
-            Map<JobID, ExecutionGraphInfo> jobs = new HashMap<>();
-            Collection<JobResult> dirtyJobResults =
-                    
recoveredDirtyJobResultsByApplicationId.remove(applicationId);
-            if (dirtyJobResults != null) {
-                for (JobResult jobResult : dirtyJobResults) {
-                    JobID jobId = jobResult.getJobId();
-                    ExecutionGraphInfo executionGraphInfo =
-                            new ExecutionGraphInfo(
-                                    
ArchivedExecutionGraph.createSparseArchivedExecutionGraph(
-                                            jobResult, -1));
-                    jobs.put(jobId, executionGraphInfo);
-
-                    runJobWithCleanupRunner(jobResult, false);
-                }
+            try {
+                startApplicationCleanup(applicationResult);
+            } catch (Throwable throwable) {
+                onFatalError(
+                        new DispatcherException(
+                                String.format(
+                                        "Could not start cleanup for 
application %s.",
+                                        applicationResult.getApplicationId()),
+                                throwable));
             }
+        }
+    }
 
-            long[] stateTimestamps = new 
long[ApplicationState.values().length];
-            stateTimestamps[ApplicationState.CREATED.ordinal()] = 
applicationResult.getStartTime();
-            stateTimestamps[applicationState.ordinal()] = 
applicationResult.getEndTime();
+    private void startApplicationCleanup(ApplicationResult applicationResult) {
+        ApplicationID applicationId = applicationResult.getApplicationId();
+        ApplicationState applicationState = 
applicationResult.getApplicationState();
 
-            ArchivedApplication sparseArchivedApplication =
-                    new ArchivedApplication(
-                            applicationId,
-                            applicationResult.getApplicationName(),
-                            applicationState,
-                            stateTimestamps,
-                            jobs,
-                            Collections.emptyList());
+        Map<JobID, ExecutionGraphInfo> jobs = new HashMap<>();
+        Collection<JobResult> dirtyJobResults =
+                recoveredDirtyJobResultsByApplicationId.remove(applicationId);
+        if (dirtyJobResults != null) {
+            for (JobResult jobResult : dirtyJobResults) {
+                JobID jobId = jobResult.getJobId();
+                ExecutionGraphInfo executionGraphInfo =
+                        new ExecutionGraphInfo(
+                                
ArchivedExecutionGraph.createSparseArchivedExecutionGraph(
+                                        jobResult, -1));
+                jobs.put(jobId, executionGraphInfo);
+
+                runJobWithCleanupRunner(jobResult, false);
+            }
+        }
 
-            writeToArchivedApplicationStore(sparseArchivedApplication);
+        long[] stateTimestamps = new long[ApplicationState.values().length];
+        stateTimestamps[ApplicationState.CREATED.ordinal()] = 
applicationResult.getStartTime();
+        stateTimestamps[applicationState.ordinal()] = 
applicationResult.getEndTime();
+
+        ArchivedApplication sparseArchivedApplication =
+                new ArchivedApplication(
+                        applicationId,
+                        applicationResult.getApplicationName(),
+                        applicationState,
+                        stateTimestamps,
+                        jobs,
+                        Collections.emptyList());
+
+        writeToArchivedApplicationStore(sparseArchivedApplication);
+
+        // the dirty result already exists
+        // create a completed future to make sure the jobs can be marked clean
+        applicationCreateDirtyResultFutures.put(applicationId, 
FutureUtils.completedVoidFuture());
+        applicationTerminationFutures.put(applicationId, new 
CompletableFuture<>());
 
-            // the dirty result already exists
-            // create a completed future to make sure the jobs can be marked 
clean
-            applicationCreateDirtyResultFutures.put(
-                    applicationId, FutureUtils.completedVoidFuture());
-            applicationTerminationFutures.put(applicationId, new 
CompletableFuture<>());
+        removeApplication(applicationId, jobs.keySet());
+    }
 
-            removeApplication(applicationId, jobs.keySet());
+    private void recoverApplicationsAndJobsInSessionMode() {
+        for (AbstractApplication suspendedApplication : 
suspendedApplications.values()) {
+            // defer starting recovered jobs, as they might be skipped based 
on user logic
+            try {
+                internalSubmitApplication(suspendedApplication).get();
+            } catch (Throwable throwable) {
+                onFatalError(
+                        new DispatcherException(
+                                String.format(
+                                        "Could not start recovered application 
%s.",
+                                        
suspendedApplication.getApplicationId()),
+                                throwable));
+            }
+        }
+
+        // start suspended jobs that do not belong to any application 
(previously submitted in a
+        // SingleJobApplication) by wrapping them into a SingleJobApplication
+        Iterator<Map.Entry<JobID, ExecutionPlan>> jobIterator = 
suspendedJobs.entrySet().iterator();
+        while (jobIterator.hasNext()) {
+            Map.Entry<JobID, ExecutionPlan> entry = jobIterator.next();
+            ExecutionPlan recoveredJob = entry.getValue();
+            ApplicationID applicationId = 
recoveredJob.getApplicationId().orElse(null);
+            if (!suspendedApplications.containsKey(applicationId)) {
+                runRecoveredJob(recoveredJob, true);
+                jobIterator.remove();
+                suspendedJobIdsByApplicationId.remove(applicationId);
+            }
+        }
+    }
+
+    private void maybeSubmitApplicationInApplicationMode() {
+        checkState(suspendedApplications.isEmpty());
+        checkState(recoveredDirtyApplicationResults.size() <= 1);
+
+        AbstractApplication application =
+                ((ApplicationBootstrap) dispatcherBootstrap).getApplication();
+        ApplicationID applicationId = application.getApplicationId();
+        boolean shutDownOnApplicationFinish =
+                
configuration.get(DeploymentOptions.SHUTDOWN_ON_APPLICATION_FINISH);
+        if (!recoveredDirtyApplicationResults.isEmpty()) {
+            // the application is already terminated but needs to be cleaned up
+            ApplicationResult applicationResult =
+                    recoveredDirtyApplicationResults.iterator().next();
+            
checkState(applicationId.equals(applicationResult.getApplicationId()));
+
+            if (shutDownOnApplicationFinish) {
+                shutDownCluster(
+                        ApplicationStatus.fromApplicationState(
+                                applicationResult.getApplicationState()));
+            }
+        } else {
+            // check whether the application is already cleaned up

Review Comment:
   -> check whether the application was submitted and is already cleaned up



##########
flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java:
##########
@@ -673,49 +636,145 @@ private void runJobWithCleanupRunner(
         }
     }
 
-    private void startApplicationCleanup() {
+    private void startApplicationsCleanup() {
         for (ApplicationResult applicationResult : 
recoveredDirtyApplicationResults) {
-            ApplicationID applicationId = applicationResult.getApplicationId();
-            ApplicationState applicationState = 
applicationResult.getApplicationState();
-
-            Map<JobID, ExecutionGraphInfo> jobs = new HashMap<>();
-            Collection<JobResult> dirtyJobResults =
-                    
recoveredDirtyJobResultsByApplicationId.remove(applicationId);
-            if (dirtyJobResults != null) {
-                for (JobResult jobResult : dirtyJobResults) {
-                    JobID jobId = jobResult.getJobId();
-                    ExecutionGraphInfo executionGraphInfo =
-                            new ExecutionGraphInfo(
-                                    
ArchivedExecutionGraph.createSparseArchivedExecutionGraph(
-                                            jobResult, -1));
-                    jobs.put(jobId, executionGraphInfo);
-
-                    runJobWithCleanupRunner(jobResult, false);
-                }
+            try {
+                startApplicationCleanup(applicationResult);
+            } catch (Throwable throwable) {
+                onFatalError(
+                        new DispatcherException(
+                                String.format(
+                                        "Could not start cleanup for 
application %s.",
+                                        applicationResult.getApplicationId()),
+                                throwable));
             }
+        }
+    }
 
-            long[] stateTimestamps = new 
long[ApplicationState.values().length];
-            stateTimestamps[ApplicationState.CREATED.ordinal()] = 
applicationResult.getStartTime();
-            stateTimestamps[applicationState.ordinal()] = 
applicationResult.getEndTime();
+    private void startApplicationCleanup(ApplicationResult applicationResult) {
+        ApplicationID applicationId = applicationResult.getApplicationId();
+        ApplicationState applicationState = 
applicationResult.getApplicationState();
 
-            ArchivedApplication sparseArchivedApplication =
-                    new ArchivedApplication(
-                            applicationId,
-                            applicationResult.getApplicationName(),
-                            applicationState,
-                            stateTimestamps,
-                            jobs,
-                            Collections.emptyList());
+        Map<JobID, ExecutionGraphInfo> jobs = new HashMap<>();
+        Collection<JobResult> dirtyJobResults =
+                recoveredDirtyJobResultsByApplicationId.remove(applicationId);
+        if (dirtyJobResults != null) {
+            for (JobResult jobResult : dirtyJobResults) {
+                JobID jobId = jobResult.getJobId();
+                ExecutionGraphInfo executionGraphInfo =
+                        new ExecutionGraphInfo(
+                                
ArchivedExecutionGraph.createSparseArchivedExecutionGraph(
+                                        jobResult, -1));
+                jobs.put(jobId, executionGraphInfo);
+
+                runJobWithCleanupRunner(jobResult, false);
+            }
+        }
 
-            writeToArchivedApplicationStore(sparseArchivedApplication);
+        long[] stateTimestamps = new long[ApplicationState.values().length];
+        stateTimestamps[ApplicationState.CREATED.ordinal()] = 
applicationResult.getStartTime();
+        stateTimestamps[applicationState.ordinal()] = 
applicationResult.getEndTime();
+
+        ArchivedApplication sparseArchivedApplication =
+                new ArchivedApplication(
+                        applicationId,
+                        applicationResult.getApplicationName(),
+                        applicationState,
+                        stateTimestamps,
+                        jobs,
+                        Collections.emptyList());
+
+        writeToArchivedApplicationStore(sparseArchivedApplication);
+
+        // the dirty result already exists
+        // create a completed future to make sure the jobs can be marked clean
+        applicationCreateDirtyResultFutures.put(applicationId, 
FutureUtils.completedVoidFuture());
+        applicationTerminationFutures.put(applicationId, new 
CompletableFuture<>());
 
-            // the dirty result already exists
-            // create a completed future to make sure the jobs can be marked 
clean
-            applicationCreateDirtyResultFutures.put(
-                    applicationId, FutureUtils.completedVoidFuture());
-            applicationTerminationFutures.put(applicationId, new 
CompletableFuture<>());
+        removeApplication(applicationId, jobs.keySet());
+    }
 
-            removeApplication(applicationId, jobs.keySet());
+    private void recoverApplicationsAndJobsInSessionMode() {
+        for (AbstractApplication suspendedApplication : 
suspendedApplications.values()) {
+            // defer starting recovered jobs, as they might be skipped based 
on user logic
+            try {
+                internalSubmitApplication(suspendedApplication).get();
+            } catch (Throwable throwable) {
+                onFatalError(
+                        new DispatcherException(
+                                String.format(
+                                        "Could not start recovered application 
%s.",
+                                        
suspendedApplication.getApplicationId()),
+                                throwable));
+            }
+        }
+
+        // start suspended jobs that do not belong to any application 
(previously submitted in a
+        // SingleJobApplication) by wrapping them into a SingleJobApplication
+        Iterator<Map.Entry<JobID, ExecutionPlan>> jobIterator = 
suspendedJobs.entrySet().iterator();
+        while (jobIterator.hasNext()) {
+            Map.Entry<JobID, ExecutionPlan> entry = jobIterator.next();
+            ExecutionPlan recoveredJob = entry.getValue();
+            ApplicationID applicationId = 
recoveredJob.getApplicationId().orElse(null);
+            if (!suspendedApplications.containsKey(applicationId)) {
+                runRecoveredJob(recoveredJob, true);
+                jobIterator.remove();
+                suspendedJobIdsByApplicationId.remove(applicationId);
+            }
+        }
+    }
+
+    private void maybeSubmitApplicationInApplicationMode() {
+        checkState(suspendedApplications.isEmpty());
+        checkState(recoveredDirtyApplicationResults.size() <= 1);
+
+        AbstractApplication application =
+                ((ApplicationBootstrap) dispatcherBootstrap).getApplication();
+        ApplicationID applicationId = application.getApplicationId();
+        boolean shutDownOnApplicationFinish =
+                
configuration.get(DeploymentOptions.SHUTDOWN_ON_APPLICATION_FINISH);
+        if (!recoveredDirtyApplicationResults.isEmpty()) {
+            // the application is already terminated but needs to be cleaned up
+            ApplicationResult applicationResult =
+                    recoveredDirtyApplicationResults.iterator().next();
+            
checkState(applicationId.equals(applicationResult.getApplicationId()));
+
+            if (shutDownOnApplicationFinish) {
+                shutDownCluster(
+                        ApplicationStatus.fromApplicationState(
+                                applicationResult.getApplicationState()));
+            }
+        } else {
+            // check whether the application is already cleaned up
+            ApplicationResult applicationResult = null;
+            try {
+                applicationResult =
+                        
applicationResultStore.getCleanApplicationResultAsync(applicationId).get();
+            } catch (Throwable throwable) {
+                onFatalError(
+                        new DispatcherException(
+                                String.format(
+                                        "Could not get clean application 
result for application %s.",
+                                        applicationId),
+                                throwable));

Review Comment:
   It should return here.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/Dispatcher.java:
##########
@@ -673,49 +636,145 @@ private void runJobWithCleanupRunner(
         }
     }
 
-    private void startApplicationCleanup() {
+    private void startApplicationsCleanup() {
         for (ApplicationResult applicationResult : 
recoveredDirtyApplicationResults) {
-            ApplicationID applicationId = applicationResult.getApplicationId();
-            ApplicationState applicationState = 
applicationResult.getApplicationState();
-
-            Map<JobID, ExecutionGraphInfo> jobs = new HashMap<>();
-            Collection<JobResult> dirtyJobResults =
-                    
recoveredDirtyJobResultsByApplicationId.remove(applicationId);
-            if (dirtyJobResults != null) {
-                for (JobResult jobResult : dirtyJobResults) {
-                    JobID jobId = jobResult.getJobId();
-                    ExecutionGraphInfo executionGraphInfo =
-                            new ExecutionGraphInfo(
-                                    
ArchivedExecutionGraph.createSparseArchivedExecutionGraph(
-                                            jobResult, -1));
-                    jobs.put(jobId, executionGraphInfo);
-
-                    runJobWithCleanupRunner(jobResult, false);
-                }
+            try {
+                startApplicationCleanup(applicationResult);
+            } catch (Throwable throwable) {
+                onFatalError(
+                        new DispatcherException(
+                                String.format(
+                                        "Could not start cleanup for 
application %s.",
+                                        applicationResult.getApplicationId()),
+                                throwable));
             }
+        }
+    }
 
-            long[] stateTimestamps = new 
long[ApplicationState.values().length];
-            stateTimestamps[ApplicationState.CREATED.ordinal()] = 
applicationResult.getStartTime();
-            stateTimestamps[applicationState.ordinal()] = 
applicationResult.getEndTime();
+    private void startApplicationCleanup(ApplicationResult applicationResult) {
+        ApplicationID applicationId = applicationResult.getApplicationId();
+        ApplicationState applicationState = 
applicationResult.getApplicationState();
 
-            ArchivedApplication sparseArchivedApplication =
-                    new ArchivedApplication(
-                            applicationId,
-                            applicationResult.getApplicationName(),
-                            applicationState,
-                            stateTimestamps,
-                            jobs,
-                            Collections.emptyList());
+        Map<JobID, ExecutionGraphInfo> jobs = new HashMap<>();
+        Collection<JobResult> dirtyJobResults =
+                recoveredDirtyJobResultsByApplicationId.remove(applicationId);
+        if (dirtyJobResults != null) {
+            for (JobResult jobResult : dirtyJobResults) {
+                JobID jobId = jobResult.getJobId();
+                ExecutionGraphInfo executionGraphInfo =
+                        new ExecutionGraphInfo(
+                                
ArchivedExecutionGraph.createSparseArchivedExecutionGraph(
+                                        jobResult, -1));
+                jobs.put(jobId, executionGraphInfo);
+
+                runJobWithCleanupRunner(jobResult, false);
+            }
+        }
 
-            writeToArchivedApplicationStore(sparseArchivedApplication);
+        long[] stateTimestamps = new long[ApplicationState.values().length];
+        stateTimestamps[ApplicationState.CREATED.ordinal()] = 
applicationResult.getStartTime();
+        stateTimestamps[applicationState.ordinal()] = 
applicationResult.getEndTime();
+
+        ArchivedApplication sparseArchivedApplication =
+                new ArchivedApplication(
+                        applicationId,
+                        applicationResult.getApplicationName(),
+                        applicationState,
+                        stateTimestamps,
+                        jobs,
+                        Collections.emptyList());
+
+        writeToArchivedApplicationStore(sparseArchivedApplication);
+
+        // the dirty result already exists
+        // create a completed future to make sure the jobs can be marked clean
+        applicationCreateDirtyResultFutures.put(applicationId, 
FutureUtils.completedVoidFuture());
+        applicationTerminationFutures.put(applicationId, new 
CompletableFuture<>());
 
-            // the dirty result already exists
-            // create a completed future to make sure the jobs can be marked 
clean
-            applicationCreateDirtyResultFutures.put(
-                    applicationId, FutureUtils.completedVoidFuture());
-            applicationTerminationFutures.put(applicationId, new 
CompletableFuture<>());
+        removeApplication(applicationId, jobs.keySet());
+    }
 
-            removeApplication(applicationId, jobs.keySet());
+    private void recoverApplicationsAndJobsInSessionMode() {
+        for (AbstractApplication suspendedApplication : 
suspendedApplications.values()) {
+            // defer starting recovered jobs, as they might be skipped based 
on user logic
+            try {
+                internalSubmitApplication(suspendedApplication).get();
+            } catch (Throwable throwable) {
+                onFatalError(
+                        new DispatcherException(
+                                String.format(
+                                        "Could not start recovered application 
%s.",
+                                        
suspendedApplication.getApplicationId()),
+                                throwable));

Review Comment:
   It should return here.



-- 
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]

Reply via email to