Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle race condition between File.length() and FileChannel.size() #140

Merged
merged 3 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions agrona/src/main/java/org/agrona/MarkFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import org.agrona.concurrent.UnsafeBuffer;

import java.io.File;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;
import java.util.function.Consumer;
import java.util.function.IntConsumer;

Expand Down Expand Up @@ -375,6 +377,8 @@ public static MappedByteBuffer mapExistingMarkFile(
sleep(16);
}

waitForFileChannelContents(markFile, timeoutMs, epochClock, startTimeMs);

final MappedByteBuffer byteBuffer = mapExistingFile(markFile, logger);
final UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer);

Expand Down Expand Up @@ -547,6 +551,30 @@ private static void validateOffsets(final int versionFieldOffset, final int time
}
}

private static void waitForFileChannelContents(
final File markFile,
final long timeoutMs,
final EpochClock epochClock,
final long startTimeMs)
{
try (FileChannel markFileChannel = FileChannel.open(markFile.toPath(), StandardOpenOption.READ))
{
while (markFileChannel.size() < 4)
{
if (epochClock.time() > (startTimeMs + timeoutMs))
{
throw new IllegalStateException("Mark file is created but not populated.");
}

sleep(16);
}
}
catch (final IOException e)
{
throw new IllegalStateException("Cannot open mark file for reading.");
}
}

static void sleep(final long durationMs)
{
try
Expand Down
36 changes: 36 additions & 0 deletions agrona/src/test/java/org/agrona/MarkFileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.agrona;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

import org.agrona.concurrent.SystemEpochClock;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class MarkFileTest
{
@Rule
public TemporaryFolder tmpDir = new TemporaryFolder();

@Test(expected = IllegalStateException.class)
public void shouldWaitForMarkFileToContainEnoughDataForVersionCheck() throws IOException
{
final File directory = tmpDir.newFolder();
final String filename = "markfile.dat";
final Path markFilePath = directory.toPath().resolve(filename);
Files.createFile(markFilePath);

try (FileChannel channel = FileChannel.open(markFilePath, StandardOpenOption.WRITE))
{
channel.write(ByteBuffer.allocate(1));
}
new MarkFile(directory, filename, 0,
16, 10, new SystemEpochClock(), v -> {}, msg -> {});
}
}