Search in sources :

Example 46 with DataOutputViewStreamWrapper

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

the class OutputEmitterTest method testWrongKeyClass.

@Test
public void testWrongKeyClass() throws Exception {
    // Test for IntValue
    final TypeComparator<Record> doubleComp = new RecordComparatorFactory(new int[] { 0 }, new Class[] { DoubleValue.class }).createComparator();
    final ChannelSelector<SerializationDelegate<Record>> selector = createChannelSelector(ShipStrategyType.PARTITION_HASH, doubleComp, 100);
    final SerializationDelegate<Record> delegate = new SerializationDelegate<>(new RecordSerializerFactory().getSerializer());
    PipedInputStream pipedInput = new PipedInputStream(1024 * 1024);
    DataInputView in = new DataInputViewStreamWrapper(pipedInput);
    DataOutputView out = new DataOutputViewStreamWrapper(new PipedOutputStream(pipedInput));
    Record record = new Record(1);
    record.setField(0, new IntValue());
    record.write(out);
    record = new Record();
    record.read(in);
    try {
        delegate.setInstance(record);
        selector.selectChannel(delegate);
    } catch (DeserializationException re) {
        return;
    }
    Assert.fail("Expected a NullKeyFieldException.");
}
Also used : RecordComparatorFactory(org.apache.flink.runtime.testutils.recordutils.RecordComparatorFactory) DataInputView(org.apache.flink.core.memory.DataInputView) RecordSerializerFactory(org.apache.flink.runtime.testutils.recordutils.RecordSerializerFactory) SerializationDelegate(org.apache.flink.runtime.plugable.SerializationDelegate) DataOutputView(org.apache.flink.core.memory.DataOutputView) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) DeserializationException(org.apache.flink.types.DeserializationException) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) DoubleValue(org.apache.flink.types.DoubleValue) Record(org.apache.flink.types.Record) IntValue(org.apache.flink.types.IntValue) Test(org.junit.Test)

Example 47 with DataOutputViewStreamWrapper

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

the class CopyOnWriteSkipListStateMapTestUtils method verifySnapshotWithoutTransform.

static <K, N, S> void verifySnapshotWithoutTransform(Map<N, Map<K, S>> referenceStates, @Nonnull CopyOnWriteSkipListStateMapSnapshot<K, N, S> snapshot, TypeSerializer<K> keySerializer, TypeSerializer<N> namespaceSerializer, TypeSerializer<S> stateSerializer, SnapshotVerificationMode verificationMode) throws IOException {
    ByteArrayOutputStreamWithPos outputStream = new ByteArrayOutputStreamWithPos();
    DataOutputView outputView = new DataOutputViewStreamWrapper(outputStream);
    if (verificationMode == SnapshotVerificationMode.ITERATOR) {
        Iterator<StateEntry<K, N, S>> iterator = snapshot.getIterator(keySerializer, namespaceSerializer, stateSerializer, null);
        assertThat(() -> iterator, containsInAnyOrder(toMatchers(referenceStates)));
    } else {
        snapshot.writeState(keySerializer, namespaceSerializer, stateSerializer, outputView, null);
        Map<N, Map<K, S>> actualStates = readStateFromSnapshot(outputStream.toByteArray(), keySerializer, namespaceSerializer, stateSerializer);
        assertEquals(referenceStates, actualStates);
    }
}
Also used : StateEntry(org.apache.flink.runtime.state.StateEntry) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) DataOutputView(org.apache.flink.core.memory.DataOutputView) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) HashMap(java.util.HashMap) Map(java.util.Map)

Example 48 with DataOutputViewStreamWrapper

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

the class CopyOnWriteSkipListStateMapTestUtils method verifySnapshotWithTransform.

static <K, N, S> void verifySnapshotWithTransform(@Nonnull Map<N, Map<K, S>> referenceStates, @Nonnull CopyOnWriteSkipListStateMapSnapshot<K, N, S> snapshot, StateSnapshotTransformer<S> transformer, TypeSerializer<K> keySerializer, TypeSerializer<N> namespaceSerializer, TypeSerializer<S> stateSerializer, SnapshotVerificationMode verificationMode) throws IOException {
    ByteArrayOutputStreamWithPos outputStream = new ByteArrayOutputStreamWithPos();
    DataOutputView outputView = new DataOutputViewStreamWrapper(outputStream);
    Map<N, Map<K, S>> transformedStates = new HashMap<>();
    for (Map.Entry<N, Map<K, S>> namespaceEntry : referenceStates.entrySet()) {
        for (Map.Entry<K, S> keyEntry : namespaceEntry.getValue().entrySet()) {
            S state = transformer.filterOrTransform(keyEntry.getValue());
            if (state != null) {
                transformedStates.computeIfAbsent(namespaceEntry.getKey(), (none) -> new HashMap<>()).put(keyEntry.getKey(), state);
            }
        }
    }
    if (verificationMode == SnapshotVerificationMode.SERIALIZED) {
        snapshot.writeState(keySerializer, namespaceSerializer, stateSerializer, outputView, transformer);
        Map<N, Map<K, S>> actualStates = readStateFromSnapshot(outputStream.toByteArray(), keySerializer, namespaceSerializer, stateSerializer);
        assertEquals(transformedStates, actualStates);
    } else {
        Iterator<StateEntry<K, N, S>> iterator = snapshot.getIterator(keySerializer, namespaceSerializer, stateSerializer, null);
        assertThat(() -> iterator, containsInAnyOrder(toMatchers(referenceStates)));
    }
}
Also used : HashMap(java.util.HashMap) StringSerializer(org.apache.flink.api.common.typeutils.base.StringSerializer) DataOutputView(org.apache.flink.core.memory.DataOutputView) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) ArrayList(java.util.ArrayList) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) HashSet(java.util.HashSet) Assert.assertThat(org.junit.Assert.assertThat) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) DEFAULT_MAX_KEYS_TO_DELETE_ONE_TIME(org.apache.flink.runtime.state.heap.CopyOnWriteSkipListStateMap.DEFAULT_MAX_KEYS_TO_DELETE_ONE_TIME) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) StateSnapshotTransformer(org.apache.flink.runtime.state.StateSnapshotTransformer) LongSerializer(org.apache.flink.api.common.typeutils.base.LongSerializer) DataInputView(org.apache.flink.core.memory.DataInputView) InternalKvState(org.apache.flink.runtime.state.internal.InternalKvState) StateEntryMatcher.entry(org.apache.flink.runtime.state.testutils.StateEntryMatcher.entry) Allocator(org.apache.flink.runtime.state.heap.space.Allocator) Nonnull(javax.annotation.Nonnull) NIL_NODE(org.apache.flink.runtime.state.heap.SkipListUtils.NIL_NODE) DEFAULT_LOGICAL_REMOVED_KEYS_RATIO(org.apache.flink.runtime.state.heap.CopyOnWriteSkipListStateMap.DEFAULT_LOGICAL_REMOVED_KEYS_RATIO) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) Iterator(java.util.Iterator) Collection(java.util.Collection) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) Collectors(java.util.stream.Collectors) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matcher(org.hamcrest.Matcher) StateEntry(org.apache.flink.runtime.state.StateEntry) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) DataOutputView(org.apache.flink.core.memory.DataOutputView) StateEntry(org.apache.flink.runtime.state.StateEntry) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayOutputStreamWithPos(org.apache.flink.core.memory.ByteArrayOutputStreamWithPos) HashMap(java.util.HashMap) Map(java.util.Map)

Example 49 with DataOutputViewStreamWrapper

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

the class SocketStreamIteratorTest method testIterator.

@Test
public void testIterator() throws Exception {
    final AtomicReference<Throwable> error = new AtomicReference<>();
    final long seed = new Random().nextLong();
    final int numElements = 1000;
    final SocketStreamIterator<Long> iterator = new SocketStreamIterator<>(LongSerializer.INSTANCE);
    Thread writer = new Thread() {

        @Override
        public void run() {
            try {
                try (Socket sock = new Socket(iterator.getBindAddress(), iterator.getPort());
                    DataOutputViewStreamWrapper out = new DataOutputViewStreamWrapper(sock.getOutputStream())) {
                    final TypeSerializer<Long> serializer = LongSerializer.INSTANCE;
                    final Random rnd = new Random(seed);
                    for (int i = 0; i < numElements; i++) {
                        serializer.serialize(rnd.nextLong(), out);
                    }
                }
            } catch (Throwable t) {
                error.set(t);
            }
        }
    };
    writer.start();
    final Random validator = new Random(seed);
    for (int i = 0; i < numElements; i++) {
        assertTrue(iterator.hasNext());
        assertTrue(iterator.hasNext());
        assertEquals(validator.nextLong(), iterator.next().longValue());
    }
    assertFalse(iterator.hasNext());
    writer.join();
    assertFalse(iterator.hasNext());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Random(java.util.Random) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) Socket(java.net.Socket) Test(org.junit.Test)

Example 50 with DataOutputViewStreamWrapper

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

the class CollectResultBufferTest method createSerializedResults.

private List<byte[]> createSerializedResults(List<Integer> values) throws Exception {
    List<byte[]> serializedResults = new ArrayList<>();
    for (int value : values) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputView wrapper = new DataOutputViewStreamWrapper(baos);
        serializer.serialize(value, wrapper);
        serializedResults.add(baos.toByteArray());
    }
    return serializedResults;
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ArrayList(java.util.ArrayList) DataOutputView(org.apache.flink.core.memory.DataOutputView) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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