Search in sources :

Example 21 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper 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 22 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper in project flink by apache.

the class HeapListState method getSerializedValue.

@Override
public byte[] getSerializedValue(final byte[] serializedKeyAndNamespace, final TypeSerializer<K> safeKeySerializer, final TypeSerializer<N> safeNamespaceSerializer, final TypeSerializer<List<V>> safeValueSerializer) throws Exception {
    Preconditions.checkNotNull(serializedKeyAndNamespace);
    Preconditions.checkNotNull(safeKeySerializer);
    Preconditions.checkNotNull(safeNamespaceSerializer);
    Preconditions.checkNotNull(safeValueSerializer);
    Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);
    List<V> result = stateTable.get(keyAndNamespace.f0, keyAndNamespace.f1);
    if (result == null) {
        return null;
    }
    final TypeSerializer<V> dupSerializer = ((ListSerializer<V>) safeValueSerializer).getElementSerializer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputViewStreamWrapper view = new DataOutputViewStreamWrapper(baos);
    // write the same as RocksDB writes lists, with one ',' separator
    for (int i = 0; i < result.size(); i++) {
        dupSerializer.serialize(result.get(i), view);
        if (i < result.size() - 1) {
            view.writeByte(',');
        }
    }
    view.flush();
    return baos.toByteArray();
}
Also used : ListSerializer(org.apache.flink.api.common.typeutils.base.ListSerializer) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 23 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper in project flink by apache.

the class MetadataV3SerializerTest method testCheckpointSerialization.

/**
 * Test checkpoint metadata (de)serialization.
 *
 * @param checkpointId The given checkpointId will write into the metadata.
 * @param operatorStates the given states for all the operators.
 * @param masterStates the masterStates of the given checkpoint/savepoint.
 */
private void testCheckpointSerialization(long checkpointId, Collection<OperatorState> operatorStates, Collection<MasterState> masterStates, @Nullable String basePath) throws IOException {
    MetadataV3Serializer serializer = MetadataV3Serializer.INSTANCE;
    ByteArrayOutputStreamWithPos baos = new ByteArrayOutputStreamWithPos();
    DataOutputStream out = new DataOutputViewStreamWrapper(baos);
    CheckpointMetadata metadata = new CheckpointMetadata(checkpointId, operatorStates, masterStates);
    MetadataV3Serializer.serialize(metadata, out);
    out.close();
    // even if it makes the tests a a tad bit more clumsy
    if (basePath != null) {
        final Path metaPath = new Path(basePath, "_metadata");
        // this is in the temp folder, so it will get automatically deleted
        FileSystem.getLocalFileSystem().create(metaPath, FileSystem.WriteMode.OVERWRITE).close();
    }
    byte[] bytes = baos.toByteArray();
    DataInputStream in = new DataInputViewStreamWrapper(new ByteArrayInputStreamWithPos(bytes));
    CheckpointMetadata deserialized = serializer.deserialize(in, getClass().getClassLoader(), basePath);
    assertEquals(checkpointId, deserialized.getCheckpointId());
    assertEquals(operatorStates, deserialized.getOperatorStates());
    assertEquals(operatorStates.stream().map(OperatorState::isFullyFinished).collect(Collectors.toList()), deserialized.getOperatorStates().stream().map(OperatorState::isFullyFinished).collect(Collectors.toList()));
    assertEquals(masterStates.size(), deserialized.getMasterStates().size());
    for (Iterator<MasterState> a = masterStates.iterator(), b = deserialized.getMasterStates().iterator(); a.hasNext(); ) {
        CheckpointTestUtils.assertMasterStateEquality(a.next(), b.next());
    }
}
Also used : Path(org.apache.flink.core.fs.Path) DataOutputStream(java.io.DataOutputStream) MasterState(org.apache.flink.runtime.checkpoint.MasterState) DataInputStream(java.io.DataInputStream) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) OperatorState(org.apache.flink.runtime.checkpoint.OperatorState)

Example 24 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper in project flink by apache.

the class AvroRecordInputFormatTest method testDeserializeToSpecificType.

/**
 * This test validates proper serialization with specific (generated POJO) types.
 */
@Test
public void testDeserializeToSpecificType() throws IOException {
    DatumReader<User> datumReader = new SpecificDatumReader<>(userSchema);
    try (FileReader<User> dataFileReader = DataFileReader.openReader(testFile, datumReader)) {
        User rec = dataFileReader.next();
        // check if record has been read correctly
        assertNotNull(rec);
        assertEquals("name not equal", TEST_NAME, rec.get("name").toString());
        assertEquals("enum not equal", TEST_ENUM_COLOR.toString(), rec.get("type_enum").toString());
        // now serialize it with our framework:
        ExecutionConfig ec = new ExecutionConfig();
        TypeInformation<User> te = TypeExtractor.createTypeInfo(User.class);
        assertEquals(AvroTypeInfo.class, te.getClass());
        TypeSerializer<User> tser = te.createSerializer(ec);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputViewStreamWrapper outView = new DataOutputViewStreamWrapper(out)) {
            tser.serialize(rec, outView);
        }
        User newRec;
        try (DataInputViewStreamWrapper inView = new DataInputViewStreamWrapper(new ByteArrayInputStream(out.toByteArray()))) {
            newRec = tser.deserialize(inView);
        }
        // check if it is still the same
        assertNotNull(newRec);
        assertEquals("name not equal", TEST_NAME, newRec.getName().toString());
        assertEquals("enum not equal", TEST_ENUM_COLOR.toString(), newRec.getTypeEnum().toString());
    }
}
Also used : User(org.apache.flink.formats.avro.generated.User) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Example 25 with DataOutputViewStreamWrapper

use of org.apache.flink.core.memory.DataOutputViewStreamWrapper in project flink by apache.

the class TypeSerializerSerializationUtilTest method testAnonymousSerializerClassWithChangedSerialVersionUID.

/**
 * Verifies that serializers of anonymous classes can be deserialized, even if serialVersionUID
 * changes.
 */
@Test
public void testAnonymousSerializerClassWithChangedSerialVersionUID() throws Exception {
    TypeSerializer anonymousClassSerializer = new AbstractIntSerializer() {

        @Override
        public TypeSerializerSnapshot<Integer> snapshotConfiguration() {
            return null;
        }
    };
    // assert that our assumption holds
    Assert.assertTrue(anonymousClassSerializer.getClass().isAnonymousClass());
    byte[] anonymousSerializerBytes;
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        TypeSerializerSerializationUtil.writeSerializer(new DataOutputViewStreamWrapper(out), anonymousClassSerializer);
        anonymousSerializerBytes = out.toByteArray();
    }
    long newSerialVersionUID = 1234567L;
    // assert that we're actually modifying to a different serialVersionUID
    Assert.assertNotEquals(ObjectStreamClass.lookup(anonymousClassSerializer.getClass()).getSerialVersionUID(), newSerialVersionUID);
    modifySerialVersionUID(anonymousSerializerBytes, anonymousClassSerializer.getClass().getName(), newSerialVersionUID);
    try (ByteArrayInputStream in = new ByteArrayInputStream(anonymousSerializerBytes)) {
        anonymousClassSerializer = TypeSerializerSerializationUtil.tryReadSerializer(new DataInputViewStreamWrapper(in), Thread.currentThread().getContextClassLoader());
    }
    // serializer should have been deserialized despite serialVersionUID mismatch
    Assert.assertNotNull(anonymousClassSerializer);
    Assert.assertTrue(anonymousClassSerializer.getClass().isAnonymousClass());
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Aggregations

DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)123 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)55 ByteArrayOutputStream (java.io.ByteArrayOutputStream)49 Test (org.junit.Test)43 ByteArrayOutputStreamWithPos (org.apache.flink.core.memory.ByteArrayOutputStreamWithPos)35 IOException (java.io.IOException)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26 ByteArrayInputStreamWithPos (org.apache.flink.core.memory.ByteArrayInputStreamWithPos)23 DataOutputView (org.apache.flink.core.memory.DataOutputView)18 HashMap (java.util.HashMap)13 ArrayList (java.util.ArrayList)12 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)11 Map (java.util.Map)10 TypeSerializerSnapshot (org.apache.flink.api.common.typeutils.TypeSerializerSnapshot)7 StateMetaInfoSnapshot (org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot)7 Before (org.junit.Before)6 Socket (java.net.Socket)5 PipedInputStream (java.io.PipedInputStream)4 PipedOutputStream (java.io.PipedOutputStream)4 List (java.util.List)4