You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2021/11/05 15:47:34 UTC

[GitHub] [flink] akalash opened a new pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

akalash opened a new pull request #17701:
URL: https://github.com/apache/flink/pull/17701


   …oser in order to avoid the resources leak
   
   <!--
   *Thank you very much for contributing to Apache Flink - we are happy that you want to help us improve Flink. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Flink a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "[FLINK-XXXX] [component] Title of the pull request", where *FLINK-XXXX* should be replaced by the actual issue number. Skip *component* if you are unsure about which is the best component.
     Typo fixes that have no associated JIRA issue should be named following this pattern: `[hotfix] [docs] Fix typo in event time introduction` or `[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes. You can set up Azure Pipelines CI to do that following [this guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   
   
   **(The sections below can be removed for hotfixes of typos)**
   -->
   
   ## What is the purpose of the change
   
   *This PR fixes resource leak in StreamTask when the constructor fails*
   
   
   ## Brief change log
   
     - *Registered all resources in one closer*
   
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   
     - *Added test in StreamTaskTest*
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (yes / **no**)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (yes / **no**)
     - The serializers: (yes / **no** / don't know)
     - The runtime per-record code paths (performance sensitive): (yes / **no** / don't know)
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (yes / **no** / don't know)
     - The S3 file system connector: (yes / **no** / don't know)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (yes / **no**)
     - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
   


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     }, {
       "hash" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551",
       "triggerID" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 7f90692a85fa44dd6027ddef3f12f3d93583b047 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     }, {
       "hash" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551",
       "triggerID" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26840",
       "triggerID" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "df5f94c57e96d558e34749b0b83676cedca8c971",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26937",
       "triggerID" : "df5f94c57e96d558e34749b0b83676cedca8c971",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * fc40896ffa0e888aecb5c52a5557fd980eba46b2 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26840) 
   * df5f94c57e96d558e34749b0b83676cedca8c971 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26937) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * aa5c9222e87b0ff7ae990593cb2162cb83442257 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] dawidwys closed pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
dawidwys closed pull request #17701:
URL: https://github.com/apache/flink/pull/17701


   


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] akalash commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
akalash commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r749439863



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
##########
@@ -989,19 +1006,6 @@ private void releaseOutputResources() throws Exception {
         }
     }
 
-    private Exception runAndSuppressThrowable(
-            ThrowingRunnable<?> runnable, @Nullable Exception originalException) {
-        try {
-            runnable.run();
-        } catch (Throwable t) {
-            // TODO: investigate why Throwable instead of Exception is used here.
-            Exception e = t instanceof Exception ? (Exception) t : new Exception(t);

Review comment:
       Yes, it is true. But according to `TODO` above it wasn't clear why we used `Throwable` at all. I personally think that it is not a problem that in case of unexpected throwable we lost other exceptions because this throwable will be the much more important. But of course, we can discuss it




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] dawidwys commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
dawidwys commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r756102896



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
##########
@@ -366,70 +368,100 @@ protected StreamTask(
             StreamTaskActionExecutor actionExecutor,
             TaskMailbox mailbox)
             throws Exception {
-        this.environment = environment;
-        this.configuration = new StreamConfig(environment.getTaskConfiguration());
-        this.recordWriter = createRecordWriterDelegate(configuration, environment);
-        this.actionExecutor = Preconditions.checkNotNull(actionExecutor);
-        this.mailboxProcessor = new MailboxProcessor(this::processInput, mailbox, actionExecutor);
-        this.mainMailboxExecutor = mailboxProcessor.getMainMailboxExecutor();
-        this.asyncExceptionHandler = new StreamTaskAsyncExceptionHandler(environment);
-        this.asyncOperationsThreadPool =
-                Executors.newCachedThreadPool(
-                        new ExecutorThreadFactory("AsyncOperations", uncaughtExceptionHandler));
-
-        environment.setMainMailboxExecutor(mainMailboxExecutor);
-        environment.setAsyncOperationsThreadPool(asyncOperationsThreadPool);
-
-        this.stateBackend = createStateBackend();
-        this.checkpointStorage = createCheckpointStorage(stateBackend);
-        this.changelogWriterAvailabilityProvider =
-                environment.getTaskStateManager().getStateChangelogStorage() == null
-                        ? null
-                        : environment
-                                .getTaskStateManager()
-                                .getStateChangelogStorage()
-                                .getAvailabilityProvider();
-
-        CheckpointStorageAccess checkpointStorageAccess =
-                checkpointStorage.createCheckpointStorage(getEnvironment().getJobID());
-
-        environment.setCheckpointStorageAccess(checkpointStorageAccess);
-
-        this.subtaskCheckpointCoordinator =
-                new SubtaskCheckpointCoordinatorImpl(
-                        checkpointStorageAccess,
-                        getName(),
-                        actionExecutor,
-                        getCancelables(),
-                        getAsyncOperationsThreadPool(),
-                        environment,
-                        this,
-                        configuration.isUnalignedCheckpointsEnabled(),
-                        configuration
-                                .getConfiguration()
-                                .get(
-                                        ExecutionCheckpointingOptions
-                                                .ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH),
-                        this::prepareInputSnapshot);
-
-        // if the clock is not already set, then assign a default TimeServiceProvider
-        if (timerService == null) {
-            this.timerService = createTimerService("Time Trigger for " + getName());
-        } else {
-            this.timerService = timerService;
-        }
+        // The registration of all closeable resources. The order of registration is important.
+        resourceCloser = new AutoCloseableRegistry();
+        try {
+            this.environment = environment;
+            this.configuration = new StreamConfig(environment.getTaskConfiguration());
+            this.mailboxProcessor =
+                    new MailboxProcessor(this::processInput, mailbox, actionExecutor);
+            // Should be closed last.
+            resourceCloser.registerCloseable(mailboxProcessor);
+
+            this.channelIOExecutor =
+                    Executors.newSingleThreadExecutor(
+                            new ExecutorThreadFactory("channel-state-unspilling"));
+            resourceCloser.registerCloseable(channelIOExecutor::shutdown);
+
+            this.recordWriter = createRecordWriterDelegate(configuration, environment);
+            // Release the output resources. this method should never fail.
+            resourceCloser.registerCloseable(this::releaseOutputResources);
+            // If the operators won't be closed explicitly, register it to a hard close.
+            resourceCloser.registerCloseable(this::closeAllOperators);
+            resourceCloser.registerCloseable(this::cleanUpInternal);
+
+            this.actionExecutor = Preconditions.checkNotNull(actionExecutor);
+            this.mainMailboxExecutor = mailboxProcessor.getMainMailboxExecutor();
+            this.asyncExceptionHandler = new StreamTaskAsyncExceptionHandler(environment);
+
+            this.asyncOperationsThreadPool =
+                    Executors.newCachedThreadPool(
+                            new ExecutorThreadFactory("AsyncOperations", uncaughtExceptionHandler));
+
+            // Register all asynchronous checkpoint threads.
+            resourceCloser.registerCloseable(this::shutdownAsyncThreads);
+            resourceCloser.registerCloseable(cancelables);
+
+            environment.setMainMailboxExecutor(mainMailboxExecutor);
+            environment.setAsyncOperationsThreadPool(asyncOperationsThreadPool);
+
+            this.stateBackend = createStateBackend();
+            this.checkpointStorage = createCheckpointStorage(stateBackend);
+            this.changelogWriterAvailabilityProvider =
+                    environment.getTaskStateManager().getStateChangelogStorage() == null
+                            ? null
+                            : environment
+                                    .getTaskStateManager()
+                                    .getStateChangelogStorage()
+                                    .getAvailabilityProvider();
+
+            CheckpointStorageAccess checkpointStorageAccess =
+                    checkpointStorage.createCheckpointStorage(getEnvironment().getJobID());
+
+            environment.setCheckpointStorageAccess(checkpointStorageAccess);
+
+            this.subtaskCheckpointCoordinator =
+                    new SubtaskCheckpointCoordinatorImpl(
+                            checkpointStorageAccess,
+                            getName(),
+                            actionExecutor,
+                            getCancelables(),
+                            getAsyncOperationsThreadPool(),
+                            environment,
+                            this,
+                            configuration.isUnalignedCheckpointsEnabled(),
+                            configuration
+                                    .getConfiguration()
+                                    .get(
+                                            ExecutionCheckpointingOptions
+                                                    .ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH),
+                            this::prepareInputSnapshot);
+
+            // if the clock is not already set, then assign a default TimeServiceProvider
+            if (timerService == null) {
+                this.timerService = createTimerService("Time Trigger for " + getName());
+            } else {
+                this.timerService = timerService;
+            }
 
-        this.systemTimerService = createTimerService("System Time Trigger for " + getName());
-        this.channelIOExecutor =
-                Executors.newSingleThreadExecutor(
-                        new ExecutorThreadFactory("channel-state-unspilling"));
+            this.systemTimerService = createTimerService("System Time Trigger for " + getName());
+            // Register to stop all timers and threads. Should be closed first.
+            resourceCloser.registerCloseable(this::tryShutdownTimerService);
 
-        injectChannelStateWriterIntoChannels();
+            injectChannelStateWriterIntoChannels();
 
-        environment.getMetricGroup().getIOMetricGroup().setEnableBusyTime(true);
-        Configuration taskManagerConf = environment.getTaskManagerInfo().getConfiguration();
+            environment.getMetricGroup().getIOMetricGroup().setEnableBusyTime(true);
+            Configuration taskManagerConf = environment.getTaskManagerInfo().getConfiguration();
 
-        this.bufferDebloatPeriod = taskManagerConf.get(BUFFER_DEBLOAT_PERIOD).toMillis();
+            this.bufferDebloatPeriod = taskManagerConf.get(BUFFER_DEBLOAT_PERIOD).toMillis();
+        } catch (Exception ex) {
+            try {
+                resourceCloser.close();

Review comment:
       Given the change in `AutoCloseableRegistry#close`, how do you feel about removing those catch blocks ?




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     }, {
       "hash" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551",
       "triggerID" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 7f90692a85fa44dd6027ddef3f12f3d93583b047 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551) 
   * fc40896ffa0e888aecb5c52a5557fd980eba46b2 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     }, {
       "hash" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551",
       "triggerID" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26840",
       "triggerID" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "df5f94c57e96d558e34749b0b83676cedca8c971",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26937",
       "triggerID" : "df5f94c57e96d558e34749b0b83676cedca8c971",
       "triggerType" : "PUSH"
     }, {
       "hash" : "c9395bb8596f55e5a56b697eb27b9183df187594",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26951",
       "triggerID" : "c9395bb8596f55e5a56b697eb27b9183df187594",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * c9395bb8596f55e5a56b697eb27b9183df187594 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26951) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] akalash commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
akalash commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r747524985



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
##########
@@ -978,6 +998,18 @@ private void shutdownAsyncThreads() throws Exception {
         }
     }
 
+    private static Closeable toCloseable(AutoCloseable autoCloseable) {
+        return () -> {
+            try {
+                autoCloseable.close();
+            } catch (IOException ex) {
+                throw ex;
+            } catch (Exception exception) {
+                rethrow(exception);
+            }
+        };
+    }
+

Review comment:
       I have updated the PR with new implementation




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] akalash commented on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
akalash commented on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-976458504


   @dawidwys, just in case, I believe we can implement new `IOUtils#closeAllThrowable` which will be able to handle all throwable exceptions which allow us to keep the old behavior if you think that it is better to not change this place in this ticket.


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] dawidwys commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
dawidwys commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r750030129



##########
File path: flink-core/src/main/java/org/apache/flink/util/AbstractCloseableRegistry.java
##########
@@ -43,145 +39,15 @@
  * @param <T> Type for potential meta data associated with the registering closeables
  */
 @Internal
-public abstract class AbstractCloseableRegistry<C extends Closeable, T> implements Closeable {
-
-    /** Lock that guards state of this registry. * */
-    private final Object lock;
-
-    /** Map from tracked Closeables to some associated meta data. */
-    @GuardedBy("lock")
-    protected final Map<Closeable, T> closeableToRef;
-
-    /** Indicates if this registry is closed. */
-    @GuardedBy("lock")
-    private boolean closed;
+public abstract class AbstractCloseableRegistry<C extends Closeable, T>
+        extends AbstractAutoCloseableRegistry<Closeable, C, T> implements Closeable {
 
     public AbstractCloseableRegistry(@Nonnull Map<Closeable, T> closeableToRef) {
-        this.lock = new Object();
-        this.closeableToRef = Preconditions.checkNotNull(closeableToRef);
-        this.closed = false;
-    }
-
-    /**
-     * Registers a {@link Closeable} with the registry. In case the registry is already closed, this
-     * method throws an {@link IllegalStateException} and closes the passed {@link Closeable}.
-     *
-     * @param closeable Closeable tor register
-     * @throws IOException exception when the registry was closed before
-     */
-    public final void registerCloseable(C closeable) throws IOException {
-
-        if (null == closeable) {
-            return;
-        }
-
-        synchronized (getSynchronizationLock()) {
-            if (!closed) {
-                doRegister(closeable, closeableToRef);
-                return;
-            }
-        }
-
-        IOUtils.closeQuietly(closeable);
-        throw new IOException(
-                "Cannot register Closeable, registry is already closed. Closing argument.");
-    }
-
-    /**
-     * Removes a {@link Closeable} from the registry.
-     *
-     * @param closeable instance to remove from the registry.
-     * @return true if the closeable was previously registered and became unregistered through this
-     *     call.
-     */
-    public final boolean unregisterCloseable(C closeable) {
-
-        if (null == closeable) {
-            return false;
-        }
-
-        synchronized (getSynchronizationLock()) {
-            return doUnRegister(closeable, closeableToRef);
-        }
+        super(closeableToRef, true);
     }
 
     @Override
     public void close() throws IOException {
-        Collection<Closeable> toCloseCopy;
-
-        synchronized (getSynchronizationLock()) {
-            if (closed) {
-                return;
-            }
-
-            closed = true;
-
-            toCloseCopy = getReferencesToClose();
-
-            closeableToRef.clear();
-        }
-
-        IOUtils.closeAllQuietly(toCloseCopy);
-    }
-
-    public boolean isClosed() {
-        synchronized (getSynchronizationLock()) {
-            return closed;
-        }
-    }
-
-    protected Collection<Closeable> getReferencesToClose() {
-        return new ArrayList<>(closeableToRef.keySet());
-    }
-
-    /**
-     * Does the actual registration of the closeable with the registry map. This should not do any
-     * long running or potentially blocking operations as is is executed under the registry's lock.
-     */
-    protected abstract void doRegister(
-            @Nonnull C closeable, @Nonnull Map<Closeable, T> closeableMap);
-
-    /**
-     * Does the actual un-registration of the closeable from the registry map. This should not do
-     * any long running or potentially blocking operations as is is executed under the registry's
-     * lock.
-     */
-    protected abstract boolean doUnRegister(
-            @Nonnull C closeable, @Nonnull Map<Closeable, T> closeableMap);
-
-    /**
-     * Returns the lock on which manipulations to members closeableToRef and closeable must be
-     * synchronized.
-     */
-    protected final Object getSynchronizationLock() {
-        return lock;
-    }
-
-    /** Adds a mapping to the registry map, respecting locking. */
-    protected final void addCloseableInternal(Closeable closeable, T metaData) {
-        synchronized (getSynchronizationLock()) {
-            closeableToRef.put(closeable, metaData);
-        }
-    }
-
-    /** Removes a mapping from the registry map, respecting locking. */
-    protected final boolean removeCloseableInternal(Closeable closeable) {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.remove(closeable) != null;
-        }
-    }
-
-    @VisibleForTesting
-    public final int getNumberOfRegisteredCloseables() {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.size();
-        }
-    }
-
-    @VisibleForTesting
-    public final boolean isCloseableRegistered(Closeable c) {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.containsKey(c);
-        }
+        IOUtils.closeAllCloseable(prepareToExceptionalClose());

Review comment:
       Sorry, but I think I am not buying into your argumentation, please take a look here: https://github.com/dawidwys/flink/commit/92bc4b1df69826bc80026c54136578a0312f1bab (I have not fixed the tests, but it should be easy to do so).




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot commented on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686






-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r747263950



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
##########
@@ -238,6 +241,8 @@
     /** The currently active background materialization threads. */
     private final CloseableRegistry cancelables = new CloseableRegistry();
 
+    private final Closer allCloseableResources;

Review comment:
       `allCloseableResources` -> `resourceCloser`
   
   I guess the name is a bit confusing. This is a closer not resource :-)

##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
##########
@@ -978,6 +998,18 @@ private void shutdownAsyncThreads() throws Exception {
         }
     }
 
+    private static Closeable toCloseable(AutoCloseable autoCloseable) {
+        return () -> {
+            try {
+                autoCloseable.close();
+            } catch (IOException ex) {
+                throw ex;
+            } catch (Exception exception) {
+                rethrow(exception);
+            }
+        };
+    }
+

Review comment:
       Just a quick sum-up on this based on the offline discussion:
   
   it is better not to wrap to RuntimeException because it changes the footprint of StreamTask 




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054) 
   * aa5c9222e87b0ff7ae990593cb2162cb83442257 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     }, {
       "hash" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * aa5c9222e87b0ff7ae990593cb2162cb83442257 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374) 
   * 7f90692a85fa44dd6027ddef3f12f3d93583b047 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686






-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] akalash commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
akalash commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r749432560



##########
File path: flink-core/src/main/java/org/apache/flink/util/AbstractCloseableRegistry.java
##########
@@ -43,145 +39,15 @@
  * @param <T> Type for potential meta data associated with the registering closeables
  */
 @Internal
-public abstract class AbstractCloseableRegistry<C extends Closeable, T> implements Closeable {
-
-    /** Lock that guards state of this registry. * */
-    private final Object lock;
-
-    /** Map from tracked Closeables to some associated meta data. */
-    @GuardedBy("lock")
-    protected final Map<Closeable, T> closeableToRef;
-
-    /** Indicates if this registry is closed. */
-    @GuardedBy("lock")
-    private boolean closed;
+public abstract class AbstractCloseableRegistry<C extends Closeable, T>
+        extends AbstractAutoCloseableRegistry<Closeable, C, T> implements Closeable {
 
     public AbstractCloseableRegistry(@Nonnull Map<Closeable, T> closeableToRef) {
-        this.lock = new Object();
-        this.closeableToRef = Preconditions.checkNotNull(closeableToRef);
-        this.closed = false;
-    }
-
-    /**
-     * Registers a {@link Closeable} with the registry. In case the registry is already closed, this
-     * method throws an {@link IllegalStateException} and closes the passed {@link Closeable}.
-     *
-     * @param closeable Closeable tor register
-     * @throws IOException exception when the registry was closed before
-     */
-    public final void registerCloseable(C closeable) throws IOException {
-
-        if (null == closeable) {
-            return;
-        }
-
-        synchronized (getSynchronizationLock()) {
-            if (!closed) {
-                doRegister(closeable, closeableToRef);
-                return;
-            }
-        }
-
-        IOUtils.closeQuietly(closeable);
-        throw new IOException(
-                "Cannot register Closeable, registry is already closed. Closing argument.");
-    }
-
-    /**
-     * Removes a {@link Closeable} from the registry.
-     *
-     * @param closeable instance to remove from the registry.
-     * @return true if the closeable was previously registered and became unregistered through this
-     *     call.
-     */
-    public final boolean unregisterCloseable(C closeable) {
-
-        if (null == closeable) {
-            return false;
-        }
-
-        synchronized (getSynchronizationLock()) {
-            return doUnRegister(closeable, closeableToRef);
-        }
+        super(closeableToRef, true);
     }
 
     @Override
     public void close() throws IOException {
-        Collection<Closeable> toCloseCopy;
-
-        synchronized (getSynchronizationLock()) {
-            if (closed) {
-                return;
-            }
-
-            closed = true;
-
-            toCloseCopy = getReferencesToClose();
-
-            closeableToRef.clear();
-        }
-
-        IOUtils.closeAllQuietly(toCloseCopy);
-    }
-
-    public boolean isClosed() {
-        synchronized (getSynchronizationLock()) {
-            return closed;
-        }
-    }
-
-    protected Collection<Closeable> getReferencesToClose() {
-        return new ArrayList<>(closeableToRef.keySet());
-    }
-
-    /**
-     * Does the actual registration of the closeable with the registry map. This should not do any
-     * long running or potentially blocking operations as is is executed under the registry's lock.
-     */
-    protected abstract void doRegister(
-            @Nonnull C closeable, @Nonnull Map<Closeable, T> closeableMap);
-
-    /**
-     * Does the actual un-registration of the closeable from the registry map. This should not do
-     * any long running or potentially blocking operations as is is executed under the registry's
-     * lock.
-     */
-    protected abstract boolean doUnRegister(
-            @Nonnull C closeable, @Nonnull Map<Closeable, T> closeableMap);
-
-    /**
-     * Returns the lock on which manipulations to members closeableToRef and closeable must be
-     * synchronized.
-     */
-    protected final Object getSynchronizationLock() {
-        return lock;
-    }
-
-    /** Adds a mapping to the registry map, respecting locking. */
-    protected final void addCloseableInternal(Closeable closeable, T metaData) {
-        synchronized (getSynchronizationLock()) {
-            closeableToRef.put(closeable, metaData);
-        }
-    }
-
-    /** Removes a mapping from the registry map, respecting locking. */
-    protected final boolean removeCloseableInternal(Closeable closeable) {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.remove(closeable) != null;
-        }
-    }
-
-    @VisibleForTesting
-    public final int getNumberOfRegisteredCloseables() {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.size();
-        }
-    }
-
-    @VisibleForTesting
-    public final boolean isCloseableRegistered(Closeable c) {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.containsKey(c);
-        }
+        IOUtils.closeAllCloseable(prepareToExceptionalClose());

Review comment:
       > Why do we need two abstract classes?
   
   Because one of them implement `AutoCloseable` while another implements `Closeable`.
   
   > prepareToExceptionalClose() will always return an empty collection for all subclasses of this class (you pass true for the close quietly flag. Lastly why does the prepareToExceptionalClose closes anything? 
   
   I pass the `true` in order to keep backward compatibility with old behavior but it will be possible to change it if we need other implementation with unquiet close.
   The main target of `prepareToExceptionalClose` is to avoid duplication of the code related to closing resources quietly. We can always return the list for the closing but in this case, we will need to check the `quiet` flag in both implementations. Also, we can rename it but I need to think of a good name.
   
   > Can't we have an abstract doClose(Collection<R> toClose) throws E instead?
   
   I wish to have it but it is not possible since `AbstractAutoCloseableRegistry` should implement `AutoCloseable` while `AbstractCloseableRegistry` should implement `Closeable`. So we can not avoid implementing `close() throws IOException`
   
   
   In a conclusion, the main problem here is the implementation of two interfaces `AutoCloseable` and `Closeable` which have different exceptions for `close` and it is impossible to generalize. (maybe I am wrong here and somehow we can use generic here but I didn't find an easy way to do so)
   




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] akalash commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
akalash commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r755060892



##########
File path: flink-core/src/main/java/org/apache/flink/util/AbstractAutoCloseableRegistry.java
##########
@@ -158,14 +157,14 @@ protected final Object getSynchronizationLock() {
     }
 
     /** Adds a mapping to the registry map, respecting locking. */
-    protected final void addCloseableInternal(Closeable closeable, T metaData) {
+    protected final void addCloseableInternal(R closeable, T metaData) {

Review comment:
       It seems it is true. We can remove it.




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     }, {
       "hash" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551",
       "triggerID" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26840",
       "triggerID" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "df5f94c57e96d558e34749b0b83676cedca8c971",
       "status" : "CANCELED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26937",
       "triggerID" : "df5f94c57e96d558e34749b0b83676cedca8c971",
       "triggerType" : "PUSH"
     }, {
       "hash" : "c9395bb8596f55e5a56b697eb27b9183df187594",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26951",
       "triggerID" : "c9395bb8596f55e5a56b697eb27b9183df187594",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * df5f94c57e96d558e34749b0b83676cedca8c971 Azure: [CANCELED](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26937) 
   * c9395bb8596f55e5a56b697eb27b9183df187594 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26951) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] dawidwys commented on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
dawidwys commented on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-978004914


   I believe it looks good. Will have a final check tomorrow morning and then merge it.


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     }, {
       "hash" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551",
       "triggerID" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26840",
       "triggerID" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 7f90692a85fa44dd6027ddef3f12f3d93583b047 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551) 
   * fc40896ffa0e888aecb5c52a5557fd980eba46b2 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26840) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] dawidwys commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
dawidwys commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r754886188



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
##########
@@ -989,19 +1006,6 @@ private void releaseOutputResources() throws Exception {
         }
     }
 
-    private Exception runAndSuppressThrowable(
-            ThrowingRunnable<?> runnable, @Nullable Exception originalException) {
-        try {
-            runnable.run();
-        } catch (Throwable t) {
-            // TODO: investigate why Throwable instead of Exception is used here.
-            Exception e = t instanceof Exception ? (Exception) t : new Exception(t);

Review comment:
       One more point to the discussion, which actually makes me prefer keeping the old behaviour.
   
   In `TaskCanceler` we catch `Throwable` and rethrow it only if it is one of `InternalError`, `UnknownError`, `ThreadDeath` all other are ignored. If we ignore an error we should have a proper resource cleaning for such an error and thus we should not skip resource cleaning for `Throwables`, imo.




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     }, {
       "hash" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551",
       "triggerID" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26840",
       "triggerID" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "df5f94c57e96d558e34749b0b83676cedca8c971",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26937",
       "triggerID" : "df5f94c57e96d558e34749b0b83676cedca8c971",
       "triggerType" : "PUSH"
     }, {
       "hash" : "c9395bb8596f55e5a56b697eb27b9183df187594",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26951",
       "triggerID" : "c9395bb8596f55e5a56b697eb27b9183df187594",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * fc40896ffa0e888aecb5c52a5557fd980eba46b2 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26840) 
   * df5f94c57e96d558e34749b0b83676cedca8c971 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26937) 
   * c9395bb8596f55e5a56b697eb27b9183df187594 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26951) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     }, {
       "hash" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551",
       "triggerID" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26840",
       "triggerID" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "df5f94c57e96d558e34749b0b83676cedca8c971",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26937",
       "triggerID" : "df5f94c57e96d558e34749b0b83676cedca8c971",
       "triggerType" : "PUSH"
     }, {
       "hash" : "c9395bb8596f55e5a56b697eb27b9183df187594",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "c9395bb8596f55e5a56b697eb27b9183df187594",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * fc40896ffa0e888aecb5c52a5557fd980eba46b2 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26840) 
   * df5f94c57e96d558e34749b0b83676cedca8c971 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26937) 
   * c9395bb8596f55e5a56b697eb27b9183df187594 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] akalash commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
akalash commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r755053001



##########
File path: flink-core/src/main/java/org/apache/flink/util/AbstractAutoCloseableRegistry.java
##########
@@ -43,33 +43,36 @@
  * @param <T> Type for potential meta data associated with the registering closeables
  */
 @Internal
-public abstract class AbstractCloseableRegistry<C extends Closeable, T> implements Closeable {
+public abstract class AbstractAutoCloseableRegistry<
+                R extends AutoCloseable, C extends R, T, E extends Exception>
+        implements AutoCloseable {
 
     /** Lock that guards state of this registry. * */
     private final Object lock;
 
     /** Map from tracked Closeables to some associated meta data. */
     @GuardedBy("lock")
-    protected final Map<Closeable, T> closeableToRef;
+    protected final Map<R, T> closeableToRef;
 
     /** Indicates if this registry is closed. */
     @GuardedBy("lock")
     private boolean closed;
 
-    public AbstractCloseableRegistry(@Nonnull Map<Closeable, T> closeableToRef) {
+    public AbstractAutoCloseableRegistry(@Nonnull Map<R, T> closeableToRef) {
         this.lock = new Object();
         this.closeableToRef = Preconditions.checkNotNull(closeableToRef);
         this.closed = false;
     }
 
     /**
-     * Registers a {@link Closeable} with the registry. In case the registry is already closed, this
-     * method throws an {@link IllegalStateException} and closes the passed {@link Closeable}.
+     * Registers a {@link AutoCloseable} with the registry. In case the registry is already closed,
+     * this method throws an {@link IllegalStateException} and closes the passed {@link
+     * AutoCloseable}.
      *
-     * @param closeable Closeable tor register
-     * @throws IOException exception when the registry was closed before
+     * @param closeable Closeable to register.
+     * @throws IOException exception when the registry was closed before.
      */
-    public final void registerCloseable(C closeable) throws IOException {
+    public void registerCloseable(C closeable) throws IOException {

Review comment:
       I returned it back




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054) 
   * aa5c9222e87b0ff7ae990593cb2162cb83442257 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot commented on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] dawidwys commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
dawidwys commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r749228879



##########
File path: flink-core/src/main/java/org/apache/flink/core/fs/AutoCloseableRegistry.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.flink.core.fs;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.util.AbstractAutoCloseableRegistry;
+
+import javax.annotation.Nonnull;
+
+import java.io.Closeable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * This class allows to register instances of {@link AutoCloseable}, which are all closed if this
+ * registry is closed.
+ *
+ * <p>Registering to an already closed registry will throw an exception and close the provided
+ * {@link AutoCloseable}
+ *
+ * <p>All methods in this class are thread-safe.
+ *
+ * <p>Unlike {@link CloseableRegistry} this class can throw the exception during close.

Review comment:
       ```suggestion
    * <p>Unlike {@link CloseableRegistry} this class can throw an exception during the close.
   ```

##########
File path: flink-core/src/main/java/org/apache/flink/core/fs/AutoCloseableRegistry.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.flink.core.fs;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.util.AbstractAutoCloseableRegistry;
+
+import javax.annotation.Nonnull;
+
+import java.io.Closeable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * This class allows to register instances of {@link AutoCloseable}, which are all closed if this
+ * registry is closed.
+ *
+ * <p>Registering to an already closed registry will throw an exception and close the provided
+ * {@link AutoCloseable}
+ *
+ * <p>All methods in this class are thread-safe.

Review comment:
       nit: `@ThreadSafe` annotation?

##########
File path: flink-core/src/main/java/org/apache/flink/util/AbstractCloseableRegistry.java
##########
@@ -43,145 +39,15 @@
  * @param <T> Type for potential meta data associated with the registering closeables
  */
 @Internal
-public abstract class AbstractCloseableRegistry<C extends Closeable, T> implements Closeable {
-
-    /** Lock that guards state of this registry. * */
-    private final Object lock;
-
-    /** Map from tracked Closeables to some associated meta data. */
-    @GuardedBy("lock")
-    protected final Map<Closeable, T> closeableToRef;
-
-    /** Indicates if this registry is closed. */
-    @GuardedBy("lock")
-    private boolean closed;
+public abstract class AbstractCloseableRegistry<C extends Closeable, T>
+        extends AbstractAutoCloseableRegistry<Closeable, C, T> implements Closeable {
 
     public AbstractCloseableRegistry(@Nonnull Map<Closeable, T> closeableToRef) {
-        this.lock = new Object();
-        this.closeableToRef = Preconditions.checkNotNull(closeableToRef);
-        this.closed = false;
-    }
-
-    /**
-     * Registers a {@link Closeable} with the registry. In case the registry is already closed, this
-     * method throws an {@link IllegalStateException} and closes the passed {@link Closeable}.
-     *
-     * @param closeable Closeable tor register
-     * @throws IOException exception when the registry was closed before
-     */
-    public final void registerCloseable(C closeable) throws IOException {
-
-        if (null == closeable) {
-            return;
-        }
-
-        synchronized (getSynchronizationLock()) {
-            if (!closed) {
-                doRegister(closeable, closeableToRef);
-                return;
-            }
-        }
-
-        IOUtils.closeQuietly(closeable);
-        throw new IOException(
-                "Cannot register Closeable, registry is already closed. Closing argument.");
-    }
-
-    /**
-     * Removes a {@link Closeable} from the registry.
-     *
-     * @param closeable instance to remove from the registry.
-     * @return true if the closeable was previously registered and became unregistered through this
-     *     call.
-     */
-    public final boolean unregisterCloseable(C closeable) {
-
-        if (null == closeable) {
-            return false;
-        }
-
-        synchronized (getSynchronizationLock()) {
-            return doUnRegister(closeable, closeableToRef);
-        }
+        super(closeableToRef, true);
     }
 
     @Override
     public void close() throws IOException {
-        Collection<Closeable> toCloseCopy;
-
-        synchronized (getSynchronizationLock()) {
-            if (closed) {
-                return;
-            }
-
-            closed = true;
-
-            toCloseCopy = getReferencesToClose();
-
-            closeableToRef.clear();
-        }
-
-        IOUtils.closeAllQuietly(toCloseCopy);
-    }
-
-    public boolean isClosed() {
-        synchronized (getSynchronizationLock()) {
-            return closed;
-        }
-    }
-
-    protected Collection<Closeable> getReferencesToClose() {
-        return new ArrayList<>(closeableToRef.keySet());
-    }
-
-    /**
-     * Does the actual registration of the closeable with the registry map. This should not do any
-     * long running or potentially blocking operations as is is executed under the registry's lock.
-     */
-    protected abstract void doRegister(
-            @Nonnull C closeable, @Nonnull Map<Closeable, T> closeableMap);
-
-    /**
-     * Does the actual un-registration of the closeable from the registry map. This should not do
-     * any long running or potentially blocking operations as is is executed under the registry's
-     * lock.
-     */
-    protected abstract boolean doUnRegister(
-            @Nonnull C closeable, @Nonnull Map<Closeable, T> closeableMap);
-
-    /**
-     * Returns the lock on which manipulations to members closeableToRef and closeable must be
-     * synchronized.
-     */
-    protected final Object getSynchronizationLock() {
-        return lock;
-    }
-
-    /** Adds a mapping to the registry map, respecting locking. */
-    protected final void addCloseableInternal(Closeable closeable, T metaData) {
-        synchronized (getSynchronizationLock()) {
-            closeableToRef.put(closeable, metaData);
-        }
-    }
-
-    /** Removes a mapping from the registry map, respecting locking. */
-    protected final boolean removeCloseableInternal(Closeable closeable) {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.remove(closeable) != null;
-        }
-    }
-
-    @VisibleForTesting
-    public final int getNumberOfRegisteredCloseables() {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.size();
-        }
-    }
-
-    @VisibleForTesting
-    public final boolean isCloseableRegistered(Closeable c) {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.containsKey(c);
-        }
+        IOUtils.closeAllCloseable(prepareToExceptionalClose());

Review comment:
       Something's really wrong with the entire hierarchy imo. Why do we need two abstract classes?
   
   If we do what's the point of this close method? `prepareToExceptionalClose()` will always return an empty collection for all subclasses of this class (you pass `true` for the close quietly flag. Lastly why does the `prepareToExceptionalClose` closes anything? That's really counterintuitive.
   
   Can't we have an abstract `doClose(Collection<R> toClose) throws E` instead?

##########
File path: flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/tasks/StreamTaskTest.java
##########
@@ -1157,6 +1158,18 @@ public void updateTaskExecutionState(TaskExecutionState taskExecutionState) {
                                 .build());
     }
 
+    @Test
+    public void testFailInEndOfConstructor() throws Exception {
+        HashMap<String, String> conf = new HashMap<>();

Review comment:
       nit: use configuration directly:
   ```
   Configuration conf = new Configuration();
   conf.setString(BUFFER_DEBLOAT_PERIOD.key(), "a");
   ```

##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
##########
@@ -989,19 +1006,6 @@ private void releaseOutputResources() throws Exception {
         }
     }
 
-    private Exception runAndSuppressThrowable(
-            ThrowingRunnable<?> runnable, @Nullable Exception originalException) {
-        try {
-            runnable.run();
-        } catch (Throwable t) {
-            // TODO: investigate why Throwable instead of Exception is used here.
-            Exception e = t instanceof Exception ? (Exception) t : new Exception(t);

Review comment:
       If I understand the new code, you changed a bit that behaviour. We no longer close other resources if a Throwable that does not extend from an exception is thrown, right?




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     }, {
       "hash" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551",
       "triggerID" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * aa5c9222e87b0ff7ae990593cb2162cb83442257 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374) 
   * 7f90692a85fa44dd6027ddef3f12f3d93583b047 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     }, {
       "hash" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551",
       "triggerID" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26840",
       "triggerID" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * fc40896ffa0e888aecb5c52a5557fd980eba46b2 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26840) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] akalash commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
akalash commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r755056468



##########
File path: flink-core/src/main/java/org/apache/flink/core/fs/CloseableRegistry.java
##########
@@ -62,10 +67,13 @@ protected boolean doUnRegister(
         return closeableMap.remove(closeable) != null;
     }
 
+    /**
+     * This implementation doesn't imply any exception during closing due to backward compatibility.
+     */
     @Override
-    protected Collection<Closeable> getReferencesToClose() {
-        ArrayList<Closeable> closeablesToClose = new ArrayList<>(closeableToRef.keySet());
-        Collections.reverse(closeablesToClose);
-        return closeablesToClose;
+    public void doClose(Collection<Closeable> toClose) throws IOException {
+        List<AutoCloseable> reversed = new ArrayList<>(toClose);

Review comment:
       Yes, I can do it




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26054",
       "triggerID" : "a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26374",
       "triggerID" : "aa5c9222e87b0ff7ae990593cb2162cb83442257",
       "triggerType" : "PUSH"
     }, {
       "hash" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26551",
       "triggerID" : "7f90692a85fa44dd6027ddef3f12f3d93583b047",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26840",
       "triggerID" : "fc40896ffa0e888aecb5c52a5557fd980eba46b2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "df5f94c57e96d558e34749b0b83676cedca8c971",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "df5f94c57e96d558e34749b0b83676cedca8c971",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * fc40896ffa0e888aecb5c52a5557fd980eba46b2 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=26840) 
   * df5f94c57e96d558e34749b0b83676cedca8c971 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] akalash commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
akalash commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r755231423



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
##########
@@ -989,19 +1006,6 @@ private void releaseOutputResources() throws Exception {
         }
     }
 
-    private Exception runAndSuppressThrowable(
-            ThrowingRunnable<?> runnable, @Nullable Exception originalException) {
-        try {
-            runnable.run();
-        } catch (Throwable t) {
-            // TODO: investigate why Throwable instead of Exception is used here.
-            Exception e = t instanceof Exception ? (Exception) t : new Exception(t);

Review comment:
       Ok, I have left the old behavior. Please take a look at the last commit.




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] akalash commented on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
akalash commented on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-975446479


   @dawidwys , I have updated the PR with mostly your proposed changes. I decided to get rid of `quiet` flag which makes implementation less flexible but simpler. And also it means that different implementations of `AbstractAutoCloseableRegistry` behaves differently on close some of them throw the exception while others don't.


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] dawidwys commented on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
dawidwys commented on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-978921510


   @akalash do you think it makes sense to backport it to 1.14.x and 1.13.x?


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] dawidwys commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
dawidwys commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r754242318



##########
File path: flink-core/src/main/java/org/apache/flink/core/fs/CloseableRegistry.java
##########
@@ -62,10 +67,13 @@ protected boolean doUnRegister(
         return closeableMap.remove(closeable) != null;
     }
 
+    /**
+     * This implementation doesn't imply any exception during closing due to backward compatibility.
+     */
     @Override
-    protected Collection<Closeable> getReferencesToClose() {
-        ArrayList<Closeable> closeablesToClose = new ArrayList<>(closeableToRef.keySet());
-        Collections.reverse(closeablesToClose);
-        return closeablesToClose;
+    public void doClose(Collection<Closeable> toClose) throws IOException {
+        List<AutoCloseable> reversed = new ArrayList<>(toClose);

Review comment:
       WDYT about reworking it to use guava's `Lists.reverse`? We would get rid off a single list copy. We would have to change the `doClose` to `doClose(List<>)`. 

##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
##########
@@ -989,19 +1006,6 @@ private void releaseOutputResources() throws Exception {
         }
     }
 
-    private Exception runAndSuppressThrowable(
-            ThrowingRunnable<?> runnable, @Nullable Exception originalException) {
-        try {
-            runnable.run();
-        } catch (Throwable t) {
-            // TODO: investigate why Throwable instead of Exception is used here.
-            Exception e = t instanceof Exception ? (Exception) t : new Exception(t);

Review comment:
       Honestly, I don't feel confident making that decision. It's quite an outer layer of the entire Flink stack, so actually it might make sense to really catch all sorts of errors and try to clean up the resources.
   
   @pnowojski WDYT?

##########
File path: flink-core/src/main/java/org/apache/flink/util/AbstractAutoCloseableRegistry.java
##########
@@ -158,14 +157,14 @@ protected final Object getSynchronizationLock() {
     }
 
     /** Adds a mapping to the registry map, respecting locking. */
-    protected final void addCloseableInternal(Closeable closeable, T metaData) {
+    protected final void addCloseableInternal(R closeable, T metaData) {

Review comment:
       My IDE says it is unused. Is that correct? If so, can we drop it?

##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
##########
@@ -366,70 +368,100 @@ protected StreamTask(
             StreamTaskActionExecutor actionExecutor,
             TaskMailbox mailbox)
             throws Exception {
-        this.environment = environment;
-        this.configuration = new StreamConfig(environment.getTaskConfiguration());
-        this.recordWriter = createRecordWriterDelegate(configuration, environment);
-        this.actionExecutor = Preconditions.checkNotNull(actionExecutor);
-        this.mailboxProcessor = new MailboxProcessor(this::processInput, mailbox, actionExecutor);
-        this.mainMailboxExecutor = mailboxProcessor.getMainMailboxExecutor();
-        this.asyncExceptionHandler = new StreamTaskAsyncExceptionHandler(environment);
-        this.asyncOperationsThreadPool =
-                Executors.newCachedThreadPool(
-                        new ExecutorThreadFactory("AsyncOperations", uncaughtExceptionHandler));
-
-        environment.setMainMailboxExecutor(mainMailboxExecutor);
-        environment.setAsyncOperationsThreadPool(asyncOperationsThreadPool);
-
-        this.stateBackend = createStateBackend();
-        this.checkpointStorage = createCheckpointStorage(stateBackend);
-        this.changelogWriterAvailabilityProvider =
-                environment.getTaskStateManager().getStateChangelogStorage() == null
-                        ? null
-                        : environment
-                                .getTaskStateManager()
-                                .getStateChangelogStorage()
-                                .getAvailabilityProvider();
-
-        CheckpointStorageAccess checkpointStorageAccess =
-                checkpointStorage.createCheckpointStorage(getEnvironment().getJobID());
-
-        environment.setCheckpointStorageAccess(checkpointStorageAccess);
-
-        this.subtaskCheckpointCoordinator =
-                new SubtaskCheckpointCoordinatorImpl(
-                        checkpointStorageAccess,
-                        getName(),
-                        actionExecutor,
-                        getCancelables(),
-                        getAsyncOperationsThreadPool(),
-                        environment,
-                        this,
-                        configuration.isUnalignedCheckpointsEnabled(),
-                        configuration
-                                .getConfiguration()
-                                .get(
-                                        ExecutionCheckpointingOptions
-                                                .ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH),
-                        this::prepareInputSnapshot);
-
-        // if the clock is not already set, then assign a default TimeServiceProvider
-        if (timerService == null) {
-            this.timerService = createTimerService("Time Trigger for " + getName());
-        } else {
-            this.timerService = timerService;
-        }
+        // The registration of all closeable resources. The order of registration is important.
+        resourceCloser = new AutoCloseableRegistry();
+        try {
+            this.environment = environment;
+            this.configuration = new StreamConfig(environment.getTaskConfiguration());
+            this.mailboxProcessor =
+                    new MailboxProcessor(this::processInput, mailbox, actionExecutor);
+            // Should be closed last.
+            resourceCloser.registerCloseable(mailboxProcessor);
+
+            this.channelIOExecutor =
+                    Executors.newSingleThreadExecutor(
+                            new ExecutorThreadFactory("channel-state-unspilling"));
+            resourceCloser.registerCloseable(channelIOExecutor::shutdown);
+
+            this.recordWriter = createRecordWriterDelegate(configuration, environment);
+            // Release the output resources. this method should never fail.
+            resourceCloser.registerCloseable(this::releaseOutputResources);
+            // If the operators won't be closed explicitly, register it to a hard close.
+            resourceCloser.registerCloseable(this::closeAllOperators);
+            resourceCloser.registerCloseable(this::cleanUpInternal);
+
+            this.actionExecutor = Preconditions.checkNotNull(actionExecutor);
+            this.mainMailboxExecutor = mailboxProcessor.getMainMailboxExecutor();
+            this.asyncExceptionHandler = new StreamTaskAsyncExceptionHandler(environment);
+
+            this.asyncOperationsThreadPool =
+                    Executors.newCachedThreadPool(
+                            new ExecutorThreadFactory("AsyncOperations", uncaughtExceptionHandler));
+
+            // Register all asynchronous checkpoint threads.
+            resourceCloser.registerCloseable(this::shutdownAsyncThreads);
+            resourceCloser.registerCloseable(cancelables);
+
+            environment.setMainMailboxExecutor(mainMailboxExecutor);
+            environment.setAsyncOperationsThreadPool(asyncOperationsThreadPool);
+
+            this.stateBackend = createStateBackend();
+            this.checkpointStorage = createCheckpointStorage(stateBackend);
+            this.changelogWriterAvailabilityProvider =
+                    environment.getTaskStateManager().getStateChangelogStorage() == null
+                            ? null
+                            : environment
+                                    .getTaskStateManager()
+                                    .getStateChangelogStorage()
+                                    .getAvailabilityProvider();
+
+            CheckpointStorageAccess checkpointStorageAccess =
+                    checkpointStorage.createCheckpointStorage(getEnvironment().getJobID());
+
+            environment.setCheckpointStorageAccess(checkpointStorageAccess);
+
+            this.subtaskCheckpointCoordinator =
+                    new SubtaskCheckpointCoordinatorImpl(
+                            checkpointStorageAccess,
+                            getName(),
+                            actionExecutor,
+                            getCancelables(),
+                            getAsyncOperationsThreadPool(),
+                            environment,
+                            this,
+                            configuration.isUnalignedCheckpointsEnabled(),
+                            configuration
+                                    .getConfiguration()
+                                    .get(
+                                            ExecutionCheckpointingOptions
+                                                    .ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH),
+                            this::prepareInputSnapshot);
+
+            // if the clock is not already set, then assign a default TimeServiceProvider
+            if (timerService == null) {
+                this.timerService = createTimerService("Time Trigger for " + getName());
+            } else {
+                this.timerService = timerService;
+            }
 
-        this.systemTimerService = createTimerService("System Time Trigger for " + getName());
-        this.channelIOExecutor =
-                Executors.newSingleThreadExecutor(
-                        new ExecutorThreadFactory("channel-state-unspilling"));
+            this.systemTimerService = createTimerService("System Time Trigger for " + getName());
+            // Register to stop all timers and threads. Should be closed first.
+            resourceCloser.registerCloseable(this::tryShutdownTimerService);
 
-        injectChannelStateWriterIntoChannels();
+            injectChannelStateWriterIntoChannels();
 
-        environment.getMetricGroup().getIOMetricGroup().setEnableBusyTime(true);
-        Configuration taskManagerConf = environment.getTaskManagerInfo().getConfiguration();
+            environment.getMetricGroup().getIOMetricGroup().setEnableBusyTime(true);
+            Configuration taskManagerConf = environment.getTaskManagerInfo().getConfiguration();
 
-        this.bufferDebloatPeriod = taskManagerConf.get(BUFFER_DEBLOAT_PERIOD).toMillis();
+            this.bufferDebloatPeriod = taskManagerConf.get(BUFFER_DEBLOAT_PERIOD).toMillis();
+        } catch (Exception ex) {
+            try {
+                resourceCloser.close();

Review comment:
       Why is it different than in lines `948-952`? 

##########
File path: flink-core/src/main/java/org/apache/flink/util/AbstractAutoCloseableRegistry.java
##########
@@ -43,33 +43,36 @@
  * @param <T> Type for potential meta data associated with the registering closeables
  */
 @Internal
-public abstract class AbstractCloseableRegistry<C extends Closeable, T> implements Closeable {
+public abstract class AbstractAutoCloseableRegistry<
+                R extends AutoCloseable, C extends R, T, E extends Exception>
+        implements AutoCloseable {
 
     /** Lock that guards state of this registry. * */
     private final Object lock;
 
     /** Map from tracked Closeables to some associated meta data. */
     @GuardedBy("lock")
-    protected final Map<Closeable, T> closeableToRef;
+    protected final Map<R, T> closeableToRef;
 
     /** Indicates if this registry is closed. */
     @GuardedBy("lock")
     private boolean closed;
 
-    public AbstractCloseableRegistry(@Nonnull Map<Closeable, T> closeableToRef) {
+    public AbstractAutoCloseableRegistry(@Nonnull Map<R, T> closeableToRef) {
         this.lock = new Object();
         this.closeableToRef = Preconditions.checkNotNull(closeableToRef);
         this.closed = false;
     }
 
     /**
-     * Registers a {@link Closeable} with the registry. In case the registry is already closed, this
-     * method throws an {@link IllegalStateException} and closes the passed {@link Closeable}.
+     * Registers a {@link AutoCloseable} with the registry. In case the registry is already closed,
+     * this method throws an {@link IllegalStateException} and closes the passed {@link
+     * AutoCloseable}.
      *
-     * @param closeable Closeable tor register
-     * @throws IOException exception when the registry was closed before
+     * @param closeable Closeable to register.
+     * @throws IOException exception when the registry was closed before.
      */
-    public final void registerCloseable(C closeable) throws IOException {
+    public void registerCloseable(C closeable) throws IOException {

Review comment:
       Why did you drop the `final` 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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] akalash commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
akalash commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r755067161



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
##########
@@ -366,70 +368,100 @@ protected StreamTask(
             StreamTaskActionExecutor actionExecutor,
             TaskMailbox mailbox)
             throws Exception {
-        this.environment = environment;
-        this.configuration = new StreamConfig(environment.getTaskConfiguration());
-        this.recordWriter = createRecordWriterDelegate(configuration, environment);
-        this.actionExecutor = Preconditions.checkNotNull(actionExecutor);
-        this.mailboxProcessor = new MailboxProcessor(this::processInput, mailbox, actionExecutor);
-        this.mainMailboxExecutor = mailboxProcessor.getMainMailboxExecutor();
-        this.asyncExceptionHandler = new StreamTaskAsyncExceptionHandler(environment);
-        this.asyncOperationsThreadPool =
-                Executors.newCachedThreadPool(
-                        new ExecutorThreadFactory("AsyncOperations", uncaughtExceptionHandler));
-
-        environment.setMainMailboxExecutor(mainMailboxExecutor);
-        environment.setAsyncOperationsThreadPool(asyncOperationsThreadPool);
-
-        this.stateBackend = createStateBackend();
-        this.checkpointStorage = createCheckpointStorage(stateBackend);
-        this.changelogWriterAvailabilityProvider =
-                environment.getTaskStateManager().getStateChangelogStorage() == null
-                        ? null
-                        : environment
-                                .getTaskStateManager()
-                                .getStateChangelogStorage()
-                                .getAvailabilityProvider();
-
-        CheckpointStorageAccess checkpointStorageAccess =
-                checkpointStorage.createCheckpointStorage(getEnvironment().getJobID());
-
-        environment.setCheckpointStorageAccess(checkpointStorageAccess);
-
-        this.subtaskCheckpointCoordinator =
-                new SubtaskCheckpointCoordinatorImpl(
-                        checkpointStorageAccess,
-                        getName(),
-                        actionExecutor,
-                        getCancelables(),
-                        getAsyncOperationsThreadPool(),
-                        environment,
-                        this,
-                        configuration.isUnalignedCheckpointsEnabled(),
-                        configuration
-                                .getConfiguration()
-                                .get(
-                                        ExecutionCheckpointingOptions
-                                                .ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH),
-                        this::prepareInputSnapshot);
-
-        // if the clock is not already set, then assign a default TimeServiceProvider
-        if (timerService == null) {
-            this.timerService = createTimerService("Time Trigger for " + getName());
-        } else {
-            this.timerService = timerService;
-        }
+        // The registration of all closeable resources. The order of registration is important.
+        resourceCloser = new AutoCloseableRegistry();
+        try {
+            this.environment = environment;
+            this.configuration = new StreamConfig(environment.getTaskConfiguration());
+            this.mailboxProcessor =
+                    new MailboxProcessor(this::processInput, mailbox, actionExecutor);
+            // Should be closed last.
+            resourceCloser.registerCloseable(mailboxProcessor);
+
+            this.channelIOExecutor =
+                    Executors.newSingleThreadExecutor(
+                            new ExecutorThreadFactory("channel-state-unspilling"));
+            resourceCloser.registerCloseable(channelIOExecutor::shutdown);
+
+            this.recordWriter = createRecordWriterDelegate(configuration, environment);
+            // Release the output resources. this method should never fail.
+            resourceCloser.registerCloseable(this::releaseOutputResources);
+            // If the operators won't be closed explicitly, register it to a hard close.
+            resourceCloser.registerCloseable(this::closeAllOperators);
+            resourceCloser.registerCloseable(this::cleanUpInternal);
+
+            this.actionExecutor = Preconditions.checkNotNull(actionExecutor);
+            this.mainMailboxExecutor = mailboxProcessor.getMainMailboxExecutor();
+            this.asyncExceptionHandler = new StreamTaskAsyncExceptionHandler(environment);
+
+            this.asyncOperationsThreadPool =
+                    Executors.newCachedThreadPool(
+                            new ExecutorThreadFactory("AsyncOperations", uncaughtExceptionHandler));
+
+            // Register all asynchronous checkpoint threads.
+            resourceCloser.registerCloseable(this::shutdownAsyncThreads);
+            resourceCloser.registerCloseable(cancelables);
+
+            environment.setMainMailboxExecutor(mainMailboxExecutor);
+            environment.setAsyncOperationsThreadPool(asyncOperationsThreadPool);
+
+            this.stateBackend = createStateBackend();
+            this.checkpointStorage = createCheckpointStorage(stateBackend);
+            this.changelogWriterAvailabilityProvider =
+                    environment.getTaskStateManager().getStateChangelogStorage() == null
+                            ? null
+                            : environment
+                                    .getTaskStateManager()
+                                    .getStateChangelogStorage()
+                                    .getAvailabilityProvider();
+
+            CheckpointStorageAccess checkpointStorageAccess =
+                    checkpointStorage.createCheckpointStorage(getEnvironment().getJobID());
+
+            environment.setCheckpointStorageAccess(checkpointStorageAccess);
+
+            this.subtaskCheckpointCoordinator =
+                    new SubtaskCheckpointCoordinatorImpl(
+                            checkpointStorageAccess,
+                            getName(),
+                            actionExecutor,
+                            getCancelables(),
+                            getAsyncOperationsThreadPool(),
+                            environment,
+                            this,
+                            configuration.isUnalignedCheckpointsEnabled(),
+                            configuration
+                                    .getConfiguration()
+                                    .get(
+                                            ExecutionCheckpointingOptions
+                                                    .ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH),
+                            this::prepareInputSnapshot);
+
+            // if the clock is not already set, then assign a default TimeServiceProvider
+            if (timerService == null) {
+                this.timerService = createTimerService("Time Trigger for " + getName());
+            } else {
+                this.timerService = timerService;
+            }
 
-        this.systemTimerService = createTimerService("System Time Trigger for " + getName());
-        this.channelIOExecutor =
-                Executors.newSingleThreadExecutor(
-                        new ExecutorThreadFactory("channel-state-unspilling"));
+            this.systemTimerService = createTimerService("System Time Trigger for " + getName());
+            // Register to stop all timers and threads. Should be closed first.
+            resourceCloser.registerCloseable(this::tryShutdownTimerService);
 
-        injectChannelStateWriterIntoChannels();
+            injectChannelStateWriterIntoChannels();
 
-        environment.getMetricGroup().getIOMetricGroup().setEnableBusyTime(true);
-        Configuration taskManagerConf = environment.getTaskManagerInfo().getConfiguration();
+            environment.getMetricGroup().getIOMetricGroup().setEnableBusyTime(true);
+            Configuration taskManagerConf = environment.getTaskManagerInfo().getConfiguration();
 
-        this.bufferDebloatPeriod = taskManagerConf.get(BUFFER_DEBLOAT_PERIOD).toMillis();
+            this.bufferDebloatPeriod = taskManagerConf.get(BUFFER_DEBLOAT_PERIOD).toMillis();
+        } catch (Exception ex) {
+            try {
+                resourceCloser.close();

Review comment:
       It is actually pretty the same. One difference is that  `948-952` should convert `Throwable` to `Exception` in order to keep the old behavior while we can leave `Throwable` untouched in the constructor because it is always will be suppressed. We also don't need to follow any old contract here because it is the new exception.




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] akalash commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
akalash commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r756209355



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
##########
@@ -366,70 +368,100 @@ protected StreamTask(
             StreamTaskActionExecutor actionExecutor,
             TaskMailbox mailbox)
             throws Exception {
-        this.environment = environment;
-        this.configuration = new StreamConfig(environment.getTaskConfiguration());
-        this.recordWriter = createRecordWriterDelegate(configuration, environment);
-        this.actionExecutor = Preconditions.checkNotNull(actionExecutor);
-        this.mailboxProcessor = new MailboxProcessor(this::processInput, mailbox, actionExecutor);
-        this.mainMailboxExecutor = mailboxProcessor.getMainMailboxExecutor();
-        this.asyncExceptionHandler = new StreamTaskAsyncExceptionHandler(environment);
-        this.asyncOperationsThreadPool =
-                Executors.newCachedThreadPool(
-                        new ExecutorThreadFactory("AsyncOperations", uncaughtExceptionHandler));
-
-        environment.setMainMailboxExecutor(mainMailboxExecutor);
-        environment.setAsyncOperationsThreadPool(asyncOperationsThreadPool);
-
-        this.stateBackend = createStateBackend();
-        this.checkpointStorage = createCheckpointStorage(stateBackend);
-        this.changelogWriterAvailabilityProvider =
-                environment.getTaskStateManager().getStateChangelogStorage() == null
-                        ? null
-                        : environment
-                                .getTaskStateManager()
-                                .getStateChangelogStorage()
-                                .getAvailabilityProvider();
-
-        CheckpointStorageAccess checkpointStorageAccess =
-                checkpointStorage.createCheckpointStorage(getEnvironment().getJobID());
-
-        environment.setCheckpointStorageAccess(checkpointStorageAccess);
-
-        this.subtaskCheckpointCoordinator =
-                new SubtaskCheckpointCoordinatorImpl(
-                        checkpointStorageAccess,
-                        getName(),
-                        actionExecutor,
-                        getCancelables(),
-                        getAsyncOperationsThreadPool(),
-                        environment,
-                        this,
-                        configuration.isUnalignedCheckpointsEnabled(),
-                        configuration
-                                .getConfiguration()
-                                .get(
-                                        ExecutionCheckpointingOptions
-                                                .ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH),
-                        this::prepareInputSnapshot);
-
-        // if the clock is not already set, then assign a default TimeServiceProvider
-        if (timerService == null) {
-            this.timerService = createTimerService("Time Trigger for " + getName());
-        } else {
-            this.timerService = timerService;
-        }
+        // The registration of all closeable resources. The order of registration is important.
+        resourceCloser = new AutoCloseableRegistry();
+        try {
+            this.environment = environment;
+            this.configuration = new StreamConfig(environment.getTaskConfiguration());
+            this.mailboxProcessor =
+                    new MailboxProcessor(this::processInput, mailbox, actionExecutor);
+            // Should be closed last.
+            resourceCloser.registerCloseable(mailboxProcessor);
+
+            this.channelIOExecutor =
+                    Executors.newSingleThreadExecutor(
+                            new ExecutorThreadFactory("channel-state-unspilling"));
+            resourceCloser.registerCloseable(channelIOExecutor::shutdown);
+
+            this.recordWriter = createRecordWriterDelegate(configuration, environment);
+            // Release the output resources. this method should never fail.
+            resourceCloser.registerCloseable(this::releaseOutputResources);
+            // If the operators won't be closed explicitly, register it to a hard close.
+            resourceCloser.registerCloseable(this::closeAllOperators);
+            resourceCloser.registerCloseable(this::cleanUpInternal);
+
+            this.actionExecutor = Preconditions.checkNotNull(actionExecutor);
+            this.mainMailboxExecutor = mailboxProcessor.getMainMailboxExecutor();
+            this.asyncExceptionHandler = new StreamTaskAsyncExceptionHandler(environment);
+
+            this.asyncOperationsThreadPool =
+                    Executors.newCachedThreadPool(
+                            new ExecutorThreadFactory("AsyncOperations", uncaughtExceptionHandler));
+
+            // Register all asynchronous checkpoint threads.
+            resourceCloser.registerCloseable(this::shutdownAsyncThreads);
+            resourceCloser.registerCloseable(cancelables);
+
+            environment.setMainMailboxExecutor(mainMailboxExecutor);
+            environment.setAsyncOperationsThreadPool(asyncOperationsThreadPool);
+
+            this.stateBackend = createStateBackend();
+            this.checkpointStorage = createCheckpointStorage(stateBackend);
+            this.changelogWriterAvailabilityProvider =
+                    environment.getTaskStateManager().getStateChangelogStorage() == null
+                            ? null
+                            : environment
+                                    .getTaskStateManager()
+                                    .getStateChangelogStorage()
+                                    .getAvailabilityProvider();
+
+            CheckpointStorageAccess checkpointStorageAccess =
+                    checkpointStorage.createCheckpointStorage(getEnvironment().getJobID());
+
+            environment.setCheckpointStorageAccess(checkpointStorageAccess);
+
+            this.subtaskCheckpointCoordinator =
+                    new SubtaskCheckpointCoordinatorImpl(
+                            checkpointStorageAccess,
+                            getName(),
+                            actionExecutor,
+                            getCancelables(),
+                            getAsyncOperationsThreadPool(),
+                            environment,
+                            this,
+                            configuration.isUnalignedCheckpointsEnabled(),
+                            configuration
+                                    .getConfiguration()
+                                    .get(
+                                            ExecutionCheckpointingOptions
+                                                    .ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH),
+                            this::prepareInputSnapshot);
+
+            // if the clock is not already set, then assign a default TimeServiceProvider
+            if (timerService == null) {
+                this.timerService = createTimerService("Time Trigger for " + getName());
+            } else {
+                this.timerService = timerService;
+            }
 
-        this.systemTimerService = createTimerService("System Time Trigger for " + getName());
-        this.channelIOExecutor =
-                Executors.newSingleThreadExecutor(
-                        new ExecutorThreadFactory("channel-state-unspilling"));
+            this.systemTimerService = createTimerService("System Time Trigger for " + getName());
+            // Register to stop all timers and threads. Should be closed first.
+            resourceCloser.registerCloseable(this::tryShutdownTimerService);
 
-        injectChannelStateWriterIntoChannels();
+            injectChannelStateWriterIntoChannels();
 
-        environment.getMetricGroup().getIOMetricGroup().setEnableBusyTime(true);
-        Configuration taskManagerConf = environment.getTaskManagerInfo().getConfiguration();
+            environment.getMetricGroup().getIOMetricGroup().setEnableBusyTime(true);
+            Configuration taskManagerConf = environment.getTaskManagerInfo().getConfiguration();
 
-        this.bufferDebloatPeriod = taskManagerConf.get(BUFFER_DEBLOAT_PERIOD).toMillis();
+            this.bufferDebloatPeriod = taskManagerConf.get(BUFFER_DEBLOAT_PERIOD).toMillis();
+        } catch (Exception ex) {
+            try {
+                resourceCloser.close();

Review comment:
       I can not remove that catch block because I should rethrow the original exception `ex` from the constructor. But if I remove the catch block around `resourceCloser`, the exception from `close` will be thrown that is not so important as the original one.




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] dawidwys commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
dawidwys commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r756213057



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/tasks/StreamTask.java
##########
@@ -366,70 +368,100 @@ protected StreamTask(
             StreamTaskActionExecutor actionExecutor,
             TaskMailbox mailbox)
             throws Exception {
-        this.environment = environment;
-        this.configuration = new StreamConfig(environment.getTaskConfiguration());
-        this.recordWriter = createRecordWriterDelegate(configuration, environment);
-        this.actionExecutor = Preconditions.checkNotNull(actionExecutor);
-        this.mailboxProcessor = new MailboxProcessor(this::processInput, mailbox, actionExecutor);
-        this.mainMailboxExecutor = mailboxProcessor.getMainMailboxExecutor();
-        this.asyncExceptionHandler = new StreamTaskAsyncExceptionHandler(environment);
-        this.asyncOperationsThreadPool =
-                Executors.newCachedThreadPool(
-                        new ExecutorThreadFactory("AsyncOperations", uncaughtExceptionHandler));
-
-        environment.setMainMailboxExecutor(mainMailboxExecutor);
-        environment.setAsyncOperationsThreadPool(asyncOperationsThreadPool);
-
-        this.stateBackend = createStateBackend();
-        this.checkpointStorage = createCheckpointStorage(stateBackend);
-        this.changelogWriterAvailabilityProvider =
-                environment.getTaskStateManager().getStateChangelogStorage() == null
-                        ? null
-                        : environment
-                                .getTaskStateManager()
-                                .getStateChangelogStorage()
-                                .getAvailabilityProvider();
-
-        CheckpointStorageAccess checkpointStorageAccess =
-                checkpointStorage.createCheckpointStorage(getEnvironment().getJobID());
-
-        environment.setCheckpointStorageAccess(checkpointStorageAccess);
-
-        this.subtaskCheckpointCoordinator =
-                new SubtaskCheckpointCoordinatorImpl(
-                        checkpointStorageAccess,
-                        getName(),
-                        actionExecutor,
-                        getCancelables(),
-                        getAsyncOperationsThreadPool(),
-                        environment,
-                        this,
-                        configuration.isUnalignedCheckpointsEnabled(),
-                        configuration
-                                .getConfiguration()
-                                .get(
-                                        ExecutionCheckpointingOptions
-                                                .ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH),
-                        this::prepareInputSnapshot);
-
-        // if the clock is not already set, then assign a default TimeServiceProvider
-        if (timerService == null) {
-            this.timerService = createTimerService("Time Trigger for " + getName());
-        } else {
-            this.timerService = timerService;
-        }
+        // The registration of all closeable resources. The order of registration is important.
+        resourceCloser = new AutoCloseableRegistry();
+        try {
+            this.environment = environment;
+            this.configuration = new StreamConfig(environment.getTaskConfiguration());
+            this.mailboxProcessor =
+                    new MailboxProcessor(this::processInput, mailbox, actionExecutor);
+            // Should be closed last.
+            resourceCloser.registerCloseable(mailboxProcessor);
+
+            this.channelIOExecutor =
+                    Executors.newSingleThreadExecutor(
+                            new ExecutorThreadFactory("channel-state-unspilling"));
+            resourceCloser.registerCloseable(channelIOExecutor::shutdown);
+
+            this.recordWriter = createRecordWriterDelegate(configuration, environment);
+            // Release the output resources. this method should never fail.
+            resourceCloser.registerCloseable(this::releaseOutputResources);
+            // If the operators won't be closed explicitly, register it to a hard close.
+            resourceCloser.registerCloseable(this::closeAllOperators);
+            resourceCloser.registerCloseable(this::cleanUpInternal);
+
+            this.actionExecutor = Preconditions.checkNotNull(actionExecutor);
+            this.mainMailboxExecutor = mailboxProcessor.getMainMailboxExecutor();
+            this.asyncExceptionHandler = new StreamTaskAsyncExceptionHandler(environment);
+
+            this.asyncOperationsThreadPool =
+                    Executors.newCachedThreadPool(
+                            new ExecutorThreadFactory("AsyncOperations", uncaughtExceptionHandler));
+
+            // Register all asynchronous checkpoint threads.
+            resourceCloser.registerCloseable(this::shutdownAsyncThreads);
+            resourceCloser.registerCloseable(cancelables);
+
+            environment.setMainMailboxExecutor(mainMailboxExecutor);
+            environment.setAsyncOperationsThreadPool(asyncOperationsThreadPool);
+
+            this.stateBackend = createStateBackend();
+            this.checkpointStorage = createCheckpointStorage(stateBackend);
+            this.changelogWriterAvailabilityProvider =
+                    environment.getTaskStateManager().getStateChangelogStorage() == null
+                            ? null
+                            : environment
+                                    .getTaskStateManager()
+                                    .getStateChangelogStorage()
+                                    .getAvailabilityProvider();
+
+            CheckpointStorageAccess checkpointStorageAccess =
+                    checkpointStorage.createCheckpointStorage(getEnvironment().getJobID());
+
+            environment.setCheckpointStorageAccess(checkpointStorageAccess);
+
+            this.subtaskCheckpointCoordinator =
+                    new SubtaskCheckpointCoordinatorImpl(
+                            checkpointStorageAccess,
+                            getName(),
+                            actionExecutor,
+                            getCancelables(),
+                            getAsyncOperationsThreadPool(),
+                            environment,
+                            this,
+                            configuration.isUnalignedCheckpointsEnabled(),
+                            configuration
+                                    .getConfiguration()
+                                    .get(
+                                            ExecutionCheckpointingOptions
+                                                    .ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH),
+                            this::prepareInputSnapshot);
+
+            // if the clock is not already set, then assign a default TimeServiceProvider
+            if (timerService == null) {
+                this.timerService = createTimerService("Time Trigger for " + getName());
+            } else {
+                this.timerService = timerService;
+            }
 
-        this.systemTimerService = createTimerService("System Time Trigger for " + getName());
-        this.channelIOExecutor =
-                Executors.newSingleThreadExecutor(
-                        new ExecutorThreadFactory("channel-state-unspilling"));
+            this.systemTimerService = createTimerService("System Time Trigger for " + getName());
+            // Register to stop all timers and threads. Should be closed first.
+            resourceCloser.registerCloseable(this::tryShutdownTimerService);
 
-        injectChannelStateWriterIntoChannels();
+            injectChannelStateWriterIntoChannels();
 
-        environment.getMetricGroup().getIOMetricGroup().setEnableBusyTime(true);
-        Configuration taskManagerConf = environment.getTaskManagerInfo().getConfiguration();
+            environment.getMetricGroup().getIOMetricGroup().setEnableBusyTime(true);
+            Configuration taskManagerConf = environment.getTaskManagerInfo().getConfiguration();
 
-        this.bufferDebloatPeriod = taskManagerConf.get(BUFFER_DEBLOAT_PERIOD).toMillis();
+            this.bufferDebloatPeriod = taskManagerConf.get(BUFFER_DEBLOAT_PERIOD).toMillis();
+        } catch (Exception ex) {
+            try {
+                resourceCloser.close();

Review comment:
       Right, sorry, my comment was too rushed. :sweat_smile: 




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot commented on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009686






-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot commented on pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #17701:
URL: https://github.com/apache/flink/pull/17701#issuecomment-962009959


   Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community
   to review your pull request. We will use this comment to track the progress of the review.
   
   
   ## Automated Checks
   Last check on commit a3b7be98fcdef1f8b6596fecf0c2e21442ae99c7 (Fri Nov 05 15:52:44 UTC 2021)
   
   **Warnings:**
    * No documentation files were touched! Remember to keep the Flink docs up to date!
   
   
   <sub>Mention the bot in a comment to re-run the automated checks.</sub>
   ## Review Progress
   
   * ❓ 1. The [description] looks good.
   * ❓ 2. There is [consensus] that the contribution should go into to Flink.
   * ❓ 3. Needs [attention] from.
   * ❓ 4. The change fits into the overall [architecture].
   * ❓ 5. Overall code [quality] is good.
   
   Please see the [Pull Request Review Guide](https://flink.apache.org/contributing/reviewing-prs.html) for a full explanation of the review process.<details>
    The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot approve description` to approve one or more aspects (aspects: `description`, `consensus`, `architecture` and `quality`)
    - `@flinkbot approve all` to approve all aspects
    - `@flinkbot approve-until architecture` to approve everything until `architecture`
    - `@flinkbot attention @username1 [@username2 ..]` to require somebody's attention
    - `@flinkbot disapprove architecture` to remove an approval you gave earlier
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] dawidwys commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
dawidwys commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r750198273



##########
File path: flink-core/src/main/java/org/apache/flink/util/AbstractCloseableRegistry.java
##########
@@ -43,145 +39,15 @@
  * @param <T> Type for potential meta data associated with the registering closeables
  */
 @Internal
-public abstract class AbstractCloseableRegistry<C extends Closeable, T> implements Closeable {
-
-    /** Lock that guards state of this registry. * */
-    private final Object lock;
-
-    /** Map from tracked Closeables to some associated meta data. */
-    @GuardedBy("lock")
-    protected final Map<Closeable, T> closeableToRef;
-
-    /** Indicates if this registry is closed. */
-    @GuardedBy("lock")
-    private boolean closed;
+public abstract class AbstractCloseableRegistry<C extends Closeable, T>
+        extends AbstractAutoCloseableRegistry<Closeable, C, T> implements Closeable {
 
     public AbstractCloseableRegistry(@Nonnull Map<Closeable, T> closeableToRef) {
-        this.lock = new Object();
-        this.closeableToRef = Preconditions.checkNotNull(closeableToRef);
-        this.closed = false;
-    }
-
-    /**
-     * Registers a {@link Closeable} with the registry. In case the registry is already closed, this
-     * method throws an {@link IllegalStateException} and closes the passed {@link Closeable}.
-     *
-     * @param closeable Closeable tor register
-     * @throws IOException exception when the registry was closed before
-     */
-    public final void registerCloseable(C closeable) throws IOException {
-
-        if (null == closeable) {
-            return;
-        }
-
-        synchronized (getSynchronizationLock()) {
-            if (!closed) {
-                doRegister(closeable, closeableToRef);
-                return;
-            }
-        }
-
-        IOUtils.closeQuietly(closeable);
-        throw new IOException(
-                "Cannot register Closeable, registry is already closed. Closing argument.");
-    }
-
-    /**
-     * Removes a {@link Closeable} from the registry.
-     *
-     * @param closeable instance to remove from the registry.
-     * @return true if the closeable was previously registered and became unregistered through this
-     *     call.
-     */
-    public final boolean unregisterCloseable(C closeable) {
-
-        if (null == closeable) {
-            return false;
-        }
-
-        synchronized (getSynchronizationLock()) {
-            return doUnRegister(closeable, closeableToRef);
-        }
+        super(closeableToRef, true);
     }
 
     @Override
     public void close() throws IOException {
-        Collection<Closeable> toCloseCopy;
-
-        synchronized (getSynchronizationLock()) {
-            if (closed) {
-                return;
-            }
-
-            closed = true;
-
-            toCloseCopy = getReferencesToClose();
-
-            closeableToRef.clear();
-        }
-
-        IOUtils.closeAllQuietly(toCloseCopy);
-    }
-
-    public boolean isClosed() {
-        synchronized (getSynchronizationLock()) {
-            return closed;
-        }
-    }
-
-    protected Collection<Closeable> getReferencesToClose() {
-        return new ArrayList<>(closeableToRef.keySet());
-    }
-
-    /**
-     * Does the actual registration of the closeable with the registry map. This should not do any
-     * long running or potentially blocking operations as is is executed under the registry's lock.
-     */
-    protected abstract void doRegister(
-            @Nonnull C closeable, @Nonnull Map<Closeable, T> closeableMap);
-
-    /**
-     * Does the actual un-registration of the closeable from the registry map. This should not do
-     * any long running or potentially blocking operations as is is executed under the registry's
-     * lock.
-     */
-    protected abstract boolean doUnRegister(
-            @Nonnull C closeable, @Nonnull Map<Closeable, T> closeableMap);
-
-    /**
-     * Returns the lock on which manipulations to members closeableToRef and closeable must be
-     * synchronized.
-     */
-    protected final Object getSynchronizationLock() {
-        return lock;
-    }
-
-    /** Adds a mapping to the registry map, respecting locking. */
-    protected final void addCloseableInternal(Closeable closeable, T metaData) {
-        synchronized (getSynchronizationLock()) {
-            closeableToRef.put(closeable, metaData);
-        }
-    }
-
-    /** Removes a mapping from the registry map, respecting locking. */
-    protected final boolean removeCloseableInternal(Closeable closeable) {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.remove(closeable) != null;
-        }
-    }
-
-    @VisibleForTesting
-    public final int getNumberOfRegisteredCloseables() {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.size();
-        }
-    }
-
-    @VisibleForTesting
-    public final boolean isCloseableRegistered(Closeable c) {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.containsKey(c);
-        }
+        IOUtils.closeAllCloseable(prepareToExceptionalClose());

Review comment:
       If you want to still have the `AbstractCloseableRegistry` fine, I am not strong on removing this class. 
   
   However, all other points are much simpler imo.




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] akalash commented on a change in pull request #17701: [FLINK-24294][streaming] Register all stream task resources to one cl…

Posted by GitBox <gi...@apache.org>.
akalash commented on a change in pull request #17701:
URL: https://github.com/apache/flink/pull/17701#discussion_r750192592



##########
File path: flink-core/src/main/java/org/apache/flink/util/AbstractCloseableRegistry.java
##########
@@ -43,145 +39,15 @@
  * @param <T> Type for potential meta data associated with the registering closeables
  */
 @Internal
-public abstract class AbstractCloseableRegistry<C extends Closeable, T> implements Closeable {
-
-    /** Lock that guards state of this registry. * */
-    private final Object lock;
-
-    /** Map from tracked Closeables to some associated meta data. */
-    @GuardedBy("lock")
-    protected final Map<Closeable, T> closeableToRef;
-
-    /** Indicates if this registry is closed. */
-    @GuardedBy("lock")
-    private boolean closed;
+public abstract class AbstractCloseableRegistry<C extends Closeable, T>
+        extends AbstractAutoCloseableRegistry<Closeable, C, T> implements Closeable {
 
     public AbstractCloseableRegistry(@Nonnull Map<Closeable, T> closeableToRef) {
-        this.lock = new Object();
-        this.closeableToRef = Preconditions.checkNotNull(closeableToRef);
-        this.closed = false;
-    }
-
-    /**
-     * Registers a {@link Closeable} with the registry. In case the registry is already closed, this
-     * method throws an {@link IllegalStateException} and closes the passed {@link Closeable}.
-     *
-     * @param closeable Closeable tor register
-     * @throws IOException exception when the registry was closed before
-     */
-    public final void registerCloseable(C closeable) throws IOException {
-
-        if (null == closeable) {
-            return;
-        }
-
-        synchronized (getSynchronizationLock()) {
-            if (!closed) {
-                doRegister(closeable, closeableToRef);
-                return;
-            }
-        }
-
-        IOUtils.closeQuietly(closeable);
-        throw new IOException(
-                "Cannot register Closeable, registry is already closed. Closing argument.");
-    }
-
-    /**
-     * Removes a {@link Closeable} from the registry.
-     *
-     * @param closeable instance to remove from the registry.
-     * @return true if the closeable was previously registered and became unregistered through this
-     *     call.
-     */
-    public final boolean unregisterCloseable(C closeable) {
-
-        if (null == closeable) {
-            return false;
-        }
-
-        synchronized (getSynchronizationLock()) {
-            return doUnRegister(closeable, closeableToRef);
-        }
+        super(closeableToRef, true);
     }
 
     @Override
     public void close() throws IOException {
-        Collection<Closeable> toCloseCopy;
-
-        synchronized (getSynchronizationLock()) {
-            if (closed) {
-                return;
-            }
-
-            closed = true;
-
-            toCloseCopy = getReferencesToClose();
-
-            closeableToRef.clear();
-        }
-
-        IOUtils.closeAllQuietly(toCloseCopy);
-    }
-
-    public boolean isClosed() {
-        synchronized (getSynchronizationLock()) {
-            return closed;
-        }
-    }
-
-    protected Collection<Closeable> getReferencesToClose() {
-        return new ArrayList<>(closeableToRef.keySet());
-    }
-
-    /**
-     * Does the actual registration of the closeable with the registry map. This should not do any
-     * long running or potentially blocking operations as is is executed under the registry's lock.
-     */
-    protected abstract void doRegister(
-            @Nonnull C closeable, @Nonnull Map<Closeable, T> closeableMap);
-
-    /**
-     * Does the actual un-registration of the closeable from the registry map. This should not do
-     * any long running or potentially blocking operations as is is executed under the registry's
-     * lock.
-     */
-    protected abstract boolean doUnRegister(
-            @Nonnull C closeable, @Nonnull Map<Closeable, T> closeableMap);
-
-    /**
-     * Returns the lock on which manipulations to members closeableToRef and closeable must be
-     * synchronized.
-     */
-    protected final Object getSynchronizationLock() {
-        return lock;
-    }
-
-    /** Adds a mapping to the registry map, respecting locking. */
-    protected final void addCloseableInternal(Closeable closeable, T metaData) {
-        synchronized (getSynchronizationLock()) {
-            closeableToRef.put(closeable, metaData);
-        }
-    }
-
-    /** Removes a mapping from the registry map, respecting locking. */
-    protected final boolean removeCloseableInternal(Closeable closeable) {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.remove(closeable) != null;
-        }
-    }
-
-    @VisibleForTesting
-    public final int getNumberOfRegisteredCloseables() {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.size();
-        }
-    }
-
-    @VisibleForTesting
-    public final boolean isCloseableRegistered(Closeable c) {
-        synchronized (getSynchronizationLock()) {
-            return closeableToRef.containsKey(c);
-        }
+        IOUtils.closeAllCloseable(prepareToExceptionalClose());

Review comment:
       If we want to make one implementation with quiet close while another with an exception then it will work but if we want to keep it configurable then it leads to code duplication in `close` method but maybe it is less evil. I will take a look at it close.




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org