Search in sources :

Example 16 with IntPair

use of org.apache.flink.runtime.operators.testutils.types.IntPair in project flink by apache.

the class FixedLengthRecordSorterTest method testSwap.

/**
	 * The swap test fills the sort buffer and swaps all elements such that they are
	 * backwards. It then resets the generator, goes backwards through the buffer
	 * and compares for equality.
	 */
@Test
public void testSwap() throws Exception {
    final int numSegments = MEMORY_SIZE / MEMORY_PAGE_SIZE;
    final List<MemorySegment> memory = this.memoryManager.allocatePages(new DummyInvokable(), numSegments);
    FixedLengthRecordSorter<IntPair> sorter = newSortBuffer(memory);
    RandomIntPairGenerator generator = new RandomIntPairGenerator(SEED);
    // write the records
    IntPair record = new IntPair();
    int num = -1;
    do {
        generator.next(record);
        num++;
    } while (sorter.write(record) && num < 3354624);
    // swap the records
    int start = 0, end = num - 1;
    while (start < end) {
        sorter.swap(start++, end--);
    }
    // re-read the records
    generator.reset();
    IntPair readTarget = new IntPair();
    int i = num - 1;
    while (i >= 0) {
        generator.next(record);
        readTarget = sorter.getRecord(readTarget, i--);
        int rk = readTarget.getKey();
        int gk = record.getKey();
        int rv = readTarget.getValue();
        int gv = record.getValue();
        Assert.assertEquals("The re-read key is wrong", gk, rk);
        Assert.assertEquals("The re-read value is wrong", gv, rv);
    }
    // release the memory occupied by the buffers
    sorter.dispose();
    this.memoryManager.release(memory);
}
Also used : DummyInvokable(org.apache.flink.runtime.operators.testutils.DummyInvokable) RandomIntPairGenerator(org.apache.flink.runtime.operators.testutils.RandomIntPairGenerator) IntPair(org.apache.flink.runtime.operators.testutils.types.IntPair) MemorySegment(org.apache.flink.core.memory.MemorySegment) Test(org.junit.Test)

Example 17 with IntPair

use of org.apache.flink.runtime.operators.testutils.types.IntPair in project flink by apache.

the class FixedLengthRecordSorterTest method testFlushPartialMemoryPage.

@Test
public void testFlushPartialMemoryPage() throws Exception {
    // Insert IntPair which would fill 2 memory pages.
    final int NUM_RECORDS = 2 * MEMORY_PAGE_SIZE / 8;
    final List<MemorySegment> memory = this.memoryManager.allocatePages(new DummyInvokable(), 3);
    FixedLengthRecordSorter<IntPair> sorter = newSortBuffer(memory);
    UniformIntPairGenerator generator = new UniformIntPairGenerator(Integer.MAX_VALUE, 1, false);
    // write the records
    IntPair record = new IntPair();
    int num = -1;
    do {
        generator.next(record);
        num++;
    } while (sorter.write(record) && num < NUM_RECORDS);
    FileIOChannel.ID channelID = this.ioManager.createChannelEnumerator().next();
    BlockChannelWriter<MemorySegment> blockChannelWriter = this.ioManager.createBlockChannelWriter(channelID);
    final List<MemorySegment> writeBuffer = this.memoryManager.allocatePages(new DummyInvokable(), 3);
    ChannelWriterOutputView outputView = new ChannelWriterOutputView(blockChannelWriter, writeBuffer, writeBuffer.get(0).size());
    sorter.writeToOutput(outputView, 1, NUM_RECORDS - 1);
    this.memoryManager.release(outputView.close());
    BlockChannelReader<MemorySegment> blockChannelReader = this.ioManager.createBlockChannelReader(channelID);
    final List<MemorySegment> readBuffer = this.memoryManager.allocatePages(new DummyInvokable(), 3);
    ChannelReaderInputView readerInputView = new ChannelReaderInputView(blockChannelReader, readBuffer, false);
    final List<MemorySegment> dataBuffer = this.memoryManager.allocatePages(new DummyInvokable(), 3);
    ChannelReaderInputViewIterator<IntPair> iterator = new ChannelReaderInputViewIterator(readerInputView, dataBuffer, this.serializer);
    record = iterator.next(record);
    int i = 1;
    while (record != null) {
        Assert.assertEquals(i, record.getKey());
        record = iterator.next(record);
        i++;
    }
    Assert.assertEquals(NUM_RECORDS, i);
    this.memoryManager.release(dataBuffer);
    // release the memory occupied by the buffers
    sorter.dispose();
    this.memoryManager.release(memory);
}
Also used : FileIOChannel(org.apache.flink.runtime.io.disk.iomanager.FileIOChannel) ChannelReaderInputViewIterator(org.apache.flink.runtime.io.disk.ChannelReaderInputViewIterator) IntPair(org.apache.flink.runtime.operators.testutils.types.IntPair) MemorySegment(org.apache.flink.core.memory.MemorySegment) ChannelWriterOutputView(org.apache.flink.runtime.io.disk.iomanager.ChannelWriterOutputView) ChannelReaderInputView(org.apache.flink.runtime.io.disk.iomanager.ChannelReaderInputView) DummyInvokable(org.apache.flink.runtime.operators.testutils.DummyInvokable) UniformIntPairGenerator(org.apache.flink.runtime.operators.testutils.UniformIntPairGenerator) Test(org.junit.Test)

Example 18 with IntPair

use of org.apache.flink.runtime.operators.testutils.types.IntPair in project flink by apache.

the class FixedLengthRecordSorterTest method testWriteAndRead.

@Test
public void testWriteAndRead() throws Exception {
    final int numSegments = MEMORY_SIZE / MEMORY_PAGE_SIZE;
    final List<MemorySegment> memory = this.memoryManager.allocatePages(new DummyInvokable(), numSegments);
    FixedLengthRecordSorter<IntPair> sorter = newSortBuffer(memory);
    RandomIntPairGenerator generator = new RandomIntPairGenerator(SEED);
    //		long startTime = System.currentTimeMillis();
    // write the records
    IntPair record = new IntPair();
    int num = -1;
    do {
        generator.next(record);
        num++;
    } while (sorter.write(record) && num < 3354624);
    //		System.out.println("WRITE TIME " + (System.currentTimeMillis() - startTime));
    // re-read the records
    generator.reset();
    IntPair readTarget = new IntPair();
    //		startTime = System.currentTimeMillis();
    int i = 0;
    while (i < num) {
        generator.next(record);
        readTarget = sorter.getRecord(readTarget, i++);
        int rk = readTarget.getKey();
        int gk = record.getKey();
        int rv = readTarget.getValue();
        int gv = record.getValue();
        if (gk != rk) {
            Assert.fail("The re-read key is wrong " + i);
        }
        if (gv != rv) {
            Assert.fail("The re-read value is wrong");
        }
    }
    //		System.out.println("READ TIME " + (System.currentTimeMillis() - startTime));
    //		System.out.println("RECORDS " + num);
    // release the memory occupied by the buffers
    sorter.dispose();
    this.memoryManager.release(memory);
}
Also used : DummyInvokable(org.apache.flink.runtime.operators.testutils.DummyInvokable) RandomIntPairGenerator(org.apache.flink.runtime.operators.testutils.RandomIntPairGenerator) IntPair(org.apache.flink.runtime.operators.testutils.types.IntPair) MemorySegment(org.apache.flink.core.memory.MemorySegment) Test(org.junit.Test)

Example 19 with IntPair

use of org.apache.flink.runtime.operators.testutils.types.IntPair in project flink by apache.

the class FixedLengthRecordSorterTest method testWriteAndIterator.

@Test
public void testWriteAndIterator() throws Exception {
    final int numSegments = MEMORY_SIZE / MEMORY_PAGE_SIZE;
    final List<MemorySegment> memory = this.memoryManager.allocatePages(new DummyInvokable(), numSegments);
    FixedLengthRecordSorter<IntPair> sorter = newSortBuffer(memory);
    RandomIntPairGenerator generator = new RandomIntPairGenerator(SEED);
    // write the records
    IntPair record = new IntPair();
    int num = -1;
    do {
        generator.next(record);
        num++;
    } while (sorter.write(record));
    // re-read the records
    generator.reset();
    MutableObjectIterator<IntPair> iter = sorter.getIterator();
    IntPair readTarget = new IntPair();
    int count = 0;
    while ((readTarget = iter.next(readTarget)) != null) {
        count++;
        generator.next(record);
        int rk = readTarget.getKey();
        int gk = record.getKey();
        int rv = readTarget.getValue();
        int gv = record.getValue();
        Assert.assertEquals("The re-read key is wrong", gk, rk);
        Assert.assertEquals("The re-read value is wrong", gv, rv);
    }
    Assert.assertEquals("Incorrect number of records", num, count);
    // release the memory occupied by the buffers
    sorter.dispose();
    this.memoryManager.release(memory);
}
Also used : DummyInvokable(org.apache.flink.runtime.operators.testutils.DummyInvokable) RandomIntPairGenerator(org.apache.flink.runtime.operators.testutils.RandomIntPairGenerator) IntPair(org.apache.flink.runtime.operators.testutils.types.IntPair) MemorySegment(org.apache.flink.core.memory.MemorySegment) Test(org.junit.Test)

Example 20 with IntPair

use of org.apache.flink.runtime.operators.testutils.types.IntPair in project flink by apache.

the class ExternalSortITCase method testSpillingSortWithIntermediateMergeIntPair.

@Test
public void testSpillingSortWithIntermediateMergeIntPair() {
    try {
        // amount of pairs
        final int PAIRS = 50000000;
        // comparator
        final RandomIntPairGenerator generator = new RandomIntPairGenerator(12345678, PAIRS);
        final TypeSerializerFactory<IntPair> serializerFactory = new IntPairSerializer.IntPairSerializerFactory();
        final TypeComparator<IntPair> comparator = new TestData.IntPairComparator();
        // merge iterator
        LOG.debug("Initializing sortmerger...");
        Sorter<IntPair> merger = new UnilateralSortMerger<IntPair>(this.memoryManager, this.ioManager, generator, this.parentTask, serializerFactory, comparator, (double) 64 / 78, 4, 0.7f, true, /*use large record handler*/
        true);
        // emit data
        LOG.debug("Emitting data...");
        // check order
        MutableObjectIterator<IntPair> iterator = merger.getIterator();
        LOG.debug("Checking results...");
        int pairsRead = 1;
        int nextStep = PAIRS / 20;
        IntPair rec1 = new IntPair();
        IntPair rec2 = new IntPair();
        Assert.assertTrue((rec1 = iterator.next(rec1)) != null);
        while ((rec2 = iterator.next(rec2)) != null) {
            final int k1 = rec1.getKey();
            final int k2 = rec2.getKey();
            pairsRead++;
            Assert.assertTrue(k1 - k2 <= 0);
            IntPair tmp = rec1;
            rec1 = rec2;
            rec2 = tmp;
            // log
            if (pairsRead == nextStep) {
                nextStep += PAIRS / 20;
            }
        }
        Assert.assertEquals("Not all pairs were read back in.", PAIRS, pairsRead);
        merger.close();
        testSuccess = true;
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : RandomIntPairGenerator(org.apache.flink.runtime.operators.testutils.RandomIntPairGenerator) IntPair(org.apache.flink.runtime.operators.testutils.types.IntPair) Test(org.junit.Test)

Aggregations

IntPair (org.apache.flink.runtime.operators.testutils.types.IntPair)36 Test (org.junit.Test)34 MemorySegment (org.apache.flink.core.memory.MemorySegment)24 UniformIntPairGenerator (org.apache.flink.runtime.operators.testutils.UniformIntPairGenerator)23 MemoryAllocationException (org.apache.flink.runtime.memory.MemoryAllocationException)13 DummyInvokable (org.apache.flink.runtime.operators.testutils.DummyInvokable)8 Random (java.util.Random)6 RandomIntPairGenerator (org.apache.flink.runtime.operators.testutils.RandomIntPairGenerator)6 Collection (java.util.Collection)5 NullKeyFieldException (org.apache.flink.types.NullKeyFieldException)5 HashMap (java.util.HashMap)4 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)4 DiscardingOutputCollector (org.apache.flink.runtime.operators.testutils.DiscardingOutputCollector)4 TestData (org.apache.flink.runtime.operators.testutils.TestData)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 IOManager (org.apache.flink.runtime.io.disk.iomanager.IOManager)3 IOManagerAsync (org.apache.flink.runtime.io.disk.iomanager.IOManagerAsync)3 UnionIterator (org.apache.flink.runtime.operators.testutils.UnionIterator)3 MutableObjectIterator (org.apache.flink.util.MutableObjectIterator)3