Search in sources :

Example 11 with CheckpointStateOutputStream

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

the class MemoryCheckpointOutputStreamTest method testStateStream.

@Test
public void testStateStream() throws Exception {
    HashMap<String, Integer> state = new HashMap<>();
    state.put("hey there", 2);
    state.put("the crazy brown fox stumbles over a sentence that does not contain every letter", 77);
    CheckpointStateOutputStream outStream = new MemoryCheckpointOutputStream(MemoryStateBackend.DEFAULT_MAX_STATE_SIZE);
    ObjectOutputStream oos = new ObjectOutputStream(outStream);
    oos.writeObject(state);
    oos.flush();
    StreamStateHandle handle = outStream.closeAndGetHandle();
    assertNotNull(handle);
    try (ObjectInputStream ois = new ObjectInputStream(handle.openInputStream())) {
        assertEquals(state, ois.readObject());
        assertTrue(ois.available() <= 0);
    }
}
Also used : StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) HashMap(java.util.HashMap) MemoryCheckpointOutputStream(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory.MemoryCheckpointOutputStream) CheckpointStateOutputStream(org.apache.flink.runtime.state.CheckpointStateOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 12 with CheckpointStateOutputStream

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

the class MemoryCheckpointStorageAccessTest method testTaskOwnedStateStream.

@Test
public void testTaskOwnedStateStream() throws Exception {
    final List<String> state = Arrays.asList("Flopsy", "Mopsy", "Cotton Tail", "Peter");
    final MemoryBackendCheckpointStorageAccess storage = new MemoryBackendCheckpointStorageAccess(new JobID(), null, null, DEFAULT_MAX_STATE_SIZE);
    StreamStateHandle stateHandle;
    try (CheckpointStateOutputStream stream = storage.createTaskOwnedStateStream()) {
        assertTrue(stream instanceof MemoryCheckpointOutputStream);
        new ObjectOutputStream(stream).writeObject(state);
        stateHandle = stream.closeAndGetHandle();
    }
    try (ObjectInputStream in = new ObjectInputStream(stateHandle.openInputStream())) {
        assertEquals(state, in.readObject());
    }
}
Also used : StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) MemoryCheckpointOutputStream(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory.MemoryCheckpointOutputStream) CheckpointStateOutputStream(org.apache.flink.runtime.state.CheckpointStateOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) JobID(org.apache.flink.api.common.JobID) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 13 with CheckpointStateOutputStream

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

the class FsCheckpointStorageAccessTest method testTaskOwnedStateStream.

@Test
public void testTaskOwnedStateStream() throws Exception {
    final List<String> state = Arrays.asList("Flopsy", "Mopsy", "Cotton Tail", "Peter");
    // we chose a small size threshold here to force the state to disk
    final FsCheckpointStorageAccess storage = new FsCheckpointStorageAccess(Path.fromLocalFile(tmp.newFolder()), null, new JobID(), 10, WRITE_BUFFER_SIZE);
    final StreamStateHandle stateHandle;
    try (CheckpointStateOutputStream stream = storage.createTaskOwnedStateStream()) {
        assertTrue(stream instanceof FsCheckpointStateOutputStream);
        new ObjectOutputStream(stream).writeObject(state);
        stateHandle = stream.closeAndGetHandle();
    }
    // the state must have gone to disk
    FileStateHandle fileStateHandle = (FileStateHandle) stateHandle;
    // check that the state is in the correct directory
    String parentDirName = fileStateHandle.getFilePath().getParent().getName();
    assertEquals(FsCheckpointStorageAccess.CHECKPOINT_TASK_OWNED_STATE_DIR, parentDirName);
    // validate the contents
    try (ObjectInputStream in = new ObjectInputStream(stateHandle.openInputStream())) {
        assertEquals(state, in.readObject());
    }
}
Also used : StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) 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) ObjectOutputStream(java.io.ObjectOutputStream) JobID(org.apache.flink.api.common.JobID) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 14 with CheckpointStateOutputStream

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

the class RocksDBStateUploader method uploadLocalFileToCheckpointFs.

private StreamStateHandle uploadLocalFileToCheckpointFs(Path filePath, CheckpointStreamFactory checkpointStreamFactory, CheckpointedStateScope stateScope, CloseableRegistry closeableRegistry) throws IOException {
    InputStream inputStream = null;
    CheckpointStateOutputStream outputStream = null;
    try {
        final byte[] buffer = new byte[READ_BUFFER_SIZE];
        inputStream = Files.newInputStream(filePath);
        closeableRegistry.registerCloseable(inputStream);
        outputStream = checkpointStreamFactory.createCheckpointStateOutputStream(stateScope);
        closeableRegistry.registerCloseable(outputStream);
        while (true) {
            int numBytes = inputStream.read(buffer);
            if (numBytes == -1) {
                break;
            }
            outputStream.write(buffer, 0, numBytes);
        }
        StreamStateHandle result = null;
        if (closeableRegistry.unregisterCloseable(outputStream)) {
            result = outputStream.closeAndGetHandle();
            outputStream = null;
        }
        return result;
    } finally {
        if (closeableRegistry.unregisterCloseable(inputStream)) {
            IOUtils.closeQuietly(inputStream);
        }
        if (closeableRegistry.unregisterCloseable(outputStream)) {
            IOUtils.closeQuietly(outputStream);
        }
    }
}
Also used : StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) InputStream(java.io.InputStream) CheckpointStateOutputStream(org.apache.flink.runtime.state.CheckpointStateOutputStream)

Example 15 with CheckpointStateOutputStream

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

the class FsCheckpointStateOutputStreamTest method testCleanupWhenClosingStream.

/**
 * Tests that the underlying stream file is deleted upon calling close.
 */
@Test
public void testCleanupWhenClosingStream() throws IOException {
    final FileSystem fs = mock(FileSystem.class);
    final FSDataOutputStream outputStream = mock(FSDataOutputStream.class);
    final ArgumentCaptor<Path> pathCaptor = ArgumentCaptor.forClass(Path.class);
    when(fs.create(pathCaptor.capture(), any(FileSystem.WriteMode.class))).thenReturn(outputStream);
    CheckpointStateOutputStream stream = new FsCheckpointStreamFactory.FsCheckpointStateOutputStream(Path.fromLocalFile(tempDir.newFolder()), fs, 4, 0, relativePaths);
    // this should create the underlying file stream
    stream.write(new byte[] { 1, 2, 3, 4, 5 });
    verify(fs).create(any(Path.class), any(FileSystem.WriteMode.class));
    stream.close();
    verify(fs).delete(eq(pathCaptor.getValue()), anyBoolean());
}
Also used : Path(org.apache.flink.core.fs.Path) FsCheckpointStateOutputStream(org.apache.flink.runtime.state.filesystem.FsCheckpointStreamFactory.FsCheckpointStateOutputStream) FileSystem(org.apache.flink.core.fs.FileSystem) FsCheckpointStateOutputStream(org.apache.flink.runtime.state.filesystem.FsCheckpointStreamFactory.FsCheckpointStateOutputStream) CheckpointStateOutputStream(org.apache.flink.runtime.state.CheckpointStateOutputStream) FSDataOutputStream(org.apache.flink.core.fs.FSDataOutputStream) 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