Search in sources :

Example 1 with CheckpointOptions

use of org.apache.flink.runtime.checkpoint.CheckpointOptions 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)

Example 2 with CheckpointOptions

use of org.apache.flink.runtime.checkpoint.CheckpointOptions in project flink by apache.

the class EventSerializer method deserializeCheckpointBarrier.

private static CheckpointBarrier deserializeCheckpointBarrier(ByteBuffer buffer) throws IOException {
    final long id = buffer.getLong();
    final long timestamp = buffer.getLong();
    final byte checkpointTypeCode = buffer.get();
    final SnapshotType snapshotType;
    if (checkpointTypeCode == CHECKPOINT_TYPE_CHECKPOINT) {
        snapshotType = CheckpointType.CHECKPOINT;
    } else if (checkpointTypeCode == CHECKPOINT_TYPE_FULL_CHECKPOINT) {
        snapshotType = CheckpointType.FULL_CHECKPOINT;
    } else if (checkpointTypeCode == CHECKPOINT_TYPE_SAVEPOINT || checkpointTypeCode == CHECKPOINT_TYPE_SAVEPOINT_SUSPEND || checkpointTypeCode == CHECKPOINT_TYPE_SAVEPOINT_TERMINATE) {
        snapshotType = decodeSavepointType(checkpointTypeCode, buffer);
    } else {
        throw new IOException("Unknown checkpoint type code: " + checkpointTypeCode);
    }
    final CheckpointStorageLocationReference locationRef;
    final int locationRefLen = buffer.getInt();
    if (locationRefLen == -1) {
        locationRef = CheckpointStorageLocationReference.getDefault();
    } else {
        byte[] bytes = new byte[locationRefLen];
        buffer.get(bytes);
        locationRef = new CheckpointStorageLocationReference(bytes);
    }
    final CheckpointOptions.AlignmentType alignmentType = CheckpointOptions.AlignmentType.values()[buffer.get()];
    final long alignmentTimeout = buffer.getLong();
    return new CheckpointBarrier(id, timestamp, new CheckpointOptions(snapshotType, locationRef, alignmentType, alignmentTimeout));
}
Also used : CheckpointBarrier(org.apache.flink.runtime.io.network.api.CheckpointBarrier) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) SnapshotType(org.apache.flink.runtime.checkpoint.SnapshotType) IOException(java.io.IOException) CheckpointStorageLocationReference(org.apache.flink.runtime.state.CheckpointStorageLocationReference)

Example 3 with CheckpointOptions

use of org.apache.flink.runtime.checkpoint.CheckpointOptions in project flink by apache.

the class EventSerializer method serializeCheckpointBarrier.

private static ByteBuffer serializeCheckpointBarrier(CheckpointBarrier barrier) throws IOException {
    final CheckpointOptions checkpointOptions = barrier.getCheckpointOptions();
    final byte[] locationBytes = checkpointOptions.getTargetLocation().isDefaultReference() ? null : checkpointOptions.getTargetLocation().getReferenceBytes();
    final ByteBuffer buf = ByteBuffer.allocate(38 + (locationBytes == null ? 0 : locationBytes.length));
    buf.putInt(CHECKPOINT_BARRIER_EVENT);
    buf.putLong(barrier.getId());
    buf.putLong(barrier.getTimestamp());
    // we do not use checkpointType.ordinal() here to make the serialization robust
    // against changes in the enum (such as changes in the order of the values)
    final SnapshotType snapshotType = checkpointOptions.getCheckpointType();
    if (snapshotType.isSavepoint()) {
        encodeSavepointType(snapshotType, buf);
    } else if (snapshotType.equals(CheckpointType.CHECKPOINT)) {
        buf.put(CHECKPOINT_TYPE_CHECKPOINT);
    } else if (snapshotType.equals(CheckpointType.FULL_CHECKPOINT)) {
        buf.put(CHECKPOINT_TYPE_FULL_CHECKPOINT);
    } else {
        throw new IOException("Unknown checkpoint type: " + snapshotType);
    }
    if (locationBytes == null) {
        buf.putInt(-1);
    } else {
        buf.putInt(locationBytes.length);
        buf.put(locationBytes);
    }
    buf.put((byte) checkpointOptions.getAlignment().ordinal());
    buf.putLong(checkpointOptions.getAlignedCheckpointTimeout());
    buf.flip();
    return buf;
}
Also used : CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) SnapshotType(org.apache.flink.runtime.checkpoint.SnapshotType) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 4 with CheckpointOptions

use of org.apache.flink.runtime.checkpoint.CheckpointOptions in project flink by apache.

the class DefaultOperatorStateBackendSnapshotStrategy method asyncSnapshot.

@Override
public SnapshotResultSupplier<OperatorStateHandle> asyncSnapshot(DefaultOperatorStateBackendSnapshotResources syncPartResource, long checkpointId, long timestamp, @Nonnull CheckpointStreamFactory streamFactory, @Nonnull CheckpointOptions checkpointOptions) {
    Map<String, PartitionableListState<?>> registeredOperatorStatesDeepCopies = syncPartResource.getRegisteredOperatorStatesDeepCopies();
    Map<String, BackendWritableBroadcastState<?, ?>> registeredBroadcastStatesDeepCopies = syncPartResource.getRegisteredBroadcastStatesDeepCopies();
    if (registeredBroadcastStatesDeepCopies.isEmpty() && registeredOperatorStatesDeepCopies.isEmpty()) {
        return snapshotCloseableRegistry -> SnapshotResult.empty();
    }
    return (snapshotCloseableRegistry) -> {
        CheckpointStateOutputStream localOut = streamFactory.createCheckpointStateOutputStream(CheckpointedStateScope.EXCLUSIVE);
        snapshotCloseableRegistry.registerCloseable(localOut);
        // get the registered operator state infos ...
        List<StateMetaInfoSnapshot> operatorMetaInfoSnapshots = new ArrayList<>(registeredOperatorStatesDeepCopies.size());
        for (Map.Entry<String, PartitionableListState<?>> entry : registeredOperatorStatesDeepCopies.entrySet()) {
            operatorMetaInfoSnapshots.add(entry.getValue().getStateMetaInfo().snapshot());
        }
        // ... get the registered broadcast operator state infos ...
        List<StateMetaInfoSnapshot> broadcastMetaInfoSnapshots = new ArrayList<>(registeredBroadcastStatesDeepCopies.size());
        for (Map.Entry<String, BackendWritableBroadcastState<?, ?>> entry : registeredBroadcastStatesDeepCopies.entrySet()) {
            broadcastMetaInfoSnapshots.add(entry.getValue().getStateMetaInfo().snapshot());
        }
        // ... write them all in the checkpoint stream ...
        DataOutputView dov = new DataOutputViewStreamWrapper(localOut);
        OperatorBackendSerializationProxy backendSerializationProxy = new OperatorBackendSerializationProxy(operatorMetaInfoSnapshots, broadcastMetaInfoSnapshots);
        backendSerializationProxy.write(dov);
        // ... and then go for the states ...
        // we put BOTH normal and broadcast state metadata here
        int initialMapCapacity = registeredOperatorStatesDeepCopies.size() + registeredBroadcastStatesDeepCopies.size();
        final Map<String, OperatorStateHandle.StateMetaInfo> writtenStatesMetaData = new HashMap<>(initialMapCapacity);
        for (Map.Entry<String, PartitionableListState<?>> entry : registeredOperatorStatesDeepCopies.entrySet()) {
            PartitionableListState<?> value = entry.getValue();
            long[] partitionOffsets = value.write(localOut);
            OperatorStateHandle.Mode mode = value.getStateMetaInfo().getAssignmentMode();
            writtenStatesMetaData.put(entry.getKey(), new OperatorStateHandle.StateMetaInfo(partitionOffsets, mode));
        }
        // ... and the broadcast states themselves ...
        for (Map.Entry<String, BackendWritableBroadcastState<?, ?>> entry : registeredBroadcastStatesDeepCopies.entrySet()) {
            BackendWritableBroadcastState<?, ?> value = entry.getValue();
            long[] partitionOffsets = { value.write(localOut) };
            OperatorStateHandle.Mode mode = value.getStateMetaInfo().getAssignmentMode();
            writtenStatesMetaData.put(entry.getKey(), new OperatorStateHandle.StateMetaInfo(partitionOffsets, mode));
        }
        // ... and, finally, create the state handle.
        OperatorStateHandle retValue = null;
        if (snapshotCloseableRegistry.unregisterCloseable(localOut)) {
            StreamStateHandle stateHandle = localOut.closeAndGetHandle();
            if (stateHandle != null) {
                retValue = new OperatorStreamStateHandle(writtenStatesMetaData, stateHandle);
            }
            return SnapshotResult.of(retValue);
        } else {
            throw new IOException("Stream was already unregistered.");
        }
    };
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) List(java.util.List) StateMetaInfoSnapshot(org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot) Map(java.util.Map) IOException(java.io.IOException) HashMap(java.util.HashMap) DataOutputView(org.apache.flink.core.memory.DataOutputView) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) Nonnull(javax.annotation.Nonnull) Collections(java.util.Collections) ArrayList(java.util.ArrayList) DataOutputView(org.apache.flink.core.memory.DataOutputView) IOException(java.io.IOException) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with CheckpointOptions

use of org.apache.flink.runtime.checkpoint.CheckpointOptions in project flink by apache.

the class SavepointStateBackendSwitchTestBase method takeSavepoint.

private void takeSavepoint(CheckpointableKeyedStateBackend<String> keyedBackend, File pathToWrite, MapStateDescriptor<Long, Long> stateDescr, ValueStateDescriptor<Long> valueStateDescriptor, ListStateDescriptor<Long> listStateDescriptor, Integer namespace1, Integer namespace2, Integer namespace3, Integer namespace4) throws Exception {
    InternalMapState<String, Integer, Long, Long> mapState = keyedBackend.createInternalState(IntSerializer.INSTANCE, stateDescr);
    InternalValueState<String, Integer, Long> valueState = keyedBackend.createInternalState(IntSerializer.INSTANCE, valueStateDescriptor);
    InternalListState<String, Integer, Long> listState = keyedBackend.createInternalState(IntSerializer.INSTANCE, listStateDescriptor);
    keyedBackend.setCurrentKey("abc");
    mapState.setCurrentNamespace(namespace1);
    mapState.put(33L, 33L);
    mapState.put(55L, 55L);
    mapState.setCurrentNamespace(namespace2);
    mapState.put(22L, 22L);
    mapState.put(11L, 11L);
    listState.setCurrentNamespace(namespace2);
    listState.add(4L);
    listState.add(5L);
    listState.add(6L);
    mapState.setCurrentNamespace(namespace3);
    mapState.put(44L, 44L);
    keyedBackend.setCurrentKey("mno");
    mapState.setCurrentNamespace(namespace3);
    mapState.put(11L, 11L);
    mapState.put(22L, 22L);
    mapState.put(33L, 33L);
    mapState.put(44L, 44L);
    mapState.put(55L, 55L);
    valueState.setCurrentNamespace(namespace3);
    valueState.update(1239L);
    listState.setCurrentNamespace(namespace3);
    listState.add(1L);
    listState.add(2L);
    listState.add(3L);
    mapState.setCurrentNamespace(namespace4);
    mapState.put(1L, 1L);
    // HEAP state backend will keep an empty map as an entry in the underlying State Table
    // we should skip such entries when serializing
    Iterator<Map.Entry<Long, Long>> iterator = mapState.iterator();
    while (iterator.hasNext()) {
        iterator.next();
        iterator.remove();
    }
    KeyGroupedInternalPriorityQueue<TimerHeapInternalTimer<String, Integer>> priorityQueue = keyedBackend.create("event-time", new TimerSerializer<>(keyedBackend.getKeySerializer(), IntSerializer.INSTANCE));
    priorityQueue.add(new TimerHeapInternalTimer<>(1234L, "mno", namespace3));
    priorityQueue.add(new TimerHeapInternalTimer<>(2345L, "mno", namespace2));
    priorityQueue.add(new TimerHeapInternalTimer<>(3456L, "mno", namespace3));
    SnapshotStrategyRunner<KeyedStateHandle, ? extends FullSnapshotResources<?>> savepointRunner = StreamOperatorStateHandler.prepareCanonicalSavepoint(keyedBackend, new CloseableRegistry());
    RunnableFuture<SnapshotResult<KeyedStateHandle>> snapshot = savepointRunner.snapshot(0L, 0L, new MemCheckpointStreamFactory(4 * 1024 * 1024), new CheckpointOptions(SavepointType.savepoint(SavepointFormatType.CANONICAL), CheckpointStorageLocationReference.getDefault()));
    snapshot.run();
    try (BufferedOutputStream bis = new BufferedOutputStream(new FileOutputStream(pathToWrite))) {
        InstantiationUtil.serializeObject(bis, snapshot.get());
    }
}
Also used : TimerHeapInternalTimer(org.apache.flink.streaming.api.operators.TimerHeapInternalTimer) SnapshotResult(org.apache.flink.runtime.state.SnapshotResult) KeyedStateHandle(org.apache.flink.runtime.state.KeyedStateHandle) CloseableRegistry(org.apache.flink.core.fs.CloseableRegistry) MemCheckpointStreamFactory(org.apache.flink.runtime.state.memory.MemCheckpointStreamFactory) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

CheckpointOptions (org.apache.flink.runtime.checkpoint.CheckpointOptions)43 Test (org.junit.Test)23 CheckpointBarrier (org.apache.flink.runtime.io.network.api.CheckpointBarrier)14 CheckpointStorageLocationReference (org.apache.flink.runtime.state.CheckpointStorageLocationReference)13 CheckpointMetaData (org.apache.flink.runtime.checkpoint.CheckpointMetaData)12 IOException (java.io.IOException)7 CheckpointMetricsBuilder (org.apache.flink.runtime.checkpoint.CheckpointMetricsBuilder)5 InputChannelInfo (org.apache.flink.runtime.checkpoint.channel.InputChannelInfo)5 TestInputChannel (org.apache.flink.runtime.io.network.partition.consumer.TestInputChannel)5 List (java.util.List)4 Map (java.util.Map)4 Buffer (org.apache.flink.runtime.io.network.buffer.Buffer)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 SnapshotType (org.apache.flink.runtime.checkpoint.SnapshotType)3 EndOfData (org.apache.flink.runtime.io.network.api.EndOfData)3 SingleInputGate (org.apache.flink.runtime.io.network.partition.consumer.SingleInputGate)3 SingleInputGateBuilder (org.apache.flink.runtime.io.network.partition.consumer.SingleInputGateBuilder)3 StreamConfig (org.apache.flink.streaming.api.graph.StreamConfig)3 ByteBuffer (java.nio.ByteBuffer)2