Search in sources :

Example 11 with MemCheckpointStreamFactory

use of org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory in project flink by apache.

the class OperatorStateBackendTest method testSnapshotBroadcastStateWithEmptyOperatorState.

@Test
public void testSnapshotBroadcastStateWithEmptyOperatorState() throws Exception {
    final AbstractStateBackend abstractStateBackend = new MemoryStateBackend(4096);
    OperatorStateBackend operatorStateBackend = abstractStateBackend.createOperatorStateBackend(createMockEnvironment(), "testOperator", emptyStateHandles, new CloseableRegistry());
    final MapStateDescriptor<Integer, Integer> broadcastStateDesc = new MapStateDescriptor<>("test-broadcast", BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.INT_TYPE_INFO);
    final Map<Integer, Integer> expected = new HashMap<>(3);
    expected.put(1, 2);
    expected.put(3, 4);
    expected.put(5, 6);
    final BroadcastState<Integer, Integer> broadcastState = operatorStateBackend.getBroadcastState(broadcastStateDesc);
    broadcastState.putAll(expected);
    final CheckpointStreamFactory streamFactory = new MemCheckpointStreamFactory(4096);
    OperatorStateHandle stateHandle = null;
    try {
        RunnableFuture<SnapshotResult<OperatorStateHandle>> snapshot = operatorStateBackend.snapshot(0L, 0L, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation());
        SnapshotResult<OperatorStateHandle> snapshotResult = FutureUtils.runIfNotDoneAndGet(snapshot);
        stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
        assertNotNull(stateHandle);
        final Map<Integer, Integer> retrieved = new HashMap<>();
        operatorStateBackend = recreateOperatorStateBackend(operatorStateBackend, abstractStateBackend, StateObjectCollection.singleton(stateHandle));
        BroadcastState<Integer, Integer> retrievedState = operatorStateBackend.getBroadcastState(broadcastStateDesc);
        for (Map.Entry<Integer, Integer> e : retrievedState.entries()) {
            retrieved.put(e.getKey(), e.getValue());
        }
        assertEquals(expected, retrieved);
        // remove an element from both expected and stored state.
        retrievedState.remove(1);
        expected.remove(1);
        snapshot = operatorStateBackend.snapshot(1L, 1L, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation());
        snapshotResult = FutureUtils.runIfNotDoneAndGet(snapshot);
        stateHandle.discardState();
        stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
        retrieved.clear();
        operatorStateBackend = recreateOperatorStateBackend(operatorStateBackend, abstractStateBackend, StateObjectCollection.singleton(stateHandle));
        retrievedState = operatorStateBackend.getBroadcastState(broadcastStateDesc);
        for (Map.Entry<Integer, Integer> e : retrievedState.immutableEntries()) {
            retrieved.put(e.getKey(), e.getValue());
        }
        assertEquals(expected, retrieved);
        // remove all elements from both expected and stored state.
        retrievedState.clear();
        expected.clear();
        snapshot = operatorStateBackend.snapshot(2L, 2L, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation());
        snapshotResult = FutureUtils.runIfNotDoneAndGet(snapshot);
        if (stateHandle != null) {
            stateHandle.discardState();
        }
        stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
        retrieved.clear();
        operatorStateBackend = recreateOperatorStateBackend(operatorStateBackend, abstractStateBackend, StateObjectCollection.singleton(stateHandle));
        retrievedState = operatorStateBackend.getBroadcastState(broadcastStateDesc);
        for (Map.Entry<Integer, Integer> e : retrievedState.immutableEntries()) {
            retrieved.put(e.getKey(), e.getValue());
        }
        assertTrue(expected.isEmpty());
        assertEquals(expected, retrieved);
        if (stateHandle != null) {
            stateHandle.discardState();
            stateHandle = null;
        }
    } finally {
        operatorStateBackend.close();
        operatorStateBackend.dispose();
        if (stateHandle != null) {
            stateHandle.discardState();
        }
    }
}
Also used : MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) BlockerCheckpointStreamFactory(org.apache.flink.runtime.util.BlockerCheckpointStreamFactory) HashMap(java.util.HashMap) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 12 with MemCheckpointStreamFactory

use of org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory in project flink by apache.

the class OperatorStateBackendTest method testSnapshotEmpty.

@Test
public void testSnapshotEmpty() throws Exception {
    final AbstractStateBackend abstractStateBackend = new MemoryStateBackend(4096);
    CloseableRegistry cancelStreamRegistry = new CloseableRegistry();
    final OperatorStateBackend operatorStateBackend = abstractStateBackend.createOperatorStateBackend(createMockEnvironment(), "testOperator", emptyStateHandles, cancelStreamRegistry);
    CheckpointStreamFactory streamFactory = new MemCheckpointStreamFactory(4096);
    RunnableFuture<SnapshotResult<OperatorStateHandle>> snapshot = operatorStateBackend.snapshot(0L, 0L, streamFactory, CheckpointOptions.forCheckpointWithDefaultLocation());
    SnapshotResult<OperatorStateHandle> snapshotResult = FutureUtils.runIfNotDoneAndGet(snapshot);
    OperatorStateHandle stateHandle = snapshotResult.getJobManagerOwnedSnapshot();
    assertNull(stateHandle);
}
Also used : MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) BlockerCheckpointStreamFactory(org.apache.flink.runtime.util.BlockerCheckpointStreamFactory) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) Test(org.junit.Test)

Aggregations

MemCheckpointStreamFactory (org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory)12 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)8 Test (org.junit.Test)8 MemoryStateBackend (org.apache.flink.runtime.state.memory.MemoryStateBackend)5 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)4 MapStateDescriptor (org.apache.flink.api.common.state.MapStateDescriptor)4 CheckpointStreamFactory (org.apache.flink.runtime.state.CheckpointStreamFactory)4 SnapshotResult (org.apache.flink.runtime.state.SnapshotResult)4 BlockerCheckpointStreamFactory (org.apache.flink.runtime.util.BlockerCheckpointStreamFactory)4 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)2 KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)2 KeyedStateHandle (org.apache.flink.runtime.state.KeyedStateHandle)2 StateSnapshotContextSynchronousImpl (org.apache.flink.runtime.state.StateSnapshotContextSynchronousImpl)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1