Search in sources :

Example 16 with DataInputView

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

the class AvroSerializerSnapshotTest method roundTrip.

// ---------------------------------------------------------------------------------------------------------------
// Utils
// ---------------------------------------------------------------------------------------------------------------
/**
 * Serialize an (avro)TypeSerializerSnapshot and deserialize it.
 */
private static <T> AvroSerializerSnapshot<T> roundTrip(TypeSerializerSnapshot<T> original) throws IOException {
    // writeSnapshot();
    DataOutputSerializer out = new DataOutputSerializer(1024);
    original.writeSnapshot(out);
    // init
    AvroSerializerSnapshot<T> restored = new AvroSerializerSnapshot<>();
    // readSnapshot();
    DataInputView in = new DataInputDeserializer(out.wrapAsByteBuffer());
    restored.readSnapshot(restored.getCurrentVersion(), in, original.getClass().getClassLoader());
    return restored;
}
Also used : DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) DataInputView(org.apache.flink.core.memory.DataInputView) DataInputDeserializer(org.apache.flink.core.memory.DataInputDeserializer)

Example 17 with DataInputView

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

the class SpillingBufferTest method testWriteReadTooMuchInMemory.

@Test
public void testWriteReadTooMuchInMemory() throws Exception {
    final TestData.TupleGenerator generator = new TestData.TupleGenerator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM, ValueMode.RANDOM_LENGTH);
    final TypeSerializer<Tuple2<Integer, String>> serializer = TestData.getIntStringTupleSerializer();
    // create the writer output view
    final ArrayList<MemorySegment> memory = new ArrayList<MemorySegment>(NUM_MEMORY_SEGMENTS);
    this.memoryManager.allocatePages(this.parentTask, memory, NUM_MEMORY_SEGMENTS);
    final SpillingBuffer outView = new SpillingBuffer(this.ioManager, new ListMemorySegmentSource(memory), this.memoryManager.getPageSize());
    // write a number of pairs
    final Tuple2<Integer, String> rec = new Tuple2<>();
    for (int i = 0; i < NUM_PAIRS_INMEM; i++) {
        generator.next(rec);
        serializer.serialize(rec, outView);
    }
    // create the reader input view
    DataInputView inView = outView.flip();
    generator.reset();
    // notifyNonEmpty and re-generate all records and compare them
    final Tuple2<Integer, String> readRec = new Tuple2<>();
    try {
        for (int i = 0; i < NUM_PAIRS_INMEM + 1; i++) {
            generator.next(rec);
            serializer.deserialize(readRec, inView);
            int k1 = rec.f0;
            String v1 = rec.f1;
            int k2 = readRec.f0;
            String v2 = readRec.f1;
            Assert.assertTrue("The re-generated and the notifyNonEmpty record do not match.", k1 == k2 && v1.equals(v2));
        }
        Assert.fail("Read too much, expected EOFException.");
    } catch (EOFException eofex) {
    // expected
    }
    // re-notifyNonEmpty the data
    inView = outView.flip();
    generator.reset();
    // notifyNonEmpty and re-generate all records and compare them
    for (int i = 0; i < NUM_PAIRS_INMEM; i++) {
        generator.next(rec);
        serializer.deserialize(readRec, inView);
        int k1 = rec.f0;
        String v1 = rec.f1;
        int k2 = readRec.f0;
        String v2 = readRec.f1;
        Assert.assertTrue("The re-generated and the notifyNonEmpty record do not match.", k1 == k2 && v1.equals(v2));
    }
    this.memoryManager.release(outView.close());
    this.memoryManager.release(memory);
}
Also used : ListMemorySegmentSource(org.apache.flink.runtime.memory.ListMemorySegmentSource) TestData(org.apache.flink.runtime.operators.testutils.TestData) DataInputView(org.apache.flink.core.memory.DataInputView) ArrayList(java.util.ArrayList) MemorySegment(org.apache.flink.core.memory.MemorySegment) Tuple2(org.apache.flink.api.java.tuple.Tuple2) EOFException(java.io.EOFException) Test(org.junit.Test)

Example 18 with DataInputView

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

the class SpillingBufferTest method testWriteReadTooMuchExternal.

@Test
public void testWriteReadTooMuchExternal() throws Exception {
    final TestData.TupleGenerator generator = new TestData.TupleGenerator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM, ValueMode.RANDOM_LENGTH);
    final TypeSerializer<Tuple2<Integer, String>> serializer = TestData.getIntStringTupleSerializer();
    // create the writer output view
    final ArrayList<MemorySegment> memory = new ArrayList<MemorySegment>(NUM_MEMORY_SEGMENTS);
    this.memoryManager.allocatePages(this.parentTask, memory, NUM_MEMORY_SEGMENTS);
    final SpillingBuffer outView = new SpillingBuffer(this.ioManager, new ListMemorySegmentSource(memory), this.memoryManager.getPageSize());
    // write a number of pairs
    final Tuple2<Integer, String> rec = new Tuple2<>();
    for (int i = 0; i < NUM_PAIRS_EXTERNAL; i++) {
        generator.next(rec);
        serializer.serialize(rec, outView);
    }
    // create the reader input view
    DataInputView inView = outView.flip();
    generator.reset();
    // notifyNonEmpty and re-generate all records and compare them
    final Tuple2<Integer, String> readRec = new Tuple2<>();
    try {
        for (int i = 0; i < NUM_PAIRS_EXTERNAL + 1; i++) {
            generator.next(rec);
            serializer.deserialize(readRec, inView);
            int k1 = rec.f0;
            String v1 = rec.f1;
            int k2 = readRec.f0;
            String v2 = readRec.f1;
            Assert.assertTrue("The re-generated and the notifyNonEmpty record do not match.", k1 == k2 && v1.equals(v2));
        }
        Assert.fail("Read too much, expected EOFException.");
    } catch (EOFException eofex) {
    // expected
    }
    // re-notifyNonEmpty the data
    inView = outView.flip();
    generator.reset();
    // notifyNonEmpty and re-generate all records and compare them
    for (int i = 0; i < NUM_PAIRS_EXTERNAL; i++) {
        generator.next(rec);
        serializer.deserialize(readRec, inView);
        int k1 = rec.f0;
        String v1 = rec.f1;
        int k2 = readRec.f0;
        String v2 = readRec.f1;
        Assert.assertTrue("The re-generated and the notifyNonEmpty record do not match.", k1 == k2 && v1.equals(v2));
    }
    this.memoryManager.release(outView.close());
    this.memoryManager.release(memory);
}
Also used : ListMemorySegmentSource(org.apache.flink.runtime.memory.ListMemorySegmentSource) TestData(org.apache.flink.runtime.operators.testutils.TestData) DataInputView(org.apache.flink.core.memory.DataInputView) ArrayList(java.util.ArrayList) MemorySegment(org.apache.flink.core.memory.MemorySegment) Tuple2(org.apache.flink.api.java.tuple.Tuple2) EOFException(java.io.EOFException) Test(org.junit.Test)

Example 19 with DataInputView

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

the class CopyOnWriteSkipListStateMapTestUtils method readStateFromSnapshot.

private static <K, N, S> Map<N, Map<K, S>> readStateFromSnapshot(byte[] data, TypeSerializer<K> keySerializer, TypeSerializer<N> namespaceSerializer, TypeSerializer<S> stateSerializer) throws IOException {
    ByteArrayInputStreamWithPos inputStream = new ByteArrayInputStreamWithPos(data);
    DataInputView dataInputView = new DataInputViewStreamWrapper(inputStream);
    int size = dataInputView.readInt();
    Map<N, Map<K, S>> states = new HashMap<>();
    for (int i = 0; i < size; i++) {
        N namespace = namespaceSerializer.deserialize(dataInputView);
        K key = keySerializer.deserialize(dataInputView);
        S state = stateSerializer.deserialize(dataInputView);
        states.computeIfAbsent(namespace, (none) -> new HashMap<>()).put(key, state);
    }
    return states;
}
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) DataInputView(org.apache.flink.core.memory.DataInputView) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with DataInputView

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

the class KeyedStateCheckpointOutputStreamTest method testReadWriteMissingKeyGroups.

@Test
public void testReadWriteMissingKeyGroups() throws Exception {
    final KeyGroupRange keyRange = new KeyGroupRange(0, 2);
    KeyedStateCheckpointOutputStream stream = createStream(keyRange);
    DataOutputView dov = new DataOutputViewStreamWrapper(stream);
    stream.startNewKeyGroup(1);
    dov.writeInt(1);
    KeyGroupsStateHandle fullHandle = stream.closeAndGetHandle();
    int count = 0;
    try (FSDataInputStream in = fullHandle.openInputStream()) {
        DataInputView div = new DataInputViewStreamWrapper(in);
        for (int kg : fullHandle.getKeyGroupRange()) {
            long off = fullHandle.getOffsetForKeyGroup(kg);
            if (off >= 0) {
                in.seek(off);
                Assert.assertEquals(1, div.readInt());
                ++count;
            }
        }
    }
    Assert.assertEquals(1, count);
}
Also used : DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) DataInputView(org.apache.flink.core.memory.DataInputView) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) DataOutputView(org.apache.flink.core.memory.DataOutputView) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Aggregations

DataInputView (org.apache.flink.core.memory.DataInputView)28 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)17 Test (org.junit.Test)13 FSDataInputStream (org.apache.flink.core.fs.FSDataInputStream)8 InputStream (java.io.InputStream)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)4 DataOutputView (org.apache.flink.core.memory.DataOutputView)4 MemorySegment (org.apache.flink.core.memory.MemorySegment)4 ListMemorySegmentSource (org.apache.flink.runtime.memory.ListMemorySegmentSource)4 TestData (org.apache.flink.runtime.operators.testutils.TestData)4 KeyGroupStatePartitionStreamProvider (org.apache.flink.runtime.state.KeyGroupStatePartitionStreamProvider)4 EOFException (java.io.EOFException)3 DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 ByteArrayInputStreamWithPos (org.apache.flink.core.memory.ByteArrayInputStreamWithPos)2 DataInputDeserializer (org.apache.flink.core.memory.DataInputDeserializer)2