Search in sources :

Example 16 with CheckpointStateOutputStream

use of org.apache.flink.runtime.state.CheckpointStateOutputStream in project flink by apache.

the class FsCheckpointStateOutputStreamTest method runTest.

private void runTest(int numBytes, int bufferSize, int threshold, boolean expectFile) throws Exception {
    CheckpointStateOutputStream stream = new FsCheckpointStreamFactory.FsCheckpointStateOutputStream(Path.fromLocalFile(tempDir.newFolder()), FileSystem.getLocalFileSystem(), bufferSize, threshold, relativePaths);
    Random rnd = new Random();
    byte[] original = new byte[numBytes];
    byte[] bytes = new byte[original.length];
    rnd.nextBytes(original);
    System.arraycopy(original, 0, bytes, 0, original.length);
    // the test writes a mixture of writing individual bytes and byte arrays
    int pos = 0;
    while (pos < bytes.length) {
        boolean single = rnd.nextBoolean();
        if (single) {
            stream.write(bytes[pos++]);
        } else {
            int num = rnd.nextBoolean() ? (bytes.length - pos) : rnd.nextInt(bytes.length - pos);
            stream.write(bytes, pos, num);
            pos += num;
        }
    }
    StreamStateHandle handle = stream.closeAndGetHandle();
    if (expectFile) {
        assertTrue(handle instanceof FileStateHandle);
    } else {
        assertTrue(handle instanceof ByteStreamStateHandle);
    }
    // make sure the writing process did not alter the original byte array
    assertArrayEquals(original, bytes);
    try (InputStream inStream = handle.openInputStream()) {
        byte[] validation = new byte[bytes.length];
        DataInputStream dataInputStream = new DataInputStream(inStream);
        dataInputStream.readFully(validation);
        assertArrayEquals(bytes, validation);
    }
    handle.discardState();
}
Also used : FsCheckpointStateOutputStream(org.apache.flink.runtime.state.filesystem.FsCheckpointStreamFactory.FsCheckpointStateOutputStream) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) ByteStreamStateHandle(org.apache.flink.runtime.state.memory.ByteStreamStateHandle) Random(java.util.Random) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) FsCheckpointStateOutputStream(org.apache.flink.runtime.state.filesystem.FsCheckpointStreamFactory.FsCheckpointStateOutputStream) CheckpointStateOutputStream(org.apache.flink.runtime.state.CheckpointStateOutputStream) ByteStreamStateHandle(org.apache.flink.runtime.state.memory.ByteStreamStateHandle) DataInputStream(java.io.DataInputStream)

Example 17 with CheckpointStateOutputStream

use of org.apache.flink.runtime.state.CheckpointStateOutputStream in project flink by apache.

the class FsCheckpointStateOutputStreamTest method testGetPos.

@Test
public void testGetPos() throws Exception {
    CheckpointStateOutputStream stream = new FsCheckpointStreamFactory.FsCheckpointStateOutputStream(Path.fromLocalFile(tempDir.newFolder()), FileSystem.getLocalFileSystem(), 31, 17, relativePaths);
    for (int i = 0; i < 64; ++i) {
        Assert.assertEquals(i, stream.getPos());
        stream.write(0x42);
    }
    stream.closeAndGetHandle();
    // ----------------------------------------------------
    stream = new FsCheckpointStreamFactory.FsCheckpointStateOutputStream(Path.fromLocalFile(tempDir.newFolder()), FileSystem.getLocalFileSystem(), 31, 17, relativePaths);
    byte[] data = "testme!".getBytes(ConfigConstants.DEFAULT_CHARSET);
    for (int i = 0; i < 7; ++i) {
        Assert.assertEquals(i * (1 + data.length), stream.getPos());
        stream.write(0x42);
        stream.write(data);
    }
    stream.closeAndGetHandle();
}
Also used : FsCheckpointStateOutputStream(org.apache.flink.runtime.state.filesystem.FsCheckpointStreamFactory.FsCheckpointStateOutputStream) FsCheckpointStateOutputStream(org.apache.flink.runtime.state.filesystem.FsCheckpointStreamFactory.FsCheckpointStateOutputStream) FsCheckpointStateOutputStream(org.apache.flink.runtime.state.filesystem.FsCheckpointStreamFactory.FsCheckpointStateOutputStream) CheckpointStateOutputStream(org.apache.flink.runtime.state.CheckpointStateOutputStream) Test(org.junit.Test)

Example 18 with CheckpointStateOutputStream

use of org.apache.flink.runtime.state.CheckpointStateOutputStream in project flink by apache.

the class RocksDBAsyncSnapshotTest method testCleanupOfSnapshotsInFailureCase.

/**
 * Test that the snapshot files are cleaned up in case of a failure during the snapshot
 * procedure.
 */
@Test
public void testCleanupOfSnapshotsInFailureCase() throws Exception {
    long checkpointId = 1L;
    long timestamp = 42L;
    MockEnvironment env = MockEnvironment.builder().build();
    final IOException testException = new IOException("Test exception");
    CheckpointStateOutputStream outputStream = spy(new FailingStream(testException));
    RocksDBStateBackend backend = new RocksDBStateBackend((StateBackend) new MemoryStateBackend());
    backend.setDbStoragePath(temporaryFolder.newFolder().toURI().toString());
    AbstractKeyedStateBackend<Void> keyedStateBackend = backend.createKeyedStateBackend(env, new JobID(), "test operator", VoidSerializer.INSTANCE, 1, new KeyGroupRange(0, 0), null, TtlTimeProvider.DEFAULT, new UnregisteredMetricsGroup(), Collections.emptyList(), new CloseableRegistry());
    try {
        // register a state so that the state backend has to checkpoint something
        keyedStateBackend.getPartitionedState("namespace", StringSerializer.INSTANCE, new ValueStateDescriptor<>("foobar", String.class));
        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshotFuture = keyedStateBackend.snapshot(checkpointId, timestamp, new TestCheckpointStreamFactory(() -> outputStream), CheckpointOptions.forCheckpointWithDefaultLocation());
        try {
            FutureUtils.runIfNotDoneAndGet(snapshotFuture);
            fail("Expected an exception to be thrown here.");
        } catch (ExecutionException e) {
            Assert.assertEquals(testException, e.getCause());
        }
        verify(outputStream).close();
    } finally {
        IOUtils.closeQuietly(keyedStateBackend);
        keyedStateBackend.dispose();
        IOUtils.closeQuietly(env);
    }
}
Also used : UnregisteredMetricsGroup(org.apache.flink.metrics.groups.UnregisteredMetricsGroup) SnapshotResult(org.apache.flink.runtime.state.SnapshotResult) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) IOException(java.io.IOException) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) TestCheckpointStreamFactory(org.apache.flink.runtime.state.testutils.TestCheckpointStreamFactory) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) StreamMockEnvironment(org.apache.flink.streaming.runtime.tasks.StreamMockEnvironment) CheckpointStateOutputStream(org.apache.flink.runtime.state.CheckpointStateOutputStream) ExecutionException(java.util.concurrent.ExecutionException) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Aggregations

CheckpointStateOutputStream (org.apache.flink.runtime.state.CheckpointStateOutputStream)18 Test (org.junit.Test)14 StreamStateHandle (org.apache.flink.runtime.state.StreamStateHandle)10 IOException (java.io.IOException)7 FsCheckpointStateOutputStream (org.apache.flink.runtime.state.filesystem.FsCheckpointStreamFactory.FsCheckpointStateOutputStream)7 ObjectOutputStream (java.io.ObjectOutputStream)4 JobID (org.apache.flink.api.common.JobID)4 Path (org.apache.flink.core.fs.Path)4 CheckpointStreamFactory (org.apache.flink.runtime.state.CheckpointStreamFactory)4 KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)4 ByteStreamStateHandle (org.apache.flink.runtime.state.memory.ByteStreamStateHandle)4 ObjectInputStream (java.io.ObjectInputStream)3 HashMap (java.util.HashMap)3 FileSystem (org.apache.flink.core.fs.FileSystem)3 File (java.io.File)2 InputStream (java.io.InputStream)2 List (java.util.List)2 Random (java.util.Random)2 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)2 FSDataOutputStream (org.apache.flink.core.fs.FSDataOutputStream)2