Search in sources :

Example 51 with FSDataInputStream

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

the class DuplicatingCheckpointOutputStreamTest method testUnalignedStreamsException.

/**
 * Tests that in case of unaligned stream positions, the secondary stream is closed and the
 * primary still works. This is important because some code may rely on seeking to stream
 * offsets in the created state files and if the streams are not aligned this code could fail.
 */
@Test
public void testUnalignedStreamsException() throws IOException {
    int streamCapacity = 1024 * 1024;
    TestMemoryCheckpointOutputStream primaryStream = new TestMemoryCheckpointOutputStream(streamCapacity);
    TestMemoryCheckpointOutputStream secondaryStream = new TestMemoryCheckpointOutputStream(streamCapacity);
    primaryStream.write(42);
    DuplicatingCheckpointOutputStream stream = new DuplicatingCheckpointOutputStream(primaryStream, secondaryStream);
    Assert.assertNotNull(stream.getSecondaryStreamException());
    Assert.assertTrue(secondaryStream.isClosed());
    stream.write(23);
    try {
        stream.closeAndGetSecondaryHandle();
        Assert.fail();
    } catch (IOException ignore) {
        Assert.assertEquals(ignore.getCause(), stream.getSecondaryStreamException());
    }
    StreamStateHandle primaryHandle = stream.closeAndGetPrimaryHandle();
    try (FSDataInputStream inputStream = primaryHandle.openInputStream()) {
        Assert.assertEquals(42, inputStream.read());
        Assert.assertEquals(23, inputStream.read());
        Assert.assertEquals(-1, inputStream.read());
    }
}
Also used : FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 52 with FSDataInputStream

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

the class ByteStreamStateHandleTest method testStreamSeekOutOfBounds.

@Test
public void testStreamSeekOutOfBounds() throws IOException {
    final int len = 10;
    final ByteStreamStateHandle handle = new ByteStreamStateHandle("name", new byte[len]);
    // check negative offset
    FSDataInputStream in = handle.openInputStream();
    try {
        in.seek(-2);
        fail("should fail with an exception");
    } catch (IOException e) {
    // expected
    }
    // check integer overflow
    in = handle.openInputStream();
    try {
        in.seek(len + 1);
        fail("should fail with an exception");
    } catch (IOException e) {
    // expected
    }
    // check integer overflow
    in = handle.openInputStream();
    try {
        in.seek(((long) Integer.MAX_VALUE) + 100L);
        fail("should fail with an exception");
    } catch (IOException e) {
    // expected
    }
}
Also used : FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 53 with FSDataInputStream

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

the class CheckpointStateOutputStreamTest method testEmptyState.

// ------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------
/**
 * Validates that even empty streams create a file and a file state handle.
 */
@Test
public void testEmptyState() throws Exception {
    final FileSystem fs = FileSystem.getLocalFileSystem();
    final Path folder = baseFolder();
    final String fileName = "myFileName";
    final Path filePath = new Path(folder, fileName);
    final FileStateHandle handle;
    try (FSDataOutputStream stream = createTestStream(fs, folder, fileName)) {
        handle = closeAndGetResult(stream);
    }
    // must have created a handle
    assertNotNull(handle);
    assertEquals(filePath, handle.getFilePath());
    // the pointer path should exist as a directory
    assertTrue(fs.exists(handle.getFilePath()));
    assertFalse(fs.getFileStatus(filePath).isDir());
    // the contents should be empty
    try (FSDataInputStream in = handle.openInputStream()) {
        assertEquals(-1, in.read());
    }
}
Also used : Path(org.apache.flink.core.fs.Path) 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 54 with FSDataInputStream

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

the class ByteStreamStateHandleTest method testBulkReadINdexOutOfBounds.

@SuppressWarnings("ResultOfMethodCallIgnored")
@Test
public void testBulkReadINdexOutOfBounds() throws IOException {
    final ByteStreamStateHandle handle = new ByteStreamStateHandle("name", new byte[10]);
    // check negative offset
    FSDataInputStream in = handle.openInputStream();
    try {
        in.read(new byte[10], -1, 5);
        fail("should fail with an exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // check offset overflow
    in = handle.openInputStream();
    try {
        in.read(new byte[10], 10, 5);
        fail("should fail with an exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // check negative length
    in = handle.openInputStream();
    try {
        in.read(new byte[10], 0, -2);
        fail("should fail with an exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // check length too large
    in = handle.openInputStream();
    try {
        in.read(new byte[10], 5, 6);
        fail("should fail with an exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // check length integer overflow
    in = handle.openInputStream();
    try {
        in.read(new byte[10], 5, Integer.MAX_VALUE);
        fail("should fail with an exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
}
Also used : FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) Test(org.junit.Test)

Example 55 with FSDataInputStream

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

the class KeyedStateCheckpointOutputStreamTest method testReadWriteMissingKeyGroups.

@Test
public void testReadWriteMissingKeyGroups() throws Exception {
    final KeyGroupRange keyRange = new KeyGroupRange(0, 2);
    KeyedStateCheckpointOutputStream stream = createStream(keyRange);
    DataOutputView dov = new DataOutputViewStreamWrapper(stream);
    stream.startNewKeyGroup(1);
    dov.writeInt(1);
    KeyGroupsStateHandle fullHandle = stream.closeAndGetHandle();
    int count = 0;
    try (FSDataInputStream in = fullHandle.openInputStream()) {
        DataInputView div = new DataInputViewStreamWrapper(in);
        for (int kg : fullHandle.getKeyGroupRange()) {
            long off = fullHandle.getOffsetForKeyGroup(kg);
            if (off >= 0) {
                in.seek(off);
                Assert.assertEquals(1, div.readInt());
                ++count;
            }
        }
    }
    Assert.assertEquals(1, count);
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) DataInputView(org.apache.flink.core.memory.DataInputView) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) DataOutputView(org.apache.flink.core.memory.DataOutputView) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Aggregations

FSDataInputStream (org.apache.flink.core.fs.FSDataInputStream)58 Test (org.junit.Test)21 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)14 IOException (java.io.IOException)12 FileSystem (org.apache.flink.core.fs.FileSystem)12 Path (org.apache.flink.core.fs.Path)10 FSDataOutputStream (org.apache.flink.core.fs.FSDataOutputStream)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 FileStatus (org.apache.flink.core.fs.FileStatus)6 KeyGroupsStateHandle (org.apache.flink.runtime.state.KeyGroupsStateHandle)6 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 File (java.io.File)4 DataInputView (org.apache.flink.core.memory.DataInputView)4 StreamStateHandle (org.apache.flink.runtime.state.StreamStateHandle)4 ObjectInputStream (java.io.ObjectInputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)2 ArrayList (java.util.ArrayList)2 LocalFileSystem (org.apache.flink.core.fs.local.LocalFileSystem)2