Search in sources :

Example 46 with FileSystem

use of org.apache.flink.core.fs.FileSystem in project flink by apache.

the class AzureFileSystemBehaviorITCase method testSimpleFileWriteAndRead.

@Test
public void testSimpleFileWriteAndRead() throws Exception {
    // 30 secs
    final long deadline = System.nanoTime() + 30_000_000_000L;
    final String testLine = "Hello Upload!";
    final Path path = new Path(getBasePath() + "/test.txt");
    final FileSystem fs = path.getFileSystem();
    try {
        try (FSDataOutputStream out = fs.create(path, FileSystem.WriteMode.OVERWRITE);
            OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
            writer.write(testLine);
        }
        // just in case, wait for the path to exist
        checkPathEventualExistence(fs, path, true, deadline);
        try (FSDataInputStream in = fs.open(path);
            InputStreamReader ir = new InputStreamReader(in, StandardCharsets.UTF_8);
            BufferedReader reader = new BufferedReader(ir)) {
            String line = reader.readLine();
            assertEquals(testLine, line);
        }
    } finally {
        fs.delete(path, false);
    }
    // now file must be gone
    checkPathEventualExistence(fs, path, false, deadline);
}
Also used : Path(org.apache.flink.core.fs.Path) InputStreamReader(java.io.InputStreamReader) FileSystem(org.apache.flink.core.fs.FileSystem) BufferedReader(java.io.BufferedReader) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) OutputStreamWriter(java.io.OutputStreamWriter) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) Test(org.junit.Test)

Example 47 with FileSystem

use of org.apache.flink.core.fs.FileSystem in project flink by apache.

the class FileCacheDirectoriesTest method testDirectoryDownloaded.

private void testDirectoryDownloaded(DistributedCache.DistributedCacheEntry entry) throws Exception {
    JobID jobID = new JobID();
    ExecutionAttemptID attemptID = new ExecutionAttemptID();
    // copy / create the file
    final String fileName = "test_file";
    Future<Path> copyResult = fileCache.createTmpFile(fileName, entry, jobID, attemptID);
    final Path dstPath = copyResult.get();
    final FileSystem fs = dstPath.getFileSystem();
    final FileStatus fileStatus = fs.getFileStatus(dstPath);
    assertTrue(fileStatus.isDir());
    final Path cacheFile = new Path(dstPath, "cacheFile");
    assertTrue(fs.exists(cacheFile));
    final String actualContent = FileUtils.readFileUtf8(new File(cacheFile.getPath()));
    assertEquals(testFileContent, actualContent);
}
Also used : Path(org.apache.flink.core.fs.Path) ExecutionAttemptID(org.apache.flink.runtime.executiongraph.ExecutionAttemptID) FileStatus(org.apache.flink.core.fs.FileStatus) FileSystem(org.apache.flink.core.fs.FileSystem) File(java.io.File) JobID(org.apache.flink.api.common.JobID)

Example 48 with FileSystem

use of org.apache.flink.core.fs.FileSystem in project flink by apache.

the class CheckpointStateOutputStreamTest method testCleanupWhenFailingCloseAndGetHandle.

/**
 * Tests that the underlying stream file is deleted if the closeAndGetHandle method fails.
 */
@Test
public void testCleanupWhenFailingCloseAndGetHandle() throws IOException {
    final Path folder = new Path(tmp.newFolder().toURI());
    final String fileName = "test_name";
    final Path filePath = new Path(folder, fileName);
    final FileSystem fs = spy(new FsWithoutRecoverableWriter((path) -> new FailingCloseStream(new File(path.getPath()))));
    FSDataOutputStream stream = createTestStream(fs, folder, fileName);
    stream.write(new byte[] { 1, 2, 3, 4, 5 });
    try {
        closeAndGetResult(stream);
        fail("Expected IOException");
    } catch (IOException ignored) {
    // expected exception
    }
    verify(fs).delete(filePath, false);
}
Also used : Path(org.apache.flink.core.fs.Path) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Arrays(java.util.Arrays) LocalFileSystem(org.apache.flink.core.fs.local.LocalFileSystem) RunWith(org.junit.runner.RunWith) Random(java.util.Random) Mockito.spy(org.mockito.Mockito.spy) CheckedThread(org.apache.flink.core.testutils.CheckedThread) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) Path(org.apache.flink.core.fs.Path) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) TestLogger(org.apache.flink.util.TestLogger) FunctionWithException(org.apache.flink.util.function.FunctionWithException) Assert.fail(org.junit.Assert.fail) Parameterized(org.junit.runners.Parameterized) LocalRecoverableWriter(org.apache.flink.core.fs.local.LocalRecoverableWriter) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) UUID(java.util.UUID) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) LocalDataOutputStream(org.apache.flink.core.fs.local.LocalDataOutputStream) EOFException(java.io.EOFException) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) Rule(org.junit.Rule) FileSystem(org.apache.flink.core.fs.FileSystem) Assert.assertFalse(org.junit.Assert.assertFalse) TemporaryFolder(org.junit.rules.TemporaryFolder) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) LocalFileSystem(org.apache.flink.core.fs.local.LocalFileSystem) FileSystem(org.apache.flink.core.fs.FileSystem) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

Example 49 with FileSystem

use of org.apache.flink.core.fs.FileSystem in project flink by apache.

the class CheckpointStateOutputStreamTest method testWriteAndRead.

/**
 * Simple write and read test.
 */
@Test
public void testWriteAndRead() throws Exception {
    final FileSystem fs = FileSystem.getLocalFileSystem();
    final Path folder = baseFolder();
    final String fileName = "fooBarName";
    final Random rnd = new Random();
    final byte[] data = new byte[1694523];
    // write the data (mixed single byte writes and array writes)
    final FileStateHandle handle;
    try (FSDataOutputStream stream = createTestStream(fs, folder, fileName)) {
        for (int i = 0; i < data.length; ) {
            if (rnd.nextBoolean()) {
                stream.write(data[i++]);
            } else {
                int len = rnd.nextInt(Math.min(data.length - i, 32));
                stream.write(data, i, len);
                i += len;
            }
        }
        handle = closeAndGetResult(stream);
    }
    // (1) stream from handle must hold the contents
    try (FSDataInputStream in = handle.openInputStream()) {
        byte[] buffer = new byte[data.length];
        readFully(in, buffer);
        assertArrayEquals(data, buffer);
    }
    // (2) the pointer must point to a file with that contents
    try (FSDataInputStream in = fs.open(handle.getFilePath())) {
        byte[] buffer = new byte[data.length];
        readFully(in, buffer);
        assertArrayEquals(data, buffer);
    }
}
Also used : Path(org.apache.flink.core.fs.Path) Random(java.util.Random) LocalFileSystem(org.apache.flink.core.fs.local.LocalFileSystem) FileSystem(org.apache.flink.core.fs.FileSystem) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) Test(org.junit.Test)

Example 50 with FileSystem

use of org.apache.flink.core.fs.FileSystem in project flink by apache.

the class CheckpointStateOutputStreamTest method testCloseDoesNotLock.

/**
 * This test validates that a close operation can happen even while a 'closeAndGetHandle()' call
 * is in progress.
 *
 * <p>That behavior is essential for fast cancellation (concurrent cleanup).
 */
@Test
public void testCloseDoesNotLock() throws Exception {
    final Path folder = new Path(tmp.newFolder().toURI());
    final String fileName = "this-is-ignored-anyways.file";
    final FileSystem fileSystem = spy(new FsWithoutRecoverableWriter((path) -> new BlockerStream()));
    final FSDataOutputStream checkpointStream = createTestStream(fileSystem, folder, fileName);
    final OneShotLatch sync = new OneShotLatch();
    final CheckedThread thread = new CheckedThread() {

        @Override
        public void go() throws Exception {
            sync.trigger();
            // that call should now block, because it accesses the position
            closeAndGetResult(checkpointStream);
        }
    };
    thread.start();
    sync.await();
    checkpointStream.close();
    // it is not important for this test, important is that the thread does not freeze/lock up
    try {
        thread.sync();
    } catch (IOException ignored) {
    }
}
Also used : Path(org.apache.flink.core.fs.Path) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) Arrays(java.util.Arrays) LocalFileSystem(org.apache.flink.core.fs.local.LocalFileSystem) RunWith(org.junit.runner.RunWith) Random(java.util.Random) Mockito.spy(org.mockito.Mockito.spy) CheckedThread(org.apache.flink.core.testutils.CheckedThread) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) Path(org.apache.flink.core.fs.Path) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) TestLogger(org.apache.flink.util.TestLogger) FunctionWithException(org.apache.flink.util.function.FunctionWithException) Assert.fail(org.junit.Assert.fail) Parameterized(org.junit.runners.Parameterized) LocalRecoverableWriter(org.apache.flink.core.fs.local.LocalRecoverableWriter) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) UUID(java.util.UUID) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) LocalDataOutputStream(org.apache.flink.core.fs.local.LocalDataOutputStream) EOFException(java.io.EOFException) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) Rule(org.junit.Rule) FileSystem(org.apache.flink.core.fs.FileSystem) Assert.assertFalse(org.junit.Assert.assertFalse) TemporaryFolder(org.junit.rules.TemporaryFolder) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) LocalFileSystem(org.apache.flink.core.fs.local.LocalFileSystem) FileSystem(org.apache.flink.core.fs.FileSystem) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) IOException(java.io.IOException) CheckedThread(org.apache.flink.core.testutils.CheckedThread) Test(org.junit.Test)

Aggregations

FileSystem (org.apache.flink.core.fs.FileSystem)102 Path (org.apache.flink.core.fs.Path)80 Test (org.junit.Test)49 IOException (java.io.IOException)28 File (java.io.File)24 FileStatus (org.apache.flink.core.fs.FileStatus)20 FSDataOutputStream (org.apache.flink.core.fs.FSDataOutputStream)18 FSDataInputStream (org.apache.flink.core.fs.FSDataInputStream)14 URI (java.net.URI)13 LocalFileSystem (org.apache.flink.core.fs.local.LocalFileSystem)13 ArrayList (java.util.ArrayList)10 Random (java.util.Random)8 Configuration (org.apache.flink.configuration.Configuration)8 JobID (org.apache.flink.api.common.JobID)7 FileNotFoundException (java.io.FileNotFoundException)5 StreamStateHandle (org.apache.flink.runtime.state.StreamStateHandle)5 InputStream (java.io.InputStream)4 URISyntaxException (java.net.URISyntaxException)4 FileBaseStatistics (org.apache.flink.api.common.io.FileInputFormat.FileBaseStatistics)4 FsCheckpointStateOutputStream (org.apache.flink.runtime.state.filesystem.FsCheckpointStreamFactory.FsCheckpointStateOutputStream)4