private boolean handleCommitException(Throwable e) {

        log.warn("FileSegment commit exception, filePath={}", this.filePath, e);

        // Get root cause here
        Throwable rootCause = e.getCause() != null ? e.getCause() : e;

        long fileSize = rootCause instanceof TieredStoreException ?
            ((TieredStoreException) rootCause).getPosition() : this.getSize();

        long expectPosition = commitPosition + fileSegmentInputStream.getContentLength();
        if (fileSize == GET_FILE_SIZE_ERROR) {
            log.error("Get file size error after commit, FileName: {}, Commit: {}, Content: {}, Expect: {}, Append: {}",
                this.getPath(), commitPosition, fileSegmentInputStream.getContentLength(), expectPosition, appendPosition);
            return false;
        }

        if (correctPosition(fileSize)) {
            fileSegmentInputStream = null;
            return true;
        } else {
            fileSegmentInputStream.rewind();
            return false;
        }
    }
@Test
    public void handleCommitExceptionTest() {
        MetadataStore metadataStore = new DefaultMetadataStore(storeConfig);
        FileSegmentFactory factory = new FileSegmentFactory(metadataStore, storeConfig);

        {
            FileSegment fileSegment = factory.createCommitLogFileSegment(MessageStoreUtil.toFilePath(mq), baseOffset);
            FileSegment fileSpySegment = Mockito.spy(fileSegment);
            fileSpySegment.append(MessageFormatUtilTest.buildMockedMessageBuffer(), 0L);
            fileSpySegment.append(MessageFormatUtilTest.buildMockedMessageBuffer(), 0L);

            Mockito.when(fileSpySegment.commit0(any(), anyLong(), anyInt(), anyBoolean()))
                .thenReturn(CompletableFuture.supplyAsync(() -> {
                    throw new TieredStoreException(TieredStoreErrorCode.IO_ERROR, "Test");
                }));
            Assert.assertFalse(fileSpySegment.commitAsync().join());
            fileSegment.destroyFile();
        }

        {
            FileSegment fileSegment = factory.createCommitLogFileSegment(MessageStoreUtil.toFilePath(mq), baseOffset);
            FileSegment fileSpySegment = Mockito.spy(fileSegment);
            fileSpySegment.append(MessageFormatUtilTest.buildMockedMessageBuffer(), 0L);
            fileSpySegment.append(MessageFormatUtilTest.buildMockedMessageBuffer(), 0L);

            Mockito.when(fileSpySegment.commit0(any(), anyLong(), anyInt(), anyBoolean()))
                .thenReturn(CompletableFuture.supplyAsync(() -> {
                    long size = MessageFormatUtilTest.buildMockedMessageBuffer().remaining();
                    TieredStoreException exception = new TieredStoreException(TieredStoreErrorCode.IO_ERROR, "Test");
                    exception.setPosition(size * 2L);
                    throw exception;
                }));
            Assert.assertTrue(fileSpySegment.commitAsync().join());
            fileSegment.destroyFile();
        }

        {
            FileSegment fileSegment = factory.createCommitLogFileSegment(MessageStoreUtil.toFilePath(mq), baseOffset);
            FileSegment fileSpySegment = Mockito.spy(fileSegment);
            fileSpySegment.append(MessageFormatUtilTest.buildMockedMessageBuffer(), 0L);
            fileSpySegment.append(MessageFormatUtilTest.buildMockedMessageBuffer(), 0L);

            Mockito.when(fileSpySegment.commit0(any(), anyLong(), anyInt(), anyBoolean()))
                .thenReturn(CompletableFuture.supplyAsync(() -> {
                    throw new RuntimeException("Runtime Error for Test");
                }));
            Mockito.when(fileSpySegment.getSize()).thenReturn(0L);
            Assert.assertFalse(fileSpySegment.commitAsync().join());
        }
    }