[IOTDB-1741] Fix double close in CompactionUtils #4186
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
During level compaction, the write stream of target file sometimes is closed unexpectedly.The error log is shown below.
Analysis
By consulting related information, we know that when an OuputStream is closed and then written, there will be a Stream Closed exception. We troubleshoot the problem based on this idea. But in the process of level compaction execution, tasks are executed in a single thread in a static function (as shown in the figure).
Moreover, the Writer of each target file is declared in a function, so this Writer and its corresponding Stream are local variables which cannot be accessed outside this function.
In the end of compaction, the Writer's endFile function wll be called, in which Writer.close will be called once, and all the contents in the Writer buffer will be written to the file at this time. After calling endFile, the close function of Writer was manually called again. Theoretically, the second close does not produce any exceptions, because the buffer has been emptied and the Writer will not write anything to the file. But because we can't find other reasons, we can only attribute the cause of this exception to the two calls of the close function for the time being.