Skip to content

Commit

Permalink
Merge #4358
Browse files Browse the repository at this point in the history
4358: Fix possible incomplete snapshot chunk write r=Zelldon a=Zelldon

## Description

As written in #4357 we need to loop on writing otherwise we may end in cases where your snapshot is corrupted because chunks have been written not completely.

<!-- Please explain the changes you made here. -->
Related guide to write to FileChannel http://tutorials.jenkov.com/java-nio/file-channel.html

## Related issues

<!-- Which issues are closed by this PR or are related -->

closes #4357 

#

Co-authored-by: Christopher Zell <zelldon91@googlemail.com>
  • Loading branch information
zeebe-bors[bot] and Zelldon committed Apr 27, 2020
2 parents dda0f3d + 92467d1 commit 3d423d1
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,22 @@ public void write(final ByteBuffer chunkId, final ByteBuffer chunkData) {

try (final var channel =
Files.newByteChannel(path, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
channel.write(chunkData);
final var expectedToWrite = chunkData.remaining();
long actualWrittenBytes = 0L;
while (chunkData.hasRemaining()) {
actualWrittenBytes += channel.write(chunkData);
}

if (actualWrittenBytes != expectedToWrite) {
throw new IllegalStateException(
"Expected to write "
+ expectedToWrite
+ " bytes of the given snapshot chunk with id "
+ chunkId
+ ", but only "
+ actualWrittenBytes
+ " bytes were written");
}
} catch (final FileAlreadyExistsException e) {
LOGGER.debug("Chunk {} of pending snapshot {} already exists at {}", filename, this, path, e);
} catch (final IOException e) {
Expand Down

0 comments on commit 3d423d1

Please sign in to comment.