Search in sources :

Example 1 with CheckpointStreamWithResultProvider

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

the class HeapSnapshotStrategy method asyncSnapshot.

@Override
public SnapshotResultSupplier<KeyedStateHandle> asyncSnapshot(HeapSnapshotResources<K> syncPartResource, long checkpointId, long timestamp, @Nonnull CheckpointStreamFactory streamFactory, @Nonnull CheckpointOptions checkpointOptions) {
    List<StateMetaInfoSnapshot> metaInfoSnapshots = syncPartResource.getMetaInfoSnapshots();
    if (metaInfoSnapshots.isEmpty()) {
        return snapshotCloseableRegistry -> SnapshotResult.empty();
    }
    final KeyedBackendSerializationProxy<K> serializationProxy = new KeyedBackendSerializationProxy<>(// get a serialized form already at state registration time in the future
    syncPartResource.getKeySerializer(), metaInfoSnapshots, !Objects.equals(UncompressedStreamCompressionDecorator.INSTANCE, keyGroupCompressionDecorator));
    final SupplierWithException<CheckpointStreamWithResultProvider, Exception> checkpointStreamSupplier = localRecoveryConfig.isLocalRecoveryEnabled() && !checkpointOptions.getCheckpointType().isSavepoint() ? () -> createDuplicatingStream(checkpointId, CheckpointedStateScope.EXCLUSIVE, streamFactory, localRecoveryConfig.getLocalStateDirectoryProvider().orElseThrow(LocalRecoveryConfig.localRecoveryNotEnabled())) : () -> createSimpleStream(CheckpointedStateScope.EXCLUSIVE, streamFactory);
    return (snapshotCloseableRegistry) -> {
        final Map<StateUID, Integer> stateNamesToId = syncPartResource.getStateNamesToId();
        final Map<StateUID, StateSnapshot> cowStateStableSnapshots = syncPartResource.getCowStateStableSnapshots();
        final CheckpointStreamWithResultProvider streamWithResultProvider = checkpointStreamSupplier.get();
        snapshotCloseableRegistry.registerCloseable(streamWithResultProvider);
        final CheckpointStateOutputStream localStream = streamWithResultProvider.getCheckpointOutputStream();
        final DataOutputViewStreamWrapper outView = new DataOutputViewStreamWrapper(localStream);
        serializationProxy.write(outView);
        final long[] keyGroupRangeOffsets = new long[keyGroupRange.getNumberOfKeyGroups()];
        for (int keyGroupPos = 0; keyGroupPos < keyGroupRange.getNumberOfKeyGroups(); ++keyGroupPos) {
            int keyGroupId = keyGroupRange.getKeyGroupId(keyGroupPos);
            keyGroupRangeOffsets[keyGroupPos] = localStream.getPos();
            outView.writeInt(keyGroupId);
            for (Map.Entry<StateUID, StateSnapshot> stateSnapshot : cowStateStableSnapshots.entrySet()) {
                StateSnapshot.StateKeyGroupWriter partitionedSnapshot = stateSnapshot.getValue().getKeyGroupWriter();
                try (OutputStream kgCompressionOut = keyGroupCompressionDecorator.decorateWithCompression(localStream)) {
                    DataOutputViewStreamWrapper kgCompressionView = new DataOutputViewStreamWrapper(kgCompressionOut);
                    kgCompressionView.writeShort(stateNamesToId.get(stateSnapshot.getKey()));
                    partitionedSnapshot.writeStateInKeyGroup(kgCompressionView, keyGroupId);
                }
            // this will just close the outer compression stream
            }
        }
        if (snapshotCloseableRegistry.unregisterCloseable(streamWithResultProvider)) {
            KeyGroupRangeOffsets kgOffs = new KeyGroupRangeOffsets(keyGroupRange, keyGroupRangeOffsets);
            SnapshotResult<StreamStateHandle> result = streamWithResultProvider.closeAndFinalizeCheckpointStreamResult();
            return toKeyedStateHandleSnapshotResult(result, kgOffs, KeyGroupsStateHandle::new);
        } else {
            throw new IOException("Stream already unregistered.");
        }
    };
}
Also used : KeyGroupRangeOffsets(org.apache.flink.runtime.state.KeyGroupRangeOffsets) KeyGroupsStateHandle(org.apache.flink.runtime.state.KeyGroupsStateHandle) LocalRecoveryConfig(org.apache.flink.runtime.state.LocalRecoveryConfig) CheckpointStreamWithResultProvider.toKeyedStateHandleSnapshotResult(org.apache.flink.runtime.state.CheckpointStreamWithResultProvider.toKeyedStateHandleSnapshotResult) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) KeyedBackendSerializationProxy(org.apache.flink.runtime.state.KeyedBackendSerializationProxy) Map(java.util.Map) StreamCompressionDecorator(org.apache.flink.runtime.state.StreamCompressionDecorator) Nonnull(javax.annotation.Nonnull) CheckpointStreamWithResultProvider(org.apache.flink.runtime.state.CheckpointStreamWithResultProvider) CheckpointedStateScope(org.apache.flink.runtime.state.CheckpointedStateScope) OutputStream(java.io.OutputStream) KeyGroupRange(org.apache.flink.runtime.state.KeyGroupRange) SnapshotResult(org.apache.flink.runtime.state.SnapshotResult) CheckpointStreamWithResultProvider.createSimpleStream(org.apache.flink.runtime.state.CheckpointStreamWithResultProvider.createSimpleStream) StateSerializerProvider(org.apache.flink.runtime.state.StateSerializerProvider) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) UncompressedStreamCompressionDecorator(org.apache.flink.runtime.state.UncompressedStreamCompressionDecorator) KeyedStateHandle(org.apache.flink.runtime.state.KeyedStateHandle) StateMetaInfoSnapshot(org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot) SnapshotStrategy(org.apache.flink.runtime.state.SnapshotStrategy) IOException(java.io.IOException) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) StreamStateHandle(org.apache.flink.runtime.state.StreamStateHandle) StateSnapshot(org.apache.flink.runtime.state.StateSnapshot) Objects(java.util.Objects) List(java.util.List) CheckpointStreamWithResultProvider.createDuplicatingStream(org.apache.flink.runtime.state.CheckpointStreamWithResultProvider.createDuplicatingStream) CheckpointStreamFactory(org.apache.flink.runtime.state.CheckpointStreamFactory) CheckpointStateOutputStream(org.apache.flink.runtime.state.CheckpointStateOutputStream) SupplierWithException(org.apache.flink.util.function.SupplierWithException) CheckpointStreamWithResultProvider.toKeyedStateHandleSnapshotResult(org.apache.flink.runtime.state.CheckpointStreamWithResultProvider.toKeyedStateHandleSnapshotResult) SnapshotResult(org.apache.flink.runtime.state.SnapshotResult) KeyGroupRangeOffsets(org.apache.flink.runtime.state.KeyGroupRangeOffsets) OutputStream(java.io.OutputStream) CheckpointStateOutputStream(org.apache.flink.runtime.state.CheckpointStateOutputStream) StateMetaInfoSnapshot(org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot) KeyedBackendSerializationProxy(org.apache.flink.runtime.state.KeyedBackendSerializationProxy) IOException(java.io.IOException) IOException(java.io.IOException) SupplierWithException(org.apache.flink.util.function.SupplierWithException) CheckpointStreamWithResultProvider(org.apache.flink.runtime.state.CheckpointStreamWithResultProvider) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) CheckpointStateOutputStream(org.apache.flink.runtime.state.CheckpointStateOutputStream) Map(java.util.Map)

Aggregations

IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Nonnull (javax.annotation.Nonnull)1 TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)1 DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)1 CheckpointOptions (org.apache.flink.runtime.checkpoint.CheckpointOptions)1 CheckpointStateOutputStream (org.apache.flink.runtime.state.CheckpointStateOutputStream)1 CheckpointStreamFactory (org.apache.flink.runtime.state.CheckpointStreamFactory)1 CheckpointStreamWithResultProvider (org.apache.flink.runtime.state.CheckpointStreamWithResultProvider)1 CheckpointStreamWithResultProvider.createDuplicatingStream (org.apache.flink.runtime.state.CheckpointStreamWithResultProvider.createDuplicatingStream)1 CheckpointStreamWithResultProvider.createSimpleStream (org.apache.flink.runtime.state.CheckpointStreamWithResultProvider.createSimpleStream)1 CheckpointStreamWithResultProvider.toKeyedStateHandleSnapshotResult (org.apache.flink.runtime.state.CheckpointStreamWithResultProvider.toKeyedStateHandleSnapshotResult)1 CheckpointedStateScope (org.apache.flink.runtime.state.CheckpointedStateScope)1 KeyGroupRange (org.apache.flink.runtime.state.KeyGroupRange)1 KeyGroupRangeOffsets (org.apache.flink.runtime.state.KeyGroupRangeOffsets)1 KeyGroupsStateHandle (org.apache.flink.runtime.state.KeyGroupsStateHandle)1 KeyedBackendSerializationProxy (org.apache.flink.runtime.state.KeyedBackendSerializationProxy)1