Search in sources :

Example 36 with KeyedStateHandle

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

the class StateBackendTestContext method dispose.

void dispose() throws Exception {
    disposeKeyedStateBackend();
    for (KeyedStateHandle snapshot : snapshots) {
        snapshot.discardState();
    }
    snapshots.clear();
    sharedStateRegistry.close();
    if (env != null) {
        env.close();
    }
}
Also used : KeyedStateHandle(org.apache.flink.runtime.state.KeyedStateHandle)

Example 37 with KeyedStateHandle

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

the class TtlStateTestBase method testIncrementalCleanup.

@Test
public void testIncrementalCleanup() throws Exception {
    assumeTrue(incrementalCleanupSupported());
    initTest(getConfBuilder(TTL).cleanupIncrementally(5, true).build());
    final int keysToUpdate = (CopyOnWriteStateMap.DEFAULT_CAPACITY >> 3) * NUMBER_OF_KEY_GROUPS;
    timeProvider.time = 0;
    // create enough keys to trigger incremental rehash
    updateKeys(0, INC_CLEANUP_ALL_KEYS, ctx().updateEmpty);
    timeProvider.time = 50;
    // update some
    updateKeys(0, keysToUpdate, ctx().updateUnexpired);
    RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshotRunnableFuture = sbetc.triggerSnapshot();
    // update more concurrently with snapshotting
    updateKeys(keysToUpdate, keysToUpdate * 2, ctx().updateUnexpired);
    // expire rest
    timeProvider.time = 120;
    triggerMoreIncrementalCleanupByOtherOps();
    // check rest expired and cleanup updated
    checkExpiredKeys(keysToUpdate * 2, INC_CLEANUP_ALL_KEYS);
    KeyedStateHandle snapshot = snapshotRunnableFuture.get().getJobManagerOwnedSnapshot();
    // restore snapshot which should discard concurrent updates
    timeProvider.time = 50;
    restoreSnapshot(snapshot, NUMBER_OF_KEY_GROUPS);
    // check rest unexpired, also after restore which should discard concurrent updates
    checkUnexpiredKeys(keysToUpdate, INC_CLEANUP_ALL_KEYS, ctx().getUpdateEmpty);
    timeProvider.time = 120;
    // remove some
    for (int i = keysToUpdate >> 1; i < keysToUpdate + (keysToUpdate >> 2); i++) {
        sbetc.setCurrentKey(Integer.toString(i));
        ctx().ttlState.clear();
    }
    // check updated not expired
    checkUnexpiredKeys(0, keysToUpdate >> 1, ctx().getUnexpired);
    triggerMoreIncrementalCleanupByOtherOps();
    // check that concurrently updated and then restored with original values are expired
    checkExpiredKeys(keysToUpdate, keysToUpdate * 2);
    timeProvider.time = 170;
    // check rest expired and cleanup updated
    checkExpiredKeys(keysToUpdate >> 1, INC_CLEANUP_ALL_KEYS);
    // check updated expired
    checkExpiredKeys(0, keysToUpdate >> 1);
}
Also used : SnapshotResult(org.apache.flink.runtime.state.SnapshotResult) KeyedStateHandle(org.apache.flink.runtime.state.KeyedStateHandle) Test(org.junit.Test)

Example 38 with KeyedStateHandle

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

the class RocksDBIncrementalCheckpointUtils method chooseTheBestStateHandleForInitial.

/**
 * Choose the best state handle according to the {@link #STATE_HANDLE_EVALUATOR} to init the
 * initial db.
 *
 * @param restoreStateHandles The candidate state handles.
 * @param targetKeyGroupRange The target key group range.
 * @return The best candidate or null if no candidate was a good fit.
 */
@Nullable
public static KeyedStateHandle chooseTheBestStateHandleForInitial(@Nonnull Collection<KeyedStateHandle> restoreStateHandles, @Nonnull KeyGroupRange targetKeyGroupRange) {
    KeyedStateHandle bestStateHandle = null;
    Score bestScore = Score.MIN;
    for (KeyedStateHandle rawStateHandle : restoreStateHandles) {
        Score handleScore = STATE_HANDLE_EVALUATOR.apply(rawStateHandle, targetKeyGroupRange);
        if (handleScore.compareTo(bestScore) > 0) {
            bestStateHandle = rawStateHandle;
            bestScore = handleScore;
        }
    }
    return bestStateHandle;
}
Also used : KeyedStateHandle(org.apache.flink.runtime.state.KeyedStateHandle) Nullable(javax.annotation.Nullable)

Example 39 with KeyedStateHandle

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

the class EmbeddedRocksDBStateBackendTest method testSharedIncrementalStateDeRegistration.

@Test
public void testSharedIncrementalStateDeRegistration() throws Exception {
    if (enableIncrementalCheckpointing) {
        CheckpointableKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);
        try {
            ValueStateDescriptor<String> kvId = new ValueStateDescriptor<>("id", String.class, null);
            kvId.initializeSerializerUnlessSet(new ExecutionConfig());
            ValueState<String> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
            Queue<IncrementalRemoteKeyedStateHandle> previousStateHandles = new LinkedList<>();
            SharedStateRegistry sharedStateRegistry = spy(new SharedStateRegistryImpl());
            for (int checkpointId = 0; checkpointId < 3; ++checkpointId) {
                reset(sharedStateRegistry);
                backend.setCurrentKey(checkpointId);
                state.update("Hello-" + checkpointId);
                RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot = backend.snapshot(checkpointId, checkpointId, createStreamFactory(), CheckpointOptions.forCheckpointWithDefaultLocation());
                snapshot.run();
                SnapshotResult<KeyedStateHandle> snapshotResult = snapshot.get();
                IncrementalRemoteKeyedStateHandle stateHandle = (IncrementalRemoteKeyedStateHandle) snapshotResult.getJobManagerOwnedSnapshot();
                Map<StateHandleID, StreamStateHandle> sharedState = new HashMap<>(stateHandle.getSharedState());
                stateHandle.registerSharedStates(sharedStateRegistry, checkpointId);
                for (Map.Entry<StateHandleID, StreamStateHandle> e : sharedState.entrySet()) {
                    verify(sharedStateRegistry).registerReference(stateHandle.createSharedStateRegistryKeyFromFileName(e.getKey()), e.getValue(), checkpointId);
                }
                previousStateHandles.add(stateHandle);
                ((CheckpointListener) backend).notifyCheckpointComplete(checkpointId);
                if (previousStateHandles.size() > 1) {
                    previousStateHandles.remove().discardState();
                }
            }
            while (!previousStateHandles.isEmpty()) {
                reset(sharedStateRegistry);
                previousStateHandles.remove().discardState();
            }
        } finally {
            IOUtils.closeQuietly(backend);
            backend.dispose();
        }
    }
}
Also used : SnapshotResult(org.apache.flink.runtime.state.SnapshotResult) HashMap(java.util.HashMap) CheckpointListener(org.apache.flink.api.common.state.CheckpointListener) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) IncrementalRemoteKeyedStateHandle(org.apache.flink.runtime.state.IncrementalRemoteKeyedStateHandle) KeyedStateHandle(org.apache.flink.runtime.state.KeyedStateHandle) LinkedList(java.util.LinkedList) SharedStateRegistry(org.apache.flink.runtime.state.SharedStateRegistry) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) StateHandleID(org.apache.flink.runtime.state.StateHandleID) SharedStateRegistryImpl(org.apache.flink.runtime.state.SharedStateRegistryImpl) IncrementalRemoteKeyedStateHandle(org.apache.flink.runtime.state.IncrementalRemoteKeyedStateHandle) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 40 with KeyedStateHandle

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

the class EmbeddedRocksDBStateBackendTest method testCompletingSnapshot.

@Test
public void testCompletingSnapshot() throws Exception {
    setupRocksKeyedStateBackend();
    try {
        RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot = keyedStateBackend.snapshot(0L, 0L, testStreamFactory, CheckpointOptions.forCheckpointWithDefaultLocation());
        Thread asyncSnapshotThread = new Thread(snapshot);
        asyncSnapshotThread.start();
        // wait for snapshot to run
        waiter.await();
        waiter.reset();
        runStateUpdates();
        // allow checkpointing to start writing
        blocker.trigger();
        // wait for snapshot stream writing to run
        waiter.await();
        SnapshotResult<KeyedStateHandle> snapshotResult = snapshot.get();
        KeyedStateHandle keyedStateHandle = snapshotResult.getJobManagerOwnedSnapshot();
        assertNotNull(keyedStateHandle);
        assertTrue(keyedStateHandle.getStateSize() > 0);
        assertEquals(2, keyedStateHandle.getKeyGroupRange().getNumberOfKeyGroups());
        for (BlockingCheckpointOutputStream stream : testStreamFactory.getAllCreatedStreams()) {
            assertTrue(stream.isClosed());
        }
        asyncSnapshotThread.join();
        verifyRocksObjectsReleased();
    } finally {
        this.keyedStateBackend.dispose();
        this.keyedStateBackend = null;
    }
    verifyRocksDBStateUploaderClosed();
}
Also used : SnapshotResult(org.apache.flink.runtime.state.SnapshotResult) BlockingCheckpointOutputStream(org.apache.flink.runtime.util.BlockingCheckpointOutputStream) IncrementalRemoteKeyedStateHandle(org.apache.flink.runtime.state.IncrementalRemoteKeyedStateHandle) KeyedStateHandle(org.apache.flink.runtime.state.KeyedStateHandle) Test(org.junit.Test)

Aggregations

KeyedStateHandle (org.apache.flink.runtime.state.KeyedStateHandle)49 Test (org.junit.Test)16 OperatorStateHandle (org.apache.flink.runtime.state.OperatorStateHandle)15 KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)14 ArrayList (java.util.ArrayList)10 KeyGroupsStateHandle (org.apache.flink.runtime.state.KeyGroupsStateHandle)10 IncrementalRemoteKeyedStateHandle (org.apache.flink.runtime.state.IncrementalRemoteKeyedStateHandle)9 OperatorSubtaskState (org.apache.flink.runtime.checkpoint.OperatorSubtaskState)8 SnapshotResult (org.apache.flink.runtime.state.SnapshotResult)8 HashMap (java.util.HashMap)7 OperatorID (org.apache.flink.runtime.jobgraph.OperatorID)7 OperatorStreamStateHandle (org.apache.flink.runtime.state.OperatorStreamStateHandle)7 StateObjectCollection (org.apache.flink.runtime.checkpoint.StateObjectCollection)6 JobVertexID (org.apache.flink.runtime.jobgraph.JobVertexID)6 List (java.util.List)5 CloseableRegistry (org.apache.flink.core.fs.CloseableRegistry)5 InputChannelStateHandle (org.apache.flink.runtime.state.InputChannelStateHandle)5 ResultSubpartitionStateHandle (org.apache.flink.runtime.state.ResultSubpartitionStateHandle)5 StreamStateHandle (org.apache.flink.runtime.state.StreamStateHandle)5 Map (java.util.Map)4