Search in sources :

Example 11 with MemorySegment

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

the class SpillingBufferTest method testWriteReadExternal.

// --------------------------------------------------------------------------------------------
@Test
public void testWriteReadExternal() 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<>();
    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));
    }
    // 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) Test(org.junit.Test)

Example 12 with MemorySegment

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

the class BufferFileWriterFileSegmentReaderTest method fillBufferWithAscendingNumbers.

public static int fillBufferWithAscendingNumbers(Buffer buffer, int currentNumber) {
    MemorySegment segment = buffer.getMemorySegment();
    final int size = buffer.getSize();
    for (int i = 0; i < size; i += 4) {
        segment.putInt(i, currentNumber++);
    }
    return currentNumber;
}
Also used : MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 13 with MemorySegment

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

the class BufferFileWriterReaderTest method verifyBufferFilledWithAscendingNumbers.

private int verifyBufferFilledWithAscendingNumbers(Buffer buffer, int currentNumber) {
    MemorySegment segment = buffer.getMemorySegment();
    final int size = buffer.getSize();
    for (int i = 0; i < size; i += 4) {
        if (segment.getInt(i) != currentNumber++) {
            throw new IllegalStateException("Read unexpected number from buffer.");
        }
    }
    return currentNumber;
}
Also used : MemorySegment(org.apache.flink.core.memory.MemorySegment)

Example 14 with MemorySegment

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

the class IOManagerAsyncTest method channelReadWriteOneSegment.

// ------------------------------------------------------------------------
//                           Test Methods
// ------------------------------------------------------------------------
@Test
public void channelReadWriteOneSegment() {
    final int NUM_IOS = 1111;
    try {
        final FileIOChannel.ID channelID = this.ioManager.createChannel();
        final BlockChannelWriter<MemorySegment> writer = this.ioManager.createBlockChannelWriter(channelID);
        MemorySegment memSeg = MemorySegmentFactory.allocateUnpooledSegment(32 * 1024);
        for (int i = 0; i < NUM_IOS; i++) {
            for (int pos = 0; pos < memSeg.size(); pos += 4) {
                memSeg.putInt(pos, i);
            }
            writer.writeBlock(memSeg);
            memSeg = writer.getNextReturnedBlock();
        }
        writer.close();
        final BlockChannelReader<MemorySegment> reader = this.ioManager.createBlockChannelReader(channelID);
        for (int i = 0; i < NUM_IOS; i++) {
            reader.readBlock(memSeg);
            memSeg = reader.getNextReturnedBlock();
            for (int pos = 0; pos < memSeg.size(); pos += 4) {
                if (memSeg.getInt(pos) != i) {
                    fail("Read memory segment contains invalid data.");
                }
            }
        }
        reader.closeAndDelete();
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Test encountered an exception: " + ex.getMessage());
    }
}
Also used : MemorySegment(org.apache.flink.core.memory.MemorySegment) IOException(java.io.IOException) Test(org.junit.Test)

Example 15 with MemorySegment

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

the class IOManagerAsyncTest method channelReadWriteMultipleSegments.

@Test
public void channelReadWriteMultipleSegments() {
    final int NUM_IOS = 1111;
    final int NUM_SEGS = 16;
    try {
        final List<MemorySegment> memSegs = new ArrayList<MemorySegment>();
        for (int i = 0; i < NUM_SEGS; i++) {
            memSegs.add(MemorySegmentFactory.allocateUnpooledSegment(32 * 1024));
        }
        final FileIOChannel.ID channelID = this.ioManager.createChannel();
        final BlockChannelWriter<MemorySegment> writer = this.ioManager.createBlockChannelWriter(channelID);
        for (int i = 0; i < NUM_IOS; i++) {
            final MemorySegment memSeg = memSegs.isEmpty() ? writer.getNextReturnedBlock() : memSegs.remove(memSegs.size() - 1);
            for (int pos = 0; pos < memSeg.size(); pos += 4) {
                memSeg.putInt(pos, i);
            }
            writer.writeBlock(memSeg);
        }
        writer.close();
        // get back the memory
        while (memSegs.size() < NUM_SEGS) {
            memSegs.add(writer.getNextReturnedBlock());
        }
        final BlockChannelReader<MemorySegment> reader = this.ioManager.createBlockChannelReader(channelID);
        while (!memSegs.isEmpty()) {
            reader.readBlock(memSegs.remove(0));
        }
        for (int i = 0; i < NUM_IOS; i++) {
            final MemorySegment memSeg = reader.getNextReturnedBlock();
            for (int pos = 0; pos < memSeg.size(); pos += 4) {
                if (memSeg.getInt(pos) != i) {
                    fail("Read memory segment contains invalid data.");
                }
            }
            reader.readBlock(memSeg);
        }
        reader.closeAndDelete();
        // get back the memory
        while (memSegs.size() < NUM_SEGS) {
            memSegs.add(reader.getNextReturnedBlock());
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("TEst encountered an exception: " + ex.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) MemorySegment(org.apache.flink.core.memory.MemorySegment) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

MemorySegment (org.apache.flink.core.memory.MemorySegment)161 Test (org.junit.Test)86 DummyInvokable (org.apache.flink.runtime.operators.testutils.DummyInvokable)38 ArrayList (java.util.ArrayList)30 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)24 IntPair (org.apache.flink.runtime.operators.testutils.types.IntPair)24 MemoryAllocationException (org.apache.flink.runtime.memory.MemoryAllocationException)22 IOException (java.io.IOException)19 TestData (org.apache.flink.runtime.operators.testutils.TestData)18 FileIOChannel (org.apache.flink.runtime.io.disk.iomanager.FileIOChannel)17 UniformIntPairGenerator (org.apache.flink.runtime.operators.testutils.UniformIntPairGenerator)16 IOManager (org.apache.flink.runtime.io.disk.iomanager.IOManager)15 IOManagerAsync (org.apache.flink.runtime.io.disk.iomanager.IOManagerAsync)15 EOFException (java.io.EOFException)14 AbstractInvokable (org.apache.flink.runtime.jobgraph.tasks.AbstractInvokable)14 Random (java.util.Random)11 ChannelReaderInputView (org.apache.flink.runtime.io.disk.iomanager.ChannelReaderInputView)10 UniformRecordGenerator (org.apache.flink.runtime.operators.testutils.UniformRecordGenerator)9 Record (org.apache.flink.types.Record)9 MutableObjectIterator (org.apache.flink.util.MutableObjectIterator)9